basalt_tui/
splash_modal.rs

1use std::marker::PhantomData;
2
3use basalt_core::obsidian::Vault;
4use ratatui::{
5    buffer::Buffer,
6    layout::{Constraint, Flex, Layout, Rect},
7    style::Stylize,
8    text::Text,
9    widgets::{Clear, StatefulWidgetRef, Widget},
10};
11
12use crate::{
13    app::Message as AppMessage,
14    vault_selector::{VaultSelector, VaultSelectorState},
15};
16
17#[derive(Clone, Debug, PartialEq)]
18pub enum Message {
19    Up,
20    Down,
21    Open,
22}
23
24pub fn update<'a>(message: &Message, state: &mut SplashModalState<'a>) -> Option<AppMessage<'a>> {
25    match message {
26        Message::Up => state.previous(),
27        Message::Down => state.next(),
28        Message::Open => {
29            state.select();
30            if let Some(vault) = state.selected_item() {
31                state.hide();
32                return Some(AppMessage::OpenVault(vault));
33            }
34        }
35    };
36
37    None
38}
39
40const TITLE: &str = "⋅𝕭𝖆𝖘𝖆𝖑𝖙⋅";
41
42pub const LOGO: [&str; 25] = [
43    "           ▒███▓░          ",
44    "          ▒█████▒░         ",
45    "        ▒███▒██▓▒▒░        ",
46    "      ▒████░██▓▒░▒▒░       ",
47    "     ▒███▒▒██▒▒░ ░▒▒░      ",
48    "   ▒████▓▓██▒░▒░  ░▒▒▒░    ",
49    " ▒█████▓▓▓██ ░▒░  ░░▒▒▒░   ",
50    "░████▓▓▒░░██ ░░ ░░░░░░▒▒░  ",
51    "▒██▓▓▒░░░▒██░░▒░░░    ░▒░  ",
52    "░███▓░░░░██▓░░▒▒▒▒░   ░▒▒  ",
53    " ▒███░░░░██░░░░▒▒▒▒▒░░░▒▒  ",
54    " ▒▒██▒░░░██░░░░░░░▒▒▒░ ░▒  ",
55    " ▓▒░██░░▒█▓░░ ░░▒▒▒▒░ ░░▒  ",
56    " █▒▒██▒░▓█░░ ░▒▒▒▒▒▒░ ░░▒░ ",
57    "▒█▒▓▒██░██░▒▒▒▒▒░░░░ ░░░▒▒░",
58    "▓█▒▓▒▓██▓█░░░░░░░░░  ░ ░░▒▒",
59    "██▓▓▒▒▓█▓▓ ░░░░░░░░░░░░░░▒▒",
60    "▒█▓▒░░ ▒▒▒░░░░ ░▒░░ ░░░▒▒▒░",
61    "░▒▒▒░░░ ░░░░░░░░░░░░░░░▒▒░ ",
62    " ░░▒▒░ ░ ░░░░░░░░░░░░▒▒░   ",
63    "   ░▒▒▒░ ░ ░░░░░░░░▒▒░░    ",
64    "     ░▒▒░░  ░░░░░░▒▒░      ",
65    "       ░▒▒░░░░░▒▒▒▒░       ",
66    "        ░░▒▒▒▒▒▒▒░         ",
67    "          ░░▒▒░            ",
68];
69
70#[derive(Debug, Default, Clone, PartialEq)]
71pub struct SplashModalState<'a> {
72    pub(crate) vault_selector_state: VaultSelectorState<'a>,
73    pub(crate) version: &'a str,
74    pub(crate) visible: bool,
75}
76
77impl<'a> SplashModalState<'a> {
78    pub fn new(version: &'a str, items: Vec<&'a Vault>, visible: bool) -> Self {
79        let vault_selector_state = VaultSelectorState::new(items);
80
81        SplashModalState {
82            version,
83            vault_selector_state,
84            visible,
85        }
86    }
87
88    pub fn hide(&mut self) {
89        self.visible = false;
90    }
91
92    pub fn select(&mut self) {
93        self.vault_selector_state.select();
94    }
95
96    pub fn items(self) -> Vec<&'a Vault> {
97        self.vault_selector_state.items
98    }
99
100    pub fn get_item(self, index: usize) -> Option<&'a Vault> {
101        self.vault_selector_state.items.get(index).cloned()
102    }
103
104    pub fn selected_item(&self) -> Option<&'a Vault> {
105        self.vault_selector_state
106            .selected()
107            .and_then(|index| self.vault_selector_state.items.get(index).cloned())
108    }
109
110    pub fn selected(&self) -> Option<usize> {
111        self.vault_selector_state.selected()
112    }
113
114    pub fn next(&mut self) {
115        self.vault_selector_state.next();
116    }
117
118    pub fn previous(&mut self) {
119        self.vault_selector_state.previous();
120    }
121}
122
123#[derive(Default)]
124pub struct SplashModal<'a> {
125    _lifetime: PhantomData<&'a ()>,
126}
127
128impl<'a> StatefulWidgetRef for SplashModal<'a> {
129    type State = SplashModalState<'a>;
130
131    fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
132        Clear.render(area, buf);
133
134        let [_, center, _] = Layout::horizontal([
135            Constraint::Fill(1),
136            Constraint::Length(79),
137            Constraint::Fill(1),
138        ])
139        .areas(area);
140
141        let [_, top, bottom, _, help] = Layout::vertical([
142            Constraint::Fill(1),
143            Constraint::Length(28),
144            Constraint::Min(6),
145            Constraint::Fill(1),
146            Constraint::Length(1),
147        ])
148        .flex(Flex::Center)
149        .margin(1)
150        .areas(center);
151
152        let [logo, title] =
153            Layout::vertical([Constraint::Fill(1), Constraint::Length(3)]).areas(top);
154
155        let [_, title, version] = Layout::horizontal([
156            Constraint::Fill(1),
157            Constraint::Fill(1),
158            Constraint::Fill(1),
159        ])
160        .flex(Flex::SpaceBetween)
161        .margin(1)
162        .areas(title);
163
164        let [bottom] = Layout::horizontal([Constraint::Length(60)])
165            .flex(Flex::Center)
166            .areas(bottom);
167
168        Text::from_iter(LOGO)
169            .dark_gray()
170            .centered()
171            .render(logo, buf);
172
173        Text::from(TITLE).dark_gray().centered().render(title, buf);
174
175        Text::from(state.version)
176            .dark_gray()
177            .italic()
178            .centered()
179            .render(version, buf);
180
181        Text::from("Press (?) for help")
182            .italic()
183            .dark_gray()
184            .centered()
185            .render(help, buf);
186
187        VaultSelector::default().render_ref(bottom, buf, &mut state.vault_selector_state);
188    }
189}