src/windows/welcome.rs
84 lines · 2108 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::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
49impl WindowOps<IambInfo> for WelcomeState {
50 fn draw(&mut self, area: Rect, buf: &mut Buffer, focused: bool, store: &mut ProgramStore) {
51 self.tbox.draw(area, buf, focused, store)
52 }
53
54 fn dup(&self, store: &mut ProgramStore) -> Self {
55 let tbox = self.tbox.dup(store);
56
57 WelcomeState { tbox }
58 }
59
60 fn close(&mut self, flags: CloseFlags, store: &mut ProgramStore) -> bool {
61 self.tbox.close(flags, store)
62 }
63
64 fn write(
65 &mut self,
66 path: Option<&str>,
67 flags: WriteFlags,
68 store: &mut ProgramStore,
69 ) -> IambResult<EditInfo> {
70 self.tbox.write(path, flags, store)
71 }
72
73 fn get_completions(&self) -> Option<CompletionList> {
74 self.tbox.get_completions()
75 }
76
77 fn get_cursor_word(&self, style: &WordStyle) -> Option<String> {
78 self.tbox.get_cursor_word(style)
79 }
80
81 fn get_selected_word(&self) -> Option<String> {
82 self.tbox.get_selected_word()
83 }
84}