PhotoCraft: An open-source, photo editor written by scratch in pure Rust
macOS: menus open on press; only the title bar's free gap drags the window (#669)
jason4short9aef02cac3a2
diff --git a/Cargo.lock b/Cargo.lockindex 61a12e903..b0f9c370d 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3428,6 +3428,9 @@ dependencies = ["egui","fmv-macos-events","log",+ "objc2 0.6.4",+ "objc2-app-kit 0.3.2",+ "objc2-foundation 0.3.2","open","photocraft-automation","photocraft-codecs",diff --git a/apps/photocraft/Cargo.toml b/apps/photocraft/Cargo.tomlindex 9c4b0976d..cb2df1161 100644--- a/apps/photocraft/Cargo.toml+++ b/apps/photocraft/Cargo.toml@@ -46,6 +46,11 @@ ron = "0.12"# registers the handler (Objective-C interop needs unsafe, which our workspace forbids) behind a# safe API. Pinned exactly: review the diff before bumping.fmv-macos-events = "=0.1.0"+# Turn off AppKit's title bar dragging (mac_window.rs) with safe AppKit calls. Already in+# Cargo.lock (winit, photocraft-tablet); pinned exactly to the same versions.+objc2 = "=0.6.4"+objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSArray", "NSEnumerator"] }+objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["std", "NSApplication", "NSResponder", "NSWindow"] }[build-dependencies]# Icon + VERSIONINFO for photocraft.exe; build.rs only uses it when targeting Windows.diff --git a/apps/photocraft/src/mac_window.rs b/apps/photocraft/src/mac_window.rsnew file mode 100644index 000000000..073ec1593--- /dev/null+++ b/apps/photocraft/src/mac_window.rs@@ -0,0 +1,20 @@+//! macOS title bar dragging. The window draws under a transparent title bar (`main.rs`), so AppKit+//! would otherwise drag the window from anywhere in that strip, menus included: pressing a menu+//! title and dragging down to an item moved the window instead. Turning AppKit's own dragging off+//! leaves only the app's drag region (the free gap between the menus and the controls,+//! `panels::title_bar`), which starts a drag explicitly (`ViewportCommand::StartDrag`, AppKit's+//! `performWindowDragWithEvent:`, which works on an unmovable window). All safe objc2 calls.++use objc2::MainThreadMarker;+use objc2_app_kit::NSApplication;++/// Stop AppKit dragging the app's windows by their title bar strip.+pub fn disable_native_title_drag() {+ let Some(mtm) = MainThreadMarker::new() else {+ log::warn!("title bar: not on the main thread; AppKit window dragging left on");+ return;+ };+ for window in NSApplication::sharedApplication(mtm).windows().iter() {+ window.setMovable(false);+ }+}diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rsindex 3141f2b82..8147d6224 100644--- a/apps/photocraft/src/main.rs+++ b/apps/photocraft/src/main.rs@@ -24,6 +24,8 @@ mod apple_events;mod control_server;mod crash_guard;mod gpu_startup;+#[cfg(target_os = "macos")]+mod mac_window;// Pure logic is tested on every platform; only Linux runs the check.#[cfg(any(target_os = "linux", test))]mod linux_libs;@@ -199,6 +201,9 @@ fn main() -> eframe::Result {}let mut app = PhotocraftApp::new(Session::new(), services);app.integrated_titlebar = cfg!(target_os = "macos");+ // Only the title bar's free gap drags the window, never the menus (mac_window.rs).+ #[cfg(target_os = "macos")]+ mac_window::disable_native_title_drag();// Long commands and file opens run as background jobs with progress and Cancel (#210).app.background_jobs = std::env::var_os("PHOTOCRAFT_INLINE_JOBS").is_none();// Displays and their profiles (#569): wait briefly so the first frames already usediff --git a/crates/ui-egui/src/menu_nav_tests.rs b/crates/ui-egui/src/menu_nav_tests.rsindex 56f74aecd..89d09deee 100644--- a/crates/ui-egui/src/menu_nav_tests.rs+++ b/crates/ui-egui/src/menu_nav_tests.rs@@ -517,3 +517,31 @@ fn a_maximized_window_or_an_unknown_monitor_uses_the_whole_window() {let h = harness((1366.0, 768.0, 1.0));assert_eq!(crate::work_area::visible_rect(&h.ctx), h.ctx.content_rect());}++/// One gesture: press a menu title (it opens on the press, not the release), drag down to an+/// item and release on it to run it. A plain click still opens the menu and leaves it open.+#[test]+fn press_drag_release_runs_a_menu_item() {+ use egui::{Modifiers, PointerButton};+ let mut h = harness((1280.0, 720.0, 1.0));+ let rulers = h.state().ui.extras.rulers;+ let title = h.get_by_label("View").rect().center();+ h.event(egui::Event::PointerMoved(title));+ h.run_steps(1);+ h.event(egui::Event::PointerButton { pos: title, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });+ h.run_steps(2);+ assert!(Nav::current(&h.ctx).rows.first().is_some_and(|r| !r.is_empty()), "the menu opens on the press");+ let item = h.get_by_label_contains("Rulers").rect().center();+ for k in 1..=8 {+ h.event(egui::Event::PointerMoved(title + (item - title) * (k as f32 / 8.0)));+ h.run_steps(1);+ }+ h.event(egui::Event::PointerButton { pos: item, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE });+ h.run_steps(3);+ assert_eq!(h.state().ui.extras.rulers, !rulers, "releasing on Rulers ran it");+ assert!(Nav::current(&h.ctx).rows.first().is_none_or(|r| r.is_empty()), "and closed the menu");+ // A click (press and release on the title) opens the menu and leaves it open.+ h.get_by_label("View").click();+ h.run_steps(4);+ assert!(Nav::current(&h.ctx).rows.first().is_some_and(|r| !r.is_empty()), "a click leaves it open");+}diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex 463feb755..b1fb6d9c4 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -758,16 +758,33 @@ pub fn menu_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> f32 {nav.bar_bottom = Some(ui.max_rect().bottom());let mut buttons = Vec::with_capacity(TOP_MENUS.len());for top in TOP_MENUS {- let r = ui.menu_button(egui::RichText::new(crate::i18n::tr(lang, top)).color(t.text_dim), |ui| {- let items = items.get_or_init(|| menu_items(app_ref));- let mine: Vec<&MenuItem> = items.iter().filter(|i| i.path.first().map(String::as_str) == Some(top)).collect();- ui.set_min_width(220.0);- if mine.is_empty() {- ui.weak(crate::i18n::tr(lang, "(coming soon)"));- }- render_level(ui, &mine, 1, &mut clicked, &mut nav);- });- buttons.push(r.response);+ // egui's menu_button toggles on release; these titles open on the press (one+ // gesture can press, drag to an item and release), so each drives its popup.+ let title = ui.add(egui::Button::new(egui::RichText::new(crate::i18n::tr(lang, top)).color(t.text_dim)));+ // The bar's close behaviour and style, as a menu (not bar) config so submenus inside+ // render as submenus.+ let bar = egui::containers::menu::MenuConfig::find(ui);+ let config = egui::containers::menu::MenuConfig::new().close_behavior(bar.close_behavior).style(bar.style.clone());+ let open = title_press(ui.ctx(), &title);+ // The release ending the press that opened this menu is a click "outside" the+ // popup: it must not close it again.+ let opening = title.clicked() && ui.ctx().data(|d| d.get_temp::<bool>(press_gesture_id())).unwrap_or(false);+ let close = if opening { egui::PopupCloseBehavior::IgnoreClicks } else { config.close_behavior };+ egui::Popup::menu(&title)+ .open_memory(open)+ .close_behavior(close)+ .style(config.style.clone())+ .info(egui::UiStackInfo::new(egui::UiKind::Menu).with_tag_value(egui::containers::menu::MenuConfig::MENU_CONFIG_TAG, config))+ .show(|ui| {+ let items = items.get_or_init(|| menu_items(app_ref));+ let mine: Vec<&MenuItem> = items.iter().filter(|i| i.path.first().map(String::as_str) == Some(top)).collect();+ ui.set_min_width(220.0);+ if mine.is_empty() {+ ui.weak(crate::i18n::tr(lang, "(coming soon)"));+ }+ render_level(ui, &mine, 1, &mut clicked, &mut nav);+ });+ buttons.push(title);}right = buttons.iter().map(|b| b.rect.right()).fold(right, f32::max);switch_on_hover(ui.ctx(), &buttons);@@ -776,6 +793,10 @@ pub fn menu_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> f32 {});});nav.store(ui.ctx());+ // The press-drag gesture ends with the button (its release was handled by the rows above).+ if ui.input(|i| i.pointer.primary_released() || !i.pointer.primary_down()) {+ ui.ctx().data_mut(|d| d.remove::<bool>(press_gesture_id()));+ }if let Some(id) = clicked {let ctx = ui.ctx().clone();let id = alt_click(id, ctx.input(|i| i.modifiers.alt));@@ -786,6 +807,34 @@ pub fn menu_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) -> f32 {right}+fn press_gesture_id() -> egui::Id {+ egui::Id::new("menu-press-gesture")+}++/// A menu title's open/close command this frame, the press opens a closed menu (and+/// starts a press-drag gesture: releasing on an item runs it) or closes an open one; the release+/// never toggles, so the menu doesn't blink.+fn title_press(ctx: &egui::Context, title: &egui::Response) -> Option<egui::SetOpenCommand> {+ // A press this frame on the title (still down, or a whole click within one frame).+ let pressed = ctx.input(|i| i.pointer.primary_pressed()) && (title.is_pointer_button_down_on() || title.clicked());+ if !pressed {+ return None;+ }+ let open = egui::Popup::is_id_open(ctx, egui::Popup::default_response_id(title));+ if !open {+ ctx.data_mut(|d| d.insert_temp(press_gesture_id(), true));+ }+ Some(egui::SetOpenCommand::Bool(!open))+}++/// Did the press-drag gesture that opened the menus end over `item` (released on it)?+fn released_on(ui: &egui::Ui, item: &egui::Response) -> bool {+ item.enabled()+ && item.contains_pointer()+ && ui.input(|i| i.pointer.primary_released())+ && ui.ctx().data(|d| d.get_temp::<bool>(press_gesture_id())).unwrap_or(false)+}+/// True only when the pointer can actually reach a menu title. A tall submenu can be/// repositioned upward by egui and overlap the menu bar; in that case the popup's layer is/// top-most and hovering it must not switch the open top-level menu.@@ -865,7 +914,7 @@ fn render_level_rows(ui: &mut egui::Ui, items: &[&MenuItem], depth: usize, click"image.mode.bits32" => r.on_hover_text(crate::i18n::tr(lang, "Floating point")),_ => r,};- let hit = r.clicked();+ let hit = r.clicked() || released_on(ui, &r);(r, hit)});if hit {diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rsindex 535662c34..2c37bfb1c 100644--- a/crates/ui-egui/src/panels.rs+++ b/crates/ui-egui/src/panels.rs@@ -289,7 +289,13 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {.frame(egui::Frame::NONE.fill(t.chrome).inner_margin(egui::Margin { left, right: 10, top: 0, bottom: 0 })).show(ui, |ui| {let full = ui.max_rect();- let drag = ui.interact(full, ui.id().with("titledrag"), Sense::click_and_drag());+ // Only the free gap between the menus and the right-hand controls drags the window: a+ // press on a menu title must open the menu, never move the window. The gap is last+ // frame's, as the menus are laid out after this.+ let span_id = ui.id().with("titledrag-span");+ let (gap_l, gap_r) = ui.ctx().data(|d| d.get_temp::<(f32, f32)>(span_id)).unwrap_or((full.right(), full.right()));+ let gap = egui::Rect::from_x_y_ranges(gap_l.max(full.left())..=gap_r.min(full.right()).max(gap_l), full.y_range());+ let drag = ui.interact(gap, ui.id().with("titledrag"), Sense::click_and_drag());if drag.drag_started() {ui.ctx().send_viewport_cmd(egui::ViewportCommand::StartDrag);}@@ -341,6 +347,7 @@ pub fn title_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {}).inner;});+ ui.ctx().data_mut(|d| d.insert_temp(span_id, (menus_right, controls_left)));let font = theme::medium(13.0);let galley = ui.painter().layout_no_wrap(title.clone(), font.clone(), t.text_dim);if let Some(x) = title_x(full.center().x, menus_right, controls_left, galley.size().x) {