Skip to main content

kimun_notes/components/dialogs/
update_dialog.rs

1use ratatui::Frame;
2use ratatui::crossterm::event::KeyCode;
3use ratatui::layout::{Constraint, Direction, Layout, Rect};
4use ratatui::style::{Modifier, Style};
5use ratatui::widgets::Paragraph;
6
7use crate::components::Component;
8use crate::components::event_state::EventState;
9use crate::components::events::{AppEvent, AppTx};
10use crate::components::panel::{ModalSpec, modal_chrome};
11use crate::settings::themes::Theme;
12use crate::update::UpdateStatus;
13
14/// Dialog shown when a newer release is available. On self-update-eligible
15/// channels it offers an in-place update; otherwise it shows the package
16/// manager's upgrade command. Either way the user can skip the version.
17///
18/// ```text
19/// ┌─ Update Available ───────────────────────────────────┐
20/// │                                                      │
21/// │  kimün 0.17.0  →  0.18.0                             │
22/// │                                                      │
23/// │  [U] Update now      [S] Skip this version           │
24/// │                                                      │
25/// │  Release notes: https://github.com/nico2sh/kimun/... │
26/// │  [Esc] Close                                          │
27/// └──────────────────────────────────────────────────────┘
28/// ```
29pub struct UpdateAvailableDialog {
30    current: String,
31    latest: String,
32    /// Whether this channel can self-update in place.
33    eligible: bool,
34    /// Upgrade command for package-manager channels (e.g. `brew upgrade kimun`).
35    upgrade_hint: Option<String>,
36}
37
38impl UpdateAvailableDialog {
39    pub fn new(status: &UpdateStatus) -> Self {
40        Self {
41            current: status.current.clone(),
42            latest: status.latest.clone(),
43            eligible: status.channel.self_update_eligible(),
44            upgrade_hint: status.channel.upgrade_hint().map(str::to_string),
45        }
46    }
47
48    pub fn handle_key(
49        &mut self,
50        key: ratatui::crossterm::event::KeyEvent,
51        tx: &AppTx,
52    ) -> EventState {
53        match key.code {
54            KeyCode::Char('u') | KeyCode::Char('U') if self.eligible => {
55                tx.send(AppEvent::ApplyUpdate).ok();
56                tx.send(AppEvent::CloseOverlay).ok();
57                EventState::Consumed
58            }
59            KeyCode::Char('s') | KeyCode::Char('S') => {
60                tx.send(AppEvent::DismissUpdate(self.latest.clone())).ok();
61                tx.send(AppEvent::CloseOverlay).ok();
62                EventState::Consumed
63            }
64            KeyCode::Esc => {
65                tx.send(AppEvent::CloseOverlay).ok();
66                EventState::Consumed
67            }
68            _ => EventState::Consumed, // swallow other keys while open
69        }
70    }
71}
72
73impl Component for UpdateAvailableDialog {
74    fn render(&mut self, f: &mut Frame, rect: Rect, theme: &Theme, _focused: bool) {
75        let popup_area = super::fixed_centered_rect(58, 11, rect);
76
77        let inner = modal_chrome(
78            f,
79            popup_area,
80            theme,
81            ModalSpec {
82                title: Some(" Update Available "),
83                border: Some(Style::default().fg(theme.accent.to_ratatui())),
84                ..Default::default()
85            },
86        );
87
88        let rows = Layout::default()
89            .direction(Direction::Vertical)
90            .constraints([
91                Constraint::Length(1), // 0: spacer
92                Constraint::Length(1), // 1: version line
93                Constraint::Length(1), // 2: separator
94                Constraint::Length(1), // 3: action row
95                Constraint::Length(1), // 4: spacer
96                Constraint::Length(1), // 5: release notes / hint
97                Constraint::Length(1), // 6: Esc hint
98                Constraint::Min(0),    // 7: remainder
99            ])
100            .split(inner);
101
102        let bg = theme.bg_panel.to_ratatui();
103        let fg = theme.fg.to_ratatui();
104        let gray = theme.gray.to_ratatui();
105        let key_fg = theme.selection_fg.to_ratatui();
106        let accent = theme.accent.to_ratatui();
107
108        // Row 1: version transition.
109        f.render_widget(
110            Paragraph::new(format!("  kimün {}  →  {}", self.current, self.latest)).style(
111                Style::default()
112                    .fg(accent)
113                    .bg(bg)
114                    .add_modifier(Modifier::BOLD),
115            ),
116            rows[1],
117        );
118
119        // Row 2: separator.
120        super::render_separator(f, rows[2], gray, bg);
121
122        // Row 3: actions.
123        let key_style = Style::default()
124            .fg(key_fg)
125            .bg(bg)
126            .add_modifier(Modifier::BOLD);
127        let label_style = Style::default().fg(fg).bg(bg);
128        if self.eligible {
129            let cols = Layout::default()
130                .direction(Direction::Horizontal)
131                .constraints([Constraint::Length(24), Constraint::Min(1)])
132                .split(rows[3]);
133            render_action(f, cols[0], "  [U]", " Update now", key_style, label_style);
134            render_action(
135                f,
136                cols[1],
137                "[S]",
138                " Skip this version",
139                key_style,
140                label_style,
141            );
142        } else {
143            // Package-manager channel: show the upgrade command instead.
144            let hint = self
145                .upgrade_hint
146                .clone()
147                .unwrap_or_else(|| "Download the latest release manually.".to_string());
148            f.render_widget(
149                Paragraph::new(format!("  Run: {hint}")).style(label_style),
150                rows[3],
151            );
152            render_action(
153                f,
154                rows[4],
155                "  [S]",
156                " Skip this version",
157                key_style,
158                label_style,
159            );
160        }
161
162        // Row 5: release notes URL.
163        f.render_widget(
164            Paragraph::new(format!("  Releases: {}", crate::update::releases_url()))
165                .style(Style::default().fg(gray).bg(bg)),
166            rows[5],
167        );
168
169        // Row 6: close hint.
170        f.render_widget(
171            Paragraph::new("  [Esc] Close").style(Style::default().fg(gray).bg(bg)),
172            rows[6],
173        );
174    }
175}
176
177fn render_action(
178    f: &mut Frame,
179    area: Rect,
180    key: &str,
181    label: &str,
182    key_style: Style,
183    label_style: Style,
184) {
185    let chunks = Layout::default()
186        .direction(Direction::Horizontal)
187        .constraints([Constraint::Length(key.len() as u16), Constraint::Min(1)])
188        .split(area);
189    f.render_widget(Paragraph::new(key.to_string()).style(key_style), chunks[0]);
190    f.render_widget(
191        Paragraph::new(label.to_string()).style(label_style),
192        chunks[1],
193    );
194}
195
196#[cfg(test)]
197mod tests {
198    use super::*;
199    use crate::update::InstallChannel;
200    use ratatui::crossterm::event::{KeyEvent, KeyModifiers};
201    use tokio::sync::mpsc;
202
203    fn status(channel: InstallChannel) -> UpdateStatus {
204        UpdateStatus {
205            current: "0.17.0".into(),
206            latest: "0.18.0".into(),
207            channel,
208            update_available: true,
209            dismissed: false,
210        }
211    }
212
213    #[test]
214    fn skip_sends_dismiss_and_close() {
215        let (tx, mut rx) = mpsc::unbounded_channel::<AppEvent>();
216        let mut d = UpdateAvailableDialog::new(&status(InstallChannel::Direct));
217        let state = d.handle_key(KeyEvent::new(KeyCode::Char('s'), KeyModifiers::NONE), &tx);
218        assert_eq!(state, EventState::Consumed);
219        assert!(matches!(rx.try_recv(), Ok(AppEvent::DismissUpdate(v)) if v == "0.18.0"));
220        assert!(matches!(rx.try_recv(), Ok(AppEvent::CloseOverlay)));
221    }
222
223    #[test]
224    fn update_now_only_on_eligible_channel() {
225        let (tx, mut rx) = mpsc::unbounded_channel::<AppEvent>();
226        // Eligible: 'u' applies.
227        let mut d = UpdateAvailableDialog::new(&status(InstallChannel::Script));
228        d.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::NONE), &tx);
229        assert!(matches!(rx.try_recv(), Ok(AppEvent::ApplyUpdate)));
230
231        // Not eligible: 'u' is swallowed, no apply.
232        let (tx2, mut rx2) = mpsc::unbounded_channel::<AppEvent>();
233        let mut d2 = UpdateAvailableDialog::new(&status(InstallChannel::Brew));
234        let state = d2.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::NONE), &tx2);
235        assert_eq!(state, EventState::Consumed);
236        assert!(rx2.try_recv().is_err());
237    }
238}