Skip to content
Merged
1 change: 1 addition & 0 deletions crates/ui-egui/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl LightcraftApp {
next_widgets: vec![],
next_widget_hits: vec![],
gesture: None,
space_hand: None,
brush_live: None,
loupe_shown: None,
region_view: None,
Expand Down
16 changes: 13 additions & 3 deletions crates/ui-egui/src/app/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,19 @@ impl LightcraftApp {
if self.synthetic.is_empty() {
return;
}
let n = match self.synthetic[0] {
egui::Event::PointerMoved(_) | egui::Event::PointerButton { .. } | egui::Event::MouseWheel { .. } => 1,
_ => self.synthetic.iter().position(|e| matches!(e, egui::Event::Key { pressed: false, .. })).map_or(self.synthetic.len(), |i| i + 1),
let pointer =
|e: &egui::Event| matches!(e, egui::Event::PointerMoved(_) | egui::Event::PointerButton { .. } | egui::Event::MouseWheel { .. });
let n = if pointer(&self.synthetic[0]) {
1
} else {
// a key sequence up to its release; a key held down (no release yet) stops before the
// pointer events that follow, which then still come one per frame
let release = self.synthetic.iter().position(|e| matches!(e, egui::Event::Key { pressed: false, .. })).map(|i| i + 1);
let next_pointer = self.synthetic.iter().position(pointer);
match (release, next_pointer) {
(Some(r), Some(q)) => r.min(q),
(r, q) => r.or(q).unwrap_or(self.synthetic.len()),
}
};
// A press needs the pointer at its position before the button event; the move that is
// itself the first event is delivered by the drain below, so it must not be sent twice.
Expand Down
13 changes: 11 additions & 2 deletions crates/ui-egui/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,8 +693,17 @@ pub(crate) fn gesture_events(app: &mut LightcraftApp, ctx: &egui::Context, metho
"ui.key" => {
let Some(k) = s("key").and_then(key_from) else { return Err("unknown or missing `key`".into()) };
let m = modifiers(p);
app.synthetic.push(egui::Event::Key { key: k, physical_key: None, pressed: true, repeat: false, modifiers: m });
app.synthetic.push(egui::Event::Key { key: k, physical_key: None, pressed: false, repeat: false, modifiers: m });
// {action?: "press" | "down" | "up"}: down … up holds the key across other input (Space
// held over a drag); a press is both in one frame
let (down, up) = match s("action").unwrap_or("press") {
"press" => (true, true),
"down" => (true, false),
"up" => (false, true),
other => return Err(format!("ui.key: action {other:?} (press, down or up)")),
};
for pressed in [down.then_some(true), up.then_some(false)].into_iter().flatten() {
app.synthetic.push(egui::Event::Key { key: k, physical_key: None, pressed, repeat: false, modifiers: m });
}
ctx.request_repaint();
Ok(GestureOutcome::done(Value::Null))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/ui-egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub mod render;
pub mod shortcuts;
pub mod sidebar;
pub mod softpaint;
pub mod space_hand;
pub mod state;
pub mod sync;
pub mod tasks;
Expand Down Expand Up @@ -259,6 +260,8 @@ pub struct LightcraftApp {
next_widget_hits: Vec<(String, widgets::WidgetHit)>,
/// In-progress on-canvas gesture (brush stroke points, gradient drag…).
pub gesture: Option<panels::detail::Gesture>,
/// Space is held over a tool on the photo: the view's hand has the pointer ([`space_hand`]).
pub space_hand: Option<space_hand::SpaceHand>,
/// The brush stroke being painted, as drawn on the photo until it's committed (issue #517).
pub brush_live: Option<brush_live::LiveStroke>,
/// What the loupe drew last frame: photo and source ("render", "cached", "embedded", "small",
Expand Down
18 changes: 15 additions & 3 deletions crates/ui-egui/src/panels/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
let tv = ToolView { right, map: &map, frame: &frame, d: &d, id, split, native, aspect };
tool_interaction(app, ui, &resp, &tv, &Views { img_rect, target_rect, before_img, before_target, areas: &areas, main_area, canvas });
}
// Space held over a tool: the view's hand takes the pointer from it (#1895)
crate::space_hand::interact(app, ui, canvas, (main_area, img_rect), split.then(|| (areas[0], before_img)));
// drawn and hit-tested above the loupe and its tools: clicks on it pan
let nav = navigator(app, ui, canvas, img_rect, id);
// what is on screen isn't the render on its way (a stand-in, the last render after an edit,
Expand Down Expand Up @@ -1028,8 +1030,19 @@ fn general_interaction(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::
}
return;
}
// click toggles Fit ↔ the chosen click-zoom ratio at the clicked point; drag pans when zoomed
let zoomed = img.width() > canvas.width() + 1.0 || img.height() > canvas.height() + 1.0;
click_zoom_and_pan(app, resp, img, canvas);
let _ = (native, aspect);
}

/// The photo `img` reaches past its pane `canvas`: a drag pans it.
pub(crate) fn zoomed(img: Rect, canvas: Rect) -> bool {
img.width() > canvas.width() + 1.0 || img.height() > canvas.height() + 1.0
}

/// The loupe's own click and drag (also Space's hand over a tool, `space_hand`): a click toggles
/// Fit ↔ the chosen click-zoom ratio at the clicked point, a drag pans when zoomed.
pub(crate) fn click_zoom_and_pan(app: &mut LightcraftApp, resp: &egui::Response, img: Rect, canvas: Rect) {
let zoomed = zoomed(img, canvas);
if resp.double_clicked() || (resp.clicked() && !zoomed) {
if let Some(q) = resp.interact_pointer_pos() {
let u = ((q.x - img.left()) / img.width()).clamp(0.0, 1.0);
Expand All @@ -1045,7 +1058,6 @@ fn general_interaction(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: &egui::
if zoomed && resp.dragged() {
pan_image(app, canvas, img, resp.drag_delta());
}
let _ = (native, aspect);
}

#[cfg(test)]
Expand Down
19 changes: 18 additions & 1 deletion crates/ui-egui/src/shortcuts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ fn keyboard_owned(app: &LightcraftApp, ctx: &egui::Context) -> bool {
/// text field, name edit or shortcut recording owns input, then fires saved command bindings,
/// aliases and fixed photo keys, respecting native menu ownership and open popups.
pub fn handle(app: &mut LightcraftApp, ctx: &egui::Context) {
// Space held over a tool ends when the key is up, whoever has the keyboard by then
crate::space_hand::tick(app, ctx);
if !matches!(app.ui.dialog, Some(crate::state::Dialog::Shortcuts)) {
app.recording_shortcut = None;
}
Expand Down Expand Up @@ -425,6 +427,8 @@ fn pressed_commands(app: &LightcraftApp, ctx: &egui::Context) -> (Vec<String>, V
{
fire.push("view.detail".to_string());
}
// a held Space repeats: it toggles the zoom (or takes the hand) once per press
let space_repeat = !i.events.iter().any(|e| matches!(e, egui::Event::Key { key: Key::Space, pressed: true, repeat: false, .. }));
for b in bindable() {
if let Some(sc) = binding(keymap, b.id, b.default)
&& let Some((m, k)) = parse(sc)
Expand All @@ -433,12 +437,21 @@ fn pressed_commands(app: &LightcraftApp, ctx: &egui::Context) -> (Vec<String>, V
&& matches(i, m, k)
&& let Some(id) = grid_key_command(keymap, grid, app.ui.fullscreen, b.id, (m, k))
{
fire.push(id.to_string());
if id == "view.zoomToggle" && k == Key::Space {
// Toggle Zoom given Space in the keymap is the Space alias: the same hand and
// the same once-per-press
if !space_repeat {
aliased.push(("view.zoomToggle", json!({})));
}
} else {
fire.push(id.to_string());
}
}
}
for (sc, id, params) in ALIASES {
if let Some((m, k)) = parse(sc).filter(|_| !native(sc))
&& !(k == Key::Escape && popup_open)
&& !(k == Key::Space && space_repeat)
&& !taken.contains(&(m, k))
&& matches(i, m, k)
{
Expand Down Expand Up @@ -505,6 +518,10 @@ fn fire_alias(app: &mut LightcraftApp, ctx: &egui::Context, id: &str, params: se
}
return;
}
// over a tool on the photo, held Space is the view's hand; a tap toggles on its release
if id == "view.zoomToggle" && crate::space_hand::begin(app, ctx) {
return;
}
// flag/rate aliases (Shift+X…) go through the culling path: active photo in Compare/Survey,
// `advance` moves to the next candidate there
if matches!(id, "photo.flag" | "photo.rate" | "photo.label") {
Expand Down
100 changes: 100 additions & 0 deletions crates/ui-egui/src/space_hand.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! Space held over a tool on the photo: the view's hand, as in Lightroom (issue #1895).
//!
//! Crop, Masking, Remove, Red Eye and the pickers (white balance, colour range, point colour, a
//! targeted adjustment) take the primary click and drag on the photo, so the loupe's own click
//! zoom and drag pan are out of reach while one is open. Holding Space lends them back until the
//! key goes up: a click zooms (Fit ↔ the click-zoom ratio, at the point clicked), a drag pans the
//! zoomed photo, and the tool sees neither. A quick tap with no click still toggles the zoom, on
//! its release. Without a tool Space toggles the zoom on the press, as before. A held key's
//! auto-repeat toggles neither (`shortcuts::handle` lets only a fresh press through).

use egui::{CursorIcon, Key, Rect, Sense};
use serde_json::json;

use crate::LightcraftApp;
use crate::state::{RightPanel, ViewMode};

/// The longest Space press (s) that still toggles the zoom when it lets go without a click.
pub(crate) const TAP: f64 = 0.4;

/// Space is held over a tool on the photo.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SpaceHand {
/// When the key went down (egui input time, s).
since: f64,
/// A pointer button went down while it was held: letting go is not a tap.
used: bool,
}

/// A tool on the photo takes the primary click and drag, so Space has to lend them to the view.
pub(crate) fn tool_owns_pointer(app: &LightcraftApp) -> bool {
// the full-screen preview shows no tool overlays, but a picker still takes its click there
let panel = !app.ui.fullscreen && matches!(app.ui.right, RightPanel::Crop | RightPanel::Masking | RightPanel::Remove | RightPanel::RedEye);
let picker = matches!(app.ui.tool.as_str(), "wbPicker" | "colorRange" | "pointColor") || app.ui.tool.starts_with("tat:");
app.ui.view == ViewMode::Detail && app.ui.slideshow.is_none() && app.session.active().is_some() && (panel || picker)
}

/// Space went down (a fresh press, not a repeat). Over a tool the hand takes over until the key
/// is up and the press does nothing else (true); otherwise false leaves it to Toggle Zoom.
pub(crate) fn begin(app: &mut LightcraftApp, ctx: &egui::Context) -> bool {
if !tool_owns_pointer(app) {
return false;
}
// in the middle of a stroke or a drag the press neither zooms nor lends the pointer
let (now, busy) = ctx.input(|i| (i.time, i.pointer.any_down()));
if !busy && app.space_hand.is_none() {
app.space_hand = Some(SpaceHand { since: now, used: false });
}
true
}

/// Runs every frame before the shortcuts: ends the hold once Space is up and a drag it started has
/// ended, or at once when the tool closes or a dialog opens. A quick tap with no click toggles the
/// zoom as it ends.
pub(crate) fn tick(app: &mut LightcraftApp, ctx: &egui::Context) {
let Some(mut hand) = app.space_hand else { return };
let (held, pointer, pressed, now) = ctx.input(|i| (i.key_down(Key::Space), i.pointer.any_down(), i.pointer.any_pressed(), i.time));
// a quick click can go down and up within one frame: no button is down then, but it pressed
hand.used |= pointer || pressed;
let tool = tool_owns_pointer(app) && app.ui.dialog.is_none();
// held, or let go in the middle of a pan: the hand stays until the button is up
if tool && (held || pointer) {
app.space_hand = Some(hand);
return;
}
app.space_hand = None;
if tool && !hand.used && now - hand.since <= TAP {
let _ = app.run("view.zoomToggle", json!({}));
}
}

/// The hand over the photo while Space is held. Registered above the loupe and the tool overlays
/// (the navigator stays above it), so it takes the click or the drag and they get neither: they
/// still draw, without the pointer. `main` is the pane the photo `img` is laid out in; `before`
/// the Before pane of a side-by-side or stacked view with its photo, where a press there acts.
pub(crate) fn interact(app: &mut LightcraftApp, ui: &mut egui::Ui, canvas: Rect, main: (Rect, Rect), before: Option<(Rect, Rect)>) {
if app.space_hand.is_none() {
return;
}
let resp = ui.interact(canvas, egui::Id::unique("loupe-hand"), Sense::click_and_drag());
crate::widgets::register(ui, "loupe:hand", canvas);
let at = resp.interact_pointer_pos().or(resp.hover_pos());
let (pane, img) = match (before, at) {
(Some((pane, img)), Some(q)) if pane.contains(q) => (pane, img),
_ => main,
};
if resp.is_pointer_button_down_on()
&& let Some(hand) = app.space_hand.as_mut()
{
hand.used = true;
}
let zoomed = crate::panels::detail::zoomed(img, pane);
crate::panels::detail::click_zoom_and_pan(app, &resp, img, pane);
if resp.hovered() || resp.dragged() {
ui.ctx().set_cursor_icon(match (zoomed, resp.dragged()) {
(true, true) => CursorIcon::Grabbing,
(true, false) => CursorIcon::Grab,
(false, _) => CursorIcon::ZoomIn,
});
}
}
1 change: 1 addition & 0 deletions crates/ui-egui/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod scroll;
mod slider_reset;
mod slideshow;
mod solo_sections;
mod space_hand;
mod switch_library;
mod sync;
mod tallies;
Expand Down
Loading
Loading