incular-macos 0.1.0

macOS platform integration for Incular
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use incular_desktop::{
    NativeMenuCommandRegistry, NativeMenuMessage, matching_menu_shortcut,
    native_menu_structure_equal,
};
use incular_widgets::{
    MenuItemId, MenuOwnerId, PlatformMenuDelegate, PlatformMenuEvent, PlatformMenuSnapshot,
    PlatformMenuSnapshotNode, PlatformMenuUpdate, ShortcutModifiers,
};
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::{ClassType, DeclaredClass, declare_class, msg_send_id, mutability, sel};
use objc2_app_kit::{NSApplication, NSEventModifierFlags, NSMenu, NSMenuDelegate, NSMenuItem};
use objc2_foundation::{MainThreadMarker, NSObject, NSObjectProtocol, NSString};
use std::{
    cell::RefCell,
    collections::{HashMap, VecDeque},
    rc::Rc,
};

struct DispatcherIvars {
    messages: Rc<RefCell<VecDeque<NativeMenuMessage<usize>>>>,
}

declare_class!(
    struct PlatformMenuDispatcher;

    // SAFETY:
    // - NSObject has no additional subclassing requirements.
    // - AppKit targets/delegates are main-thread-only objects.
    // - The dispatcher owns only ordinary Rust ivars and implements no custom
    //   deallocation contract.
    unsafe impl ClassType for PlatformMenuDispatcher {
        type Super = NSObject;
        type Mutability = mutability::MainThreadOnly;
        const NAME: &'static str = "IncularPlatformMenuDispatcher";
    }

    impl DeclaredClass for PlatformMenuDispatcher {
        type Ivars = DispatcherIvars;
    }

    unsafe impl NSObjectProtocol for PlatformMenuDispatcher {}

    unsafe impl PlatformMenuDispatcher {
        #[method(incularMenuItemSelected:)]
        fn selected(&self, sender: &NSMenuItem) {
            // SAFETY: AppKit invokes this action with the NSMenuItem whose tag
            // was assigned from the generation-safe native command registry.
            let tag = unsafe { sender.tag() };
            if let Ok(command) = u32::try_from(tag) {
                self.ivars()
                    .messages
                    .borrow_mut()
                    .push_back(NativeMenuMessage::Command(command));
            }
        }
    }

    unsafe impl NSMenuDelegate for PlatformMenuDispatcher {
        #[method(menuWillOpen:)]
        fn menu_will_open(&self, menu: &NSMenu) {
            self.ivars()
                .messages
                .borrow_mut()
                .push_back(NativeMenuMessage::Opened(menu as *const NSMenu as usize));
        }

        #[method(menuDidClose:)]
        fn menu_did_close(&self, menu: &NSMenu) {
            self.ivars()
                .messages
                .borrow_mut()
                .push_back(NativeMenuMessage::Closed(menu as *const NSMenu as usize));
        }
    }
);

impl PlatformMenuDispatcher {
    fn new(
        messages: Rc<RefCell<VecDeque<NativeMenuMessage<usize>>>>,
        mtm: MainThreadMarker,
    ) -> Retained<Self> {
        let this = mtm.alloc().set_ivars(DispatcherIvars { messages });
        // SAFETY: NSObject's designated init is valid for this subclass after
        // the Rust ivars have been initialized.
        unsafe { msg_send_id![super(this), init] }
    }
}

#[derive(Default)]
struct MacosMenuState {
    owner: Option<MenuOwnerId>,
    snapshot: Option<PlatformMenuSnapshot>,
    event_handler: Option<Rc<dyn Fn(PlatformMenuEvent)>>,
    registry: NativeMenuCommandRegistry,
    baseline: Option<BaselineMainMenu>,
    menu_ids: HashMap<usize, MenuItemId>,
    dispatcher: Option<Retained<PlatformMenuDispatcher>>,
}

struct BaselineMainMenu {
    /// `None` only when the embedding NSApplication had no main menu before
    /// Incular attached. Winit normally installs one with About/Services/Hide/
    /// Quit, but embedders remain supported without inventing one as baseline.
    original: Option<Retained<NSMenu>>,
    /// Root menu Incular appends its owned top-level entries to. When an
    /// original menu exists this is the exact same NSMenu, preserving all
    /// platform/application items and their native identity.
    root: Retained<NSMenu>,
    original_item_count: usize,
}

/// Application-global AppKit main-menu backend. Native menu actions never run
/// retained Rust callbacks from the Objective-C stack; the dispatcher queues
/// stable native command/menu identities and the desktop loop flushes them.
pub(crate) struct MacosPlatformMenuDelegate {
    state: RefCell<MacosMenuState>,
    native_messages: Rc<RefCell<VecDeque<NativeMenuMessage<usize>>>>,
}

impl Default for MacosPlatformMenuDelegate {
    fn default() -> Self {
        Self {
            state: RefCell::new(MacosMenuState::default()),
            native_messages: Rc::new(RefCell::new(VecDeque::new())),
        }
    }
}

impl MacosPlatformMenuDelegate {
    pub(crate) fn flush_events(&self) {
        let messages = self
            .native_messages
            .borrow_mut()
            .drain(..)
            .collect::<Vec<_>>();
        if messages.is_empty() {
            return;
        }
        let (handler, events) = {
            let state = self.state.borrow();
            let Some(handler) = state.event_handler.clone() else {
                return;
            };
            (
                handler,
                state.registry.resolve_messages(messages, &state.menu_ids),
            )
        };
        for event in events {
            handler(event);
        }
    }
}

impl PlatformMenuDelegate for MacosPlatformMenuDelegate {
    fn acquire(&self, owner: MenuOwnerId) -> PlatformMenuUpdate {
        let mut state = self.state.borrow_mut();
        match state.owner {
            None => {
                state.owner = Some(owner);
                PlatformMenuUpdate::Applied
            }
            Some(active) if active == owner => PlatformMenuUpdate::Applied,
            Some(_) => PlatformMenuUpdate::RejectedOwnedByOther,
        }
    }

    fn set_menus(&self, owner: MenuOwnerId, snapshot: PlatformMenuSnapshot) -> PlatformMenuUpdate {
        let Some(mtm) = MainThreadMarker::new() else {
            return PlatformMenuUpdate::RejectedByPlatform;
        };
        let mut state = self.state.borrow_mut();
        if state.owner != Some(owner) {
            return PlatformMenuUpdate::RejectedOwnedByOther;
        }
        if state.snapshot.as_ref() == Some(&snapshot) {
            return PlatformMenuUpdate::Unchanged;
        }

        let mut next_registry = state.registry.clone();
        if next_registry.synchronize(&snapshot).is_err() {
            return PlatformMenuUpdate::RejectedByPlatform;
        }
        let dispatcher = state
            .dispatcher
            .get_or_insert_with(|| PlatformMenuDispatcher::new(self.native_messages.clone(), mtm))
            .clone();
        let application = NSApplication::sharedApplication(mtm);
        if state.baseline.is_none() {
            state.baseline = Some(capture_baseline_main_menu(&application, mtm));
        }

        if state
            .snapshot
            .as_ref()
            .is_some_and(|current| native_menu_structure_equal(current, &snapshot))
        {
            let baseline = state
                .baseline
                .as_ref()
                .expect("AppKit baseline captured before update");
            if !update_menu_nodes(
                &baseline.root,
                baseline.original_item_count,
                &snapshot.menus,
                &next_registry,
            ) {
                if let Some(previous) = state.snapshot.as_ref() {
                    let _ = update_menu_nodes(
                        &baseline.root,
                        baseline.original_item_count,
                        &previous.menus,
                        &state.registry,
                    );
                }
                return PlatformMenuUpdate::RejectedByPlatform;
            }
        } else {
            let mut menu_ids = HashMap::new();
            let items = build_menu_items(
                &snapshot.menus,
                &next_registry,
                &dispatcher,
                mtm,
                &mut menu_ids,
            );
            let baseline = state
                .baseline
                .as_ref()
                .expect("AppKit baseline captured before replacement");
            remove_owned_top_level_items(&baseline.root, baseline.original_item_count);
            for item in items {
                baseline.root.addItem(&item);
            }
            application.setMainMenu(Some(&baseline.root));
            state.menu_ids = menu_ids;
        }

        state.registry = next_registry;
        state.snapshot = Some(snapshot);
        PlatformMenuUpdate::Applied
    }

    fn clear_menus(&self, owner: MenuOwnerId) -> PlatformMenuUpdate {
        let mut state = self.state.borrow_mut();
        if state.owner != Some(owner) {
            return PlatformMenuUpdate::RejectedOwnedByOther;
        }
        if let (Some(mtm), Some(baseline)) = (MainThreadMarker::new(), state.baseline.as_ref()) {
            remove_owned_top_level_items(&baseline.root, baseline.original_item_count);
            NSApplication::sharedApplication(mtm).setMainMenu(baseline.original.as_deref());
        }
        state.menu_ids.clear();
        let _ = state
            .registry
            .synchronize(&PlatformMenuSnapshot { menus: Vec::new() });
        state.snapshot = None;
        PlatformMenuUpdate::Applied
    }

    fn release(&self, owner: MenuOwnerId) -> PlatformMenuUpdate {
        let mut state = self.state.borrow_mut();
        if state.owner != Some(owner) {
            return PlatformMenuUpdate::RejectedOwnedByOther;
        }
        state.owner = None;
        state.event_handler = None;
        PlatformMenuUpdate::Applied
    }

    fn set_event_handler(
        &self,
        owner: MenuOwnerId,
        handler: Option<Rc<dyn Fn(PlatformMenuEvent)>>,
    ) -> PlatformMenuUpdate {
        let mut state = self.state.borrow_mut();
        if state.owner != Some(owner) {
            return PlatformMenuUpdate::RejectedOwnedByOther;
        }
        state.event_handler = handler;
        PlatformMenuUpdate::Applied
    }

    fn handle_shortcut(&self, key: &str, modifiers: ShortcutModifiers) -> bool {
        // AppKit's main menu is the shortcut authority. If Winit surfaces the
        // same key event after native handling, consume it without invoking the
        // retained callback a second time.
        self.state
            .borrow()
            .snapshot
            .as_ref()
            .is_some_and(|snapshot| matching_menu_shortcut(snapshot, key, modifiers).is_some())
    }
}

fn capture_baseline_main_menu(
    application: &NSApplication,
    mtm: MainThreadMarker,
) -> BaselineMainMenu {
    // SAFETY: queried synchronously on AppKit's main thread. Winit normally
    // initializes the standard application menu before native windows mount.
    let original = unsafe { application.mainMenu() };
    let root = original.clone().unwrap_or_else(|| {
        let title = NSString::from_str("");
        // SAFETY: creating a detached NSMenu on the main thread is valid.
        unsafe { NSMenu::initWithTitle(mtm.alloc(), &title) }
    });
    // SAFETY: `root` is a live retained NSMenu on the main thread.
    let original_item_count = usize::try_from(unsafe { root.numberOfItems() }).unwrap_or(0);
    BaselineMainMenu {
        original,
        root,
        original_item_count,
    }
}

fn remove_owned_top_level_items(root: &NSMenu, original_item_count: usize) {
    // Remove from the end so every original item keeps its exact index and
    // identity. Incular owns only entries appended after the captured baseline.
    loop {
        // SAFETY: main-thread-only menu query.
        let count = usize::try_from(unsafe { root.numberOfItems() }).unwrap_or(0);
        if count <= original_item_count {
            break;
        }
        // SAFETY: `count - 1` is an existing Incular-owned item index.
        unsafe { root.removeItemAtIndex(isize::try_from(count - 1).expect("NSMenu index fits")) };
    }
}

fn build_menu_items(
    nodes: &[PlatformMenuSnapshotNode],
    registry: &NativeMenuCommandRegistry,
    dispatcher: &PlatformMenuDispatcher,
    mtm: MainThreadMarker,
    menu_ids: &mut HashMap<usize, MenuItemId>,
) -> Vec<Retained<NSMenuItem>> {
    nodes
        .iter()
        .map(|node| build_menu_item(node, registry, dispatcher, mtm, menu_ids))
        .collect()
}

fn append_menu_nodes(
    parent: &NSMenu,
    nodes: &[PlatformMenuSnapshotNode],
    registry: &NativeMenuCommandRegistry,
    dispatcher: &PlatformMenuDispatcher,
    mtm: MainThreadMarker,
    menu_ids: &mut HashMap<usize, MenuItemId>,
) {
    for item in build_menu_items(nodes, registry, dispatcher, mtm, menu_ids) {
        parent.addItem(&item);
    }
}

fn build_menu_item(
    node: &PlatformMenuSnapshotNode,
    registry: &NativeMenuCommandRegistry,
    dispatcher: &PlatformMenuDispatcher,
    mtm: MainThreadMarker,
    menu_ids: &mut HashMap<usize, MenuItemId>,
) -> Retained<NSMenuItem> {
    if node.separator {
        return NSMenuItem::separatorItem(mtm);
    }

    let title = NSString::from_str(&node.label);
    let key = NSString::from_str(
        node.shortcut
            .as_ref()
            .filter(|_| node.selectable)
            .map_or("", |shortcut| shortcut.key.as_str()),
    );
    let action = node.selectable.then_some(sel!(incularMenuItemSelected:));
    // SAFETY: the selector belongs to PlatformMenuDispatcher and AppKit menu
    // construction occurs on the main thread.
    let item = unsafe {
        NSMenuItem::initWithTitle_action_keyEquivalent(mtm.alloc(), &title, action, &key)
    };
    unsafe {
        item.setEnabled(node.enabled);
        if let Some(tooltip) = &node.tooltip {
            item.setToolTip(Some(&NSString::from_str(tooltip)));
        }
    }

    if node.selectable {
        let command = registry
            .command_for(&node.id)
            .expect("selectable AppKit menu node has native command ID");
        unsafe {
            item.setTag(isize::try_from(command.get()).expect("AppKit NSInteger fits u32"));
            item.setTarget(Some(dispatcher.as_super().as_super()));
        }
        item.setKeyEquivalentModifierMask(appkit_modifiers(
            node.shortcut
                .as_ref()
                .map_or(ShortcutModifiers::empty(), |shortcut| shortcut.modifiers),
        ));
    } else {
        let submenu_title = NSString::from_str(&node.label);
        // SAFETY: construction and delegate assignment are main-thread-only.
        let submenu = unsafe { NSMenu::initWithTitle(mtm.alloc(), &submenu_title) };
        unsafe {
            submenu.setAutoenablesItems(false);
            submenu.setDelegate(Some(ProtocolObject::from_ref(dispatcher)));
        }
        append_menu_nodes(
            &submenu,
            &node.children,
            registry,
            dispatcher,
            mtm,
            menu_ids,
        );
        menu_ids.insert(&*submenu as *const NSMenu as usize, node.id.clone());
        item.setSubmenu(Some(&submenu));
    }
    item
}

fn update_menu_nodes(
    parent: &NSMenu,
    start_index: usize,
    nodes: &[PlatformMenuSnapshotNode],
    registry: &NativeMenuCommandRegistry,
) -> bool {
    for (index, node) in nodes.iter().enumerate() {
        let Some(index) = start_index.checked_add(index) else {
            return false;
        };
        let Ok(index) = isize::try_from(index) else {
            return false;
        };
        // SAFETY: native_menu_structure_equal guarantees an item at every
        // corresponding retained position.
        let Some(item) = (unsafe { parent.itemAtIndex(index) }) else {
            return false;
        };
        if node.separator {
            // SAFETY: structure equality guarantees the existing item is also a
            // separator, so it has no mutable command attributes to apply.
            if !unsafe { item.isSeparatorItem() } {
                return false;
            }
            continue;
        }
        unsafe {
            item.setTitle(&NSString::from_str(&node.label));
            item.setEnabled(node.enabled);
            item.setToolTip(
                node.tooltip
                    .as_ref()
                    .map(|value| NSString::from_str(value))
                    .as_deref(),
            );
        }
        if node.selectable {
            let Some(command) = registry.command_for(&node.id) else {
                return false;
            };
            let shortcut = node.shortcut.as_ref();
            unsafe {
                item.setTag(isize::try_from(command.get()).expect("AppKit NSInteger fits u32"));
                item.setKeyEquivalent(&NSString::from_str(
                    shortcut.map_or("", |value| value.key.as_str()),
                ));
            }
            item.setKeyEquivalentModifierMask(appkit_modifiers(
                shortcut.map_or(ShortcutModifiers::empty(), |value| value.modifiers),
            ));
        } else {
            // SAFETY: structure equality guarantees a retained submenu at this
            // position; reusing it preserves native menu/item identity.
            let Some(submenu) = (unsafe { item.submenu() }) else {
                return false;
            };
            if !update_menu_nodes(&submenu, 0, &node.children, registry) {
                return false;
            }
        }
    }
    true
}

fn appkit_modifiers(modifiers: ShortcutModifiers) -> NSEventModifierFlags {
    let mut native = NSEventModifierFlags::empty();
    if modifiers.contains(ShortcutModifiers::SHIFT) {
        native |= NSEventModifierFlags::NSEventModifierFlagShift;
    }
    if modifiers.contains(ShortcutModifiers::CONTROL) {
        native |= NSEventModifierFlags::NSEventModifierFlagControl;
    }
    if modifiers.contains(ShortcutModifiers::ALT) {
        native |= NSEventModifierFlags::NSEventModifierFlagOption;
    }
    if modifiers.contains(ShortcutModifiers::META) {
        native |= NSEventModifierFlags::NSEventModifierFlagCommand;
    }
    native
}