Skip to main content

ModalHost

Struct ModalHost 

Source
pub struct ModalHost<M> { /* private fields */ }
Expand description

Generic modal transition state machine used by higher-level hosts.

Implementations§

Source§

impl<M: Copy> ModalHost<M>

Source

pub const fn new() -> Self

Creates an empty modal host.

Examples found in repository?
examples/alerts.rs (line 37)
11fn main() {
12    let bounds = Rectangle::new(Point::zero(), Size::new(320, 240));
13    let theme = support::theme();
14    let i18n = support::i18n();
15    let mut canvas = support::NullCanvas::new(bounds.size);
16
17    let spec = AlertSpec::confirm(
18        Localized::new("alert.title", "Delete item?"),
19        Localized::new("alert.message", "This action cannot be undone."),
20    );
21    let mut alert = AlertView::new(spec);
22    let panel = alert.panel(bounds, &theme, &i18n);
23    alert.draw(&mut canvas, panel, &theme, &i18n);
24    let _ = alert.handle_touch(TouchEvent::new(panel.center(), TouchPhase::Start, 1), panel);
25
26    let mut host = AlertHost::<u8, 2>::new();
27    let _ = host.present(7, spec);
28    let _ = host.layer(bounds, &theme, &i18n);
29    host.draw(&mut canvas, bounds, &theme, &i18n);
30    let _ = host.handle_touch(
31        TouchEvent::new(Point::new(160, 160), TouchPhase::End, 2),
32        bounds,
33        &theme,
34        &i18n,
35    );
36
37    let mut modal = ModalHost::new();
38    modal.show(3_u8);
39    let _ = modal.current(bounds, &theme);
40    let _ = Rgb565::BLACK;
41}
More examples
Hide additional examples
src/alert_host/mod.rs (line 40)
38    pub const fn new() -> Self {
39        Self {
40            host: ModalHost::new(),
41            alert: None,
42        }
43    }
Source

pub fn show(&mut self, modal: M)

Starts presenting modal.

Examples found in repository?
examples/alerts.rs (line 38)
11fn main() {
12    let bounds = Rectangle::new(Point::zero(), Size::new(320, 240));
13    let theme = support::theme();
14    let i18n = support::i18n();
15    let mut canvas = support::NullCanvas::new(bounds.size);
16
17    let spec = AlertSpec::confirm(
18        Localized::new("alert.title", "Delete item?"),
19        Localized::new("alert.message", "This action cannot be undone."),
20    );
21    let mut alert = AlertView::new(spec);
22    let panel = alert.panel(bounds, &theme, &i18n);
23    alert.draw(&mut canvas, panel, &theme, &i18n);
24    let _ = alert.handle_touch(TouchEvent::new(panel.center(), TouchPhase::Start, 1), panel);
25
26    let mut host = AlertHost::<u8, 2>::new();
27    let _ = host.present(7, spec);
28    let _ = host.layer(bounds, &theme, &i18n);
29    host.draw(&mut canvas, bounds, &theme, &i18n);
30    let _ = host.handle_touch(
31        TouchEvent::new(Point::new(160, 160), TouchPhase::End, 2),
32        bounds,
33        &theme,
34        &i18n,
35    );
36
37    let mut modal = ModalHost::new();
38    modal.show(3_u8);
39    let _ = modal.current(bounds, &theme);
40    let _ = Rgb565::BLACK;
41}
More examples
Hide additional examples
src/alert_host/mod.rs (line 52)
46    pub fn present<const N: usize>(
47        &mut self,
48        key: Modal,
49        spec: AlertSpec<'a, N>,
50    ) -> Result<(), AlertHostError> {
51        self.alert = Some(PresentedAlert::new(key, spec)?);
52        self.host.show(key);
53        Ok(())
54    }
Source

pub fn dismiss(&mut self)

Starts dismissing the current modal, if any.

Examples found in repository?
src/alert_host/mod.rs (line 58)
57    pub fn dismiss_presented(&mut self) {
58        self.host.dismiss();
59    }
60
61    /// Alias for [`Self::dismiss_presented`].
62    pub fn dismiss(&mut self) {
63        self.dismiss_presented();
64    }
65
66    /// Clears transient button state.
67    pub fn clear_touch_state(&mut self) {
68        if let Some(alert) = self.alert.as_mut() {
69            alert.clear_touch_state();
70        }
71    }
72
73    /// Returns whether the host is animating a modal transition.
74    pub const fn is_animating(&self) -> bool {
75        self.host.is_animating()
76    }
77
78    /// Advances modal animation state.
79    pub fn advance(&mut self, dt_ms: u32) -> bool {
80        let changed = self.host.advance(dt_ms);
81        if !self.host.has_modal() {
82            self.alert = None;
83        }
84        changed
85    }
86
87    /// Returns the currently presented modal key, if any.
88    pub fn presented_key(&self) -> Option<Modal> {
89        self.alert.as_ref().map(PresentedAlert::key)
90    }
91
92    /// Returns the title of the currently presented modal, if any.
93    pub fn presented_title(&self) -> Option<Localized<'a>> {
94        self.alert.as_ref().map(PresentedAlert::title)
95    }
96
97    /// Returns the current modal layer geometry.
98    pub fn layer(
99        &self,
100        bounds: Rectangle,
101        theme: &FsTheme,
102        i18n: &I18n<'a>,
103    ) -> Option<ModalLayer<Modal>> {
104        let alert = self.alert.as_ref()?;
105        let panel = alert.panel(bounds, theme, i18n);
106        self.host.current_with_panel(bounds, panel)
107    }
108
109    /// Routes one touch event to the alert host.
110    pub fn handle_touch(
111        &mut self,
112        touch: TouchEvent,
113        bounds: Rectangle,
114        theme: &FsTheme,
115        i18n: &I18n<'a>,
116    ) -> AlertHostResponse<Modal> {
117        let Some(alert) = self.alert.as_mut() else {
118            return AlertHostResponse {
119                action: None,
120                captured: false,
121                redraw: false,
122            };
123        };
124
125        let panel = alert.panel(bounds, theme, i18n);
126        let Some(layer) = self.host.current_with_panel(bounds, panel) else {
127            return AlertHostResponse {
128                action: None,
129                captured: false,
130                redraw: false,
131            };
132        };
133
134        let response = alert.handle_touch(touch, layer.translated_panel());
135        if let Some(action) = response.action {
136            self.host.dismiss();
137            return AlertHostResponse {
138                action: Some((alert.key(), action)),
139                captured: true,
140                redraw: true,
141            };
142        }
143
144        AlertHostResponse {
145            action: None,
146            captured: true,
147            redraw: response.redraw,
148        }
149    }
Source

pub const fn is_animating(&self) -> bool

Returns whether a show or hide animation is in progress.

Examples found in repository?
src/alert_host/mod.rs (line 75)
74    pub const fn is_animating(&self) -> bool {
75        self.host.is_animating()
76    }
Source

pub const fn has_modal(&self) -> bool

Returns whether any modal is currently owned by the host.

Examples found in repository?
src/alert_host/mod.rs (line 81)
79    pub fn advance(&mut self, dt_ms: u32) -> bool {
80        let changed = self.host.advance(dt_ms);
81        if !self.host.has_modal() {
82            self.alert = None;
83        }
84        changed
85    }
Source

pub fn advance(&mut self, dt_ms: u32) -> bool

Advances the modal animation state.

Examples found in repository?
src/alert_host/mod.rs (line 80)
79    pub fn advance(&mut self, dt_ms: u32) -> bool {
80        let changed = self.host.advance(dt_ms);
81        if !self.host.has_modal() {
82            self.alert = None;
83        }
84        changed
85    }
Source

pub fn current( &self, bounds: Rectangle, theme: &FsTheme, ) -> Option<ModalLayer<M>>

Returns the current modal layer using the default themed panel.

Examples found in repository?
examples/alerts.rs (line 39)
11fn main() {
12    let bounds = Rectangle::new(Point::zero(), Size::new(320, 240));
13    let theme = support::theme();
14    let i18n = support::i18n();
15    let mut canvas = support::NullCanvas::new(bounds.size);
16
17    let spec = AlertSpec::confirm(
18        Localized::new("alert.title", "Delete item?"),
19        Localized::new("alert.message", "This action cannot be undone."),
20    );
21    let mut alert = AlertView::new(spec);
22    let panel = alert.panel(bounds, &theme, &i18n);
23    alert.draw(&mut canvas, panel, &theme, &i18n);
24    let _ = alert.handle_touch(TouchEvent::new(panel.center(), TouchPhase::Start, 1), panel);
25
26    let mut host = AlertHost::<u8, 2>::new();
27    let _ = host.present(7, spec);
28    let _ = host.layer(bounds, &theme, &i18n);
29    host.draw(&mut canvas, bounds, &theme, &i18n);
30    let _ = host.handle_touch(
31        TouchEvent::new(Point::new(160, 160), TouchPhase::End, 2),
32        bounds,
33        &theme,
34        &i18n,
35    );
36
37    let mut modal = ModalHost::new();
38    modal.show(3_u8);
39    let _ = modal.current(bounds, &theme);
40    let _ = Rgb565::BLACK;
41}
Source

pub fn current_with_panel( &self, bounds: Rectangle, panel: Rectangle, ) -> Option<ModalLayer<M>>

Returns the current modal layer using an explicit panel rectangle.

Examples found in repository?
src/modal.rs (line 96)
95    pub fn current(&self, bounds: Rectangle, theme: &FsTheme) -> Option<ModalLayer<M>> {
96        self.current_with_panel(bounds, modal_panel(bounds, theme))
97    }
More examples
Hide additional examples
src/alert_host/mod.rs (line 106)
98    pub fn layer(
99        &self,
100        bounds: Rectangle,
101        theme: &FsTheme,
102        i18n: &I18n<'a>,
103    ) -> Option<ModalLayer<Modal>> {
104        let alert = self.alert.as_ref()?;
105        let panel = alert.panel(bounds, theme, i18n);
106        self.host.current_with_panel(bounds, panel)
107    }
108
109    /// Routes one touch event to the alert host.
110    pub fn handle_touch(
111        &mut self,
112        touch: TouchEvent,
113        bounds: Rectangle,
114        theme: &FsTheme,
115        i18n: &I18n<'a>,
116    ) -> AlertHostResponse<Modal> {
117        let Some(alert) = self.alert.as_mut() else {
118            return AlertHostResponse {
119                action: None,
120                captured: false,
121                redraw: false,
122            };
123        };
124
125        let panel = alert.panel(bounds, theme, i18n);
126        let Some(layer) = self.host.current_with_panel(bounds, panel) else {
127            return AlertHostResponse {
128                action: None,
129                captured: false,
130                redraw: false,
131            };
132        };
133
134        let response = alert.handle_touch(touch, layer.translated_panel());
135        if let Some(action) = response.action {
136            self.host.dismiss();
137            return AlertHostResponse {
138                action: Some((alert.key(), action)),
139                captured: true,
140                redraw: true,
141            };
142        }
143
144        AlertHostResponse {
145            action: None,
146            captured: true,
147            redraw: response.redraw,
148        }
149    }

Trait Implementations§

Source§

impl<M: Clone> Clone for ModalHost<M>

Source§

fn clone(&self) -> ModalHost<M>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug> Debug for ModalHost<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<M: PartialEq> PartialEq for ModalHost<M>

Source§

fn eq(&self, other: &ModalHost<M>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<M: Copy> Copy for ModalHost<M>

Source§

impl<M: Eq> Eq for ModalHost<M>

Source§

impl<M> StructuralPartialEq for ModalHost<M>

Auto Trait Implementations§

§

impl<M> Freeze for ModalHost<M>
where M: Freeze,

§

impl<M> RefUnwindSafe for ModalHost<M>
where M: RefUnwindSafe,

§

impl<M> Send for ModalHost<M>
where M: Send,

§

impl<M> Sync for ModalHost<M>
where M: Sync,

§

impl<M> Unpin for ModalHost<M>
where M: Unpin,

§

impl<M> UnsafeUnpin for ModalHost<M>
where M: UnsafeUnpin,

§

impl<M> UnwindSafe for ModalHost<M>
where M: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Az for T

Source§

fn az<Dst>(self) -> Dst
where T: Cast<Dst>,

Casts the value.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Src, Dst> CastFrom<Src> for Dst
where Src: Cast<Dst>,

Source§

fn cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> CheckedAs for T

Source§

fn checked_as<Dst>(self) -> Option<Dst>
where T: CheckedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> CheckedCastFrom<Src> for Dst
where Src: CheckedCast<Dst>,

Source§

fn checked_cast_from(src: Src) -> Option<Dst>

Casts the value.
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> OverflowingAs for T

Source§

fn overflowing_as<Dst>(self) -> (Dst, bool)
where T: OverflowingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> OverflowingCastFrom<Src> for Dst
where Src: OverflowingCast<Dst>,

Source§

fn overflowing_cast_from(src: Src) -> (Dst, bool)

Casts the value.
Source§

impl<T> SaturatingAs for T

Source§

fn saturating_as<Dst>(self) -> Dst
where T: SaturatingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> SaturatingCastFrom<Src> for Dst
where Src: SaturatingCast<Dst>,

Source§

fn saturating_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> UnwrappedAs for T

Source§

fn unwrapped_as<Dst>(self) -> Dst
where T: UnwrappedCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> UnwrappedCastFrom<Src> for Dst
where Src: UnwrappedCast<Dst>,

Source§

fn unwrapped_cast_from(src: Src) -> Dst

Casts the value.
Source§

impl<T> WrappingAs for T

Source§

fn wrapping_as<Dst>(self) -> Dst
where T: WrappingCast<Dst>,

Casts the value.
Source§

impl<Src, Dst> WrappingCastFrom<Src> for Dst
where Src: WrappingCast<Dst>,

Source§

fn wrapping_cast_from(src: Src) -> Dst

Casts the value.