Skip to content

Commit

Permalink
chore: move query args to struct
Browse files Browse the repository at this point in the history
  • Loading branch information
ten3roberts committed Mar 14, 2024
1 parent 836596d commit 8865ddb
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 141 deletions.
16 changes: 6 additions & 10 deletions violet-core/src/constraints.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use glam::{vec2, BVec2, Vec2};

use crate::layout::{Direction, LayoutLimits, SizeResolver, SizingHints};
use crate::layout::{Direction, QueryArgs, SizeResolver, SizingHints};

pub struct FixedAreaConstraint {
pub area: f32,
Expand All @@ -24,16 +24,12 @@ impl SizeResolver for FixedAreaConstraint {
// }
// }

fn query(
&mut self,
_: &flax::EntityRef,
_content_area: Vec2,
limits: LayoutLimits,
squeeze: Direction,
) -> (Vec2, Vec2, SizingHints) {
let size = (limits.max_size / self.unit_size).floor().max(Vec2::ONE);
fn query(&mut self, _: &flax::EntityRef, args: QueryArgs) -> (Vec2, Vec2, SizingHints) {
let size = (args.limits.max_size / self.unit_size)
.floor()
.max(Vec2::ONE);

let min = match squeeze {
let min = match args.direction {
Direction::Horizontal => vec2((self.area / size.y).ceil(), size.y),
Direction::Vertical => vec2(size.x, (self.area / size.x).ceil()),
};
Expand Down
6 changes: 3 additions & 3 deletions violet-core/src/layout/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl LayoutCache {
pub(crate) fn insert_query_row(&mut self, value: CachedValue<Row>) {
self.query_row = Some(value);
if let Some(f) = self.on_invalidated.as_ref() {
f(LayoutUpdate::SizeQueryUpdate)
// f(LayoutUpdate::SizeQueryUpdate)
}
}

Expand All @@ -83,8 +83,8 @@ impl LayoutCache {
self.layout.as_ref()
}

pub fn query(&self) -> &[Option<CachedValue<Sizing>>; 2] {
&self.query
pub(crate) fn get_query(&self, direction: Direction) -> Option<&CachedValue<Sizing>> {
self.query[direction as usize].as_ref()
}
}

Expand Down
96 changes: 64 additions & 32 deletions violet-core/src/layout/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use crate::{
};

use super::{
cache::LayoutCache, resolve_pos, update_subtree, Block, Direction, LayoutLimits, Sizing,
cache::LayoutCache, resolve_pos, update_subtree, Block, Direction, LayoutLimits, QueryArgs,
Sizing,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -193,7 +194,16 @@ impl FlowLayout {

// Query the minimum and preferred size of this flow layout, optimizing for minimum size in
// the direction of this axis.
let row = self.query_row(world, cache, children, content_area, limits);
let row = self.query_row(
world,
cache,
children,
QueryArgs {
limits,
content_area: content_area.size(),
direction: self.direction,
},
);

// tracing::info!(?row.margin, "row margins to be contained");
self.distribute_children(world, entity, &row, content_area, limits, preferred_size)
Expand Down Expand Up @@ -405,9 +415,7 @@ impl FlowLayout {
&self,
world: &World,
row: &Row,
content_area: Rect,
limits: LayoutLimits,
direction: Direction,
args: QueryArgs,
preferred_size: Vec2,
) -> Sizing {
puffin::profile_function!();
Expand Down Expand Up @@ -441,7 +449,7 @@ impl FlowLayout {

// Clipped maximum that we remap to
let target_inner_size = distribute_size
.min(limits.max_size.dot(axis) - minimum_inner_size)
.min(args.limits.max_size.dot(axis) - minimum_inner_size)
.max(0.0);

tracing::debug!(
Expand All @@ -452,12 +460,10 @@ impl FlowLayout {
"distribute"
);

let available_size = limits.max_size;
let available_size = args.limits.max_size;

let mut min_cursor =
MarginCursor::new(content_area.min, axis, cross_axis, self.contain_margins);
let mut cursor =
MarginCursor::new(content_area.min, axis, cross_axis, self.contain_margins);
let mut min_cursor = MarginCursor::new(Vec2::ZERO, axis, cross_axis, self.contain_margins);
let mut cursor = MarginCursor::new(Vec2::ZERO, axis, cross_axis, self.contain_margins);

let mut sum = 0.0;

Expand Down Expand Up @@ -537,7 +543,12 @@ impl FlowLayout {

// NOTE: optimize for the minimum size in the query direction, not the
// direction of the flow
let sizing = query_size(world, &entity, content_area.size(), child_limits, direction);
let sizing = query_size(world, &entity, QueryArgs {
limits: child_limits,
content_area: args.content_area,
// Use the query direction, not the flow direction
direction: args.direction,
});

hints = hints.combine(sizing.hints);

Expand All @@ -557,8 +568,8 @@ impl FlowLayout {
.to_edges(cursor.main_margin, cursor.cross_margin, self.reverse);

Sizing {
min: min_rect.max_size(limits.min_size),
preferred: rect.max_size(limits.min_size),
min: min_rect.max_size(args.limits.min_size),
preferred: rect.max_size(args.limits.min_size),
margin,
hints,
}
Expand All @@ -569,12 +580,11 @@ impl FlowLayout {
world: &World,
cache: &mut LayoutCache,
children: &[Entity],
content_area: Rect,
limits: LayoutLimits,
args: QueryArgs,
) -> Row {
puffin::profile_function!();
if let Some(value) = cache.query_row.as_ref() {
if validate_cached_row(value, limits, content_area.size()) {
if validate_cached_row(value, args.limits, args.content_area) {
return value.value.clone();
}
}
Expand All @@ -601,21 +611,34 @@ impl FlowLayout {
let entity = world.entity(child).expect("Invalid child");

let child_margin = if self.contain_margins {
query_size(world, &entity, content_area.size(), limits, self.direction).margin
query_size(
world,
&entity,
QueryArgs {
limits: LayoutLimits {
min_size: Vec2::ZERO,
max_size: args.limits.max_size,
},
content_area: args.content_area,
direction: self.direction,
},
)
.margin
} else {
Edges::ZERO
};

let sizing = query_size(
world,
&entity,
content_area.size(),
LayoutLimits {
min_size: Vec2::ZERO,
// max_size: limits.max_size,
max_size: limits.max_size - child_margin.size(),
QueryArgs {
limits: LayoutLimits {
min_size: Vec2::ZERO,
max_size: args.limits.max_size - child_margin.size(),
},
content_area: args.content_area,
direction: self.direction,
},
self.direction,
);

hints = hints.combine(sizing.hints);
Expand Down Expand Up @@ -649,7 +672,11 @@ impl FlowLayout {
hints,
};

cache.insert_query_row(CachedValue::new(limits, content_area.size(), row.clone()));
cache.insert_query_row(CachedValue::new(
args.limits,
args.content_area,
row.clone(),
));
row
}

Expand All @@ -658,12 +685,10 @@ impl FlowLayout {
world: &World,
cache: &mut LayoutCache,
children: &[Entity],
content_area: Rect,
limits: LayoutLimits,
direction: Direction,
args: QueryArgs,
preferred_size: Vec2,
) -> Sizing {
puffin::profile_function!(format!("{direction:?}"));
puffin::profile_function!(format!("{args:?}"));

// We want to query the min/preferred size in the direction orthogonal to the flows
// layout
Expand Down Expand Up @@ -695,10 +720,17 @@ impl FlowLayout {
// exist a better solution where some widgets may get slightly more space and still
// fall within the max height. If anybody comes across a non-iterative solution for
// this, be sure to let me know :)
let row = self.query_row(world, cache, children, content_area, limits);
let row = self.query_row(
world,
cache,
children,
QueryArgs {
direction: self.direction,
..args
},
);

let sizing =
self.distribute_query(world, &row, content_area, limits, direction, preferred_size);
let sizing = self.distribute_query(world, &row, args, preferred_size);
tracing::debug!(?self.direction, ?sizing, "query");
sizing
}
Expand Down
Loading

0 comments on commit 8865ddb

Please sign in to comment.