Context Menu 上下文菜单 macos
The menu opened at the pointer by right-clicking or Control-clicking an item
在项目上右键点按或 Control-点按,在指针处打开的菜单
标本可交互 —— 点点看。Specimen is live — try it.
Anatomy — every part, named解剖 —— 每个部件的名字
-
1 Selection highlight选中高亮
NSMenuItem“The blue bar behind the option under the pointer” is the menu item's selection highlight.
「指针下那个选项背后的蓝色条」就是菜单项的选中高亮。
Prompt fragmentPrompt 片段the native selection highlight behind the active NSMenuItem in a contextual NSMenu
上下文 NSMenu 中当前 NSMenuItem 背后的原生选中高亮
-
2 Separator item分隔线项
NSMenuItem.separator()“The little line between groups of right-click options” is a separator item.
「右键选项分组之间那条小细线」就是分隔线项。
Prompt fragmentPrompt 片段an NSMenuItem.separator() dividing related groups in the context menu
一个 NSMenuItem.separator(),把上下文菜单里的相关项分组隔开
-
3 Key equivalent快捷键(key equivalent)
NSMenuItem.keyEquivalent“The keyboard shortcut text on the right” is the item's key equivalent.
「右侧的快捷键文字」就是该项的 key equivalent。
Prompt fragmentPrompt 片段the NSMenuItem.keyEquivalent aligned at the trailing edge of a context-menu item, such as ⌘C
NSMenuItem.keyEquivalent,对齐在上下文菜单项的后缘,例如 ⌘C
-
4 Submenu indicator子菜单指示箭头
NSMenuItem.submenu“The sideways arrow that opens more options” is the submenu indicator.
「那个朝旁边、能打开更多选项的箭头」就是子菜单指示箭头。
Prompt fragmentPrompt 片段the trailing submenu indicator on an NSMenuItem whose submenu opens beside the context menu
NSMenuItem 后缘的子菜单指示箭头,其子菜单在上下文菜单旁边打开
Prompt — paste into your agentPrompt —— 直接粘贴给你的代理
Add a Context Menu using NSMenu on the target NSView (SwiftUI: View.contextMenu), opened by right-click or Control-click at the item under the pointer. Populate it with actions specific to that item; do not turn it into a primary-click dropdown anchored to a button.
用 NSMenu 给目标 NSView 加上下文菜单(SwiftUI:View.contextMenu),通过右键点按或 Control-点按指针下的项目来打开。菜单里放针对该项目的操作;别把它做成锚定在按钮上、主键点击打开的下拉菜单。
Debug prompt — when it misbehaves调试 Prompt —— 当它不听话时
Debug my macOS context menu (NSMenu, menu(for:), SwiftUI .contextMenu). Rule out: the menu built once and gone stale — implement menuNeedsUpdate/menuWillOpen; items greyed out because their target is nil and nothing in the responder chain implements the action; right-click, control-click and two-finger tap taking different code paths; a SwiftUI .contextMenu swallowing plain clicks on the row. The symptom:
调试我的 macOS 上下文菜单(NSMenu、menu(for:)、SwiftUI .contextMenu)。排除以下可能:菜单只构建了一次、内容过期——实现 menuNeedsUpdate/menuWillOpen;菜单项变灰,因为 target 是 nil 且响应链里没人实现该 action;右键点按、Control-点按和双指轻点走了不同代码路径;SwiftUI .contextMenu 吞掉了行上的普通点击。症状是:
In code代码里叫什么
| Framework框架 | Symbol符号 | Note说明 |
|---|---|---|
| AppKit | NSMenu | |
| SwiftUI | View.contextMenu(menuItems:) | |
| AppKit | NSView.menu | the contextual menu associated with a view与某个视图关联的上下文菜单 |
| AppKit | NSMenuItem |