src/windows/welcome.rs

88 lines · 2212 bytes

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