Skip to content

Commit

Permalink
Fix layout translation in overlay::Group
Browse files Browse the repository at this point in the history
This bug produced improper positioning of overlays of elements inside a
`Scrollable`.
  • Loading branch information
hecrj committed Jan 30, 2023
1 parent a28bc3e commit bfb1c0c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions core/src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ impl<T> Vector<T> {
}
}

impl Vector {
/// The zero [`Vector`].
pub const ZERO: Self = Self::new(0.0, 0.0);
}

impl<T> std::ops::Add for Vector<T>
where
T: std::ops::Add<Output = T>,
Expand Down
10 changes: 8 additions & 2 deletions native/src/overlay/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ where
}

/// Computes the layout of the [`Element`] in the given bounds.
pub fn layout(&self, renderer: &Renderer, bounds: Size) -> layout::Node {
self.overlay.layout(renderer, bounds, self.position)
pub fn layout(
&self,
renderer: &Renderer,
bounds: Size,
translation: Vector,
) -> layout::Node {
self.overlay
.layout(renderer, bounds, self.position + translation)
}

/// Processes a runtime [`Event`].
Expand Down
6 changes: 4 additions & 2 deletions native/src/overlay/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ where
&self,
renderer: &Renderer,
bounds: Size,
_position: Point,
position: Point,
) -> layout::Node {
let translation = position - Point::ORIGIN;

layout::Node::with_children(
bounds,
self.children
.iter()
.map(|child| child.layout(renderer, bounds))
.map(|child| child.layout(renderer, bounds, translation))
.collect(),
)
}
Expand Down
18 changes: 10 additions & 8 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use crate::mouse;
use crate::renderer;
use crate::widget;
use crate::window;
use crate::{Clipboard, Element, Layout, Point, Rectangle, Shell, Size};
use crate::{
Clipboard, Element, Layout, Point, Rectangle, Shell, Size, Vector,
};

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

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

for event in events.iter().cloned() {
Expand Down Expand Up @@ -252,7 +254,7 @@ where
overlay = manual_overlay.as_mut().unwrap();

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

Expand Down Expand Up @@ -434,10 +436,9 @@ where
.as_widget_mut()
.overlay(&mut self.state, Layout::new(&self.base), renderer)
{
let overlay_layout = self
.overlay
.take()
.unwrap_or_else(|| overlay.layout(renderer, self.bounds));
let overlay_layout = self.overlay.take().unwrap_or_else(|| {
overlay.layout(renderer, self.bounds, Vector::ZERO)
});

let new_cursor_position =
if overlay_layout.bounds().contains(cursor_position) {
Expand Down Expand Up @@ -538,7 +539,8 @@ where
renderer,
) {
if self.overlay.is_none() {
self.overlay = Some(overlay.layout(renderer, self.bounds));
self.overlay =
Some(overlay.layout(renderer, self.bounds, Vector::ZERO));
}

overlay.operate(
Expand Down

0 comments on commit bfb1c0c

Please sign in to comment.