Skip to main content

ferroui_native/
mac_os_activatable_lifetime.rs

1use ferroui_base::reactive::IDisposable;
2use ferroui_base::{FerroLocator, LocatorExtensions};
3use ferroui_controls::application_lifetimes::{
4    ActivatableLifetimeBase, ActivatedEventArgs, ActivationKind, IActivatableLifetime,
5};
6use ferroui_controls::platform::INativeApplicationCommands;
7use std::rc::Rc;
8
9/// The activatable lifetime of a macOS application: entering the background
10/// hides the application, leaving it shows the application again.
11pub struct MacOSActivatableLifetime {
12    base: Rc<ActivatableLifetimeBase>,
13}
14
15impl Default for MacOSActivatableLifetime {
16    fn default() -> Self {
17        Self { base: ActivatableLifetimeBase::new() }
18    }
19}
20
21impl MacOSActivatableLifetime {
22    pub fn new() -> Self {
23        Self::default()
24    }
25
26    /// Raises the activated event.
27    pub fn on_activated(&self, kind: ActivationKind) {
28        self.base.on_activated_kind(kind);
29    }
30
31    /// Raises the activated event with the given arguments.
32    pub fn on_activated_with(&self, event_args: ActivatedEventArgs) {
33        self.base.on_activated(event_args);
34    }
35
36    /// Raises the deactivated event.
37    pub fn on_deactivated(&self, kind: ActivationKind) {
38        self.base.on_deactivated_kind(kind);
39    }
40}
41
42impl IActivatableLifetime for MacOSActivatableLifetime {
43    fn activated(&self, handler: Rc<dyn Fn(&ActivatedEventArgs)>) -> Rc<dyn IDisposable> {
44        self.base.activated(handler)
45    }
46
47    fn deactivated(&self, handler: Rc<dyn Fn(&ActivatedEventArgs)>) -> Rc<dyn IDisposable> {
48        self.base.deactivated(handler)
49    }
50
51    fn try_leave_background(&self) -> bool {
52        let native_application_commands = FerroLocator::current().get_service::<dyn INativeApplicationCommands>();
53        if let Some(commands) = native_application_commands {
54            commands.show_app();
55        }
56
57        true
58    }
59
60    fn try_enter_background(&self) -> bool {
61        let native_application_commands = FerroLocator::current().get_service::<dyn INativeApplicationCommands>();
62        if let Some(commands) = native_application_commands {
63            commands.hide_app();
64        }
65
66        true
67    }
68}