fix: capture mouse motion and mouse interactions in overlay

This commit is contained in:
Ashley Wulber 2026-03-05 15:38:28 -05:00 • committed by Michael Murphy
parent 1810bedfa5
commit 1970499459
4 changed files with 417 additions and 407 deletions

View file

@ -135,7 +135,7 @@ impl<'a, S: AsRef<str>, Message: 'a, Item: Clone + PartialEq + 'static>
self.on_selected.as_ref(),
self.selections,
|| tree.state.downcast_mut::<State<Item>>(),
)
);
}
fn mouse_interaction(

View file

@ -585,9 +585,9 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
Cow::Borrowed(_) => panic!(),
Cow::Owned(o) => o.as_mut_slice(),
};
let menu_status = process_menu_events(
process_menu_events(
self,
&event,
event,
view_cursor,
renderer,
clipboard,
@ -629,8 +629,7 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
if !self.is_overlay && !view_cursor.is_over(viewport) {
return None;
}
let (new_root, status) = process_overlay_events(
let new_root = process_overlay_events(
self,
renderer,
viewport_size,
@ -641,6 +640,10 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
shell,
);
if self.is_overlay && view_cursor.is_over(viewport) {
shell.capture_event();
}
return new_root;
}
@ -680,8 +683,9 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
feature = "winit",
feature = "surface-message"
))]
if matches!(WINDOWING_SYSTEM.get(), Some(WindowingSystem::Wayland)) {
if let Some(handler) = self.on_surface_action.as_ref() {
if matches!(WINDOWING_SYSTEM.get(), Some(WindowingSystem::Wayland))
&& let Some(handler) = self.on_surface_action.as_ref()
{
let mut root = self.window_id;
let mut depth = self.depth;
while let Some(parent) =
@ -694,10 +698,8 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
root = *parent.0;
depth = depth.saturating_sub(1);
}
shell.publish((handler)(crate::surface::Action::DestroyPopup(
root,
)));
}
shell
.publish((handler)(crate::surface::Action::DestroyPopup(root)));
}
state.reset();
@ -708,7 +710,7 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
if self.bar_bounds.contains(overlay_cursor) {
state.reset();
}
})
});
}
_ => {}
@ -804,8 +806,8 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
let menu_color = styling.background;
r.fill_quad(menu_quad, menu_color);
// draw path hightlight
if let (true, Some(active)) = (draw_path, ms.index) {
if let Some(active_layout) = children_layout
if let (true, Some(active)) = (draw_path, ms.index)
&& let Some(active_layout) = children_layout
.children()
.nth(active.saturating_sub(start_index))
{
@ -824,7 +826,6 @@ impl<'b, Message: Clone + 'static> Menu<'b, Message> {
r.fill_quad(path_quad, styling.path);
}
}
if start_index < menu_roots.len() {
// draw item
menu_roots[start_index..=end_index]
@ -894,6 +895,19 @@ impl<Message: Clone + 'static> overlay::Overlay<Message, crate::Theme, crate::Re
) {
self.draw(renderer, theme, style, layout, cursor);
}
fn mouse_interaction(
&self,
layout: Layout<'_>,
cursor: mouse::Cursor,
_renderer: &crate::Renderer,
) -> mouse::Interaction {
if cursor.is_over(layout.bounds()) {
mouse::Interaction::Idle
} else {
mouse::Interaction::None
}
}
}
impl<Message: std::clone::Clone + 'static> Widget<Message, crate::Theme, crate::Renderer>
@ -948,8 +962,9 @@ impl<Message: std::clone::Clone + 'static> Widget<Message, crate::Theme, crate::
feature = "winit",
feature = "surface-message"
))]
if matches!(WINDOWING_SYSTEM.get(), Some(WindowingSystem::Wayland)) {
if let Some((new_root, new_ms)) = new_root {
if matches!(WINDOWING_SYSTEM.get(), Some(WindowingSystem::Wayland))
&& let Some((new_root, new_ms)) = new_root
{
use iced_runtime::platform_specific::wayland::popup::{
SctkPopupSettings, SctkPositioner,
};
@ -1085,10 +1100,22 @@ impl<Message: std::clone::Clone + 'static> Widget<Message, crate::Theme, crate::
}),
),
));
return;
}
}
fn mouse_interaction(
&self,
_tree: &Tree,
layout: Layout<'_>,
cursor: mouse::Cursor,
_viewport: &Rectangle,
_renderer: &crate::Renderer,
) -> mouse::Interaction {
if cursor.is_over(layout.bounds()) {
mouse::Interaction::Idle
} else {
mouse::Interaction::None
}
}
}
@ -1331,7 +1358,7 @@ fn process_menu_events<Message: std::clone::Clone>(
shell,
&Rectangle::default(),
);
})
});
}
#[allow(unused_results, clippy::too_many_lines, clippy::too_many_arguments)]
@ -1343,12 +1370,11 @@ fn process_overlay_events<Message>(
view_cursor: Cursor,
overlay_cursor: Point,
cross_offset: f32,
_shell: &mut Shell<'_, Message>,
) -> (Option<(usize, MenuState)>, event::Status)
shell: &mut Shell<'_, Message>,
) -> Option<(usize, MenuState)>
where
Message: std::clone::Clone,
{
use event::Status::{Captured, Ignored};
/*
if no active root || pressed:
return
@ -1431,8 +1457,8 @@ where
state.open = false;
}
}
return (new_menu_root, Captured);
shell.capture_event();
return new_menu_root;
};
let last_menu_bounds = &last_menu_state.menu_bounds;
@ -1446,7 +1472,8 @@ where
{
last_menu_state.index = None;
return (new_menu_root, Captured);
shell.capture_event();
return new_menu_root;
}
// calc new index
@ -1461,7 +1488,7 @@ where
};
if state.pressed {
return (new_menu_root, Ignored);
return new_menu_root;
}
let roots = active_root.iter().skip(1).fold(
&menu.menu_roots[active_root[0]].children,
@ -1494,7 +1521,7 @@ where
if matches!(WINDOWING_SYSTEM.get(), Some(WindowingSystem::Wayland)) && remove {
if let Some(id) = state.popup_id.remove(&menu.window_id) {
state.active_root.truncate(menu.depth + 1);
_shell.publish((menu.on_surface_action.as_ref().unwrap())({
shell.publish((menu.on_surface_action.as_ref().unwrap())({
crate::surface::action::destroy_popup(id)
}));
}
@ -1555,7 +1582,8 @@ where
state.menu_states.truncate(menu.depth + 1);
}
(new_menu_root, Captured)
shell.capture_event();
new_menu_root
})
}

View file

@ -20,7 +20,7 @@ use iced::clipboard::mime::AllowedMimeTypes;
use iced::touch::Finger;
use iced::{
Alignment, Background, Color, Event, Length, Padding, Rectangle, Size, Task, Vector, alignment,
event, keyboard, mouse, touch, window,
keyboard, mouse, touch, window,
};
use iced_core::mouse::ScrollDelta;
use iced_core::text::{self, Ellipsize, LineHeight, Renderer as TextRenderer, Shaping, Wrapping};
@ -36,7 +36,6 @@ use std::collections::HashSet;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::time::{Duration, Instant};
thread_local! {
@ -609,8 +608,8 @@ where
.text
.get(button)
.zip(state.paragraphs.entry(button))
&& !text.is_empty()
{
if !text.is_empty() {
icon_spacing = f32::from(self.button_spacing);
let paragraph = entry.or_insert_with(|| {
crate::Plain::new(Text {
@ -630,7 +629,6 @@ where
let size = paragraph.min_bounds();
width += size.width;
}
}
// Add indent to measurement if found.
if let Some(indent) = self.model.indent(button) {
@ -895,12 +893,12 @@ where
}
// Unfocus if another segmented control was focused.
if let Some(f) = state.focused.as_ref() {
if f.updated_at != LAST_FOCUS_UPDATE.with(|f| f.get()) {
if let Some(f) = state.focused.as_ref()
&& f.updated_at != LAST_FOCUS_UPDATE.with(|f| f.get())
{
state.unfocus();
}
}
}
fn size(&self) -> Size<Length> {
Size::new(self.width, self.height)
@ -1162,6 +1160,9 @@ where
None::<fn(_, _) -> Message>,
on_drop,
);
if matches!(ret, iced::event::Status::Captured) {
shell.capture_event();
}
if let Some(msg) = maybe_msg {
log::trace!(
target: TAB_REORDER_LOG_TARGET,
@ -1200,9 +1201,8 @@ where
}
Event::Touch(touch::Event::FingerLifted { id, .. }) => {
state.fingers_pressed.remove(&id);
state.fingers_pressed.remove(id);
}
_ => (),
}
@ -1301,8 +1301,8 @@ where
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left))
)
&& !over_close_button
&& let Some(position) = cursor_position.position()
{
if let Some(position) = cursor_position.position() {
state.tab_drag_candidate = Some(TabDragCandidate {
entity: key,
bounds,
@ -1323,17 +1323,15 @@ where
);
}
}
}
if is_lifted(&event) {
state.unfocus();
}
if let Some(on_activate) = self.on_activate.as_ref() {
if is_pressed(&event) {
if is_pressed(event) {
state.pressed_item = Some(Item::Tab(key));
} else if is_lifted(&event) {
if self.button_is_pressed(state, key) {
} else if is_lifted(&event) && self.button_is_pressed(state, key) {
shell.publish(on_activate(key));
state.set_focused();
state.focused_item = Item::Tab(key);
@ -1342,17 +1340,15 @@ where
return;
}
}
}
// Present a context menu on a right click event.
if self.context_menu.is_some() {
if let Some(on_context) = self.on_context.as_ref() {
if right_button_released(&event)
|| (touch_lifted(&event) && fingers_pressed == 2)
if self.context_menu.is_some()
&& let Some(on_context) = self.on_context.as_ref()
&& (right_button_released(&event)
|| (touch_lifted(&event) && fingers_pressed == 2))
{
state.show_context = Some(key);
state.context_cursor =
cursor_position.position().unwrap_or_default();
state.context_cursor = cursor_position.position().unwrap_or_default();
state.menu_state.inner.with_data_mut(|data| {
data.open = true;
@ -1363,8 +1359,6 @@ where
shell.capture_event();
return;
}
}
}
if let Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Middle)) =
event
@ -1385,9 +1379,10 @@ where
}
}
if self.scrollable_focus {
if let Some(on_activate) = self.on_activate.as_ref() {
if let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event {
if self.scrollable_focus
&& let Some(on_activate) = self.on_activate.as_ref()
&& let Event::Mouse(mouse::Event::WheelScrolled { delta }) = event
{
let current = Instant::now();
// Permit successive scroll wheel events only after a given delay.
@ -1438,8 +1433,6 @@ where
}
}
}
}
}
} else {
if let Item::Tab(key) = std::mem::replace(&mut state.hovered, Item::None) {
for key in self.model.order.iter().copied() {
@ -1460,11 +1453,11 @@ where
if let (Some(tab_drag), Some(candidate)) =
(self.tab_drag.as_ref(), state.tab_drag_candidate)
&& let Event::Mouse(mouse::Event::CursorMoved { .. }) = event
&& let Some(position) = cursor_position.position()
&& position.distance(candidate.origin) >= tab_drag.threshold
&& let Some(candidate) = state.tab_drag_candidate.take()
{
if let Event::Mouse(mouse::Event::CursorMoved { .. }) = event {
if let Some(position) = cursor_position.position() {
if position.distance(candidate.origin) >= tab_drag.threshold {
if let Some(candidate) = state.tab_drag_candidate.take() {
log::trace!(
target: TAB_REORDER_LOG_TARGET,
"tab drag threshold met entity={:?} distance={:.2} threshold={}",
@ -1483,10 +1476,6 @@ where
return;
}
}
}
}
}
}
if matches!(
event,
@ -1504,14 +1493,14 @@ where
{
state.focused_visible = true;
return if *modifiers == keyboard::Modifiers::SHIFT {
self.focus_previous(state, shell)
self.focus_previous(state, shell);
} else if modifiers.is_empty() {
self.focus_next(state, shell)
self.focus_next(state, shell);
};
}
if let Some(on_activate) = self.on_activate.as_ref() {
if let Event::Keyboard(keyboard::Event::KeyReleased {
if let Some(on_activate) = self.on_activate.as_ref()
&& let Event::Keyboard(keyboard::Event::KeyReleased {
key: keyboard::Key::Named(keyboard::key::Named::Enter),
..
}) = event
@ -1526,33 +1515,31 @@ where
state.buttons_offset -= 1;
// If the change would cause it to be insensitive, focus the first tab.
if !self.prev_tab_sensitive(state) {
if let Some(first) = self.first_tab(state) {
if !self.prev_tab_sensitive(state)
&& let Some(first) = self.first_tab(state)
{
state.focused_item = Item::Tab(first);
}
}
}
}
Item::NextButton => {
if self.next_tab_sensitive(state) {
state.buttons_offset += 1;
// If the change would cause it to be insensitive, focus the last tab.
if !self.next_tab_sensitive(state) {
if let Some(last) = self.last_tab(state) {
if !self.next_tab_sensitive(state)
&& let Some(last) = self.last_tab(state)
{
state.focused_item = Item::Tab(last);
}
}
}
}
Item::None | Item::Set => (),
}
shell.capture_event();
return;
}
}
}
}
@ -1794,14 +1781,15 @@ where
let original_bounds = bounds;
let center_y = bounds.center_y();
if show_drop_hint_marker {
if matches!(
if show_drop_hint_marker
&& matches!(
drop_hint_marker,
Some(DropHint {
entity,
side: DropSide::Before
}) if entity == key
) {
)
{
draw_drop_indicator(
renderer,
original_bounds,
@ -1810,7 +1798,6 @@ where
appearance.active.text_color,
);
}
}
let menu_open = || {
state.show_context == Some(key)
@ -1882,15 +1869,17 @@ where
let mut indent_padding = 0.0;
// Adjust bounds by indent
if let Some(indent) = self.model.indent(key) {
if indent > 0 {
if let Some(indent) = self.model.indent(key)
&& indent > 0
{
let adjustment = f32::from(indent) * f32::from(self.indent_spacing);
bounds.x += adjustment;
bounds.width -= adjustment;
// Draw indent line
if let crate::theme::SegmentedButton::FileNav = self.style {
if indent > 1 {
if let crate::theme::SegmentedButton::FileNav = self.style
&& indent > 1
{
indent_padding = 7.0;
for level in 1..indent {
@ -1917,8 +1906,6 @@ where
indent_padding += 4.0;
}
}
}
}
// Render the background of the button.
if key_is_focused || status_appearance.background.is_some() {
@ -1990,8 +1977,7 @@ where
bounds.x += offset;
} else {
// Draw the selection indicator if widget is a segmented selection, and the item is selected.
if key_is_active {
if let crate::theme::SegmentedButton::Control = self.style {
if key_is_active && let crate::theme::SegmentedButton::Control = self.style {
let mut image_bounds = bounds;
image_bounds.y = center_y - 8.0;
@ -2007,17 +1993,14 @@ where
height: 16.0,
..image_bounds
},
crate::widget::icon(
match crate::widget::common::object_select().data() {
crate::widget::icon(match crate::widget::common::object_select().data() {
crate::iced_core::svg::Data::Bytes(bytes) => {
crate::widget::icon::from_svg_bytes(bytes.as_ref())
.symbolic(true)
crate::widget::icon::from_svg_bytes(bytes.as_ref()).symbolic(true)
}
crate::iced_core::svg::Data::Path(path) => {
crate::widget::icon::from_path(path.clone())
}
},
),
}),
);
let offset = 16.0 + f32::from(self.button_spacing);
@ -2025,7 +2008,6 @@ where
bounds.x += offset;
}
}
}
// Whether to show the close button on this tab.
let show_close_button =

View file

@ -248,7 +248,7 @@ where
clipboard,
shell,
&layout.bounds(),
)
);
}
fn mouse_interaction(