mirror of
https://github.com/danbulant/cushy
synced 2026-07-07 20:20:46 +00:00
Fixing gutter calculations + scroll canvas sizing
This commit is contained in:
parent
bd37279282
commit
a826b91173
3 changed files with 54 additions and 25 deletions
|
|
@ -172,12 +172,12 @@ impl Widget for Scroll {
|
||||||
Size::new(available_space.width.max(), available_space.height.max()).into_signed();
|
Size::new(available_space.width.max(), available_space.height.max()).into_signed();
|
||||||
let max_extents = Size::new(
|
let max_extents = Size::new(
|
||||||
if self.enabled.x {
|
if self.enabled.x {
|
||||||
ConstraintLimit::SizeToFit((control_size.width).into_unsigned())
|
ConstraintLimit::SizeToFit(UPx::MAX)
|
||||||
} else {
|
} else {
|
||||||
available_space.width
|
available_space.width
|
||||||
},
|
},
|
||||||
if self.enabled.y {
|
if self.enabled.y {
|
||||||
ConstraintLimit::SizeToFit((control_size.height).into_unsigned())
|
ConstraintLimit::SizeToFit(UPx::MAX)
|
||||||
} else {
|
} else {
|
||||||
available_space.height
|
available_space.height
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -420,22 +420,28 @@ impl Layout {
|
||||||
|
|
||||||
// Measure the children that fit their content
|
// Measure the children that fit their content
|
||||||
self.other = UPx::ZERO;
|
self.other = UPx::ZERO;
|
||||||
for (fit_index, &id) in self.fit_to_content.iter().enumerate() {
|
let mut requires_gutter = false;
|
||||||
|
for &id in &self.fit_to_content {
|
||||||
let index = self.children.index_of_id(id).expect("child not found");
|
let index = self.children.index_of_id(id).expect("child not found");
|
||||||
|
|
||||||
let (measured, other) = self.orientation.split_size(measure(
|
let (measured, other) = self.orientation.split_size(measure(
|
||||||
index,
|
index,
|
||||||
self.orientation.make_size(
|
self.orientation.make_size(
|
||||||
ConstraintLimit::SizeToFit(available_space),
|
ConstraintLimit::SizeToFit(remaining.saturating_sub(if requires_gutter {
|
||||||
|
gutter
|
||||||
|
} else {
|
||||||
|
UPx::ZERO
|
||||||
|
})),
|
||||||
other_constraint,
|
other_constraint,
|
||||||
),
|
),
|
||||||
!needs_final_layout,
|
!needs_final_layout,
|
||||||
));
|
));
|
||||||
self.layouts[index].size = measured;
|
self.layouts[index].size = measured;
|
||||||
if measured > 0 {
|
if measured > 0 {
|
||||||
if fit_index < self.fit_to_content.len() - 1
|
if requires_gutter {
|
||||||
|| self.fit_to_content.len() != self.children.len()
|
|
||||||
{
|
|
||||||
remaining = remaining.saturating_sub(gutter);
|
remaining = remaining.saturating_sub(gutter);
|
||||||
|
} else {
|
||||||
|
requires_gutter = true;
|
||||||
}
|
}
|
||||||
self.other = self.other.max(other);
|
self.other = self.other.max(other);
|
||||||
}
|
}
|
||||||
|
|
@ -458,8 +464,14 @@ impl Layout {
|
||||||
|
|
||||||
// Measure the weighted children within the remaining space
|
// Measure the weighted children within the remaining space
|
||||||
if self.total_weights > 0 {
|
if self.total_weights > 0 {
|
||||||
let space_per_weight = (remaining / self.total_weights).floor();
|
let mut needed_gutters = u32::try_from(self.fractional.len()).unwrap_or(u32::MAX);
|
||||||
remaining -= space_per_weight * self.total_weights;
|
if !requires_gutter {
|
||||||
|
needed_gutters -= 1;
|
||||||
|
}
|
||||||
|
let gutters = gutter * needed_gutters;
|
||||||
|
let space_per_weight =
|
||||||
|
((remaining.saturating_sub(gutters)) / self.total_weights).floor();
|
||||||
|
remaining = remaining.saturating_sub(space_per_weight * self.total_weights + gutters);
|
||||||
for (fractional_index, &(id, weight)) in self.fractional.iter().enumerate() {
|
for (fractional_index, &(id, weight)) in self.fractional.iter().enumerate() {
|
||||||
let index = self.children.index_of_id(id).expect("child not found");
|
let index = self.children.index_of_id(id).expect("child not found");
|
||||||
let mut size = space_per_weight * u32::from(weight);
|
let mut size = space_per_weight * u32::from(weight);
|
||||||
|
|
@ -499,26 +511,43 @@ impl Layout {
|
||||||
ConstraintLimit::SizeToFit(clip_limit) => self.other.min(clip_limit),
|
ConstraintLimit::SizeToFit(clip_limit) => self.other.min(clip_limit),
|
||||||
};
|
};
|
||||||
|
|
||||||
// Finally, compute the offsets of all of the widgets.
|
let measured = self.update_offsets(needs_final_layout, gutter, scale, measure);
|
||||||
|
|
||||||
|
self.orientation.make_size(measured, self.other)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_offsets(
|
||||||
|
&mut self,
|
||||||
|
needs_final_layout: bool,
|
||||||
|
gutter: UPx,
|
||||||
|
scale: Fraction,
|
||||||
|
mut measure: impl FnMut(usize, Size<ConstraintLimit>, bool) -> Size<UPx>,
|
||||||
|
) -> UPx {
|
||||||
let mut offset = UPx::ZERO;
|
let mut offset = UPx::ZERO;
|
||||||
for index in 0..self.children.len() {
|
for index in 0..self.children.len() {
|
||||||
self.layouts[index].offset = offset;
|
let visible = self.layouts[index].size > 0;
|
||||||
if self.layouts[index].size > 0 {
|
|
||||||
offset += self.layouts[index].size + gutter;
|
if visible && offset > 0 {
|
||||||
|
offset += gutter;
|
||||||
}
|
}
|
||||||
if needs_final_layout {
|
|
||||||
self.orientation.split_size(measure(
|
self.layouts[index].offset = offset;
|
||||||
index,
|
|
||||||
self.orientation.make_size(
|
if visible {
|
||||||
ConstraintLimit::Fill(self.layouts[index].size.into_upx(scale)),
|
offset += self.layouts[index].size;
|
||||||
ConstraintLimit::Fill(self.other),
|
if needs_final_layout {
|
||||||
),
|
measure(
|
||||||
true,
|
index,
|
||||||
));
|
self.orientation.make_size(
|
||||||
|
ConstraintLimit::Fill(self.layouts[index].size.into_upx(scale)),
|
||||||
|
ConstraintLimit::Fill(self.other),
|
||||||
|
),
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
offset
|
||||||
self.orientation.make_size(offset, self.other)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -507,7 +507,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Eq, PartialEq)]
|
#[derive(Clone, Copy, Eq, PartialEq, Debug)]
|
||||||
enum RootMode {
|
enum RootMode {
|
||||||
Fit,
|
Fit,
|
||||||
Expand,
|
Expand,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue