use std::cell::RefCell;
use std::rc::Rc;
#[cfg(not(target_arch = "wasm32"))]
#[linkme::distributed_slice]
pub static LAUNCHER_SHORTCUTS: [&'static [(&'static str, &'static str)]];
pub fn launcher_shortcuts() -> Vec<(&'static str, &'static str)> {
#[cfg(not(target_arch = "wasm32"))]
{
LAUNCHER_SHORTCUTS
.iter()
.flat_map(|list| list.iter().copied())
.collect()
}
#[cfg(target_arch = "wasm32")]
{
Vec::new()
}
}
day_reactive::tls_slots! {
shortcuts;
static DYNAMIC: RefCell<Vec<day_spec::LauncherShortcut>> = const { RefCell::new(Vec::new()) };
static ACTIONS: RefCell<Vec<(String, u64)>> = const { RefCell::new(Vec::new()) };
}
fn action_for(route: &str) -> u64 {
if let Some(id) = ACTIONS.with(|a| {
a.borrow()
.iter()
.find(|(r, _)| r == route)
.map(|(_, id)| *id)
}) {
return id;
}
let owned = route.to_owned();
let id = crate::menu::register_menu_action(Rc::new(move || {
crate::request_open_url(&owned);
}));
ACTIONS.with(|a| a.borrow_mut().push((route.to_owned(), id)));
id
}
pub fn set_launcher_shortcuts(shortcuts: Vec<day_spec::LauncherShortcut>) {
DYNAMIC.with(|d| *d.borrow_mut() = shortcuts.clone());
crate::with_tree(|t| t.set_launcher_shortcuts(&shortcuts));
crate::menu::install_dock_menu();
}
pub fn shortcut_menu_items() -> Vec<day_spec::MenuItem> {
let dynamic = DYNAMIC.with(|d| d.borrow().clone());
let declared = launcher_shortcuts()
.into_iter()
.map(|(route, label)| (route.to_owned(), day_localization::t(label)));
dynamic
.into_iter()
.map(|s| (s.route, s.label))
.chain(declared)
.map(|(route, label)| day_spec::MenuItem::Action {
id: Some(format!("shortcut:{route}")),
action: action_for(&route),
label,
shortcut: None,
enabled: true,
checked: None,
role: None,
icon: None,
})
.collect()
}