Skip to content

Commit

Permalink
Orientation::place returns bounds, add compute_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 10, 2023
1 parent 5d58d01 commit bcd638a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 30 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

* `LinearLayout` now implements `ViewGroup`
* `LinearLayout::inner{_mut}`
* `Orientation::compute_offset`

## Changed

* `Orientation::place` now returns the bounds of the placed View.

0.3.2 (2023-08-23)
==================
Expand Down
4 changes: 2 additions & 2 deletions src/layout/linear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ where
// arrange
let mut bounds = Rectangle::new(self.position, size);
for i in 0..view_count {
self.direction
bounds = self
.direction
.place(self.views.at_mut(i), size, bounds, i, view_count);
bounds = self.views.at(i).bounds();
}

self
Expand Down
80 changes: 52 additions & 28 deletions src/layout/linear/orientation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,30 @@ pub trait Orientation: Copy + Clone {
/// Create a `Size` from primary and secondary size values
fn create_size(primary: u32, secondary: u32) -> Size;

/// Computes translation for the next view.
fn compute_offset(
&self,
bounds: Rectangle,
size: Size,
previous: Rectangle,
n: usize,
count: usize,
) -> Point;

/// Place view
fn place(&self, view: &mut dyn View, size: Size, previous: Rectangle, n: usize, count: usize);
#[inline]
fn place(
&self,
view: &mut dyn View,
size: Size,
previous: Rectangle,
n: usize,
count: usize,
) -> Rectangle {
let offset = self.compute_offset(view.bounds(), size, previous, n, count);
view.translate_impl(offset);
view.bounds()
}
}

/// Horizontal layout direction
Expand Down Expand Up @@ -95,37 +117,35 @@ where
}

#[inline]
fn place(&self, view: &mut dyn View, size: Size, previous: Rectangle, n: usize, count: usize) {
fn compute_offset(
&self,
bounds: Rectangle,
size: Size,
previous: Rectangle,
n: usize,
count: usize,
) -> Point {
let (primary_size, _) = Self::destructure_size(size);
let view_bounds = view.bounds();

let translation = if n == 0 {
if n == 0 {
Point::new(
self.spacing.align(
horizontal::Left,
view_bounds,
previous,
n,
count,
primary_size,
),
Secondary::First::default().align(view_bounds, previous),
self.spacing
.align(horizontal::Left, bounds, previous, n, count, primary_size),
Secondary::First::default().align(bounds, previous),
)
} else {
Point::new(
self.spacing.align(
horizontal::LeftToRight,
view_bounds,
bounds,
previous,
n,
count,
primary_size,
),
Secondary::default().align(view_bounds, previous),
Secondary::default().align(bounds, previous),
)
};

view.translate_impl(translation);
}
}
}

Expand Down Expand Up @@ -198,30 +218,34 @@ where
}

#[inline]
fn place(&self, view: &mut dyn View, size: Size, previous: Rectangle, n: usize, count: usize) {
fn compute_offset(
&self,
bounds: Rectangle,
size: Size,
previous: Rectangle,
n: usize,
count: usize,
) -> Point {
let (primary_size, _) = Self::destructure_size(size);
let view_bounds = view.bounds();

let translation = if n == 0 {
if n == 0 {
Point::new(
Secondary::First::default().align(view_bounds, previous),
Secondary::First::default().align(bounds, previous),
self.spacing
.align(vertical::Top, view_bounds, previous, n, count, primary_size),
.align(vertical::Top, bounds, previous, n, count, primary_size),
)
} else {
Point::new(
Secondary::default().align(view_bounds, previous),
Secondary::default().align(bounds, previous),
self.spacing.align(
vertical::TopToBottom,
view_bounds,
bounds,
previous,
n,
count,
primary_size,
),
)
};

view.translate_impl(translation);
}
}
}

0 comments on commit bcd638a

Please sign in to comment.