//! Update check and installation dialog. use opencode_ui::dialog::{DialogButton, DialogButtons, DialogCx}; use opencode_ui::motion::Spinner; use crate::features::update_notification::{self as updates, UpdateState}; use crate::prelude::*; #[derive(Clone, Debug, PartialEq, Eq)] enum Shown { Checking, CheckFailed(String), Unavailable(String), Current, Update(UpdateState), } #[derive(Default)] struct Check { loading: bool, error: Option, unavailable: Option, } pub fn show(tui: &Tui, checks: bool) { let check = Rc::new(RefCell::new(Check { loading: checks, ..Check::default() })); let weak = tui.weak(); tui.dialogs.replace( { let check = check.clone(); move |cx| { cx.dialogs.set_centered(true); let Some(tui) = weak.upgrade() else { return View::new(&cx.app) }; let root = View::new(&cx.app); let row: Rc>> = Rc::default(); let draw: Rc = Rc::new({ let (weak, cx, root, check, row) = (tui.weak(), Rc::downgrade(cx), root.clone(), check.clone(), row.clone()); move || { let (Some(tui), Some(cx)) = (weak.upgrade(), cx.upgrade()) else { return }; *row.borrow_mut() = draw(&tui, &cx, &root, &check.borrow()); } }); draw(); *tui.updates.dialog.borrow_mut() = Some(Rc::downgrade(&draw)); let command = |key: &str, title: &str, run: fn(&DialogButtons)| { let row = row.clone(); TuiCommand::inline(key, move |_, _| { if let Some(buttons) = row.borrow().as_ref() { run(buttons); } }) .title(title) .group("Dialog") }; cx.layer(KeymapLayer::new(vec![ command("return", "Confirm update action", DialogButtons::run), command("left", "Previous update action", |buttons| buttons.step(-1)), command("shift+tab", "Previous update action", |buttons| buttons.step(-1)), command("right", "Next update action", |buttons| buttons.step(1)), command("tab", "Next update action", |buttons| buttons.step(1)), ])); // The dialog owns the drawing; a check that ends after it closed is dropped. let owner = draw.clone(); cx.on_close(Disposer::new(move || drop(owner))); cx.on_dismiss(bind!(tui, |tui| { tui.updates.generation.set(tui.updates.generation.get() + 1); tui.updates.dialog.borrow_mut().take(); })); root } }, None, DialogOptions::default(), ); // After the replace: closing a dialog this one replaces moves the generation on too. let generation = tui.updates.generation.get() + 1; tui.updates.generation.set(generation); if !checks { return; } updates::check( tui, Box::new(bind!(tui, |tui, result| { if tui.updates.generation.get() != generation { return; } { let mut check = check.borrow_mut(); check.loading = false; match result { Ok(unavailable) => check.unavailable = unavailable, Err(error) => check.error = Some(error), } } tui.updates.redraw_dialog(); })), ); } fn shown(tui: &Tui, check: &Check) -> Shown { if let Some(message) = &check.error { return Shown::CheckFailed(message.clone()); } let current = tui.updates.state.borrow().clone(); if let Some(UpdateState::Installing(version)) = current { return Shown::Update(UpdateState::Installing(version)); } if check.loading { return Shown::Checking; } if let Some(message) = &check.unavailable { return Shown::Unavailable(message.clone()); } current.map_or(Shown::Current, Shown::Update) } type Button = (&'static str, Rc); /// `skip`, then `update` or `restart` when they apply; none while installing. fn buttons(tui: &Tui, shown: &Shown) -> Vec