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
5 changes: 5 additions & 0 deletions .changes/macos-popup-clamp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"muda": patch
---

On macOS, fix context-menu popups overflowing off the bottom or right edge of the screen with scroll arrows by switching `show_context_menu_for_nsview` to `popUpContextMenu:withEvent:forView:`. AppKit clamps and auto-flips the menu against the active screen's visible frame for free, including menu bar / Dock margins and multi-monitor setups.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"NSAttributedString",
"NSData",
"NSDate",
"NSDictionary",
"NSGeometry",
"NSString",
Expand All @@ -68,6 +69,7 @@ objc2-app-kit = { version = "0.3.0", default-features = false, features = [
"NSApplication",
"NSCell",
"NSEvent",
"NSGraphicsContext",
"NSImage",
"NSMenu",
"NSMenuItem",
Expand Down
73 changes: 57 additions & 16 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ use objc2_app_kit::{
NSAboutPanelOptionApplicationIcon, NSAboutPanelOptionApplicationName,
NSAboutPanelOptionApplicationVersion, NSAboutPanelOptionCredits, NSAboutPanelOptionVersion,
NSApplication, NSControlStateValueOff, NSControlStateValueOn, NSEvent, NSEventModifierFlags,
NSImage, NSImageName, NSMenu, NSMenuDelegate, NSMenuItem, NSRunningApplication, NSView,
NSEventType, NSImage, NSImageName, NSMenu, NSMenuDelegate, NSMenuItem, NSRunningApplication,
NSView,
};
use objc2_foundation::{
ns_string, MainThreadMarker, NSAttributedString, NSDictionary, NSInteger, NSObject, NSPoint,
NSSize, NSString,
ns_string, MainThreadMarker, NSAttributedString, NSDate, NSDictionary, NSInteger, NSObject,
NSPoint, NSSize, NSString,
};

use self::util::strip_mnemonic;
use crate::{
accelerator::KeyAccelerator,
dpi::{LogicalPosition, Position},
dpi::Position,
icon::{Icon, NativeIcon},
items::*,
util::{AddOp, Counter},
Expand Down Expand Up @@ -1197,21 +1198,61 @@ unsafe fn show_context_menu(

let window = view.window().expect("view must be installed in a window");
let scale_factor = window.backingScaleFactor();
let (location, in_view) = if let Some(pos) = position.map(|p| p.to_logical(scale_factor)) {
let view_rect = view.frame();
let location = NSPoint::new(pos.x, view_rect.size.height - pos.y);
(location, Some(view))
} else {
let mouse_location = unsafe { NSEvent::mouseLocation() };
let pos = LogicalPosition {
x: mouse_location.x,
y: mouse_location.y,

// Compute the popup location in `window`'s coordinate space (bottom-left
// origin). The synthesized event below carries this location, and AppKit
// interprets it relative to the event's window — which must be the same
// window the target view belongs to.
let location_in_window: NSPoint =
if let Some(pos) = position.map(|p| p.to_logical(scale_factor)) {
// `pos` is in the view's logical coordinate system with top-left
// origin (the rest of muda's API). Flip Y to AppKit's view coords
// (bottom-left), then convert to window coords.
let view_rect = view.frame();
let view_point = NSPoint::new(pos.x, view_rect.size.height - pos.y);
unsafe { view.convertPoint_toView(view_point, None) }
} else {
// No explicit position → use the current mouse location. It is
// in screen coords; convert into the view's window's coords.
let mouse = unsafe { NSEvent::mouseLocation() };
window.convertPointFromScreen(mouse)
};

// Synthesize a right-mouse-down event so we can call
// `popUpContextMenu:withEvent:forView:`, which (unlike
// `popUpMenuPositioningItem:atLocation:inView:`) auto-flips and clamps
// the menu against the screen's visible frame for free.
let timestamp = NSDate::timeIntervalSinceReferenceDate_class();
let event = unsafe {
NSEvent::mouseEventWithType_location_modifierFlags_timestamp_windowNumber_context_eventNumber_clickCount_pressure(
NSEventType::RightMouseDown,
location_in_window,
NSEventModifierFlags(0),
timestamp,
window.windowNumber(),
None,
0,
1,
1.0,
)
};

let Some(event) = event else {
// Synthesizing an event can fail in pathological situations; fall
// back to the legacy positioning call so we still pop up something.
return unsafe {
ns_menu.popUpMenuPositioningItem_atLocation_inView(None, location_in_window, Some(view))
};
let location = NSPoint::new(pos.x, pos.y);
(location, None)
};

unsafe { ns_menu.popUpMenuPositioningItem_atLocation_inView(None, location, in_view) }
// `popUpContextMenu:withEvent:forView:` lets macOS append contextual-menu
// plug-in items (AutoFill, Services, third-party plug-ins) to the menu.
// The legacy positioning call never did that, so opt out to keep the
// menu's contents exactly what the consumer built.
ns_menu.setAllowsContextMenuPlugIns(false);

unsafe { NSMenu::popUpContextMenu_withEvent_forView(ns_menu, &event, view) };
true
}

impl NativeIcon {
Expand Down
Loading