Merge pull request #1827 from MarcelP102/auto-hide-fixes

This commit is contained in:
Hakan Seven committed 2026-10-16 01:09:10 +03:00
commit 57d80654be
7 files changed
+502 -10

No files matched your search

+1 -1
View File
@@ -322,5 +322,5 @@ pub fn drop_down_below<'a>(
if let Some(w) = popup_width {
dd = dd.width(w);
}
dd.into()
crate::ui::drop_down_fit::drop_down_fit(dd)
}
+131
View File
@@ -0,0 +1,131 @@
//! Keeps an `iced_aw::DropDown` list under its field inside a scrollable.
//!
//! The drop-down keeps its list on screen by testing it against the viewport
//! it is handed, but reads only that viewport's width and height, as if it
//! started at the window's top-left corner. Inside a scrollable the viewport
//! is the scrollable's visible area in window coordinates, so a field in a
//! dock pallet (which does not start at the corner) sees its list "overflow",
//! and the list jumps left by its own width or up over the field. Wrapped
//! here, the drop-down gets a viewport reaching from the window corner to the
//! visible area's far edges: the test then trips only where the list really
//! leaves the visible area.
use iced::advanced::layout::{self, Layout};
use iced::advanced::widget::{self, tree, Tree, Widget};
use iced::advanced::{mouse, overlay, renderer, Shell};
use iced::{Element, Event, Length, Rectangle, Renderer, Size, Theme, Vector};
use crate::app::Message;
/// Wrap a drop-down so its list is placed against the visible area's real
/// edges.
pub fn drop_down_fit<'a>(drop_down: impl Into<Element<'a, Message>>) -> Element<'a, Message> {
Element::new(DropDownFit {
content: drop_down.into(),
})
}
struct DropDownFit<'a> {
content: Element<'a, Message>,
}
impl Widget<Message, Theme, Renderer> for DropDownFit<'_> {
fn tag(&self) -> tree::Tag {
self.content.as_widget().tag()
}
fn state(&self) -> tree::State {
self.content.as_widget().state()
}
fn diff(&mut self, tree: &mut Tree) {
self.content.as_widget_mut().diff(tree);
}
fn size(&self) -> Size<Length> {
self.content.as_widget().size()
}
fn layout(
&mut self,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.content.as_widget_mut().layout(tree, renderer, limits)
}
fn operate(
&mut self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation,
) {
self.content
.as_widget_mut()
.operate(tree, layout, renderer, operation);
}
fn update(
&mut self,
tree: &mut Tree,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &Renderer,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) {
self.content
.as_widget_mut()
.update(tree, event, layout, cursor, renderer, shell, viewport);
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.content
.as_widget()
.mouse_interaction(tree, layout, cursor, viewport, renderer)
}
fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
self.content
.as_widget()
.draw(tree, renderer, theme, style, layout, cursor, viewport);
}
fn overlay<'b>(
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'b>,
renderer: &Renderer,
viewport: &Rectangle,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
let from_corner = Rectangle {
x: 0.0,
y: 0.0,
width: viewport.x + viewport.width,
height: viewport.y + viewport.height,
};
self.content
.as_widget_mut()
.overlay(tree, layout, renderer, &from_corner, translation)
}
}
+175
View File
@@ -0,0 +1,175 @@
//! A pointer-enter / leave area that keeps counting the pointer as inside
//! while it rests on an overlay (a dropdown, a combo box list) drawn over
//! its bounds.
//!
//! `mouse_area` reads such a pointer as gone: iced hands the layer below an
//! overlay that wants the pointer an unavailable cursor, with no position. An
//! auto-hiding dock edge then hid as soon as one of its own dropdowns opened
//! under the pointer, taking the dropdown with it. Here an unavailable cursor
//! keeps the last state, so only a pointer seen outside the bounds, or one
//! that leaves the window, exits the area.
use iced::advanced::layout::{self, Layout};
use iced::advanced::widget::{self, tree, Tree, Widget};
use iced::advanced::{mouse, overlay, renderer, Shell};
use iced::{Element, Event, Length, Rectangle, Renderer, Size, Theme, Vector};
use crate::app::Message;
/// Wrap `content`, publishing `on_enter` / `on_exit` as the pointer enters
/// and leaves its bounds, overlays drawn over them included.
pub fn hover_zone<'a>(
content: impl Into<Element<'a, Message>>,
on_enter: Message,
on_exit: Message,
) -> Element<'a, Message> {
Element::new(HoverZone {
content: content.into(),
on_enter,
on_exit,
})
}
struct HoverZone<'a> {
content: Element<'a, Message>,
on_enter: Message,
on_exit: Message,
}
#[derive(Default)]
struct State {
hovered: bool,
}
impl Widget<Message, Theme, Renderer> for HoverZone<'_> {
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
}
fn state(&self) -> tree::State {
tree::State::new(State::default())
}
fn diff(&mut self, tree: &mut Tree) {
tree.diff_children(std::slice::from_mut(&mut self.content));
}
fn size(&self) -> Size<Length> {
self.content.as_widget().size()
}
fn layout(
&mut self,
tree: &mut Tree,
renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
self.content
.as_widget_mut()
.layout(&mut tree.children[0], renderer, limits)
}
fn operate(
&mut self,
tree: &mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
operation: &mut dyn widget::Operation,
) {
self.content
.as_widget_mut()
.operate(&mut tree.children[0], layout, renderer, operation);
}
fn update(
&mut self,
tree: &mut Tree,
event: &Event,
layout: Layout<'_>,
cursor: mouse::Cursor,
renderer: &Renderer,
shell: &mut Shell<'_, Message>,
viewport: &Rectangle,
) {
self.content.as_widget_mut().update(
&mut tree.children[0],
event,
layout,
cursor,
renderer,
shell,
viewport,
);
// Unlike `mouse_area`, this runs for captured events too, and a
// cursor without a position (on an overlay) changes nothing.
let hovered = match (event, cursor.land()) {
(Event::Mouse(mouse::Event::CursorLeft), _) => false,
(_, mouse::Cursor::Available(p)) => layout.bounds().contains(p),
_ => return,
};
let state = tree.state.downcast_mut::<State>();
if hovered != state.hovered {
state.hovered = hovered;
shell.publish(if hovered {
self.on_enter.clone()
} else {
self.on_exit.clone()
});
}
}
fn mouse_interaction(
&self,
tree: &Tree,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
renderer: &Renderer,
) -> mouse::Interaction {
self.content.as_widget().mouse_interaction(
&tree.children[0],
layout,
cursor,
viewport,
renderer,
)
}
fn draw(
&self,
tree: &Tree,
renderer: &mut Renderer,
theme: &Theme,
style: &renderer::Style,
layout: Layout<'_>,
cursor: mouse::Cursor,
viewport: &Rectangle,
) {
self.content.as_widget().draw(
&tree.children[0],
renderer,
theme,
style,
layout,
cursor,
viewport,
);
}
fn overlay<'b>(
&'b mut self,
tree: &'b mut Tree,
layout: Layout<'b>,
renderer: &Renderer,
viewport: &Rectangle,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
self.content.as_widget_mut().overlay(
&mut tree.children[0],
layout,
renderer,
viewport,
translation,
)
}
}
+2
View File
@@ -5,6 +5,8 @@ pub const ROW_H: f32 = 26.0;
pub mod color_select;
pub mod command_line;
pub mod dock;
pub mod drop_down_fit;
pub mod hover_zone;
pub mod icons;
pub mod menu_bar;
pub mod modal;