src/windows/welcome.rs
85 lines · 2149 bytes
1//! Welcome Window
2use std::ops::{Deref, DerefMut};
3
4use ratatui::{buffer::Buffer, layout::Rect};
5
6use modalkit_ratatui::{textbox::TextBoxState, TermOffset, TerminalCursor, WindowOps};
7
8use modalkit::editing::action::EditInfo;
9use modalkit::editing::completion::CompletionList;
10use modalkit::prelude::*;
11
12use crate::base::{IambBufferId, IambInfo, IambResult, ProgramStore};
13
14const WELCOME_TEXT: &str = include_str!("welcome.md");
15
16pub struct WelcomeState {
17 tbox: TextBoxState<IambInfo>,
18}
19
20impl WelcomeState {
21 pub fn new(store: &mut ProgramStore) -> Self {
22 let buf = store.buffers.load_str(IambBufferId::Welcome, WELCOME_TEXT);
23 let mut tbox = TextBoxState::new(buf);
24 tbox.set_readonly(true);
25
26 WelcomeState { tbox }
27 }
28}
29
30impl Deref for WelcomeState {
31 type Target = TextBoxState<IambInfo>;
32
33 fn deref(&self) -> &Self::Target {
34 return &self.tbox;
35 }
36}
37
38impl DerefMut for WelcomeState {
39 fn deref_mut(&mut self) -> &mut Self::Target {
40 return &mut self.tbox;
41 }
42}
43
44impl TerminalCursor for WelcomeState {
45 fn get_term_cursor(&self) -> Option<TermOffset> {
46 self.tbox.get_term_cursor()
47 }
48}
49
50impl WindowOps<IambInfo> for WelcomeState {
51 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
52 self.tbox.draw(area, buf, focused, store)
53 }
54
55 fn dup(&self, store: &mut ProgramStore) -> Self {
56 let tbox = self.tbox.dup(store);
57
58 WelcomeState { tbox }
59 }
60
61 fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
62 self.tbox.close(flags, store)
63 }
64
65 fn write(
66 &mut self,
67 path: Option<&str>,
68 flags: WriteFlags,
69 store: &mut ProgramStore,
70 ) -> IambResult<EditInfo> {
71 self.tbox.write(path, flags, store)
72 }
73
74 fn get_completions(&self) -> Option<CompletionList> {
75 self.tbox.get_completions()
76 }
77
78 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
79 self.tbox.get_cursor_word(style)
80 }
81
82 fn get_selected_word(&self) -> Option<String> {
83 self.tbox.get_selected_word()
84 }
85}