src/windows/welcome.rs

89 lines · 2275 bytes

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