Sign in Join
Wiki copytemporary

Cocoa Internals/Menu: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Cocoa Internals/Menu

OldNewLine
1+The menu logic in macOS is different from the one in Windows and other systems. The menu bar runs along the top of the screen on the Mac. Sub-menus and icons in the menu bar let the user choose commands, perform tasks and check status. There is an option to automatically hide the menu bar so it's shown only when you move the pointer to the top of the screen. There are no "per window" menus attached (an application window could use a Toolbar instead). Only the Apple menu, system status menus (including third-party ones eg fan monitoring) and the **active** application's menu appear in the menu bar.
2+
3+## Basic Logic
4+
5+This is how the contents of the menu is defined:
6+
7+- The TMainMenu of the active window is set as the Application menu
8+- If active window doesn't have TMainMenu the last active window with TMainMenu is still used
9+- The Window running as modal disables the main menu, UNLESS it has its own main menu.
10+
11+## Implementation
12+
13+Used classes are:
14+
15+- NSMenu
16+- NSMenuItem
17+
18+### Application Menu
19+
20+If there's a TMainMenu allocated for LCL form, WSCocoa internally allocates the "Application" menu. The "Application" menu is a macOS convention that any app should use. Since there's no logical concept of "Application" menu in the LCL, CocoaWS simply always creates it for you.
21+
22+### Application Menu Translation
23+
24+The titles (caption) of the Application menus are stored as [[IDE Window: Make ResourceString|"resourcestring"]] and could be translated by a translation module.
25+
26+### Menus and Menu Items Clicks
27+
28+There's a difference between Menus and MenuItems. "Menus" is simply a "MenuItem" with a submenu (and other subitems).
29+
30+The difference is how CocoaWS is reporting "clicks" to the LCL.
31+
32+For "Menus" the clicks are reported when a mouse HOVERs over the menuitem. (this is due to the fact of the click is called at **menuNeedUpdate:**. Calling at **menuWillOpen:** is too late, as macOS directly forbids changing the submenu items at that time).
33+
34+"Clicks" for "MenuItems" are called only when the menu is already closed (the released the mouse). Yet, visually, it looks the as if the action takes place immediately.
35+
36+## Popup (Context) Menu
37+
38+Cocoa provides NSView - based mechanism for handling context menus. The mechanism is not used by LCL.
39+
40+Instead, mouse events are listened. When either "right" click or "ctrl+click" is detected LM_ContextMenu event would be fired.
41+
42+Note: unlike Windows, macOS context menus are shown on "mouse-down" event. (on Windows, context menu event follows "mouse up" event).
43+
44+MacOS context menu doesn't have a corresponding keyboard short cut. "Context Menu" button (VK_APPS) is recognized as a button (and corresponding KeyDown, KeyUp events are called), but it doesn't cause the context menu to be shown up (anywhere in macOS system).
45+
46+## See also
47+
48+- [[Cocoa Internals]] - Overview.
49+- [[Cocoa Internals/Application]]
50+- [[Cocoa Internals/Forms]]
51+- [[Cocoa Internals/Input]]
52+
53+---
54+*From the Free Pascal wiki, page "Cocoa Internals/Menu", as it stood on 2022-05-14 after a change by Alextp, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
55+Category: Cocoa
56+Category: Lazarus internals
57+Category: macOS

Back to history