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
//! Menus of views ([`super::desktop::set_application_menu`],
//! [`super::desktop::popup_menu`]; TS: `Menu.setApplicationMenu`,
//! `Menu.popup`) and iOS header bar buttons
//! ([`super::host::Screen::header_left_items`],
//! [`super::host::Screen::header_right_items`]).
//!
//! A view's menu is decoded into [`crate::desktop::MenuItem`]s with an id
//! per item that has a handler, the handlers kept here by id. The platform
//! reports a chosen item as the root's `Menu` event; the core runs a view's
//! handler itself and leaves the others to JS. Views' item ids start at
//! [`FIRST_ID`], above any JS gives, so the two never meet. A context menu
//! closing is reported to both sides: each settles the one it showed.
//! Header bar buttons use the same ids and the same `Menu` event; each
//! binding of a page's items keeps a generation so replacing the items or
//! the page going away drops its handlers.
use std::rc::Rc;
use super::task::AnswerSlot;
use crate::{
desktop::Shortcut,
geometry::{Bounds, Points},
};
/// A menu item's handler.
pub(crate) type MenuHandler = Rc<dyn Fn()>;
/// The first id of a view's menu item.
pub(crate) const FIRST_ID: u32 = 1 << 31;
/// The views' menu handlers.
#[derive(Default)]
pub(crate) struct ViewMenus {
next: u32,
/// The application menu's handlers, by id.
pub(crate) app: Vec<(u32, MenuHandler)>,
/// Counts the application menus set, so the owner of one replaced
/// leaves the new one's handlers when it goes.
pub(crate) app_generation: u64,
/// The last context menu's handlers.
pub(crate) popup: Vec<(u32, MenuHandler)>,
/// Counts the context menus shown, as `app_generation`.
pub(crate) popup_generation: u64,
/// Header bar button handlers: (generation, handlers). Each page's
/// binding of `header_left_items` / `header_right_items` holds one
/// generation.
pub(crate) header: Vec<(u64, Vec<(u32, MenuHandler)>)>,
/// Counts header bindings, as `app_generation`.
pub(crate) header_generation: u64,
/// Settles the context menu shown, once the platform reports it closed.
pub(crate) closed: Option<AnswerSlot<()>>,
/// The tray's menu handlers.
pub(crate) tray: Vec<(u32, MenuHandler)>,
/// The tray click's id and handler.
pub(crate) tray_click: Option<(u32, TrayClickHandler)>,
/// Counts the trays set, as `app_generation`.
pub(crate) tray_generation: u64,
/// The global shortcuts registered.
pub(crate) globals: Vec<Global>,
/// The application menu, made on first use (under the core's runtime).
pub(crate) app_menu: Option<super::Signal<AppMenu>>,
}
/// The application menu as the core last sent it, with the labels of the
/// menus NSH adds: what [`super::desktop::application_menu`] reads.
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct AppMenu {
pub(crate) items: Option<std::sync::Arc<[crate::desktop::MenuItem]>>,
pub(crate) labels: crate::desktop::MenuLabels,
}
/// The tray click's handler, given the icon's frame on the screen.
pub(crate) type TrayClickHandler = Rc<dyn Fn(Option<Bounds<Points>>)>;
/// A global shortcut a view registered.
pub(crate) struct Global {
pub(crate) id: u32,
pub(crate) shortcut: Shortcut,
pub(crate) on_press: MenuHandler,
/// Settled with whether the system took it, once it says.
pub(crate) registered: Option<AnswerSlot<bool>>,
}
impl ViewMenus {
/// A new item id.
pub(crate) fn next_id(&mut self) -> u32 {
let id = FIRST_ID + self.next;
self.next = self.next.wrapping_add(1) % FIRST_ID;
id
}
/// Item `id`'s handler.
pub(crate) fn handler(&self, id: u32) -> Option<MenuHandler> {
self.app
.iter()
.chain(&self.popup)
.chain(&self.tray)
.chain(self.header.iter().flat_map(|(_, handlers)| handlers))
.find(|(i, _)| *i == id)
.map(|(_, h)| h.clone())
.or_else(|| {
self.globals
.iter()
.find(|g| g.id == id)
.map(|g| g.on_press.clone())
})
}
/// The tray click's handler, when `id` is its.
pub(crate) fn tray_click(&self, id: u32) -> Option<TrayClickHandler> {
self.tray_click
.as_ref()
.filter(|(i, _)| *i == id)
.map(|(_, h)| h.clone())
}
/// Settles global shortcut `id`'s registration; one the system refused
/// is forgotten.
pub(crate) fn registered(&mut self, id: u32, registered: bool) {
let Some(index) = self.globals.iter().position(|g| g.id == id) else {
return;
};
if let Some(slot) = self.globals[index].registered.take() {
slot.borrow_mut().deliver(registered);
}
if !registered {
self.globals.remove(index);
}
}
/// Keeps `handlers` for a header binding; returns its generation so a
/// cleanup can drop them when the binding is replaced or the page goes.
pub(crate) fn push_header(&mut self, handlers: Vec<(u32, MenuHandler)>) -> u64 {
self.header_generation += 1;
let generation = self.header_generation;
self.header.push((generation, handlers));
generation
}
/// Drops the header handlers of `generation`, if they are still this
/// binding's (a later binding of the same page has another).
pub(crate) fn take_header(&mut self, generation: u64) {
self.header.retain(|(g, _)| *g != generation);
}
/// Settles the context menu shown, if there is one.
pub(crate) fn settle_popup(&mut self) {
if let Some(slot) = self.closed.take() {
slot.borrow_mut().deliver(());
}
}
}
/// The application menu's signal, made on first use.
pub(crate) fn app_menu(menus: &std::cell::RefCell<ViewMenus>) -> super::Signal<AppMenu> {
if let Some(signal) = menus.borrow().app_menu {
return signal;
}
let signal = super::Owner::detached().run(|| super::signal(AppMenu::default()));
menus.borrow_mut().app_menu = Some(signal);
signal
}