use std::boxed::Box;
use std::path::PathBuf;
use std::string::String;
use std::sync::{Mutex, OnceLock};
use std::vec::Vec;
use crate::solver::Region;
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum NativeMenuItem {
Action {
id: u32,
label: String,
accelerator: Option<String>,
enabled: bool,
checked: Option<bool>,
},
Separator,
Submenu {
label: String,
items: Vec<NativeMenuItem>,
},
}
impl NativeMenuItem {
pub fn action(id: u32, label: impl Into<String>, accelerator: Option<&str>) -> Self {
Self::Action {
id,
label: label.into(),
accelerator: accelerator.map(String::from),
enabled: true,
checked: None,
}
}
pub fn checkbox(id: u32, label: impl Into<String>, checked: bool) -> Self {
Self::Action {
id,
label: label.into(),
accelerator: None,
enabled: true,
checked: Some(checked),
}
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct NativeMenuSection {
pub title: String,
pub items: Vec<NativeMenuItem>,
}
#[derive(Clone, PartialEq, Eq, Debug, Default)]
pub struct NativeMenuBar {
pub sections: Vec<NativeMenuSection>,
}
impl NativeMenuBar {
pub fn new() -> Self {
Self {
sections: Vec::new(),
}
}
pub fn add_section(mut self, title: impl Into<String>, items: Vec<NativeMenuItem>) -> Self {
self.sections.push(NativeMenuSection {
title: title.into(),
items,
});
self
}
pub fn find_action_label(&self, target_id: u32) -> Option<&str> {
fn search(items: &[NativeMenuItem], id: u32) -> Option<&str> {
for item in items {
match item {
NativeMenuItem::Action {
id: item_id, label, ..
} if *item_id == id => return Some(label.as_str()),
NativeMenuItem::Submenu { items: sub, .. } => {
if let Some(found) = search(sub, id) {
return Some(found);
}
}
_ => {}
}
}
None
}
for sec in &self.sections {
if let Some(found) = search(&sec.items, target_id) {
return Some(found);
}
}
None
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct SystemTraySpec {
pub id: String,
pub tooltip: String,
pub badge_count: u32,
pub menu: Vec<NativeMenuItem>,
}
impl SystemTraySpec {
pub fn new(id: impl Into<String>, tooltip: impl Into<String>) -> Self {
Self {
id: id.into(),
tooltip: tooltip.into(),
badge_count: 0,
menu: Vec::new(),
}
}
pub fn with_menu(mut self, items: Vec<NativeMenuItem>) -> Self {
self.menu = items;
self
}
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum FileDialogMode {
OpenFile,
OpenMultiple,
SaveFile,
PickFolder,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FileFilter {
pub name: String,
pub extensions: Vec<String>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct FileDialogRequest {
pub mode: FileDialogMode,
pub title: String,
pub default_name: Option<String>,
pub filters: Vec<FileFilter>,
}
impl FileDialogRequest {
pub fn open_file(title: impl Into<String>) -> Self {
Self {
mode: FileDialogMode::OpenFile,
title: title.into(),
default_name: None,
filters: Vec::new(),
}
}
pub fn save_file(title: impl Into<String>, default_name: impl Into<String>) -> Self {
Self {
mode: FileDialogMode::SaveFile,
title: title.into(),
default_name: Some(default_name.into()),
filters: Vec::new(),
}
}
pub fn add_filter(mut self, name: impl Into<String>, exts: &[&str]) -> Self {
self.filters.push(FileFilter {
name: name.into(),
extensions: exts.iter().map(|s| String::from(*s)).collect(),
});
self
}
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum FileDialogResult {
Selected(Vec<PathBuf>),
Cancelled,
}
type MockDialogFn = Box<dyn Fn(&FileDialogRequest) -> FileDialogResult + Send + Sync>;
static MOCK_FILE_DIALOG: OnceLock<Mutex<Option<MockDialogFn>>> = OnceLock::new();
pub fn set_mock_file_dialog_handler<F>(handler: Option<F>)
where
F: Fn(&FileDialogRequest) -> FileDialogResult + Send + Sync + 'static,
{
let lock = MOCK_FILE_DIALOG.get_or_init(|| Mutex::new(None));
if let Ok(mut g) = lock.lock() {
*g = handler.map(|f| Box::new(f) as MockDialogFn);
}
}
pub fn run_file_dialog(req: &FileDialogRequest) -> FileDialogResult {
let lock = MOCK_FILE_DIALOG.get_or_init(|| Mutex::new(None));
if let Ok(g) = lock.lock() {
if let Some(ref f) = *g {
return f(req);
}
}
FileDialogResult::Cancelled
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct DragPreviewGhost {
pub title: String,
pub subtitle: String,
pub badge_count: u32,
pub pointer_x: i16,
pub pointer_y: i16,
pub width: i16,
pub height: i16,
}
impl DragPreviewGhost {
pub fn new(title: impl Into<String>, subtitle: impl Into<String>, px: i16, py: i16) -> Self {
Self {
title: title.into(),
subtitle: subtitle.into(),
badge_count: 1,
pointer_x: px,
pointer_y: py,
width: 196,
height: 44,
}
}
#[inline]
pub fn clamped_bounds(&self, viewport: Region) -> Region {
let x = (self.pointer_x + 14).clamp(
viewport.x,
(viewport.right() - self.width).max(viewport.x),
);
let y = (self.pointer_y + 14).clamp(
viewport.y,
(viewport.bottom() - self.height).max(viewport.y),
);
Region::new(x, y, self.width, self.height)
}
}