Skip to content
Open
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
33 changes: 31 additions & 2 deletions crates/ui-egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use super::{
AiModel, Caches, CommandError, ControlRequest, ControlResponse, ErrorKind, GESTURE_TIMEOUT_MS, LightcraftApp, MAX_PENDING_GESTURES,
MAX_PENDING_SHOTS, MAX_SHOT_BYTES, MAX_SHOT_TOTAL_BYTES, MAX_TEXTURE_SIDE, PendingGesture, PendingShot, Perf, Receiver, SCREEN_PROFILE,
SCREENSHOT_FALLBACK_MS, SCREENSHOT_SETTLE_MS, SHOT_FORCE_BUSY_MS, SHOT_SETTLE_FRAMES, SHOT_SETTLE_MS, Sender, Session, ShotGeometry,
TOAST_SECONDS, UiState, Value, docking, drop_overlay, export_task, focus_scan, frame_rate, headless, i18n, import, lightroom_import, menus,
merge, model_setup, panels, pick, recheck, render, shortcuts, slideshow_export, state, sync, tasks, theme, updates_ui, widgets,
TOAST_SECONDS, UNDO_TOAST_SECONDS, UiState, Value, docking, drop_overlay, export_task, focus_scan, frame_rate, headless, i18n, import,
lightroom_import, menus, merge, model_setup, panels, pick, recheck, render, shortcuts, slideshow_export, state, sync, tasks, theme, updates_ui,
widgets,
};

mod control;
Expand Down Expand Up @@ -437,6 +438,33 @@ impl LightcraftApp {
pub fn toast_for(&mut self, _ctx: &egui::Context, text: impl Into<String>, secs: f64) {
let t = self.tick_now();
self.ui.toast = Some((text.into(), t + secs, None));
self.ui.toast_undo = None;
}

/// A toast for an action that has just committed one undo step (a drop or a menu move that moved, added or tagged
/// photos): it carries an Undo button for [`UNDO_TOAST_SECONDS`], until the next step is committed or
/// the toast is replaced, whichever comes first.
pub fn toast_undoable(&mut self, ctx: &egui::Context, text: impl Into<String>) {
let text = text.into();
self.toast_for(ctx, text.clone(), UNDO_TOAST_SECONDS);
self.ui.toast_undo = Some((text, self.session.commits(), self.session.undo.len()));
}

/// Whether the toast on screen offers Undo: it is the one [`Self::toast_undoable`] showed, nothing has been
/// committed since (a later step would make Undo reach that instead), the undo stack is as the drop left it
/// (Edit > Undo from the menu or a new library would change it, and a click would then undo something else),
/// and no dialog is open over the toast.
pub fn toast_offers_undo(&self) -> bool {
match (&self.ui.toast, &self.ui.toast_undo) {
(Some((shown, _, _)), Some((text, commits, steps))) => {
shown == text
&& *commits == self.session.commits()
&& *steps == self.session.undo.len()
&& self.session.redo.is_empty()
&& self.ui.dialog.is_none()
}
_ => false,
}
}

/// Reverse geocoding: say how a place-names download ended (a failure stays readable, with what to do).
Expand Down Expand Up @@ -524,6 +552,7 @@ impl LightcraftApp {
pub fn toast_error(&mut self, _ctx: &egui::Context, text: impl Into<String>) {
let t = self.tick_now();
self.ui.toast = Some((text.into(), t + 6.0, None));
self.ui.toast_undo = None;
}

/// The process is about to end (settings and library are saved by then): make sure nothing is
Expand Down
9 changes: 7 additions & 2 deletions crates/ui-egui/src/dialogs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,13 @@ fn confirmed(app: &mut LightcraftApp, ctx: &egui::Context, dlg: &mut Dialog) ->
// a move says how it went, or why nothing moved
Ok(r) if matches!(dlg, Dialog::MovePhotos { .. }) => {
if let Dialog::MovePhotos { name, .. } = dlg {
let text = crate::panels::left::moved_text(r["moved"].as_u64().unwrap_or(0), name);
app.toast(ctx, text);
let moved = r["moved"].as_u64().unwrap_or(0);
let text = crate::panels::left::moved_text(moved, name);
if moved > 0 {
app.toast_undoable(ctx, text);
} else {
app.toast(ctx, text);
}
}
closed = true;
}
Expand Down
2 changes: 2 additions & 0 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ pub use services::{
use state::{GestureFrame, InjectedPress};

const TOAST_SECONDS: f64 = 1.4;
/// How long a toast that offers Undo stays: time to read it and reach the button.
const UNDO_TOAST_SECONDS: f64 = 6.0;
/// The `path` of a display profile read from the screen rather than chosen as a file.
pub const SCREEN_PROFILE: &str = "screen";

Expand Down
64 changes: 58 additions & 6 deletions crates/ui-egui/src/panels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,81 @@ pub fn resizable_side(ui: &mut egui::Ui, side: SidePanel, add: impl FnOnce(&mut
Some(w.round().clamp(limits.min, max))
}

/// The HUD toast at the bottom centre of the canvas.
/// A toast that offers Undo: the gap between its text and the button, and the button's height.
const TOAST_UNDO_GAP: f32 = 16.0;
const TOAST_UNDO_HEIGHT: f32 = 26.0;

/// The HUD toast at the bottom centre of the canvas. One that tells of an action just done (a drop that moved,
/// added or tagged photos) carries an Undo button ([`LightcraftApp::toast_undoable`]); any other toast is only
/// painted, never takes a click.
pub fn toast(app: &mut LightcraftApp, ctx: &egui::Context) {
let now = app.tick_now();
let Some((text, until, label)) = app.ui.toast.clone() else { return };
if now > until {
app.ui.toast = None;
app.ui.toast_undo = None;
return;
}
let Some(canvas) = app.canvas_rect else { return };
let undo = app.toast_offers_undo();
let t = Tokens::get(ctx);
let fade = ((until - now) / 0.25).clamp(0.0, 1.0) as f32;
let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Foreground, egui::Id::unique("toast")));
let button_w = if undo { painter.layout_no_wrap(crate::i18n::tr("Undo").to_string(), t.semibold(13.0), t.text).size().x + 28.0 } else { 0.0 };
// long messages (a save warning, the web build's notices) wrap within the canvas
let wrap = (canvas.width() - 80.0).clamp(200.0, 720.0);
let (background, foreground) = label.map(crate::theme::label_toast_colors).unwrap_or((egui::Color32::from_black_alpha(200), t.text));
let foreground = foreground.gamma_multiply(fade);
let wrap = (canvas.width() - 80.0 - if undo { button_w + TOAST_UNDO_GAP } else { 0.0 }).clamp(200.0, 720.0);
let (background, ink) = label.map(crate::theme::label_toast_colors).unwrap_or((egui::Color32::from_black_alpha(200), t.text));
let foreground = ink.gamma_multiply(fade);
let galley = painter.layout(text, t.font(14.0), foreground, wrap);
let size = vec2(galley.size().x + 40.0, (galley.size().y + 22.0).max(40.0));
let extra = if undo { button_w + TOAST_UNDO_GAP - 8.0 } else { 0.0 };
let size = vec2(galley.size().x + 40.0 + extra, (galley.size().y + 22.0).max(40.0));
// a slideshow's playback controls sit at the bottom: the toast goes to the top there, clear of them
let y = if app.ui.slideshow.is_some() { canvas.top() + 60.0 } else { canvas.bottom() - 60.0 };
let r = Rect::from_center_size(pos2(canvas.center().x, y), size);
painter.rect_filled(r, 6.0, background.gamma_multiply(fade));
painter.galley(r.center() - galley.size() / 2.0, galley, foreground);
painter.galley(pos2(r.left() + 20.0, r.center().y - galley.size().y / 2.0), galley, foreground);
if undo {
let button =
Rect::from_min_size(pos2(r.right() - 12.0 - button_w, r.center().y - TOAST_UNDO_HEIGHT / 2.0), vec2(button_w, TOAST_UNDO_HEIGHT));
// (above the toast's own layer, whose backdrop would otherwise dim it)
let clicked = egui::Area::new(egui::Id::unique("toast-undo"))
.order(egui::Order::Tooltip)
.fixed_pos(button.min)
.show(ctx, |ui| {
ui.set_opacity(fade);
// drawn in the toast's own colours (it is dark whatever the theme): a theme's button fills
// would not read on it
let resp = ui.interact(button, egui::Id::unique("toast-undo-button"), egui::Sense::click());
resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::Button, true, crate::i18n::tr("Undo")));
crate::widgets::register_response("button:toastUndo", button, &resp);
if resp.hovered() {
ctx.set_cursor_icon(egui::CursorIcon::PointingHand);
}
let wash = if resp.is_pointer_button_down_on() {
0.3
} else if resp.hovered() {
0.16
} else {
0.0
};
let p = ui.painter();
p.rect_filled(button, 4.0, ink.gamma_multiply(wash));
p.rect_stroke(button, 4.0, egui::Stroke::new(1.0, ink.gamma_multiply(0.6)), egui::StrokeKind::Inside);
if resp.has_focus() {
p.rect_stroke(button.expand(2.0), 6.0, egui::Stroke::new(2.0, t.accent), egui::StrokeKind::Outside);
}
p.text(button.center(), Align2::CENTER_CENTER, crate::i18n::tr("Undo"), t.semibold(13.0), ink);
resp.clicked()
})
.inner;
if clicked {
app.ui.toast_undo = None;
match app.run("edit.undo", serde_json::json!({})) {
Ok(_) => app.toast(ctx, crate::i18n::tr("Undo")),
Err(e) => app.toast_error(ctx, e),
}
}
}
ctx.request_repaint_after(std::time::Duration::from_millis(30));
}

Expand Down
17 changes: 11 additions & 6 deletions crates/ui-egui/src/panels/keyword_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,20 +329,25 @@ fn drop_target(app: &mut LightcraftApp, ui: &mut egui::Ui, rect: Rect, path: &st
app.session
.catalog
.photo(PhotoId(*id))
.is_some_and(|p| !p.meta.keywords.iter().any(|k| same(&lightcraft_catalog::keywords::clean(k), path)))
// (as `photo.setMeta` compares: the stored keyword as it is, with the cleaned name)
.is_some_and(|p| !p.meta.keywords.iter().any(|k| same(k, &lightcraft_catalog::keywords::clean(path))))
};
let n = ids.iter().filter(|id| lacking(id)).count();
app.ui.dragging_photos = None;
match app.run("photo.setMeta", json!({"ids": ids, "addKeywords": [path]})) {
Ok(_) => app.toast(
ui.ctx(),
crate::i18n::tr_format!(
Ok(_) => {
let text = crate::i18n::tr_format!(
"Added “{keyword}” to {n} photo{}",
if n == 1 { "" } else { "s" },
keyword = path.replace('|', " › "),
n = n
),
),
);
if n > 0 {
app.toast_undoable(ui.ctx(), text);
} else {
app.toast(ui.ctx(), text);
}
}
Err(e) => app.toast(ui.ctx(), e),
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/ui-egui/src/panels/left/albums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,15 @@ fn drop_target(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::Response
/// Adds the dragged photos to the album and toasts how many went in (and how many Local ones were skipped).
fn drop_photos_on_album(app: &mut LightcraftApp, ui: &egui::Ui, a: &Album, ids: Vec<u64>) {
match app.run("album.addPhotos", json!({"id": a.id.0, "ids": ids})) {
Ok(r) => app.toast(ui.ctx(), added_message(a, &r)),
Ok(r) => {
let msg = added_message(a, &r);
// an Undo button only when something was added (the drop is then one undo step)
if r.get("added").and_then(serde_json::Value::as_u64).unwrap_or(0) > 0 {
app.toast_undoable(ui.ctx(), msg);
} else {
app.toast(ui.ctx(), msg);
}
}
Err(e) => app.toast(ui.ctx(), e),
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ui-egui/src/panels/left/folders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ pub(crate) fn moved_text(n: u64, name: &str) -> String {
/// [`move_photos`], saying how it went in a toast.
fn move_photos_here(app: &mut LightcraftApp, ctx: &egui::Context, ids: Option<&[u64]>, path: &str, name: &str) {
match move_photos(app, ids, path) {
Ok(n) if n > 0 => app.toast_undoable(ctx, moved_text(n, name)),
Ok(n) => app.toast(ctx, moved_text(n, name)),
Err(e) => app.toast(ctx, e),
}
Expand Down
6 changes: 6 additions & 0 deletions crates/ui-egui/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ pub struct UiState {
/// Transient toast text, expiry (seconds of app time), and optional colour-label styling.
#[serde(skip)]
pub toast: Option<(String, f64, Option<lightcraft_catalog::ColorLabel>)>,
/// The toast that offers Undo: its text, how many undo steps the session had committed, and how many the undo
/// stack held, when the action it tells of finished. The toast shows the button only while its text is still on
/// screen and the stacks are as that action left them, so Undo can only reach that action ([`crate::LightcraftApp::toast_undoable`]).
#[serde(skip)]
pub toast_undo: Option<(String, u64, usize)>,
/// The result of the last Find Missing Photos (it searches in the background).
#[serde(skip)]
pub last_find_missing: Option<serde_json::Value>,
Expand Down Expand Up @@ -485,6 +490,7 @@ impl Default for UiState {
compare: None,
reference: None,
toast: None,
toast_undo: None,
last_find_missing: None,
last_find_missing_result: None,
#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions crates/ui-egui/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod dialog_tab;
mod dialogs;
mod docking_acceptance;
mod drop_import;
mod drop_undo;
mod edit_layout;
mod empty_library;
mod error_kinds;
Expand Down
Loading
Loading