Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/i18n/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@
"whatsNew": "What's new",
"documentation": "Documentation",
"shortcuts": "Keyboard shortcuts",
"updateWaiting": "An update is available"
"updateWaiting": "An update is available",
"updateAvailable": "An update is available: {{version}}"
},
"trash": {
"title": "Trash",
Expand Down
3 changes: 2 additions & 1 deletion crates/i18n/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,8 @@
"whatsNew": "Nouveautés",
"documentation": "Documentation",
"shortcuts": "Raccourcis clavier",
"updateWaiting": "Une mise à jour est disponible"
"updateWaiting": "Une mise à jour est disponible",
"updateAvailable": "Une mise à jour est disponible : {{version}}"
},
"trash": {
"title": "Corbeille",
Expand Down
2 changes: 2 additions & 0 deletions crates/ui/src/app/captures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ fn the_update_prompt() {
click(&mut harness, "update-later");
settle(&mut harness);
capture(&mut harness, "update-dot");
click(&mut harness, "about-open");
capture(&mut harness, "about-menu-update-known");
}

/// A library holding `API Paiements`, the HTTP area shown.
Expand Down
63 changes: 62 additions & 1 deletion crates/ui/src/app/update_prompt_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::sync::Arc;

use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use egui_kittest::kittest::{NodeT, Queryable};

use super::DevNotesApp;
use super::tests::{click, open_library, present, settle};
Expand Down Expand Up @@ -119,3 +119,64 @@ fn an_unsaved_request_is_asked_about_before_the_install() {
eframe::App::on_exit(harness.state_mut());
assert!(*fake.relaunched.lock().unwrap());
}

/// The launch's check, silent, over the library open: then the menu.
fn launched(directory: &std::path::Path, fake: Arc<FakeUpdater>) -> Harness<'static, DevNotesApp> {
let mut harness = open_library(directory);
harness.state_mut().state.updates = UpdateState::with(fake);
let app = harness.state_mut();
app.state.updates.check(&app.backend, true);
settle(&mut harness);
harness
}

fn rect(harness: &Harness<'_, DevNotesApp>, author_id: &str) -> egui::Rect {
harness
.get_by(|node| node.author_id() == Some(author_id))
.rect()
}

#[test]
fn the_menu_says_nothing_of_a_check_it_did_not_ask_and_forgets_one_it_did() {
let directory = tempfile::tempdir().unwrap();
let mut harness = launched(directory.path(), Arc::new(FakeUpdater::default()));

click(&mut harness, "about-open");
assert!(
!present(&harness, "about-check-status"),
"nothing was asked"
);

click(&mut harness, "about-check-updates");
settle(&mut harness);
assert!(present(&harness, "about-check-status"));

harness.key_press(egui::Key::Escape);
settle(&mut harness);
click(&mut harness, "about-open");
assert!(
!present(&harness, "about-check-status"),
"what was found then is not said again"
);
}

#[test]
fn an_update_known_is_named_by_its_entry_drawn_clear_of_the_next() {
let directory = tempfile::tempdir().unwrap();
let mut harness = launched(directory.path(), FakeUpdater::offering("0.26.0"));
click(&mut harness, "update-later");

click(&mut harness, "about-open");
let entry = harness.get_by(|node| node.author_id() == Some("about-check-updates"));
let label = entry.accesskit_node().label().unwrap_or_default();
assert!(label.contains("0.26.0"), "{label}");
let (entry, next) = (
rect(&harness, "about-check-updates"),
rect(&harness, "about-whats-new"),
);
// The separator between them is 7 points tall.
assert!(next.top() >= entry.bottom() + 6.0, "{entry:?} {next:?}");

click(&mut harness, "about-check-updates");
assert!(present(&harness, "update-prompt"), "the offer reopened");
}
8 changes: 7 additions & 1 deletion crates/ui/src/banners/update_prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ pub(crate) struct UpdateView {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ask {
Check,
/// The known update's offer, put away by « Later », shown again.
Reopen,
/// The menu closed: what its check found is not said again.
ForgetCheck,
Install,
Later { skip: bool },
Later {
skip: bool,
},
}

pub(crate) fn id() -> Id {
Expand Down
41 changes: 35 additions & 6 deletions crates/ui/src/services/updates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ pub enum Status {
Installed,
}

/// Apart from `Status`: the prompt must not depend on a check that led nowhere.
/// Apart from `Status`: the prompt must not depend on a check that led nowhere. A check asked
/// from the menu alone writes it, and the menu forgets it once closed.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CheckState {
#[default]
Expand Down Expand Up @@ -98,7 +99,27 @@ impl UpdateState {

/// Something exists and is not installed: outlives the prompt it opened.
pub fn has_pending(&self) -> bool {
self.update.is_some() && self.status != Status::Installed
self.pending().is_some()
}

pub fn pending(&self) -> Option<&AvailableUpdate> {
self.update
.as_ref()
.filter(|_| self.status != Status::Installed)
}

/// The offer put away by « Later », shown again: no new check.
pub fn reopen(&mut self) {
if self.update.is_some() && self.status == Status::Idle {
self.status = Status::Available;
}
}

/// What a check asked from the menu found, once the menu is closed.
pub fn forget_check(&mut self) {
if matches!(self.check, CheckState::UpToDate | CheckState::Failed) {
self.check = CheckState::Idle;
}
}

pub fn progress(&self) -> Option<u32> {
Expand All @@ -115,7 +136,9 @@ impl UpdateState {
if !self.can_check() || self.status != Status::Idle || self.asked.is_some() {
return;
}
self.check = CheckState::Checking;
if !silent {
self.check = CheckState::Checking;
}
let updater = Arc::clone(&self.updater);
let pending = backend.compute(move |_| Ok(updater.check()));
self.asked = Some((silent, pending));
Expand Down Expand Up @@ -215,11 +238,12 @@ impl UpdateState {
self.update = Some(update);
self.check = CheckState::Idle;
}
Ok(None) => self.check = CheckState::UpToDate,
Ok(None) if !silent => self.check = CheckState::UpToDate,
Ok(None) => {}
Err(detail) => {
self.check = CheckState::Failed;
log::warn!("The update check failed: {detail}");
if !silent {
self.check = CheckState::Failed;
errors.notify(Message::new(keys::errors::UPDATE_CHECK_FAILED));
}
}
Expand Down Expand Up @@ -401,10 +425,15 @@ mod tests {
let mut bench = bench(fake);

bench.check(true);
assert_eq!(bench.updates.check_state(), CheckState::Failed);
assert_eq!(
bench.updates.check_state(),
CheckState::Idle,
"the menu says nothing"
);
assert!(bench.errors.notice().is_none());

bench.check(false);
assert_eq!(bench.updates.check_state(), CheckState::Failed);
assert!(bench.errors.notice().is_some());
}

Expand Down
2 changes: 2 additions & 0 deletions crates/ui/src/state/apply_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ impl AppState {
use crate::banners::update_prompt::Ask;
match asked {
Ask::Check => self.updates.check(backend, false),
Ask::Reopen => self.updates.reopen(),
Ask::ForgetCheck => self.updates.forget_check(),
Ask::Install => {
self.ask_switch(
libraries::Goal::Install,
Expand Down
42 changes: 31 additions & 11 deletions crates/ui/src/titlebar/about_menu/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use i18n::{Message, keys};
use super::{Action, Tone};
use crate::action::{Action as Asked, Actions};
use crate::banners::update_prompt::Ask;
use crate::services::updates::CheckState;
use crate::state::AppState;
use crate::theme::palette;
use crate::widgets;
Expand Down Expand Up @@ -65,10 +66,17 @@ pub(super) fn show(ui: &mut Ui, state: &AppState, view: &mut View, actions: &mut
if state.updates.offered().is_some() {
Popup::close_id(ui.ctx(), Popup::default_response_id(&anchor));
}
Popup::menu(&anchor)
let shown = Popup::menu(&anchor)
.frame(widgets::menu_frame(ui.ctx()))
.close_behavior(PopupCloseBehavior::CloseOnClickOutside)
.show(|ui| widgets::fitted_menu(ui, anchor.rect.width(), |ui| entries(ui, state, actions)));
let answered = matches!(
state.updates.check_state(),
CheckState::UpToDate | CheckState::Failed
);
if shown.is_none() && answered {
actions.push(Asked::Menu(Action::Update(Ask::ForgetCheck)));
}
}

fn entries(ui: &mut Ui, state: &AppState, actions: &mut Actions) {
Expand Down Expand Up @@ -113,31 +121,43 @@ fn entries(ui: &mut Ui, state: &AppState, actions: &mut Actions) {
widgets::menu_arrows(ui, &ids);
}

/// The trigger's says something is waiting; the entry's says what for.
fn dot(ui: &mut Ui, under: &egui::Response, state: &AppState, author_id: &str) {
/// An update waits: painted at the corner of `under`, taking no room in the layout.
fn paint_dot(ui: &Ui, under: &egui::Response) -> Rect {
let p = palette(ui.ctx());
let centre = under.rect.right_top() + Vec2::new(-5.0, 6.0);
ui.painter().circle_filled(centre, 3.0, p.amber_fill);
Rect::from_center_size(centre, Vec2::splat(6.0))
}

/// The trigger's dot says something is waiting, to a reader too; the entry names it.
fn dot(ui: &mut Ui, under: &egui::Response, state: &AppState, author_id: &str) {
let rect = paint_dot(ui, under);
let waiting = i18n::translate(state.language, keys::about::UPDATE_WAITING, &[]);
let rect = Rect::from_center_size(centre, Vec2::splat(6.0));
let marked = ui.allocate_rect(rect, Sense::hover());
let marked = widgets::region(ui, rect, author_id, Sense::hover());
marked.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Label, true, &waiting));
widgets::address(ui, &marked, author_id);
}

/// « Check for updates », and under it what the check answered: it comes after the click, and
/// is said without moving the focus.
/// is said without moving the focus. An update known names itself instead, and opens its offer.
fn check_entry(ui: &mut Ui, state: &AppState, actions: &mut Actions) -> Id {
use crate::services::updates::CheckState;
let language = state.language;
let p = palette(ui.ctx());
let label = i18n::translate(language, keys::about::CHECK_UPDATES, &[]);
let pending = state.updates.pending();
let label = match pending {
Some(update) => Message::new(keys::about::UPDATE_AVAILABLE)
.with("version", update.version.as_str())
.text(language),
None => i18n::translate(language, keys::about::CHECK_UPDATES, &[]),
};
let row = super::menu_option(ui, &label, "about-check-updates", Tone::Plain);
if state.updates.has_pending() {
dot(ui, &row, state, "update-dot-option");
if pending.is_some() {
paint_dot(ui, &row);
}
let checking = state.updates.check_state() == CheckState::Checking;
if row.clicked() && !checking {
if row.clicked() && pending.is_some() {
actions.push(Asked::Menu(Action::Update(Ask::Reopen)));
} else if row.clicked() && !checking {
actions.push(Asked::Menu(Action::Update(Ask::Check)));
}
let said = match state.updates.check_state() {
Expand Down
Loading