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, S = ()> {
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>,
app_state: Option<&'a mut S>,
}
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, app_state: None }
}
}
impl<'a, S> CommandScope<'a, S> {
#[allow(clippy::too_many_arguments)]
pub fn with_state(
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>,
app_state: &'a mut S,
) -> Self {
Self { shell, tree, lifecycle, args, source, needs_redraw, needs_rebuild, app_state: Some(app_state) }
}
pub fn app_state_mut(&mut self) -> &mut S {
self.app_state.as_deref_mut().expect("CommandScope 未注入类型化应用状态")
}
pub fn try_app_state_mut(&mut self) -> Option<&mut S> {
self.app_state.as_deref_mut()
}
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<S> = Box<dyn FnMut(&mut CommandScope<'_, S>) -> CommandOutcome>;
pub struct CommandRouter<S = ()> {
handlers: HashMap<CommandId, CommandHandler<S>>,
stats: CommandStats,
}
impl<S> Default for CommandRouter<S> {
fn default() -> Self {
Self { handlers: HashMap::new(), stats: CommandStats::default() }
}
}
impl<S> std::fmt::Debug for CommandRouter<S> {
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<S> CommandRouter<S> {
pub fn new() -> Self {
Self::default()
}
pub fn register<F>(&mut self, id: CommandId, handler: F) -> &mut Self
where
F: FnMut(&mut CommandScope<'_, S>) -> 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<'_, S>, 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
}
}
}
}