use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::rc::Rc;
use waft_embedder::{IconImage, MenuEntry, MenuId, TrayIcon, TrayIconError, TrayIconEvent};
use waft_foundation::{App, Handle, VoidCallback};
use crate::WidgetsBinding;
use crate::framework::{
BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};
use crate::widgets::basic::SizedBox;
use crate::widgets::platform_menu_bar::{
PlatformMenuItemRef, same_items, select_menu_item, strip_dividers,
};
pub struct PlatformTrayIcon {
pub key: Option<KeyRef>,
pub child: Option<WidgetRef>,
pub image: IconImage,
pub tooltip: Option<String>,
pub menus: Vec<PlatformMenuItemRef>,
pub on_activated: Option<VoidCallback>,
pub on_double_clicked: Option<VoidCallback>,
}
impl PlatformTrayIcon {
pub fn new(image: IconImage) -> PlatformTrayIcon {
PlatformTrayIcon {
key: None,
child: None,
image,
tooltip: None,
menus: Vec::new(),
on_activated: None,
on_double_clicked: None,
}
}
pub fn key(mut self, key: KeyRef) -> PlatformTrayIcon {
self.key = Some(key);
self
}
pub fn child<K>(mut self, child: impl IntoWidget<K>) -> PlatformTrayIcon {
self.child = Some(child.into_widget());
self
}
pub fn tooltip(mut self, tooltip: impl Into<String>) -> PlatformTrayIcon {
self.tooltip = Some(tooltip.into());
self
}
pub fn menus(mut self, menus: Vec<PlatformMenuItemRef>) -> PlatformTrayIcon {
self.menus = menus;
self
}
pub fn on_activated(mut self, on_activated: VoidCallback) -> PlatformTrayIcon {
self.on_activated = Some(on_activated);
self
}
pub fn on_double_clicked(mut self, on_double_clicked: VoidCallback) -> PlatformTrayIcon {
self.on_double_clicked = Some(on_double_clicked);
self
}
}
impl Debug for PlatformTrayIcon {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PlatformTrayIcon")
.field("tooltip", &self.tooltip)
.field("menus", &self.menus)
.finish_non_exhaustive()
}
}
impl StatefulWidget for PlatformTrayIcon {
type State = PlatformTrayIconState;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_state(&self) -> PlatformTrayIconState {
PlatformTrayIconState {
state: StateData::new(),
icon: None,
entries: Vec::new(),
id_map: HashMap::new(),
serial: 0,
descendants: Vec::new(),
}
}
}
pub struct PlatformTrayIconState {
state: StateData<PlatformTrayIcon>,
icon: Option<Box<dyn TrayIcon>>,
entries: Vec<MenuEntry>,
id_map: HashMap<MenuId, PlatformMenuItemRef>,
serial: MenuId,
descendants: Vec<PlatformMenuItemRef>,
}
impl PlatformTrayIconState {
fn add_icon(self: Handle<Self>, app: &mut App) {
let platform = app.platform();
let Some(tray_icons) = platform.tray_icons() else {
return;
};
let widget = self.widget(app);
let adding = tray_icons.add(&widget.image, widget.tooltip.as_deref());
app.spawn(async move |cx| {
let added = adding.await;
cx.update(|app| self.icon_added(app, added));
});
}
fn icon_added(
self: Handle<Self>,
app: &mut App,
added: Result<Box<dyn TrayIcon>, TrayIconError>,
) {
let Ok(icon) = added else {
return;
};
if !self.mounted(app) {
return;
}
let widget = self.widget(app);
icon.set_image(&widget.image);
icon.set_tooltip(widget.tooltip.as_deref());
let id = icon.id();
app.get_mut(self).icon = Some(icon);
WidgetsBinding::instance(app).add_tray_icon_listener(
app,
id,
Rc::new(move |app, event| self.handle_event(app, event)),
);
self.update_menu(app);
}
fn update_menu(self: Handle<Self>, app: &mut App) {
let menus = self.widget(app).menus.clone();
let delegate = WidgetsBinding::instance(app).platform_menu_delegate(app);
let mut serial = app.get(self).serial;
let mut id_map = HashMap::new();
let mut entries = Vec::new();
for item in &menus {
entries.extend(Rc::clone(item).to_menu_entries(&*delegate, &mut |item| {
serial += 1;
id_map.insert(serial, item);
serial
}));
}
strip_dividers(&mut entries);
let state = app.get_mut(self);
state.serial = serial;
state.id_map = id_map;
state.entries = entries;
let state = app.get(self);
if let Some(attached) = state.icon.as_ref().and_then(|icon| icon.attached_menu()) {
attached.set_menu(&state.entries);
}
}
fn handle_event(self: Handle<Self>, app: &mut App, event: TrayIconEvent) {
match event {
TrayIconEvent::Activated { .. } => {
if let Some(on_activated) = self.widget(app).on_activated.clone() {
on_activated(app);
}
}
TrayIconEvent::DoubleClicked => {
if let Some(on_double_clicked) = self.widget(app).on_double_clicked.clone() {
on_double_clicked(app);
}
}
TrayIconEvent::ContextMenuRequested { .. } => self.open_menu_at_icon(app),
TrayIconEvent::MenuSelected(id) => self.select(app, id),
TrayIconEvent::SecondaryActivated { .. } | TrayIconEvent::Scrolled { .. } => {}
}
}
fn open_menu_at_icon(self: Handle<Self>, app: &mut App) {
let state = app.get(self);
let Some(icon) = &state.icon else {
return;
};
if icon.attached_menu().is_some() || state.entries.is_empty() {
return;
}
let Some(at_icon) = icon.menu_at_icon() else {
return;
};
let shown = at_icon.show(&state.entries);
app.spawn(async move |cx| {
if let Some(id) = shown.await {
cx.update(|app| {
if self.mounted(app) {
self.select(app, id);
}
});
}
});
}
fn select(self: Handle<Self>, app: &mut App, id: MenuId) {
let item = app.get(self).id_map.get(&id).cloned();
if let Some(item) = item {
select_menu_item(app, &item);
}
}
}
impl State for PlatformTrayIconState {
type Widget = PlatformTrayIcon;
crate::state_accessors!();
fn init_state(self: Handle<Self>, app: &mut App) {
let mut descendants = Vec::new();
for item in &self.widget(app).menus {
descendants.push(Rc::clone(item));
descendants.extend(item.descendants());
}
app.get_mut(self).descendants = descendants;
self.add_icon(app);
}
fn dispose(self: Handle<Self>, app: &mut App) {
if let Some(icon) = app.get_mut(self).icon.take() {
WidgetsBinding::instance(app).remove_tray_icon_listener(app, icon.id());
}
}
fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &PlatformTrayIcon) {
let widget = self.widget(app);
let (image, tooltip) = (widget.image.clone(), widget.tooltip.clone());
if let Some(icon) = &app.get(self).icon {
if image != old_widget.image {
icon.set_image(&image);
}
if tooltip != old_widget.tooltip {
icon.set_tooltip(tooltip.as_deref());
}
}
let mut new_descendants = Vec::new();
for item in &self.widget(app).menus {
new_descendants.push(Rc::clone(item));
new_descendants.extend(item.descendants());
}
if !same_items(&new_descendants, &app.get(self).descendants) {
app.get_mut(self).descendants = new_descendants;
if app.get(self).icon.is_some() {
self.update_menu(app);
}
}
}
fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
self.widget(app)
.child
.clone()
.unwrap_or_else(|| SizedBox::new().into_widget())
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::sync::Arc;
use std::time::Duration;
use waft_embedder::{Offset, TargetPlatform};
use waft_test::{MockTrayIcons, TestPlatform};
use super::*;
use crate::testing::WidgetTester;
use crate::widgets::platform_menu_bar::{
PlainPlatformMenuItem, PlatformMenuItemGroup, PlatformProvidedMenuItem,
PlatformProvidedMenuItemType,
};
type Heard = Rc<RefCell<Vec<&'static str>>>;
fn tester_with_tray(tray: &MockTrayIcons) -> WidgetTester {
WidgetTester::with_platform(
TestPlatform::new()
.on(TargetPlatform::MacOS)
.with_tray_icons(Rc::new(tray.clone())),
)
}
fn mount(tester: &mut WidgetTester, widget: WidgetRef) {
tester.pump_widget(widget);
tester.pump_by(Duration::ZERO);
}
fn image() -> IconImage {
IconImage {
width: 1,
height: 1,
rgba: Arc::from([0u8, 0, 0, 255]),
template: true,
}
}
fn heard(heard: &Heard, what: &'static str) -> VoidCallback {
let heard = Rc::clone(heard);
Rc::new(move |_app| heard.borrow_mut().push(what))
}
fn icon(heard_into: &Heard, tooltip: &str) -> PlatformTrayIcon {
PlatformTrayIcon::new(image())
.tooltip(tooltip)
.menus(vec![
Rc::new(PlainPlatformMenuItem::new("Open").on_selected(heard(heard_into, "Open"))),
Rc::new(PlatformMenuItemGroup::new(vec![Rc::new(
PlainPlatformMenuItem::new("Save"),
)])),
Rc::new(PlatformProvidedMenuItem::new(
PlatformProvidedMenuItemType::Copy,
)),
])
.on_activated(heard(heard_into, "activated"))
.on_double_clicked(heard(heard_into, "double clicked"))
}
fn report(tester: &mut WidgetTester, id: u64, event: TrayIconEvent) {
tester.update(|app| {
let callback = app
.platform_callbacks()
.on_tray_icon_event
.clone()
.expect("the binding hears tray icon events");
callback(app, id, event);
});
tester.pump_by(Duration::ZERO);
}
fn the_menu() -> Vec<MenuEntry> {
vec![
MenuEntry::Item {
id: 1,
item: waft_embedder::MenuItem::new("Open"),
},
MenuEntry::Separator,
MenuEntry::Item {
id: 2,
item: waft_embedder::MenuItem::new("Save").enabled(false),
},
MenuEntry::Separator,
MenuEntry::Provided {
id: 3,
item: waft_embedder::ProvidedItem::Copy,
enabled: true,
},
]
}
#[test]
fn where_the_system_opens_the_menu_it_is_handed_the_menu_and_reports_the_choice() {
let tray = MockTrayIcons::new().without_menus_at_icon();
let tester = &mut tester_with_tray(&tray);
let heard = Heard::default();
mount(tester, icon(&heard, "Notes").into_widget());
let [shown] = &tray.icons()[..] else {
panic!("one icon: {:?}", tray.icons());
};
assert_eq!(shown.tooltip.as_deref(), Some("Notes"));
assert_eq!(shown.attached_menu, the_menu());
report(tester, shown.id, TrayIconEvent::MenuSelected(1));
report(
tester,
shown.id,
TrayIconEvent::ContextMenuRequested {
position: Offset::ZERO,
},
);
assert!(
tray.icons()[0].menus_opened.is_empty(),
"the system opens the menu itself"
);
report(
tester,
shown.id,
TrayIconEvent::Activated {
position: Offset::ZERO,
},
);
report(tester, shown.id, TrayIconEvent::DoubleClicked);
assert_eq!(*heard.borrow(), ["Open", "activated", "double clicked"]);
}
#[test]
fn elsewhere_the_menu_opens_at_the_icon_when_the_user_asks_for_it() {
let tray = MockTrayIcons::new()
.without_attached_menus()
.answering(Some(1));
let tester = &mut tester_with_tray(&tray);
let heard = Heard::default();
mount(tester, icon(&heard, "Notes").into_widget());
let id = tray.icons()[0].id;
assert!(heard.borrow().is_empty());
report(
tester,
id,
TrayIconEvent::ContextMenuRequested {
position: Offset::ZERO,
},
);
assert_eq!(tray.icons()[0].menus_opened, [the_menu()]);
assert_eq!(*heard.borrow(), ["Open"], "the choice reaches its item");
}
#[test]
fn the_icon_is_there_while_the_widget_is_and_follows_its_rebuilds() {
let tray = MockTrayIcons::new();
let tester = &mut tester_with_tray(&tray);
let heard = Heard::default();
mount(tester, icon(&heard, "Notes").into_widget());
let id = tray.icons()[0].id;
mount(
tester,
icon(&heard, "Notes, 3 new")
.menus(vec![Rc::new(PlainPlatformMenuItem::new("Quit"))])
.into_widget(),
);
let [shown] = &tray.icons()[..] else {
panic!("the same icon: {:?}", tray.icons());
};
assert_eq!(shown.id, id, "a rebuild keeps the icon");
assert_eq!(shown.tooltip.as_deref(), Some("Notes, 3 new"));
let [MenuEntry::Item { item, .. }] = &shown.attached_menu[..] else {
panic!("the new menu: {:?}", shown.attached_menu);
};
assert_eq!(item.label, "Quit");
mount(tester, SizedBox::new().into_widget());
assert!(tray.icons().is_empty(), "the icon goes with the widget");
}
#[test]
fn several_icons_are_each_their_own() {
let tray = MockTrayIcons::new();
let tester = &mut tester_with_tray(&tray);
let (first, second) = (Heard::default(), Heard::default());
mount(
tester,
icon(&first, "First")
.child(icon(&second, "Second"))
.into_widget(),
);
let icons = tray.icons();
assert_eq!(icons.len(), 2);
report(tester, icons[1].id, TrayIconEvent::MenuSelected(1));
assert!(first.borrow().is_empty());
assert_eq!(*second.borrow(), ["Open"], "ids are each icon's own");
}
#[test]
fn an_icon_the_system_refuses_shows_nowhere() {
let tray = MockTrayIcons::new().refusing("no tray");
let tester = &mut tester_with_tray(&tray);
mount(tester, icon(&Heard::default(), "Notes").into_widget());
assert!(tray.icons().is_empty());
}
}