Skip to main content

ferroui_browser/
browser_activatable_lifetime.rs

1use ferroui_base::reactive::IDisposable;
2use ferroui_controls::application_lifetimes::{
3    ActivatableLifetimeBase, ActivatedEventArgs, ActivationKind, IActivatableLifetime,
4};
5use std::rc::Rc;
6
7/// Activation of the application follows the visibility of the page.
8pub struct BrowserActivatableLifetime {
9    base: Rc<ActivatableLifetimeBase>,
10}
11
12impl Default for BrowserActivatableLifetime {
13    fn default() -> Self {
14        Self { base: ActivatableLifetimeBase::new() }
15    }
16}
17
18impl BrowserActivatableLifetime {
19    /// Creates the lifetime.
20    pub fn new() -> Rc<Self> {
21        Rc::new(Self::default())
22    }
23
24    /// The page reported its visibility state (`"visible"`, `"hidden"`).
25    pub fn on_visibility_state_changed(&self, visibility_state: &str) {
26        let visible = visibility_state == "visible";
27        if visible {
28            self.base.on_activated_kind(ActivationKind::Background);
29        } else {
30            self.base.on_deactivated_kind(ActivationKind::Background);
31        }
32    }
33}
34
35impl IActivatableLifetime for BrowserActivatableLifetime {
36    fn activated(&self, handler: Rc<dyn Fn(&ActivatedEventArgs)>) -> Rc<dyn IDisposable> {
37        self.base.activated(handler)
38    }
39
40    fn deactivated(&self, handler: Rc<dyn Fn(&ActivatedEventArgs)>) -> Rc<dyn IDisposable> {
41        self.base.deactivated(handler)
42    }
43
44    fn try_leave_background(&self) -> bool {
45        self.base.try_leave_background()
46    }
47
48    fn try_enter_background(&self) -> bool {
49        self.base.try_enter_background()
50    }
51}