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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech
//! Process-unique identifier for a menu item.
//!
//! A [`MenuItemId`] is the stable token that ties a logical menu item (declared
//! in the `teksilo-widgets` `MenuModel`) to its mirror in a native platform menu
//! (`NSMenuItem` on macOS, `HMENU` item on Windows, …). It is a plain `u64`
//! newtype — `Send + Sync + Copy` — so it can ride inside an `AppEvent::External`
//! payload posted from a platform menu callback back to the UI loop, where the
//! app resolves it to the item's intent / action.
//!
//! Ids are allocated from a single process-wide monotonic counter and never
//! reused. They live in `teksilo-core` (not `teksilo-widgets`) so that
//! `teksilo-platform` — which renders the native menu and posts the click
//! payload — can name the type without depending on the widget layer.
use std::sync::atomic::{AtomicU64, Ordering};
/// A process-unique, never-reused menu item identifier.
///
/// Allocate with [`MenuItemId::next`]. The numeric value is opaque to
/// application code; it exists only to correlate a native menu item with the
/// logical item that produced it.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct MenuItemId(u64);
static NEXT_MENU_ITEM_ID: AtomicU64 = AtomicU64::new(1);
impl MenuItemId {
/// Allocate the next process-unique id.
pub fn next() -> Self {
Self(NEXT_MENU_ITEM_ID.fetch_add(1, Ordering::Relaxed))
}
/// The raw numeric id (for the platform layer's item-tag round-trip).
pub fn raw(self) -> u64 {
self.0
}
/// Reconstruct an id from a raw value carried back across the platform
/// boundary (e.g. an `NSMenuItem.tag`). Intended for the platform backend,
/// not application code.
pub fn from_raw(raw: u64) -> Self {
Self(raw)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ids_are_unique_and_monotonic() {
let a = MenuItemId::next();
let b = MenuItemId::next();
assert_ne!(a, b);
assert!(b.raw() > a.raw());
}
#[test]
fn raw_roundtrips() {
let a = MenuItemId::next();
assert_eq!(MenuItemId::from_raw(a.raw()), a);
}
}