From fcdbe5ac7fe79d846e9ddd36675e1f1ce59a3999 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 3 Dec 2026 15:36:56 +0800 Subject: [PATCH] desktop: Route Edit menu Copy to the focused view's key bindings Choosing Edit > Copy with the mouse sent copy: to the root view on macOS, which only copied selected text: a focused DataTable (whose Cmd+C is a `mod+c` binding) copied nothing, yet the item stayed enabled. The menu's edit commands now run the bindings their standard key has where the focus is, as pressing the key does, before the default. - macOS: the root view's copy: and selectAll: run the Cmd+C / Cmd+A bindings first; new cut: and paste: run Cmd+X / Cmd+V bindings. validateMenuItem: enables Copy only when a binding or selected text takes it, and Cut / Paste when a binding does. A key-down in flight is skipped (the keyboard monitor already ran the bindings). - Linux: the edit roles run the Ctrl+ bindings before the focused widget's clipboard / selection action. - Windows already replays Ctrl+ through the keymap; unchanged. No public API changes. Adds the native check menu_copy (NSH_MENU_COPY_CHECK_ONLY=1) and keystroke unit tests. Refs #4177 Co-Authored-By: Claude Opus 5.5 Agent: rust-maintainer --- crates/nsh-platform-linux/src/chrome.rs | 15 +- crates/nsh-platform-linux/src/keymap.rs | 53 +++++- crates/nsh-platform-macos/src/keymap.rs | 123 ++++++++++-- crates/nsh-platform-macos/src/view.rs | 84 ++++++++- .../nsh-platform-macos/tests/native/main.rs | 5 + .../tests/native/menu_copy.rs | 178 ++++++++++++++++++ 6 files changed, 420 insertions(+), 38 deletions(-) create mode 100644 crates/nsh-platform-macos/tests/native/menu_copy.rs diff --git a/crates/nsh-platform-linux/src/chrome.rs b/crates/nsh-platform-linux/src/chrome.rs index 4ef7fbe2bf..7b0e0d349a 100644 --- a/crates/nsh-platform-linux/src/chrome.rs +++ b/crates/nsh-platform-linux/src/chrome.rs @@ -959,13 +959,18 @@ pub(crate) fn choose(surface: &Rc, id: u32, role: Option) { _ => "selection.select-all", }; // Once the menu closed and gave the focus back to the field. + // The bindings of the role's key where the focus is come first, + // as when the key is pressed (a focused table copies its + // cells); else the focused widget's own action. let weak = Rc::downgrade(surface); glib::idle_add_local_once(move || { - let focus = weak - .upgrade() - .and_then(|s| window(&s)) - .and_then(|w| GtkWindowExt::focus(&w)); - if let Some(focus) = focus { + let Some(surface) = weak.upgrade() else { + return; + }; + if crate::keymap::menu_command(&surface, role) { + return; + } + if let Some(focus) = window(&surface).and_then(|w| GtkWindowExt::focus(&w)) { let _ = focus.activate_action(name, None); } }); diff --git a/crates/nsh-platform-linux/src/keymap.rs b/crates/nsh-platform-linux/src/keymap.rs index 1973d6c080..69a3af5247 100644 --- a/crates/nsh-platform-linux/src/keymap.rs +++ b/crates/nsh-platform-linux/src/keymap.rs @@ -199,11 +199,39 @@ pub(crate) fn clear(surface: &Surface) { surface.keymap.keymap.borrow_mut().take(); } +/// An Edit menu command chosen in a menu (`role`, Copy): the bindings of +/// its standard key (Ctrl+C) where the focus is decide first, as when the +/// key is pressed, so a focused table copies its cells. Whether one took +/// the command (one passing its key through does not: the focused +/// widget's own action still runs). +pub(crate) fn menu_command(surface: &Surface, role: nsh_core::desktop::MenuRole) -> bool { + menu_keystroke(role).is_some_and(|stroke| press(surface, stroke)) +} + +/// The keystroke of a menu command's standard shortcut off a Mac (Copy's +/// Ctrl+C); none for a key other than a character's. +fn menu_keystroke(role: nsh_core::desktop::MenuRole) -> Option { + let shortcut = role.shortcut(false)?; + let nsh_core::desktop::Key::Char(c) = shortcut.key else { + return None; + }; + Some(Keystroke { + modifiers: Modifiers { + ctrl: shortcut.ctrl, + alt: shortcut.alt, + shift: shortcut.shift, + cmd: shortcut.meta, + alt_graph: false, + }, + key: Key::Char(c), + }) +} + /// A key pressed without a GDK event (GTK takes no made-up ones; `nsh test -/// --target linux`): the newest binding of `stroke` that fires where the -/// focus is reports it, as its shortcut's action would. Whether it took -/// the key: false if none fired or the one that did passes it through. -#[cfg(feature = "dev")] +/// --target linux`; a menu command, [`menu_command`]): the newest binding +/// of `stroke` that fires where the focus is reports it, as its shortcut's +/// action would. Whether it took the key: false if none fired or the one +/// that did passes it through. pub(crate) fn press(surface: &Surface, stroke: Keystroke) -> bool { let keymap = surface.keymap.keymap.borrow().clone(); let Some(keymap) = keymap else { @@ -576,6 +604,23 @@ fn trigger( #[cfg(test)] mod tests { + + /// An Edit menu command runs the bindings of the key an app writes for + /// it (`mod+c` for Copy, #4177). + #[test] + fn a_menu_command_is_the_key_its_bindings_name() { + use nsh_core::{desktop::MenuRole, keymap::Keystroke, platform::Os}; + for (role, keys) in [ + (MenuRole::Copy, "mod+c"), + (MenuRole::Cut, "mod+x"), + (MenuRole::Paste, "mod+v"), + (MenuRole::SelectAll, "mod+a"), + ] { + let bound = Keystroke::parse(keys, Os::Linux).unwrap(); + assert_eq!(super::menu_keystroke(role), Some(bound), "{role:?}"); + } + assert_eq!(super::menu_keystroke(MenuRole::About), None); + } use gtk4 as gtk; use nsh_core::{ ROOT_ID, diff --git a/crates/nsh-platform-macos/src/keymap.rs b/crates/nsh-platform-macos/src/keymap.rs index 2561850862..9876a852ad 100644 --- a/crates/nsh-platform-macos/src/keymap.rs +++ b/crates/nsh-platform-macos/src/keymap.rs @@ -78,21 +78,10 @@ pub(crate) fn pressed(surface: &Rc, event: &NSEvent) -> bool { }; // Before a binding acts: the focus it moves shows (`focus-visible:`). surface.control(|c| c.set_input_modality(nsh_core::platform::InputModality::Keyboard)); - let own = root.ns() != surface.root.ns(); - let window_root = root.id(); - if crate::window::blocked(surface, own.then_some(&root)) { - return false; - } - crate::input::enter(surface, own.then_some(root)); - let Some(editing) = editing(surface, &window) else { + let control = || crate::input::control_activation_target(surface, event).is_some(); + let Some((window_root, focus)) = focus_in(surface, &window, root, control) else { return false; }; - if composing(&window) { - return false; - } - // Return and Space on a focused control fire only the focus' own - // bindings; else the control takes them (`input::key`). - let control = crate::input::control_activation_target(surface, event).is_some(); // A key another layout types no US key for (`keystroke` is none) can // still type text. let stroke = keystroke(event); @@ -100,13 +89,6 @@ pub(crate) fn pressed(surface: &Rc, event: &NSEvent) -> bool { if cfg!(feature = "dev") && stroke.is_some_and(|k| k.reloads(Os::MacOs)) { return false; } - // Only the bindings of the window the key is in (and JS's) take it. - // A focus a dialog covers has no keys of its own: the dialog's take - // them. - let mut focus = nsh_core::keymap::KeyFocus::default(); - focus.editing = editing; - focus.control = control; - focus.covered = crate::input::focus_covered(surface); let repeat = event.isARepeat(); let found = match stroke { Some(keystroke) => table.find_with(window_root, keystroke, focus, repeat), @@ -137,6 +119,90 @@ pub(crate) fn pressed(surface: &Rc, event: &NSEvent) -> bool { found.consumed() } +/// The window root and the focus the bindings of a key in `window` are +/// matched for; none when the key is not the surface's (a modal blocks +/// it, the focus is in another surface's views, an input method is +/// composing). `control`: whether the focus is on a control taking Return +/// and Space, asked once the input is in `window`. +fn focus_in( + surface: &Rc, + window: &NSWindow, + root: crate::view::NshView, + control: impl FnOnce() -> bool, +) -> Option<(nsh_core::NodeId, nsh_core::keymap::KeyFocus)> { + let own = root.ns() != surface.root.ns(); + let window_root = root.id(); + if crate::window::blocked(surface, own.then_some(&root)) { + return None; + } + crate::input::enter(surface, own.then_some(root)); + let editing = editing(surface, window)?; + if composing(window) { + return None; + } + // Only the bindings of the window the key is in (and JS's) take it. + // A focus a dialog covers has no keys of its own: the dialog's take + // them. Return and Space on a focused control fire only the focus' + // own bindings; else the control takes them (`input::key`). + let mut focus = nsh_core::keymap::KeyFocus::default(); + focus.editing = editing; + focus.control = control(); + focus.covered = crate::input::focus_covered(surface); + Some((window_root, focus)) +} + +/// An Edit menu command chosen with the mouse (or by accessibility) in +/// `window`, whose standard shortcut is `stroke` (Copy's Cmd+C): the +/// bindings that key would fire where the focus is decide, as they do when +/// the key is pressed (the monitor runs them before the menu's key +/// equivalent), so a focused table copies its cells. Reports the binding +/// when `fire`; whether one takes the command (one passing its key +/// through does not: the default still runs). +pub(crate) fn command( + surface: &Rc, + window: &NSWindow, + stroke: Keystroke, + fire: bool, +) -> bool { + let Some(table) = surface.keymap.table.borrow().clone() else { + return false; + }; + let Some(root) = crate::window::root_in(surface, window) else { + return false; + }; + let Some((window_root, focus)) = focus_in(surface, window, root, || false) else { + return false; + }; + let found = table.find_with(window_root, stroke, focus, false); + if fire && let Some(id) = found.fired() { + surface.event( + ROOT_ID, + EventKind::KeyCommand, + EventPayload::Value(PayloadValue::Number(id as f64)), + ); + } + found.consumed() +} + +/// The keystroke of a menu command's standard shortcut on a Mac (Copy's +/// Cmd+C). +pub(crate) fn menu_keystroke(role: nsh_core::desktop::MenuRole) -> Option { + let shortcut = role.shortcut(true)?; + let nsh_core::desktop::Key::Char(c) = shortcut.key else { + return None; + }; + Some(Keystroke { + modifiers: Modifiers { + ctrl: shortcut.ctrl, + alt: shortcut.alt, + shift: shortcut.shift, + cmd: shortcut.meta, + alt_graph: false, + }, + key: Key::Char(c), + }) +} + /// Whether an input method is composing in the focused input client (it has /// marked text: pinyin, kana). Every key is then the input method's: /// Backspace deletes a letter of the composition, Return and Space @@ -290,6 +356,23 @@ pub(crate) fn function_key_code(n: u8) -> Option { #[cfg(test)] mod tests { + + /// An Edit menu command runs the bindings of the key an app writes for + /// it (`mod+c` for Copy, #4177). + #[test] + fn a_menu_command_is_the_key_its_bindings_name() { + use nsh_core::{desktop::MenuRole, keymap::Keystroke, platform::Os}; + for (role, keys) in [ + (MenuRole::Copy, "mod+c"), + (MenuRole::Cut, "mod+x"), + (MenuRole::Paste, "mod+v"), + (MenuRole::SelectAll, "mod+a"), + ] { + let bound = Keystroke::parse(keys, Os::MacOs).unwrap(); + assert_eq!(super::menu_keystroke(role), Some(bound), "{role:?}"); + } + assert_eq!(super::menu_keystroke(MenuRole::About), None); + } #[test] fn function_keys() { assert_eq!(super::function_key(122), Some(1)); diff --git a/crates/nsh-platform-macos/src/view.rs b/crates/nsh-platform-macos/src/view.rs index 2464f0be5b..9da9332eca 100644 --- a/crates/nsh-platform-macos/src/view.rs +++ b/crates/nsh-platform-macos/src/view.rs @@ -42,7 +42,7 @@ use std::{ use nsh_core::{ Bounds, EventMask, Matrix, NodeId, PointerEvents, Points, ROOT_ID, ViewKind, ViewProps, - ViewStyle, animation::AnimValues, + ViewStyle, animation::AnimValues, desktop::MenuRole, }; use objc2::{ AnyThread, DefinedClass, MainThreadMarker, MainThreadOnly, Message, define_class, msg_send, @@ -51,9 +51,9 @@ use objc2::{ sel, }; use objc2_app_kit::{ - NSAccessibility, NSDragOperation, NSDraggingDestination, NSDraggingInfo, NSEvent, NSEventType, - NSResponder, NSTrackingArea, NSTrackingAreaOptions, NSView, NSViewLayerContentsRedrawPolicy, - NSWindowOrderingMode, + NSAccessibility, NSApplication, NSDragOperation, NSDraggingDestination, NSDraggingInfo, + NSEvent, NSEventType, NSResponder, NSTrackingArea, NSTrackingAreaOptions, NSView, + NSViewLayerContentsRedrawPolicy, NSWindowOrderingMode, }; use objc2_core_foundation::{CGAffineTransform, CGFloat, CGPoint, CGRect, CGSize}; use objc2_core_graphics::CGContext; @@ -1004,23 +1004,70 @@ define_class!( self.pointer(event); } - /// The Edit menu's Copy (Cmd+C): the selected text. + /// The Edit menu's Copy (Cmd+C): what the focused view binds to + /// the key (a table's cells), else the selected text. #[unsafe(method(copy:))] fn copy(&self, _sender: Option<&AnyObject>) { - if let Some(surface) = self.input_surface() { + if let Some(surface) = self.input_surface() + && !self.menu_binding(&surface, MenuRole::Copy, true) + { crate::text::copy(&surface); } } - /// The Edit menu's Select All (Cmd+A): a selectable text's or a - /// selectable view's. + /// The Edit menu's Select All (Cmd+A): what the focused view binds + /// to the key, else a selectable text's or a selectable view's. #[unsafe(method(selectAll:))] fn select_all(&self, _sender: Option<&AnyObject>) { - if let Some(surface) = self.input_surface() { + if let Some(surface) = self.input_surface() + && !self.menu_binding(&surface, MenuRole::SelectAll, true) + { crate::text::select_all(&surface); } } + /// The Edit menu's Cut (Cmd+X): what the focused view binds to the + /// key (an editor's selection); nothing else cuts here. + #[unsafe(method(cut:))] + fn cut(&self, _sender: Option<&AnyObject>) { + if let Some(surface) = self.input_surface() { + self.menu_binding(&surface, MenuRole::Cut, true); + } + } + + /// The Edit menu's Paste (Cmd+V): what the focused view binds to the + /// key; nothing else pastes here. + #[unsafe(method(paste:))] + fn paste(&self, _sender: Option<&AnyObject>) { + if let Some(surface) = self.input_surface() { + self.menu_binding(&surface, MenuRole::Paste, true); + } + } + + /// Copy is enabled when it copies something: a binding of its key + /// where the focus is, or selected text; Cut and Paste when their + /// key has a binding there. + #[unsafe(method(validateMenuItem:))] + fn validate_menu_item(&self, item: &objc2_app_kit::NSMenuItem) -> objc2::runtime::Bool { + let action = item.action(); + let role = if action == Some(sel!(copy:)) { + MenuRole::Copy + } else if action == Some(sel!(cut:)) { + MenuRole::Cut + } else if action == Some(sel!(paste:)) { + MenuRole::Paste + } else { + return objc2::runtime::Bool::YES; + }; + let Some(surface) = self.surface().or_else(|| self.ivars().window.borrow().upgrade()) + else { + return objc2::runtime::Bool::NO; + }; + let acts = (role == MenuRole::Copy && crate::text::can_copy(&surface)) + || self.menu_binding(&surface, role, false); + objc2::runtime::Bool::new(acts) + } + /// AppKit Services can take the selected text as plain text. #[unsafe(method(validRequestorForSendType:returnType:))] fn valid_requestor( @@ -1620,6 +1667,25 @@ impl ViewObj { Some(surface) } + /// An Edit menu command chosen in the menu rather than by its key + /// (Copy's Cmd+C): whether a binding of its key takes it where the + /// focus is, reported when `fire`. A pressed key already ran the + /// bindings in the keyboard monitor before the menu's key equivalent + /// reached here, so a key-down in flight takes nothing. + fn menu_binding(&self, surface: &std::rc::Rc, role: MenuRole, fire: bool) -> bool { + let pressed = NSApplication::sharedApplication(self.mtm()) + .currentEvent() + .is_some_and(|e| e.r#type() == NSEventType::KeyDown); + if fire && pressed { + return false; + } + let (Some(window), Some(stroke)) = (self.window(), crate::keymap::menu_keystroke(role)) + else { + return false; + }; + crate::keymap::command(surface, &window, stroke, fire) + } + /// What the root's `hitTest:` returns inside it: a hosted AppKit view /// nsh's picking lands on (past overlays that let the pointer /// through), else the root itself. `point` is in the root's diff --git a/crates/nsh-platform-macos/tests/native/main.rs b/crates/nsh-platform-macos/tests/native/main.rs index 2aa7c45744..1922f71e5e 100644 --- a/crates/nsh-platform-macos/tests/native/main.rs +++ b/crates/nsh-platform-macos/tests/native/main.rs @@ -79,6 +79,8 @@ mod layout_animation_bench; #[cfg(target_os = "macos")] mod mask_corners; #[cfg(target_os = "macos")] +mod menu_copy; +#[cfg(target_os = "macos")] mod menu_labels; #[cfg(target_os = "macos")] mod menu_lifetime; @@ -266,6 +268,8 @@ fn main() { zoom::run(); } else if std::env::var_os("NSH_IME_CHECK_ONLY").is_some() { ime::run(); + } else if std::env::var_os("NSH_MENU_COPY_CHECK_ONLY").is_some() { + menu_copy::run(); } else if std::env::var_os("NSH_IME_KEYMAP_CHECK_ONLY").is_some() { keymap_ime::run(); } else if std::env::var_os("NSH_KEYBOARD_CHECK_ONLY").is_some() { @@ -351,6 +355,7 @@ fn main() { mask_corners::run(); menu_labels::run(); menu_lifetime::run(); + menu_copy::run(); file_panels::run(); web_file_input::run(); tray_owner::run(); diff --git a/crates/nsh-platform-macos/tests/native/menu_copy.rs b/crates/nsh-platform-macos/tests/native/menu_copy.rs new file mode 100644 index 0000000000..8c3e336da9 --- /dev/null +++ b/crates/nsh-platform-macos/tests/native/menu_copy.rs @@ -0,0 +1,178 @@ +//! The Edit menu's Copy, Cut and Paste chosen with the mouse (an action +//! sent to an NSH view, no key-down in flight) run the bindings their keys +//! have where the focus is, as Cmd+C does when pressed: a focused table +//! copies its cells (#4177). The items are enabled only when something +//! takes them; a text field keeps its own Copy. + +use std::{ + sync::atomic::{AtomicUsize, Ordering}, + time::{Duration, Instant}, +}; + +use nsh_core::{keymap::When, prelude::*}; +use nsh_platform_macos::{ + self as platform, + objc2::{MainThreadMarker, MainThreadOnly, msg_send, rc::Retained, runtime::Bool, sel}, + objc2_app_kit::{ + NSApplication, NSApplicationActivationPolicy, NSBackingStoreType, NSMenuItem, NSTextView, + NSView, NSWindow, NSWindowStyleMask, + }, + objc2_foundation::{NSDate, NSDefaultRunLoopMode, NSPoint, NSRect, NSSize, NSString}, +}; + +static COPIES: AtomicUsize = AtomicUsize::new(0); +static PASTES: AtomicUsize = AtomicUsize::new(0); + +#[component] +fn Fixture() -> impl IntoElement { + for (key, count) in [("mod+c", &COPIES), ("mod+v", &PASTES)] { + on_key_with( + key, + KeyOptions { + when: When::NotEditing, + repeat: false, + ..Default::default() + }, + move |_| { + count.fetch_add(1, Ordering::SeqCst); + }, + ); + } + view() + .flex_1() + .p_4() + .child(text("Menu Copy reaches the focused view's binding")) + .child(view().test_id("host").focusable(true).h(px(50.0)).w_full()) +} + +fn pump(ms: u64) { + let app = NSApplication::sharedApplication(MainThreadMarker::new().unwrap()); + let deadline = Instant::now() + Duration::from_millis(ms); + // SAFETY: AppKit's immutable default run-loop mode. + let mode = unsafe { NSDefaultRunLoopMode }; + while Instant::now() < deadline { + if let Some(event) = app.nextEventMatchingMask_untilDate_inMode_dequeue( + platform::objc2_app_kit::NSEventMask::Any, + Some(&NSDate::dateWithTimeIntervalSinceNow(0.005)), + mode, + true, + ) { + app.sendEvent(&event); + } + app.updateWindows(); + } +} + +fn find(view: &NSView) -> Option> { + use platform::{objc2::Message, objc2_app_kit::NSAccessibility}; + if view + .accessibilityIdentifier() + .is_some_and(|id| id.to_string() == "host") + { + return Some(view.retain()); + } + view.subviews().iter().find_map(|child| find(&child)) +} + +/// Whether `target` enables a menu item of `action`, as the menu asks it. +fn enables(target: &NSView, action: platform::objc2::runtime::Sel) -> bool { + let mtm = MainThreadMarker::new().unwrap(); + // SAFETY: a new item with a valid selector, on the main thread. + let item = unsafe { + NSMenuItem::initWithTitle_action_keyEquivalent( + NSMenuItem::alloc(mtm), + &NSString::from_str("Item"), + Some(action), + &NSString::new(), + ) + }; + // SAFETY: NSH's views answer `validateMenuItem:` with a BOOL. + let enabled: Bool = unsafe { msg_send![target, validateMenuItem: &*item] }; + enabled.as_bool() +} + +/// Chooses a menu command for `target`: its action, with no key pressed. +fn choose(app: &NSApplication, target: &NSView, action: platform::objc2::runtime::Sel) { + // SAFETY: a standard action sent to a view that answers it. + let sent = unsafe { app.sendAction_to_from(action, Some(target), None) }; + assert!(sent, "the action reached the view"); + pump(100); +} + +#[allow(clippy::field_reassign_with_default)] +pub(super) fn run() { + COPIES.store(0, Ordering::SeqCst); + PASTES.store(0, Ordering::SeqCst); + let mtm = MainThreadMarker::new().unwrap(); + let app = NSApplication::sharedApplication(mtm); + app.setActivationPolicy(NSApplicationActivationPolicy::Regular); + app.finishLaunching(); + // SAFETY: The window is created and retained on the main thread. + let window = unsafe { + NSWindow::initWithContentRect_styleMask_backing_defer( + NSWindow::alloc(mtm), + NSRect::new(NSPoint::new(700.0, 300.0), NSSize::new(480.0, 200.0)), + NSWindowStyleMask::Titled, + NSBackingStoreType::Buffered, + false, + ) + }; + // SAFETY: The scope retains the window through close. + unsafe { window.setReleasedWhenClosed(false) }; + let mut options = platform::AppOptions::default(); + options.size = (480, 200); + let surface = platform::AppView::new(mtm, options); + window.setContentView(Some(&surface.view())); + surface.load(platform::App::Rust(platform::Root::of::())); + window.makeKeyAndOrderFront(None); + let deadline = Instant::now() + Duration::from_secs(5); + let host = loop { + if let Some(host) = find(&surface.view()) { + break host; + } + assert!(Instant::now() < deadline, "fixture did not mount"); + pump(10); + }; + // AppKit's focus is the root, as in an app: NSH moves its own focus + // among its views, and the Edit menu's actions reach the root. + let container = surface.view(); + let mut root = host.clone(); + // SAFETY: the views are alive and read on the main thread. + while let Some(parent) = unsafe { root.superview() } + && parent != container + { + root = parent; + } + assert!(window.makeFirstResponder(Some(&root))); + pump(50); + + // Copy and Paste have bindings, Cut none. + assert!(enables(&root, sel!(copy:)), "Copy enabled by its binding"); + assert!(enables(&root, sel!(paste:)), "Paste enabled by its binding"); + assert!(!enables(&root, sel!(cut:)), "Cut has nothing to cut"); + choose(&app, &root, sel!(copy:)); + assert_eq!(COPIES.load(Ordering::SeqCst), 1, "menu Copy fires mod+c"); + choose(&app, &root, sel!(paste:)); + assert_eq!(PASTES.load(Ordering::SeqCst), 1, "menu Paste fires mod+v"); + choose(&app, &root, sel!(cut:)); + assert_eq!(COPIES.load(Ordering::SeqCst), 1); + assert_eq!(PASTES.load(Ordering::SeqCst), 1); + + // A text field has the focus: its own Copy, not the app's binding + // (`When::NotEditing`). + let field = NSTextView::initWithFrame(NSTextView::alloc(mtm), host.bounds()); + host.addSubview(&field); + assert!(window.makeFirstResponder(Some(&field))); + choose(&app, &field, sel!(copy:)); + assert_eq!( + COPIES.load(Ordering::SeqCst), + 1, + "a focused field keeps its own Copy" + ); + // The root asked while the field has the focus: the binding is off. + assert!(!enables(&root, sel!(copy:)), "no binding while editing"); + + surface.stop(); + window.close(); + println!("menu-copy: Edit menu commands reach the focused view's bindings"); +}