Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Overlay composition #2142

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::widget::Tree;
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size};
use crate::{Clipboard, Layout, Point, Rectangle, Shell, Size, Vector};

/// An interactive component that can be displayed on top of other widgets.
pub trait Overlay<Message, Renderer>
Expand All @@ -29,6 +29,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node;

/// Draws the [`Overlay`] using the associated `Renderer`.
Expand Down
20 changes: 16 additions & 4 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::any::Any;
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message, Renderer> {
position: Point,
translation: Vector,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
}

Expand All @@ -25,7 +26,11 @@ where
position: Point,
overlay: Box<dyn Overlay<Message, Renderer> + 'a>,
) -> Self {
Self { position, overlay }
Self {
position,
overlay,
translation: Vector::ZERO,
}
}

/// Returns the position of the [`Element`].
Expand All @@ -36,6 +41,7 @@ where
/// Translates the [`Element`].
pub fn translate(mut self, translation: Vector) -> Self {
self.position = self.position + translation;
self.translation = self.translation + translation;
self
}

Expand All @@ -48,6 +54,7 @@ where
{
Element {
position: self.position,
translation: self.translation,
overlay: Box::new(Map::new(self.overlay, f)),
}
}
Expand All @@ -59,8 +66,12 @@ where
bounds: Size,
translation: Vector,
) -> layout::Node {
self.overlay
.layout(renderer, bounds, self.position + translation)
self.overlay.layout(
renderer,
bounds,
self.position + translation,
self.translation + translation,
)
}

/// Processes a runtime [`Event`].
Expand Down Expand Up @@ -154,8 +165,9 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.content.layout(renderer, bounds, position)
self.content.layout(renderer, bounds, position, translation)
}

fn operate(
Expand Down
9 changes: 5 additions & 4 deletions core/src/overlay/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::widget;
use crate::{Clipboard, Event, Layout, Overlay, Point, Rectangle, Shell, Size};
use crate::{
Clipboard, Event, Layout, Overlay, Point, Rectangle, Shell, Size, Vector,
};

/// An [`Overlay`] container that displays multiple overlay [`overlay::Element`]
/// children.
Expand Down Expand Up @@ -64,10 +66,9 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_position: Point,
translation: Vector,
) -> layout::Node {
let translation = position - Point::ORIGIN;

layout::Node::with_children(
bounds,
self.children
Expand Down
2 changes: 2 additions & 0 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod modal {
use iced::mouse;
use iced::{
BorderRadius, Color, Element, Event, Length, Point, Rectangle, Size,
Vector,
};

/// A widget that centers a modal element over some base element
Expand Down Expand Up @@ -413,6 +414,7 @@ mod modal {
renderer: &Renderer,
_bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, self.size)
.width(Length::Fill)
Expand Down
1 change: 1 addition & 0 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ mod toast {
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, bounds)
.width(Length::Fill)
Expand Down
15 changes: 8 additions & 7 deletions runtime/src/overlay/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use crate::core::mouse;
use crate::core::overlay;
use crate::core::renderer;
use crate::core::widget;
use crate::core::{Clipboard, Event, Layout, Point, Rectangle, Shell, Size};
use crate::core::{
Clipboard, Event, Layout, Point, Rectangle, Shell, Size, Vector,
};

/// An overlay container that displays nested overlays
#[allow(missing_debug_implementations)]
Expand Down Expand Up @@ -33,19 +35,18 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_position: Point,
translation: Vector,
) -> layout::Node {
fn recurse<Message, Renderer>(
element: &mut overlay::Element<'_, Message, Renderer>,
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node
where
Renderer: renderer::Renderer,
{
let translation = position - Point::ORIGIN;

let node = element.layout(renderer, bounds, translation);

if let Some(mut nested) =
Expand All @@ -55,15 +56,15 @@ where
node.size(),
vec![
node,
recurse(&mut nested, renderer, bounds, position),
recurse(&mut nested, renderer, bounds, translation),
],
)
} else {
layout::Node::with_children(node.size(), vec![node])
}
}

recurse(&mut self.overlay, renderer, bounds, position)
recurse(&mut self.overlay, renderer, bounds, translation)
}

/// Draws the [`Nested`] overlay using the associated `Renderer`.
Expand Down
30 changes: 23 additions & 7 deletions runtime/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::core::mouse;
use crate::core::renderer;
use crate::core::widget;
use crate::core::window;
use crate::core::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
use crate::core::{
Clipboard, Element, Layout, Point, Rectangle, Shell, Size, Vector,
};
use crate::overlay;

/// A set of interactive graphical elements with a specific [`Layout`].
Expand Down Expand Up @@ -199,7 +201,8 @@ where
let bounds = self.bounds;

let mut overlay = manual_overlay.as_mut().unwrap();
let mut layout = overlay.layout(renderer, bounds, Point::ORIGIN);
let mut layout =
overlay.layout(renderer, bounds, Point::ORIGIN, Vector::ZERO);
let mut event_statuses = Vec::new();

for event in events.iter().cloned() {
Expand Down Expand Up @@ -253,8 +256,12 @@ where
overlay = manual_overlay.as_mut().unwrap();

shell.revalidate_layout(|| {
layout =
overlay.layout(renderer, bounds, Point::ORIGIN);
layout = overlay.layout(
renderer,
bounds,
Point::ORIGIN,
Vector::ZERO,
);
});
}

Expand Down Expand Up @@ -448,7 +455,12 @@ where
.map(overlay::Nested::new)
{
let overlay_layout = self.overlay.take().unwrap_or_else(|| {
overlay.layout(renderer, self.bounds, Point::ORIGIN)
overlay.layout(
renderer,
self.bounds,
Point::ORIGIN,
Vector::ZERO,
)
});

let cursor = if cursor
Expand Down Expand Up @@ -566,8 +578,12 @@ where
.map(overlay::Nested::new)
{
if self.overlay.is_none() {
self.overlay =
Some(overlay.layout(renderer, self.bounds, Point::ORIGIN));
self.overlay = Some(overlay.layout(
renderer,
self.bounds,
Point::ORIGIN,
Vector::ZERO,
));
}

overlay.operate(
Expand Down
5 changes: 3 additions & 2 deletions widget/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::core::widget::tree::{self, Tree};
use crate::core::widget::{self, Widget};
use crate::core::Element;
use crate::core::{
self, Clipboard, Hasher, Length, Point, Rectangle, Shell, Size,
self, Clipboard, Hasher, Length, Point, Rectangle, Shell, Size, Vector,
};
use crate::runtime::overlay::Nested;

Expand Down Expand Up @@ -333,9 +333,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
3 changes: 2 additions & 1 deletion widget/src/lazy/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,9 +577,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
6 changes: 4 additions & 2 deletions widget/src/lazy/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::core::renderer;
use crate::core::widget;
use crate::core::widget::tree::{self, Tree};
use crate::core::{
self, Clipboard, Element, Length, Point, Rectangle, Shell, Size, Widget,
self, Clipboard, Element, Length, Point, Rectangle, Shell, Size, Vector,
Widget,
};
use crate::horizontal_space;
use crate::runtime::overlay::Nested;
Expand Down Expand Up @@ -367,9 +368,10 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.with_overlay_maybe(|overlay| {
overlay.layout(renderer, bounds, position)
overlay.layout(renderer, bounds, position, translation)
})
.unwrap_or_default()
}
Expand Down
1 change: 1 addition & 0 deletions widget/src/overlay/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let space_below = bounds.height - (position.y + self.target_height);
let space_above = position.y;
Expand Down
1 change: 1 addition & 0 deletions widget/src/tooltip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ where
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let viewport = Rectangle::with_size(bounds);

Expand Down
Loading