Skip to content

Commit 50eaef2

Browse files
committed
Merge branch 'master' into explore-input-method2
2 parents 141290c + 0c0651d commit 50eaef2

File tree

5 files changed

+68
-24
lines changed

5 files changed

+68
-24
lines changed

core/src/mouse/cursor.rs

+32-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use crate::{Point, Rectangle, Transformation, Vector};
22

3-
use std::ops::Mul;
4-
53
/// The mouse cursor state.
64
#[derive(Debug, Clone, Copy, PartialEq, Default)]
75
pub enum Cursor {
86
/// The cursor has a defined position.
97
Available(Point),
108

9+
/// The cursor has a defined position, but it's levitating over a layer above.
10+
Levitating(Point),
11+
1112
/// The cursor is currently unavailable (i.e. out of bounds or busy).
1213
#[default]
1314
Unavailable,
@@ -18,7 +19,7 @@ impl Cursor {
1819
pub fn position(self) -> Option<Point> {
1920
match self {
2021
Cursor::Available(position) => Some(position),
21-
Cursor::Unavailable => None,
22+
Cursor::Levitating(_) | Cursor::Unavailable => None,
2223
}
2324
}
2425

@@ -51,17 +52,41 @@ impl Cursor {
5152
pub fn is_over(self, bounds: Rectangle) -> bool {
5253
self.position_over(bounds).is_some()
5354
}
55+
56+
/// Returns true if the [`Cursor`] is levitating over a layer above.
57+
pub fn is_levitating(self) -> bool {
58+
matches!(self, Self::Levitating(_))
59+
}
60+
61+
/// Makes the [`Cursor`] levitate over a layer above.
62+
pub fn levitate(self) -> Self {
63+
match self {
64+
Self::Available(position) => Self::Levitating(position),
65+
_ => self,
66+
}
67+
}
68+
69+
/// Brings the [`Cursor`] back to the current layer.
70+
pub fn land(self) -> Self {
71+
match self {
72+
Cursor::Levitating(position) => Cursor::Available(position),
73+
_ => self,
74+
}
75+
}
5476
}
5577

56-
impl Mul<Transformation> for Cursor {
78+
impl std::ops::Mul<Transformation> for Cursor {
5779
type Output = Self;
5880

5981
fn mul(self, transformation: Transformation) -> Self {
6082
match self {
61-
Cursor::Unavailable => Cursor::Unavailable,
62-
Cursor::Available(point) => {
63-
Cursor::Available(point * transformation)
83+
Self::Available(position) => {
84+
Self::Available(position * transformation)
85+
}
86+
Self::Levitating(position) => {
87+
Self::Levitating(position * transformation)
6488
}
89+
Self::Unavailable => Self::Unavailable,
6590
}
6691
}
6792
}

core/src/shell.rs

+12
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ impl<'a, Message> Shell<'a, Message> {
7777
self.redraw_request
7878
}
7979

80+
/// Replaces the redraw request of the [`Shell`]; without conflict resolution.
81+
///
82+
/// This is useful if you want to overwrite the redraw request to a previous value.
83+
/// Since it's a fairly advanced use case and should rarely be used, it is a static
84+
/// method.
85+
pub fn replace_redraw_request(
86+
shell: &mut Self,
87+
redraw_request: window::RedrawRequest,
88+
) {
89+
shell.redraw_request = redraw_request;
90+
}
91+
8092
/// Requests the current [`InputMethod`] strategy.
8193
///
8294
/// __Important__: This request will only be honored by the

widget/src/helpers.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -871,16 +871,19 @@ where
871871
shell.request_redraw();
872872
}
873873

874+
let is_visible =
875+
is_hovered || self.is_top_focused || self.is_top_overlay_active;
876+
874877
if matches!(
875878
event,
876879
Event::Mouse(
877880
mouse::Event::CursorMoved { .. }
878881
| mouse::Event::ButtonReleased(_)
879882
)
880-
) || is_hovered
881-
|| self.is_top_focused
882-
|| self.is_top_overlay_active
883+
) || is_visible
883884
{
885+
let redraw_request = shell.redraw_request();
886+
884887
self.top.as_widget_mut().update(
885888
top_tree,
886889
event.clone(),
@@ -891,6 +894,11 @@ where
891894
shell,
892895
viewport,
893896
);
897+
898+
// Ignore redraw requests of invisible content
899+
if !is_visible {
900+
Shell::replace_redraw_request(shell, redraw_request);
901+
}
894902
};
895903

896904
if shell.is_event_captured() {

widget/src/scrollable.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,8 @@ where
564564
Event::Mouse(mouse::Event::CursorMoved { .. })
565565
| Event::Touch(touch::Event::FingerMoved { .. }) => {
566566
if let Some(scrollbar) = scrollbars.y {
567-
let Some(cursor_position) = cursor.position()
567+
let Some(cursor_position) =
568+
cursor.land().position()
568569
else {
569570
return;
570571
};
@@ -636,7 +637,8 @@ where
636637
match event {
637638
Event::Mouse(mouse::Event::CursorMoved { .. })
638639
| Event::Touch(touch::Event::FingerMoved { .. }) => {
639-
let Some(cursor_position) = cursor.position() else {
640+
let Some(cursor_position) = cursor.land().position()
641+
else {
640642
return;
641643
};
642644

widget/src/stack.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -216,15 +216,15 @@ where
216216
viewport: &Rectangle,
217217
) {
218218
let is_over = cursor.is_over(layout.bounds());
219-
let is_mouse_movement =
220-
matches!(event, Event::Mouse(mouse::Event::CursorMoved { .. }));
219+
let end = self.children.len() - 1;
221220

222-
for ((child, state), layout) in self
221+
for (i, ((child, state), layout)) in self
223222
.children
224223
.iter_mut()
225224
.rev()
226225
.zip(tree.children.iter_mut().rev())
227226
.zip(layout.children().rev())
227+
.enumerate()
228228
{
229229
child.as_widget_mut().update(
230230
state,
@@ -237,22 +237,19 @@ where
237237
viewport,
238238
);
239239

240-
if is_over
241-
&& !is_mouse_movement
242-
&& cursor != mouse::Cursor::Unavailable
243-
{
240+
if shell.is_event_captured() {
241+
return;
242+
}
243+
244+
if i < end && is_over && !cursor.is_levitating() {
244245
let interaction = child.as_widget().mouse_interaction(
245246
state, layout, cursor, viewport, renderer,
246247
);
247248

248249
if interaction != mouse::Interaction::None {
249-
cursor = mouse::Cursor::Unavailable;
250+
cursor = cursor.levitate();
250251
}
251252
}
252-
253-
if shell.is_event_captured() {
254-
return;
255-
}
256253
}
257254
}
258255

0 commit comments

Comments
 (0)