Skip to main content

ButtonTouchState

Struct ButtonTouchState 

Source
pub struct ButtonTouchState<K> { /* private fields */ }
Expand description

Shared touch tracker for a set of buttons.

Implementations§

Source§

impl<K: Copy + Eq> ButtonTouchState<K>

Source

pub const fn new() -> Self

Creates an empty touch state.

Examples found in repository?
src/alert/view.rs (line 33)
30    pub const fn new(spec: AlertSpec<'a, N>) -> Self {
31        Self {
32            spec,
33            touch_state: ButtonTouchState::new(),
34        }
35    }
More examples
Hide additional examples
src/containers.rs (line 98)
94    pub fn new(root: ViewId, title_for: TitleFor) -> Self {
95        Self {
96            nav: StackNav::new(root),
97            title_for,
98            header_touch: ButtonTouchState::new(),
99        }
100    }
src/stack_view.rs (line 115)
111    pub fn draw_static_chrome<D>(&self, display: &mut D, theme: &FsTheme, i18n: &I18n<'a>)
112    where
113        D: DrawTarget<Color = Rgb565>,
114    {
115        let touch = ButtonTouchState::new();
116        self.draw_chrome(display, theme, i18n, &touch);
117    }
src/app/ui_app.rs (line 27)
22    pub fn new(root: ViewId, delegate: Delegate, theme: FsTheme, i18n: I18n<'a>) -> Self {
23        Self {
24            theme,
25            i18n,
26            stack: StackNav::new(root),
27            nav_touch: ButtonTouchState::new(),
28            delegate,
29        }
30    }
src/alert_host/presented.rs (line 50)
34    pub(super) fn new<const N: usize>(
35        key: Modal,
36        spec: AlertSpec<'a, N>,
37    ) -> Result<Self, AlertHostError> {
38        let mut actions = Vec::new();
39        for action in spec.actions {
40            actions
41                .push(action)
42                .map_err(|_| AlertHostError::TooManyActions)?;
43        }
44        Ok(Self {
45            key,
46            kind: spec.kind,
47            title: spec.title,
48            message: spec.message,
49            actions,
50            touch_state: ButtonTouchState::new(),
51        })
52    }
Source

pub fn clear(&mut self)

Clears the active button, if any.

Examples found in repository?
src/alert/view.rs (line 39)
38    pub fn clear_touch_state(&mut self) {
39        self.touch_state.clear();
40    }
More examples
Hide additional examples
src/alert_host/presented.rs (line 63)
62    pub(super) fn clear_touch_state(&mut self) {
63        self.touch_state.clear();
64    }
src/app/ui_app.rs (line 147)
146    pub fn clear_touch_state(&mut self) {
147        self.nav_touch.clear();
148        self.delegate.clear_touch_state();
149    }
src/containers.rs (line 105)
103    pub fn set_root(&mut self, root: ViewId) {
104        self.nav = StackNav::new(root);
105        self.header_touch.clear();
106    }
107
108    /// Pushes a new view onto the stack.
109    pub fn push_view(&mut self, view: ViewId) -> Result<(), StackError> {
110        self.nav.push(view)
111    }
112
113    /// Pops the current view off the stack.
114    pub fn pop_view(&mut self) -> Result<ViewId, StackError> {
115        self.nav.pop()
116    }
117
118    /// Returns the top-most view identifier.
119    pub fn top_view(&self) -> ViewId {
120        self.nav.top()
121    }
122
123    /// Returns the current stack depth.
124    pub fn depth(&self) -> usize {
125        self.nav.depth()
126    }
127
128    /// Returns whether the stack is animating.
129    pub fn is_animating(&self) -> bool {
130        self.nav.is_animating()
131    }
132
133    /// Advances stack animation state.
134    pub fn advance(&mut self, dt_ms: u32) -> bool {
135        self.nav.advance(dt_ms)
136    }
137
138    /// Builds a navigation view for the supplied frame.
139    pub fn nav_view(&self, frame: Rectangle) -> NavView<'a, ViewId> {
140        self.nav.nav_view(frame, &self.title_for)
141    }
142
143    /// Clears transient header touch state.
144    pub fn clear_touch_state(&mut self) {
145        self.header_touch.clear();
146    }
src/stack_view.rs (line 226)
218    pub fn handle_touch(
219        &self,
220        touch_state: &mut ButtonTouchState<NavHeaderAction>,
221        touch: TouchEvent,
222    ) -> ButtonTouchResponse<NavHeaderAction> {
223        if let Some(button) = self.back_button.as_ref() {
224            touch_state.handle_touch(touch, core::slice::from_ref(button))
225        } else {
226            touch_state.clear();
227            ButtonTouchResponse {
228                action: None,
229                captured: false,
230                redraw: false,
231            }
232        }
233    }
Source

pub fn is_highlighted(&self, button: &Button<'_, K>) -> bool

Returns whether the supplied button is currently highlighted.

Examples found in repository?
src/alert/view.rs (line 122)
62    pub fn draw_state<D>(&self, display: &mut D, panel: Rectangle, theme: &FsTheme, i18n: &I18n<'a>)
63    where
64        D: embedded_graphics::draw_target::DrawTarget<Color = Rgb565>,
65    {
66        let layout = alert_layout_in_panel::<N>(panel, i18n.text(self.spec.message));
67        RoundedRectangle::with_equal_corners(
68            Rectangle::new(panel.top_left + Point::new(6, 8), panel.size),
69            Size::new(22, 22),
70        )
71        .into_styled(PrimitiveStyle::with_fill(theme.dim))
72        .draw(display)
73        .ok();
74
75        let panel_style = PrimitiveStyleBuilder::new()
76            .fill_color(theme.surface)
77            .stroke_color(theme.surface_alt)
78            .stroke_width(2)
79            .build();
80        RoundedRectangle::with_equal_corners(panel, Size::new(22, 22))
81            .into_styled(panel_style)
82            .draw(display)
83            .ok();
84
85        let title_style = MonoTextStyleBuilder::new()
86            .font(&FONT_9X18_BOLD)
87            .text_color(kind_color(self.spec.kind, theme))
88            .build();
89        let message_style = MonoTextStyleBuilder::new()
90            .font(&FONT_7X14)
91            .text_color(theme.text_secondary)
92            .build();
93        let text_style = TextStyleBuilder::new()
94            .alignment(Alignment::Center)
95            .baseline(Baseline::Middle)
96            .build();
97
98        Text::with_text_style(
99            i18n.text(self.spec.title),
100            panel.center() + Point::new(0, -40),
101            title_style,
102            text_style,
103        )
104        .draw(display)
105        .ok();
106        for (index, line) in layout.message_lines.iter().enumerate() {
107            Text::with_text_style(
108                line,
109                Point::new(panel.center().x, message_top(panel) + (index as i32 * 18)),
110                message_style,
111                text_style,
112            )
113            .draw(display)
114            .ok();
115        }
116
117        for button in self.buttons(panel) {
118            button.draw_state(
119                display,
120                theme,
121                i18n,
122                self.touch_state.is_highlighted(&button),
123            );
124        }
125    }
More examples
Hide additional examples
src/alert_host/presented.rs (line 143)
79    pub(super) fn draw<D>(
80        &self,
81        display: &mut D,
82        panel: Rectangle,
83        theme: &FsTheme,
84        i18n: &I18n<'a>,
85    ) where
86        D: UiCanvas,
87    {
88        let layout = alert_layout_in_panel::<1>(panel, i18n.text(self.message));
89        RoundedRectangle::with_equal_corners(
90            Rectangle::new(panel.top_left + Point::new(6, 8), panel.size),
91            Size::new(22, 22),
92        )
93        .into_styled(PrimitiveStyle::with_fill(theme.dim))
94        .draw(display)
95        .ok();
96        RoundedRectangle::with_equal_corners(panel, Size::new(22, 22))
97            .into_styled(
98                PrimitiveStyleBuilder::new()
99                    .fill_color(theme.surface)
100                    .stroke_color(theme.surface_alt)
101                    .stroke_width(2)
102                    .build(),
103            )
104            .draw(display)
105            .ok();
106
107        let title_style = MonoTextStyleBuilder::new()
108            .font(&FONT_9X18_BOLD)
109            .text_color(kind_color(self.kind, theme))
110            .build();
111        let message_style = MonoTextStyleBuilder::new()
112            .font(&FONT_7X14)
113            .text_color(theme.text_secondary)
114            .build();
115        let text_style = TextStyleBuilder::new()
116            .alignment(Alignment::Center)
117            .baseline(Baseline::Middle)
118            .build();
119
120        Text::with_text_style(
121            i18n.text(self.title),
122            panel.center() + Point::new(0, -40),
123            title_style,
124            text_style,
125        )
126        .draw(display)
127        .ok();
128        for (index, line) in layout.message_lines.iter().enumerate() {
129            Text::with_text_style(
130                line,
131                Point::new(panel.center().x, message_top(panel) + (index as i32 * 18)),
132                message_style,
133                text_style,
134            )
135            .draw(display)
136            .ok();
137        }
138        for button in self.buttons(panel) {
139            button.draw_state(
140                display,
141                theme,
142                i18n,
143                self.touch_state.is_highlighted(&button),
144            );
145        }
146    }
src/stack_view.rs (line 167)
120    pub fn draw_chrome<D>(
121        &self,
122        display: &mut D,
123        theme: &FsTheme,
124        i18n: &I18n<'a>,
125        touch: &ButtonTouchState<NavHeaderAction>,
126    ) where
127        D: DrawTarget<Color = Rgb565>,
128    {
129        let shell = PrimitiveStyleBuilder::new()
130            .fill_color(theme.surface_alt)
131            .stroke_color(theme.surface)
132            .stroke_width(2)
133            .build();
134        self.frame.into_styled(shell).draw(display).ok();
135
136        self.header
137            .into_styled(PrimitiveStyle::with_fill(theme.surface))
138            .draw(display)
139            .ok();
140        Line::new(
141            self.header.top_left + Point::new(0, self.header.size.height as i32),
142            self.header.bottom_right().unwrap_or(self.header.top_left),
143        )
144        .into_styled(PrimitiveStyle::with_stroke(theme.surface_alt, 2))
145        .draw(display)
146        .ok();
147
148        let title_style = MonoTextStyleBuilder::new()
149            .font(&FONT_9X18_BOLD)
150            .text_color(theme.text_primary)
151            .build();
152        let back_style = MonoTextStyleBuilder::new()
153            .font(&FONT_7X14)
154            .text_color(theme.text_primary)
155            .build();
156        let text_style = TextStyleBuilder::new()
157            .alignment(Alignment::Center)
158            .baseline(Baseline::Middle)
159            .build();
160        let settled_back_button = self.back_title.is_some() && self.secondary_title.is_none();
161
162        if settled_back_button {
163            let button = self
164                .back_button
165                .as_ref()
166                .expect("settled stacked headers include a back button");
167            button.draw_state(display, theme, i18n, touch.is_highlighted(button));
168            let arrow = MonoTextStyleBuilder::new()
169                .font(&FONT_7X14)
170                .text_color(theme.text_primary)
171                .build();
172            Text::with_text_style(
173                "<",
174                button.frame.top_left + Point::new(18, (button.frame.size.height / 2) as i32),
175                arrow,
176                TextStyleBuilder::new()
177                    .alignment(Alignment::Center)
178                    .baseline(Baseline::Middle)
179                    .build(),
180            )
181            .draw(display)
182            .ok();
183        }
184
185        let mut header = display.clipped(&self.header);
186        if settled_back_button {
187            let title = self
188                .back_title
189                .expect("settled stacked headers include a back title");
190            Text::with_text_style(
191                i18n.text(title),
192                back_title_center(self.back_button.as_ref()),
193                back_style,
194                text_style,
195            )
196            .draw(&mut header)
197            .ok();
198        }
199        draw_title(
200            &mut header,
201            i18n.text(self.active_title.title),
202            self.active_title.center,
203            title_style,
204            text_style,
205        );
206        if let Some(title) = self.secondary_title {
207            draw_title(
208                &mut header,
209                i18n.text(title.title),
210                title.center,
211                title_style,
212                text_style,
213            );
214        }
215    }
Source

pub fn handle_touch<'a>( &mut self, touch: TouchEvent, buttons: &[Button<'a, K>], ) -> ButtonTouchResponse<K>

Applies a touch sample to a button slice.

Examples found in repository?
src/alert/view.rs (line 50)
48    pub fn handle_touch(&mut self, touch: TouchEvent, panel: Rectangle) -> ButtonTouchResponse<u8> {
49        let buttons = self.buttons(panel);
50        self.touch_state.handle_touch(touch, &buttons)
51    }
More examples
Hide additional examples
src/alert_host/presented.rs (line 76)
70    pub(super) fn handle_touch(
71        &mut self,
72        touch: TouchEvent,
73        panel: Rectangle,
74    ) -> ButtonTouchResponse<u8> {
75        let buttons = self.buttons(panel);
76        self.touch_state.handle_touch(touch, buttons.as_slice())
77    }
src/stack_view.rs (line 224)
218    pub fn handle_touch(
219        &self,
220        touch_state: &mut ButtonTouchState<NavHeaderAction>,
221        touch: TouchEvent,
222    ) -> ButtonTouchResponse<NavHeaderAction> {
223        if let Some(button) = self.back_button.as_ref() {
224            touch_state.handle_touch(touch, core::slice::from_ref(button))
225        } else {
226            touch_state.clear();
227            ButtonTouchResponse {
228                action: None,
229                captured: false,
230                redraw: false,
231            }
232        }
233    }

Trait Implementations§

Source§

impl<K: Clone> Clone for ButtonTouchState<K>

Source§

fn clone(&self) -> ButtonTouchState<K>

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<K: Debug> Debug for ButtonTouchState<K>

Source§

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

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

impl<K: PartialEq> PartialEq for ButtonTouchState<K>

Source§

fn eq(&self, other: &ButtonTouchState<K>) -> 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<K: Copy> Copy for ButtonTouchState<K>

Source§

impl<K: Eq> Eq for ButtonTouchState<K>

Source§

impl<K> StructuralPartialEq for ButtonTouchState<K>

Auto Trait Implementations§

§

impl<K> Freeze for ButtonTouchState<K>
where K: Freeze,

§

impl<K> RefUnwindSafe for ButtonTouchState<K>
where K: RefUnwindSafe,

§

impl<K> Send for ButtonTouchState<K>
where K: Send,

§

impl<K> Sync for ButtonTouchState<K>
where K: Sync,

§

impl<K> Unpin for ButtonTouchState<K>
where K: Unpin,

§

impl<K> UnsafeUnpin for ButtonTouchState<K>
where K: UnsafeUnpin,

§

impl<K> UnwindSafe for ButtonTouchState<K>
where K: 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.