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

Remove position from overlay::Element #2226

Merged
merged 1 commit into from
Feb 1, 2024
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
8 changes: 6 additions & 2 deletions core/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,12 @@ where
tree: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, B, Theme, Renderer>> {
let mapper = &self.mapper;

self.widget
.overlay(tree, layout, renderer)
.overlay(tree, layout, renderer, translation)
.map(move |overlay| overlay.map(mapper))
}
}
Expand Down Expand Up @@ -596,7 +597,10 @@ where
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
self.element.widget.overlay(state, layout, renderer)
self.element
.widget
.overlay(state, layout, renderer, translation)
}
}
13 changes: 5 additions & 8 deletions core/src/overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ where
/// user interface.
///
/// [`Node`]: layout::Node
fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node;
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node;

/// Draws the [`Overlay`] using the associated `Renderer`.
fn draw(
Expand Down Expand Up @@ -120,6 +114,7 @@ pub fn from_children<'a, Message, Theme, Renderer>(
tree: &'a mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<Element<'a, Message, Theme, Renderer>>
where
Renderer: crate::Renderer,
Expand All @@ -129,7 +124,9 @@ where
.zip(&mut tree.children)
.zip(layout.children())
.filter_map(|((child, state), layout)| {
child.as_widget_mut().overlay(state, layout, renderer)
child
.as_widget_mut()
.overlay(state, layout, renderer, translation)
})
.collect::<Vec<_>>();

Expand Down
41 changes: 4 additions & 37 deletions core/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use std::any::Any;
/// A generic [`Overlay`].
#[allow(missing_debug_implementations)]
pub struct Element<'a, Message, Theme, Renderer> {
position: Point,
translation: Vector,
overlay: Box<dyn Overlay<Message, Theme, Renderer> + 'a>,
}

Expand All @@ -23,26 +21,9 @@ where
{
/// Creates a new [`Element`] containing the given [`Overlay`].
pub fn new(
position: Point,
overlay: Box<dyn Overlay<Message, Theme, Renderer> + 'a>,
) -> Self {
Self {
position,
overlay,
translation: Vector::ZERO,
}
}

/// Returns the position of the [`Element`].
pub fn position(&self) -> Point {
self.position
}

/// Translates the [`Element`].
pub fn translate(mut self, translation: Vector) -> Self {
self.position = self.position + translation;
self.translation = self.translation + translation;
self
Self { overlay }
}

/// Applies a transformation to the produced message of the [`Element`].
Expand All @@ -57,8 +38,6 @@ where
B: 'a,
{
Element {
position: self.position,
translation: self.translation,
overlay: Box::new(Map::new(self.overlay, f)),
}
}
Expand All @@ -68,14 +47,8 @@ where
&mut self,
renderer: &Renderer,
bounds: Size,
translation: Vector,
) -> layout::Node {
self.overlay.layout(
renderer,
bounds,
self.position + translation,
self.translation + translation,
)
self.overlay.layout(renderer, bounds)
}

/// Processes a runtime [`Event`].
Expand Down Expand Up @@ -165,14 +138,8 @@ impl<'a, A, B, Theme, Renderer> Overlay<B, Theme, Renderer>
where
Renderer: crate::Renderer,
{
fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
translation: Vector,
) -> layout::Node {
self.content.layout(renderer, bounds, position, translation)
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {
self.content.layout(renderer, bounds)
}

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

/// An [`Overlay`] container that displays multiple overlay [`overlay::Element`]
/// children.
Expand Down Expand Up @@ -44,7 +42,7 @@ where

/// Turns the [`Group`] into an overlay [`overlay::Element`].
pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> {
overlay::Element::new(Point::ORIGIN, Box::new(self))
overlay::Element::new(Box::new(self))
}
}

Expand All @@ -65,18 +63,12 @@ impl<'a, Message, Theme, Renderer> Overlay<Message, Theme, Renderer>
where
Renderer: crate::Renderer,
{
fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
_position: Point,
translation: Vector,
) -> layout::Node {
fn layout(&mut self, renderer: &Renderer, bounds: Size) -> layout::Node {
layout::Node::with_children(
bounds,
self.children
.iter_mut()
.map(|child| child.layout(renderer, bounds, translation))
.map(|child| child.layout(renderer, bounds))
.collect(),
)
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::layout::{self, Layout};
use crate::mouse;
use crate::overlay;
use crate::renderer;
use crate::{Clipboard, Length, Rectangle, Shell, Size};
use crate::{Clipboard, Length, Rectangle, Shell, Size, Vector};

/// A component that displays information and allows interaction.
///
Expand Down Expand Up @@ -146,6 +146,7 @@ where
_state: &'a mut Tree,
_layout: Layout<'_>,
_renderer: &Renderer,
_translation: Vector,
) -> Option<overlay::Element<'a, Message, Theme, Renderer>> {
None
}
Expand Down
23 changes: 11 additions & 12 deletions examples/modal/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,15 @@ mod modal {
state: &'b mut widget::Tree,
layout: Layout<'_>,
_renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
Some(overlay::Element::new(
layout.position(),
Box::new(Overlay {
content: &mut self.modal,
tree: &mut state.children[1],
size: layout.bounds().size(),
on_blur: self.on_blur.clone(),
}),
))
Some(overlay::Element::new(Box::new(Overlay {
position: layout.position() + translation,
content: &mut self.modal,
tree: &mut state.children[1],
size: layout.bounds().size(),
on_blur: self.on_blur.clone(),
})))
}

fn mouse_interaction(
Expand Down Expand Up @@ -392,6 +391,7 @@ mod modal {
}

struct Overlay<'a, 'b, Message, Theme, Renderer> {
position: Point,
content: &'b mut Element<'a, Message, Theme, Renderer>,
tree: &'b mut widget::Tree,
size: Size,
Expand All @@ -409,8 +409,6 @@ mod modal {
&mut self,
renderer: &Renderer,
_bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, self.size)
.width(Length::Fill)
Expand All @@ -423,7 +421,7 @@ mod modal {
.align(Alignment::Center, Alignment::Center, limits.max());

layout::Node::with_children(self.size, vec![child])
.move_to(position)
.move_to(self.position)
}

fn on_event(
Expand Down Expand Up @@ -530,6 +528,7 @@ mod modal {
self.tree,
layout.children().next().unwrap(),
renderer,
Vector::ZERO,
)
}
}
Expand Down
25 changes: 12 additions & 13 deletions examples/toast/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ mod toast {
state: &'b mut Tree,
layout: Layout<'_>,
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
let instants = state.state.downcast_mut::<Vec<Option<Instant>>>();

Expand All @@ -465,19 +466,18 @@ mod toast {
&mut content_state[0],
layout,
renderer,
translation,
);

let toasts = (!self.toasts.is_empty()).then(|| {
overlay::Element::new(
layout.bounds().position(),
Box::new(Overlay {
toasts: &mut self.toasts,
state: toasts_state,
instants,
on_close: &self.on_close,
timeout_secs: self.timeout_secs,
}),
)
overlay::Element::new(Box::new(Overlay {
position: layout.bounds().position() + translation,
toasts: &mut self.toasts,
state: toasts_state,
instants,
on_close: &self.on_close,
timeout_secs: self.timeout_secs,
}))
});
let overlays =
content.into_iter().chain(toasts).collect::<Vec<_>>();
Expand All @@ -488,6 +488,7 @@ mod toast {
}

struct Overlay<'a, 'b, Message> {
position: Point,
toasts: &'b mut [Element<'a, Message>],
state: &'b mut [Tree],
instants: &'b mut [Option<Instant>],
Expand All @@ -502,8 +503,6 @@ mod toast {
&mut self,
renderer: &Renderer,
bounds: Size,
position: Point,
_translation: Vector,
) -> layout::Node {
let limits = layout::Limits::new(Size::ZERO, bounds);

Expand All @@ -519,7 +518,7 @@ mod toast {
self.toasts,
self.state,
)
.translate(Vector::new(position.x, position.y))
.translate(Vector::new(self.position.x, self.position.y))
}

fn on_event(
Expand Down
21 changes: 4 additions & 17 deletions runtime/src/overlay/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ 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, Vector,
};
use crate::core::{Clipboard, Event, Layout, Point, Rectangle, Shell, Size};

/// An overlay container that displays nested overlays
#[allow(missing_debug_implementations)]
Expand All @@ -25,48 +23,37 @@ where
Self { overlay: element }
}

/// Returns the position of the [`Nested`] overlay.
pub fn position(&self) -> Point {
self.overlay.position()
}

/// Returns the layout [`Node`] of the [`Nested`] overlay.
///
/// [`Node`]: layout::Node
pub fn layout(
&mut self,
renderer: &Renderer,
bounds: Size,
_position: Point,
translation: Vector,
) -> layout::Node {
fn recurse<Message, Theme, Renderer>(
element: &mut overlay::Element<'_, Message, Theme, Renderer>,
renderer: &Renderer,
bounds: Size,
translation: Vector,
) -> layout::Node
where
Renderer: renderer::Renderer,
{
let node = element.layout(renderer, bounds, translation);
let node = element.layout(renderer, bounds);

if let Some(mut nested) =
element.overlay(Layout::new(&node), renderer)
{
layout::Node::with_children(
node.size(),
vec![
node,
recurse(&mut nested, renderer, bounds, translation),
],
vec![node, recurse(&mut nested, renderer, bounds)],
)
} else {
layout::Node::with_children(node.size(), vec![node])
}
}

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

/// Draws the [`Nested`] overlay using the associated `Renderer`.
Expand Down
Loading
Loading