use std::collections::HashMap;
use mgui_app_lifecycle::FeatureLifecycle;
use mgui_core::element::ElementTree;
use mgui_shell::WindowShell;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct CommandId(pub u64);
impl CommandId {
pub const fn new(id: u64) -> Self {
Self(id)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommandSource {
Menu,
TrayMenu,
TrayClick,
GlobalShortcut,
SecondInstance,
}
impl CommandSource {
pub fn as_str(self) -> &'static str {
match self {
Self::Menu => "menu",
Self::TrayMenu => "tray_menu",
Self::TrayClick => "tray_click",
Self::GlobalShortcut => "global_shortcut",
Self::SecondInstance => "second_instance",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommandOutcome {
Consumed,
PassThrough,
}
pub struct CommandScope<'a> {
shell: &'a dyn WindowShell,
tree: &'a mut ElementTree,
lifecycle: Option<&'a FeatureLifecycle>,
args: &'a [String],
source: CommandSource,
needs_redraw: Option<&'a mut bool>,
needs_rebuild: Option<&'a mut bool>,
}
impl<'a> CommandScope<'a> {
pub fn new(
shell: &'a dyn WindowShell,
tree: &'a mut ElementTree,
lifecycle: Option<&'a FeatureLifecycle>,
args: &'a [String],
source: CommandSource,
needs_redraw: Option<&'a mut bool>,
needs_rebuild: Option<&'a mut bool>,
) -> Self {
Self { shell, tree, lifecycle, args, source, needs_redraw, needs_rebuild }
}
pub fn shell(&self) -> &dyn WindowShell {
self.shell
}
pub fn tree_mut(&mut self) -> &mut ElementTree {
self.tree
}
pub fn lifecycle(&self) -> Option<&FeatureLifecycle> {
self.lifecycle
}
pub fn source(&self) -> CommandSource {
self.source
}
pub fn args(&self) -> &[String] {
self.args
}
pub fn request_redraw(&mut self) {
if let Some(flag) = self.needs_redraw.as_deref_mut() {
*flag = true;
}
}
pub fn request_rebuild(&mut self) {
if let Some(flag) = self.needs_rebuild.as_deref_mut() {
*flag = true;
}
self.request_redraw();
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct CommandStats {
pub delivered: u64,
pub consumed: u64,
pub passthrough: u64,
pub menu: u64,
pub tray_menu: u64,
pub tray_click: u64,
pub global_shortcut: u64,
pub second_instance: u64,
}
type CommandHandler = Box<dyn FnMut(&mut CommandScope<'_>) -> CommandOutcome>;
#[derive(Default)]
pub struct CommandRouter {
handlers: HashMap<CommandId, CommandHandler>,
stats: CommandStats,
}
impl std::fmt::Debug for CommandRouter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CommandRouter").field("registered", &self.handlers.len()).field("stats", &self.stats).finish()
}
}
impl CommandRouter {
pub fn new() -> Self {
Self::default()
}
pub fn register<F>(&mut self, id: CommandId, handler: F) -> &mut Self
where
F: FnMut(&mut CommandScope<'_>) -> CommandOutcome + 'static,
{
self.handlers.insert(id, Box::new(handler));
self
}
pub fn registered(&self) -> usize {
self.handlers.len()
}
pub fn stats(&self) -> CommandStats {
self.stats
}
pub fn dispatch(&mut self, scope: &mut CommandScope<'_>, id: CommandId) -> CommandOutcome {
let source = scope.source;
self.stats.delivered += 1;
match source {
CommandSource::Menu => self.stats.menu += 1,
CommandSource::TrayMenu => self.stats.tray_menu += 1,
CommandSource::TrayClick => self.stats.tray_click += 1,
CommandSource::GlobalShortcut => self.stats.global_shortcut += 1,
CommandSource::SecondInstance => self.stats.second_instance += 1,
}
match self.handlers.get_mut(&id) {
Some(handler) => {
let outcome = handler(scope);
if outcome == CommandOutcome::Consumed {
self.stats.consumed += 1;
} else {
self.stats.passthrough += 1;
}
outcome
}
None => {
self.stats.passthrough += 1;
CommandOutcome::PassThrough
}
}
}
}