src/windows/welcome.rs

73 lines · 1790 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::base::{CloseFlags, WordStyle};
12
13use crate::base::{IambBufferId, IambInfo, ProgramStore};
14
15const WELCOME_TEXT: &str = include_str!("welcome.md");
16
17pub struct WelcomeState {
18    tbox: TextBoxState<IambInfo>,
19}
20
21impl WelcomeState {
22    pub fn new(store: &mut ProgramStore) -> Self {
23        let buf = store.buffers.load_str(IambBufferId::Welcome, WELCOME_TEXT);
24        let mut tbox = TextBoxState::new(buf);
25        tbox.set_readonly(true);
26
27        WelcomeState { tbox }
28    }
29}
30
31impl Deref for WelcomeState {
32    type Target = TextBoxState<IambInfo>;
33
34    fn deref(&self) -> &Self::Target {
35        return &self.tbox;
36    }
37}
38
39impl DerefMut for WelcomeState {
40    fn deref_mut(&mut self) -> &mut Self::Target {
41        return &mut self.tbox;
42    }
43}
44
45impl TerminalCursor for WelcomeState {
46    fn get_term_cursor(&self) -> Option<TermOffset> {
47        self.tbox.get_term_cursor()
48    }
49}
50
51impl WindowOps<IambInfo> for WelcomeState {
52    fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
53        self.tbox.draw(area, buf, focused, store)
54    }
55
56    fn dup(&self, store: &mut ProgramStore) -> Self {
57        let tbox = self.tbox.dup(store);
58
59        WelcomeState { tbox }
60    }
61
62    fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
63        self.tbox.close(flags, store)
64    }
65
66    fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
67        self.tbox.get_cursor_word(style)
68    }
69
70    fn get_selected_word(&self) -> Option<String> {
71        self.tbox.get_selected_word()
72    }
73}