src/windows/welcome.rs
89 lines · 2231 bytes
1//! Welcome Window
2use std::ops::{Deref, DerefMut};
3
4use modalkit::tui::{buffer::Buffer, layout::Rect};
5
6use modalkit::{
7 widgets::textbox::TextBoxState,
8 widgets::WindowOps,
9 widgets::{TermOffset, TerminalCursor},
10};
11
12use modalkit::editing::action::EditInfo;
13use modalkit::editing::base::{CloseFlags, WordStyle, WriteFlags};
14use modalkit::editing::completion::CompletionList;
15
16use crate::base::{IambBufferId, IambInfo, IambResult, ProgramStore};
17
18const WELCOME_TEXT: &str = include_str!("welcome.md");
19
20pub struct WelcomeState {
21 tbox: TextBoxState<IambInfo>,
22}
23
24impl WelcomeState {
25 pub fn new(store: &mut ProgramStore) -> Self {
26 let buf = store.buffers.load_str(IambBufferId::Welcome, WELCOME_TEXT);
27 let mut tbox = TextBoxState::new(buf);
28 tbox.set_readonly(true);
29
30 WelcomeState { tbox }
31 }
32}
33
34impl Deref for WelcomeState {
35 type Target = TextBoxState<IambInfo>;
36
37 fn deref(&self) -> &Self::Target {
38 return &self.tbox;
39 }
40}
41
42impl DerefMut for WelcomeState {
43 fn deref_mut(&mut self) -> &mut Self::Target {
44 return &mut self.tbox;
45 }
46}
47
48impl TerminalCursor for WelcomeState {
49 fn get_term_cursor(&self) -> Option<TermOffset> {
50 self.tbox.get_term_cursor()
51 }
52}
53
54impl WindowOps<IambInfo> for WelcomeState {
55 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
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}