LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust
Top bar: a press that starts on a button or the search field never drags the window
Nathaniel Ledfordcc8d6174597b
diff --git a/crates/ui-egui/src/panels/topbar.rs b/crates/ui-egui/src/panels/topbar.rsindex 7451c8acb..d06a75594 100644--- a/crates/ui-egui/src/panels/topbar.rs+++ b/crates/ui-egui/src/panels/topbar.rs@@ -14,10 +14,22 @@ fn window_handle(app: &LightcraftApp, ui: &mut egui::Ui, content: Rect, margin_luse crate::titlebar::{Gesture, WindowState, command_for};let bar = Rect::from_min_max(pos2(content.left() - margin_left, content.top()), pos2(content.right() + margin_right, content.bottom()));register(ui.ctx(), "region:titlebar", bar);- let resp = ui.interact(bar, egui::Id::new("titlebar-handle"), Sense::click_and_drag());+ let handle = egui::Id::new("titlebar-handle");+ let resp = ui.interact(bar, handle, Sense::click_and_drag());+ // a press that began on a button or the search field is theirs: dragging off it must not move the window+ // (while pressing, egui hovers every widget under the pointer; once the drag starts, only the dragged one)+ let ctx = ui.ctx().clone();+ let memo = egui::Id::new("titlebar-press-on-widget");+ if resp.is_pointer_button_down_on() && ctx.input(|i| i.pointer.primary_pressed()) {+ let hovered = ctx.interaction_snapshot(|s| s.hovered.clone());+ let on_widget =+ hovered.iter().any(|id| *id != handle && ctx.read_response(*id).is_some_and(|r| r.sense.senses_click() || r.sense.senses_drag()));+ ctx.data_mut(|d| d.insert_temp(memo, on_widget));+ }+ let on_widget = ctx.data(|d| d.get_temp::<bool>(memo)).unwrap_or(false);let gesture = if resp.double_clicked_by(egui::PointerButton::Primary) {Some(Gesture::DoubleClicked)- } else if resp.drag_started_by(egui::PointerButton::Primary) {+ } else if resp.drag_started_by(egui::PointerButton::Primary) && !on_widget {Some(Gesture::DragStarted)} else {Nonediff --git a/crates/ui-egui/src/tests_titlebar.rs b/crates/ui-egui/src/tests_titlebar.rsindex f1ff782ef..40eba4b13 100644--- a/crates/ui-egui/src/tests_titlebar.rs+++ b/crates/ui-egui/src/tests_titlebar.rs@@ -46,7 +46,7 @@ fn double_click_on_the_empty_bar_zooms_then_restores() {play(&mut h);assert_eq!(h.window_commands, vec![ViewportCommand::Maximized(true)]);// a pause longer than egui's multi-click window, or the second pair would count as a triple click- for _ in 0..120 {+ for _ in 0..30 {h.step();}h.request("ui.click", json!({"x": p.x, "y": p.y, "count": 2}), T);@@ -84,6 +84,16 @@ fn double_click_on_a_button_does_not_zoom() {assert!(h.window_commands.is_empty(), "{:?}", h.window_commands);}+// Scenario: pressing a button and dragging off it does not move the window (as in a native title bar)+#[test]+fn dragging_from_a_button_does_not_move_the_window() {+ let mut h = integrated();+ let r = widget(&h, "icon:help");+ h.request("ui.drag", json!({"x": r.center().x, "y": r.center().y, "toX": r.center().x - 150.0, "toY": r.center().y + 30.0}), T);+ play(&mut h);+ assert!(h.window_commands.is_empty(), "{:?}", h.window_commands);+}+// Scenario: double-clicking the search field selects a word, it does not zoom the window#[test]fn double_click_in_the_search_field_does_not_zoom() {