sui_widgets/controls/
interaction.rs1use super::{set_hover_animation_target, set_press_animation_target};
2use crate::{DefaultTheme, Progress};
3use sui_core::{Event, KeyState, PointerButton, PointerEventKind, PointerKind, Rect};
4use sui_runtime::EventCtx;
5use sui_runtime::FrameClock;
6
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
15pub enum InteractionPreview {
16 #[default]
18 None,
19 Hovered,
21 Pressed,
23 Focused,
25}
26
27impl InteractionPreview {
28 pub(crate) fn hover(self) -> f32 {
30 matches!(self, Self::Hovered | Self::Pressed) as u8 as f32
31 }
32
33 pub(crate) fn press(self) -> f32 {
35 (self == Self::Pressed) as u8 as f32
36 }
37
38 pub(crate) fn focus(self) -> f32 {
40 (self == Self::Focused) as u8 as f32
41 }
42
43 pub(crate) fn is_focused(self) -> bool {
45 self == Self::Focused
46 }
47}
48
49pub(super) struct PressInteraction {
51 pub(super) hovered: bool,
52 pub(super) pressed: bool,
53 pub(super) hover_animation: Progress,
54 pub(super) press_animation: Progress,
55 pub(super) preview: InteractionPreview,
56 pub(super) focus_on_press: bool,
59 touch_press_bounds: Option<Rect>,
64}
65
66impl Default for PressInteraction {
67 fn default() -> Self {
68 Self {
69 hovered: false,
70 pressed: false,
71 hover_animation: Progress::new(0.0),
72 press_animation: Progress::new(0.0),
73 preview: InteractionPreview::None,
74 focus_on_press: true,
75 touch_press_bounds: None,
76 }
77 }
78}
79
80impl PressInteraction {
81 pub(super) fn hover_progress(&self, clock: &impl FrameClock) -> f32 {
83 self.hover_animation.get(clock).max(self.preview.hover())
84 }
85
86 pub(super) fn press_progress(&self, clock: &impl FrameClock) -> f32 {
88 self.press_animation.get(clock).max(self.preview.press())
89 }
90
91 pub(super) fn event(
92 &mut self,
93 ctx: &mut EventCtx,
94 event: &Event,
95 enabled: bool,
96 theme: impl Fn() -> DefaultTheme,
97 ) -> bool {
98 if !enabled {
99 if self.hovered || self.pressed {
100 let theme = theme();
101 self.hovered = false;
102 self.pressed = false;
103 self.touch_press_bounds = None;
104 set_hover_animation_target(&mut self.hover_animation, 0.0, &theme, ctx);
105 set_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx);
106 ctx.request_paint();
107 ctx.request_semantics();
108 }
109 return false;
110 }
111
112 match event {
113 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Move => {
114 let bounds = self.touch_press_bounds.unwrap_or(ctx.bounds());
115 self.set_hovered(bounds.contains(pointer.position), ctx, &theme);
116 }
117 Event::Pointer(_pointer) if matches!(_pointer.kind, PointerEventKind::Enter) => {
118 self.set_hovered(true, ctx, &theme);
119 }
120 Event::Pointer(_pointer) if matches!(_pointer.kind, PointerEventKind::Leave) => {
121 self.set_hovered(false, ctx, &theme);
122 }
123 Event::Pointer(pointer)
124 if pointer.kind == PointerEventKind::Down
125 && pointer.button == Some(PointerButton::Primary) =>
126 {
127 let theme = theme();
128 self.pressed = true;
129 self.hovered = true;
130 self.touch_press_bounds =
131 (pointer.pointer_kind == PointerKind::Touch).then(|| ctx.bounds());
132 set_hover_animation_target(&mut self.hover_animation, 1.0, &theme, ctx);
133 set_press_animation_target(&mut self.press_animation, 1.0, &theme, ctx);
134 ctx.request_pointer_capture(pointer.pointer_id);
135 if self.focus_on_press {
136 ctx.request_focus();
137 } else {
138 ctx.keep_focus();
139 }
140 ctx.request_paint();
141 ctx.request_semantics();
142 ctx.set_handled();
143 }
144 Event::Pointer(pointer)
145 if pointer.kind == PointerEventKind::Up
146 && (pointer.button == Some(PointerButton::Primary) || self.pressed) =>
147 {
148 let theme = theme();
149 let bounds = self.touch_press_bounds.take().unwrap_or(ctx.bounds());
150 let hovered = bounds.contains(pointer.position);
151 let activate = self.pressed && hovered;
152 self.pressed = false;
153 self.hovered = hovered;
154 set_hover_animation_target(
155 &mut self.hover_animation,
156 hovered as u8 as f32,
157 &theme,
158 ctx,
159 );
160 set_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx);
161 ctx.release_pointer_capture(pointer.pointer_id);
162 if activate {
163 return true;
164 }
165 ctx.request_paint();
166 ctx.request_semantics();
167 ctx.set_handled();
168 }
169 Event::Pointer(pointer) if pointer.kind == PointerEventKind::Cancel => {
170 if self.pressed {
171 let theme = theme();
172 self.pressed = false;
173 self.hovered = false;
174 self.touch_press_bounds = None;
175 set_hover_animation_target(&mut self.hover_animation, 0.0, &theme, ctx);
176 set_press_animation_target(&mut self.press_animation, 0.0, &theme, ctx);
177 ctx.release_pointer_capture(pointer.pointer_id);
178 ctx.request_paint();
179 ctx.request_semantics();
180 ctx.set_handled();
181 }
182 }
183 Event::Keyboard(key)
184 if key.state == KeyState::Pressed
185 && ctx.is_focused()
186 && matches!(key.key.as_str(), "Enter" | " ") =>
187 {
188 return true;
189 }
190 _ => {}
191 }
192
193 false
194 }
195
196 fn set_hovered(
197 &mut self,
198 hovered: bool,
199 ctx: &mut EventCtx,
200 theme: &impl Fn() -> DefaultTheme,
201 ) {
202 if self.hovered != hovered {
203 let theme = theme();
204 self.hovered = hovered;
205 set_hover_animation_target(
206 &mut self.hover_animation,
207 hovered as u8 as f32,
208 &theme,
209 ctx,
210 );
211 ctx.request_paint();
212 ctx.request_semantics();
213 }
214 }
215}