Skip to content

Commit

Permalink
Fix cursor passthrough in Stack during draw
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Aug 14, 2024
1 parent 8b45d62 commit 9ed7fb8
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
28 changes: 15 additions & 13 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,21 @@ where
{
stack![
base.into(),
mouse_area(center(opaque(content)).style(|_theme| {
container::Style {
background: Some(
Color {
a: 0.8,
..Color::BLACK
}
.into(),
),
..container::Style::default()
}
}))
.on_press(on_blur)
opaque(
mouse_area(center(opaque(content)).style(|_theme| {
container::Style {
background: Some(
Color {
a: 0.8,
..Color::BLACK
}
.into(),
),
..container::Style::default()
}
}))
.on_press(on_blur)
)
]
.into()
}
69 changes: 52 additions & 17 deletions widget/src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,53 @@ where
viewport: &Rectangle,
) {
if let Some(clipped_viewport) = layout.bounds().intersection(viewport) {
for (i, ((layer, state), layout)) in self
let layers_below = if cursor == mouse::Cursor::Unavailable {
self.children.len()
} else {
self.children
.iter()
.rev()
.zip(tree.children.iter().rev())
.zip(layout.children().rev())
.position(|((layer, state), layout)| {
let interaction = layer.as_widget().mouse_interaction(
state, layout, cursor, viewport, renderer,
);

interaction != mouse::Interaction::None
})
.map(|i| self.children.len() - i - 1)
.unwrap_or(self.children.len())
};

let mut layers = self
.children
.iter()
.zip(&tree.children)
.zip(layout.children())
.enumerate()
{
if i > 0 {
renderer.with_layer(clipped_viewport, |renderer| {
.enumerate();

let layers = layers.by_ref();

let mut draw_layer =
|i,
layer: &Element<'a, Message, Theme, Renderer>,
state,
layout,
cursor| {
if i > 0 {
renderer.with_layer(clipped_viewport, |renderer| {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
);
});
} else {
layer.as_widget().draw(
state,
renderer,
Expand All @@ -287,18 +325,15 @@ where
cursor,
&clipped_viewport,
);
});
} else {
layer.as_widget().draw(
state,
renderer,
theme,
style,
layout,
cursor,
&clipped_viewport,
);
}
}
};

for (i, ((layer, state), layout)) in layers.take(layers_below) {
draw_layer(i, layer, state, layout, mouse::Cursor::Unavailable);
}

for (i, ((layer, state), layout)) in layers {
draw_layer(i, layer, state, layout, cursor);
}
}
}
Expand Down

0 comments on commit 9ed7fb8

Please sign in to comment.