Scrolled Stack bodies share their view: the children that fill y take what the view leaves, at least their content, and the body scrolls past it; the Color panel body scrolls in every mode

This commit is contained in:
aiongg
2026-11-13 19:26:50 +00:00
parent fdeeec6d41
commit 05b1103621
9 changed files with 121 additions and 39 deletions
+2 -7
View File
@@ -542,15 +542,10 @@ impl Body<'_> {
/// The cubes take the height the readout leaves them, as Photoshop's field fills its dock group. In the slider modes
/// the channel rows come first and the ramp under them takes the room they leave, at least a colour band thick, as
/// Photoshop's does; in a group shorter than both the ramp is cut at the bottom. The wheel scrolls where the group
/// is shorter than it.
/// Photoshop's does. In a group shorter than the content at its least the body scrolls.
fn show(mut self, ui: &mut Ui) -> Self {
let mode = self.now.mode;
let mut body = Stack::new().fill();
if mode == ColorPanelMode::ColorWheel {
body = body.scroll(Scroll::vertical());
}
body.show(ui, |ui, _| {
Stack::new().fill().scroll(Scroll::vertical()).show(ui, |ui, _| {
self.content(ui);
if mode.is_sliders() {
self.ramp(ui);
+8 -1
View File
@@ -131,7 +131,7 @@ fn the_cubes_take_the_room_the_readout_and_the_strip_leave() {
/// The slider modes show each channel on one line: its slider and its number field side by side, every slider starting
/// at one x past the channels' letters. The rows come first and stay in view in a short group; the ramp under them
/// takes the room they leave.
/// takes the room they leave. In a group shorter than the rows and the band the body scrolls.
#[trial]
fn the_slider_modes_show_a_channel_on_one_line() {
let (rgb, cmyk, hsb) = ((ColorPanelMode::RgbSliders, 3), (ColorPanelMode::CmykSliders, 4), (ColorPanelMode::HsbSliders, 3));
@@ -160,6 +160,13 @@ fn the_slider_modes_show_a_channel_on_one_line() {
assert!(ramp.bottom() > room.y - 40.0, "{case}: the ramp takes the room the rows leave ({ramp:?})");
}
}
// Four rows and the band do not fit the short group: the body scrolls, and the K row comes into view.
let mut rig = PanelRig::in_room(ColorPanelMode::CmykSliders, short);
let k = |rig: &PanelRig| rig.fixture.by_role(Role::SPIN_BUTTON).last().expect("the K field").rect;
assert!(k(&rig).bottom() > short.y + 0.5, "CMYK in {short:?}: the K row starts below the view ({:?})", k(&rig));
rig.fixture.frame(vec![moved(egui::pos2(short.x / 2.0, short.y / 2.0))]);
rig.fixture.frame(vec![craft_ui::testing::pointer::wheel(-200.0)]).settle();
assert!(k(&rig).bottom() <= short.y + 0.5 && k(&rig).top() >= 0.0, "CMYK in {short:?}: the K row scrolls into view ({:?})", k(&rig));
}
const SWATCHES: [(&str, [f32; 3]); 6] = [
+28 -5
View File
@@ -346,9 +346,11 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
// A container placed at an explicit rect, or mounted on a layer or in a window, is no flow child of that body:
// it takes its own room, not a share.
let flows = matches!(at.at, ShowAt::Flow) && self.in_flow(comp);
// Along an axis the flow does not bound it fills nothing (`sized`): it measures its content there.
// Along an axis the flow does not bound it fills nothing (`sized`): it measures its content there. In a `Stack`
// body that scrolls along y it shares what the view shows.
let bounded = crate::layout::flow_room(at.ui);
let (fills_x, fills_y) = (self.given.0 && bounded.x.is_finite(), self.given.1 && bounded.y.is_finite());
let scrolled_y = !bounded.y.is_finite() && crate::layout::shares_y(at.ui) && crate::layout::view_room(at.ui, y).is_some();
let (fills_x, fills_y) = (self.given.0 && bounded.x.is_finite(), self.given.1 && (bounded.y.is_finite() || scrolled_y));
if flows
&& at.ui.is_sizing_pass()
&& ((fills_x && crate::layout::counting_fills(at.ui, x)) || (fills_y && crate::layout::counting_fills(at.ui, y)))
@@ -358,12 +360,15 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
if let Some(share) = crate::layout::fill_share(at.ui, x).filter(|_| flows && self.given.0) {
self.room.x = share;
}
let mut scrolled_share = false;
if let Some(share) = crate::layout::fill_share(at.ui, y).filter(|_| flows && self.given.1) {
self.room.y = share;
scrolled_share = scrolled_y;
}
// A body that fills both axes its parent gives it takes the room, whatever its contents take: a parent's
// measuring run does not run them. It measures them in its own real run.
if at.ui.is_sizing_pass() && self.given == (true, true) && self.room.is_finite() {
// measuring run does not run them. It measures them in its own real run. A share of a scrolling view is the
// least of what it takes: its content may be taller.
if at.ui.is_sizing_pass() && self.given == (true, true) && self.room.is_finite() && !scrolled_share {
return self.room;
}
let pad = Vec2::splat(2.0 * pad_of(at.scx.tokens, props.pad));
@@ -390,6 +395,16 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
}
took
}
// A share of a scrolling view is laid out as given while the content fits it, so the children that fill
// share it; taller, the body takes its content height (the children that fill at their least) and the view
// scrolls.
None if scrolled_share => {
let fits = self.room.y + tolerance;
self.given.1 = false;
let (content, kept) = crate::contract::draft(&ctx, || self.measure_run(comp, at, cx, key.clone(), None), |took| took.y > fits);
self.given.1 = true;
if kept { content } else { self.measure_run(comp, at, cx, key, None) }
}
// A capped body takes its content height while that fits its room. Taller, the run is taken back and the body
// is laid out as given the room, at the footer and header this run measured, so the children that fill share
// it; its record of the content height lets later runs of the pass and kept transcripts lay it out the same.
@@ -470,13 +485,21 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
// content-sized layer offers its whole room before the footer is known) must not keep its remembered
// size once the room is less. Along an axis it is not given it takes its measured size. Along the main
// axis of the body it sits in it takes its share of what its siblings leave.
// In a `Stack` body that scrolls along y its share there is the least it takes: its content may be
// taller, and the view scrolls.
let axis = |room: f32, given: bool, along: craft_behavior::Axis, measured: f32| {
if !(room.is_finite() && given) {
let scrolled = along == craft_behavior::Axis::Y
&& !room.is_finite()
&& crate::layout::shares_y(ui)
&& crate::layout::view_room(ui, along).is_some();
if !((room.is_finite() || scrolled) && given) {
return measured;
}
match crate::layout::main_fill(ui, id, along) {
crate::layout::MainFill::Measuring => 0.0,
crate::layout::MainFill::Share(share) if scrolled => measured.max(share),
crate::layout::MainFill::Share(share) => share,
crate::layout::MainFill::Room if scrolled => measured,
crate::layout::MainFill::Room => room,
}
};
+5 -1
View File
@@ -268,7 +268,11 @@ impl<'b> BodyHooks<'b> {
Some(crate::layout::Shrink::Apply(row)) if !fills => crate::layout::shrink_cap(ui.ctx(), row, id).map(|cap| cap.width),
_ => None,
};
Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(room.y))
// A body that fills the height of a `Stack` body that scrolls along y takes a share of what the view
// shows (`layout::view_room`): it runs in that room, bounded like a body in a room that does not scroll.
let shares_view = !room.y.is_finite() && self.sizing(props) == Sizing::Fill && crate::layout::shares_y(ui);
let y = shares_view.then(|| crate::layout::view_room(ui, craft_behavior::Axis::Y)).flatten().unwrap_or(room.y);
Vec2::new(cap.map_or(room.x, |cap| room.x.min(cap)), props.room_height.unwrap_or(y))
}
},
}
+3 -2
View File
@@ -306,6 +306,7 @@ impl<C: Component> ShapeHooks<C> for LeafHooks {
// measuring run, where the child measured at its own size.
let env = Env::of(ui);
let (content, line) = (env.content, env.row_line.and_then(|line| line.height));
let scrolls = |along: craft_behavior::Axis| if along == craft_behavior::Axis::X { env.unbounded.0 } else { env.unbounded.1 };
let fill = |measured: f32, offered: f32, content: bool, along: craft_behavior::Axis| {
if content {
return match (along, line) {
@@ -318,8 +319,8 @@ impl<C: Component> ShapeHooks<C> for LeafHooks {
}
match crate::layout::main_fill(ui, id, along) {
crate::layout::MainFill::Measuring => 0.0,
// In a body that scrolls along x its share never cuts its content: what is past the view scrolls.
crate::layout::MainFill::Share(share) if along == craft_behavior::Axis::X && env.unbounded.0 => measured.max(share),
// In a body that scrolls along the axis its share never cuts its content: what is past the view scrolls.
crate::layout::MainFill::Share(share) if scrolls(along) => measured.max(share),
crate::layout::MainFill::Share(share) => share,
crate::layout::MainFill::Room => measured.max(offered),
}
+26 -17
View File
@@ -420,7 +420,7 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
}
let room = shrink_room(ui);
let order = if Env::of(ui).values.get::<InBar>().0 { ShrinkOrder::FromEnd } else { ShrinkOrder::Even };
let (drops, gap) = (view_room(ui).is_none(), ui.spacing().item_spacing.x);
let (drops, gap) = (view_room(ui, Axis::X).is_none(), ui.spacing().item_spacing.x);
// Records the width each unit gets, and returns whether one took another width in the first sweep.
let settle = |leaves: &[Reported], fixed: f32| {
let units: Vec<ShrinkUnit> = leaves.iter().map(|leaf| leaf.widths).collect();
@@ -480,18 +480,24 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
}
}
/// In a body that scrolls along x, what its view shows from the start of the rest of `ui`'s layout: the width a row
/// there shrinks to fit and a child that fills its width fills. `None` outside such a body.
fn view_room(ui: &Ui) -> Option<f32> {
/// In a body that scrolls along `axis`, what its view shows along it from the start of the rest of `ui`'s layout: along
/// x the width a row there shrinks to fit and a child that fills its width fills, along y the height the children that
/// fill a `Stack` there share. `None` outside such a body.
pub(crate) fn view_room(ui: &Ui, axis: Axis) -> Option<f32> {
let env = Env::of(ui);
env.lazy.filter(|view| env.unbounded.0 && view.axes.0).map(|view| (view.rect.width() - view.start_of(ui.available_rect_before_wrap()).x).max(0.0))
let scrolls = |view: &crate::runtime::lazy::View| if axis == Axis::X { env.unbounded.0 && view.axes.0 } else { env.unbounded.1 && view.axes.1 };
env.lazy.filter(scrolls).map(|view| {
let start = view.start_of(ui.available_rect_before_wrap());
let (length, at) = if axis == Axis::X { (view.rect.width(), start.x) } else { (view.rect.height(), start.y) };
(length - at).max(0.0)
})
}
/// The width the leaves of a row in `ui` shrink to fit: the row's flow room, except in a body that scrolls along x,
/// where it is what the view shows from the row's start. There the leaves narrow to their least before the body
/// scrolls, and when even their least does not fit they keep their natural widths and the body scrolls.
fn shrink_room(ui: &Ui) -> f32 {
view_room(ui).unwrap_or_else(|| flow_room(ui).x)
view_room(ui, Axis::X).unwrap_or_else(|| flow_room(ui).x)
}
/// Runs `body` as the content of the `Stack` (along y) or unwrapped `Row` (along x) body `id` in `ui`, the block
@@ -509,11 +515,9 @@ fn run_sharing<R>(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui::Layo
}
let key = sub(id, "share");
let along = |size: Vec2| if axis == Axis::Y { size.y } else { size.x };
// Along x in a body that scrolls along x, the children that fill share what the view shows.
let room_of = |ui: &Ui| match axis {
Axis::X => view_room(ui).unwrap_or_else(|| ui.available_rect_before_wrap().width()),
_ => along(ui.available_rect_before_wrap().size()),
};
// In a body that scrolls along the axis, the children that fill share what the view shows: shorter than the view,
// the content leaves them its rest; longer, they take their least and the body scrolls (`MainFill::Share`).
let room_of = |ui: &Ui| view_room(ui, axis).unwrap_or_else(|| along(ui.available_rect_before_wrap().size()));
let sweep = |ui: &mut Ui, share: Option<f32>, body: &mut dyn FnMut(&mut Ui) -> R| {
let installed = Env::install_for(
ui,
@@ -787,14 +791,19 @@ pub(crate) fn flow_fill_room(ui: &Ui) -> Vec2 {
cell.unwrap_or_else(|| open_room(ui))
}
/// `flow_fill_room` for a leaf that fills its width: along x in a body that scrolls along x it is what the view shows
/// from the leaf's start, so the leaf fills the view and what is past it scrolls.
/// `flow_fill_room` for a leaf that fills: along x in a body that scrolls along x it is what the view shows from the
/// leaf's start, so the leaf fills the view and what is past it scrolls; along y the same in a `Stack` body that
/// scrolls along y, whose children that fill share it.
pub(crate) fn leaf_fill_room(ui: &Ui) -> Vec2 {
let room = flow_fill_room(ui);
match view_room(ui).filter(|_| room.x.is_infinite()) {
Some(view) => Vec2::new(view, room.y),
None => room,
}
let x = view_room(ui, Axis::X).filter(|_| room.x.is_infinite());
let y = view_room(ui, Axis::Y).filter(|_| room.y.is_infinite() && shares_y(ui));
Vec2::new(x.unwrap_or(room.x), y.unwrap_or(room.y))
}
/// Whether the children in flow in `ui` are the children of a `Stack` body that shares its height among those that fill.
pub(crate) fn shares_y(ui: &Ui) -> bool {
Env::of(ui).fill_body.is_some_and(|(_, along)| along == Axis::Y)
}
/// Whether the next child shown in flow in `ui` goes in a `Content` column of the `Grid` body that encloses `ui`
+2 -1
View File
@@ -23,5 +23,6 @@ pub(crate) use crate::runtime::env::Shrink;
pub use body::place_in_flow;
pub(crate) use body::{
InFlow, MainFill, close_first_line, counting_fills, fill_share, flow_fill_room, flow_region, flow_room, in_grid_content_column, leaf_fill_room,
main_fill, open_first_lines, place_in_cell, report_least, report_shrink, row_alone_room, run_body, run_body_measured, shrink_cap, take_in_flow,
main_fill, open_first_lines, place_in_cell, report_least, report_shrink, row_alone_room, run_body, run_body_measured, shares_y, shrink_cap,
take_in_flow, view_room,
};
+2 -1
View File
@@ -148,7 +148,8 @@ pub struct Scoped {
/// page and end moves a component leaves untaken go to it.
pub(crate) scroll_owner: Option<ScrollOwner>,
/// The axes (horizontal, vertical) the innermost scrolling body scrolls on: the room along them is unbounded, so
/// a child that fills there takes its content size.
/// a child that fills there takes its content size, except the children that fill the main axis of a body that
/// shares it: they share what the view shows, at least their content size (`layout::view_room`).
pub(crate) unbounded: (bool, bool),
/// The view of the innermost scroll area, for the children that run only in view (`contract::Lazy`).
pub(crate) lazy: Option<crate::runtime::lazy::View>,
+45 -4
View File
@@ -1,11 +1,15 @@
//! A leaf's `Sizing::FillWidth`: the leaf takes the room its row leaves after the other members.
//! A leaf's `Sizing::FillWidth`: the leaf takes the room its row leaves after the other members. In a `Stack` body
//! that scrolls along y the children that fill its height share what the view leaves, at least their content.
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing};
use craft_ui::contract::{ContainerProps, Props as _, Scroll, Show as _, ShowContainer as _, Sizing};
use craft_ui::layout::BodyLayout;
use craft_ui::testing::{Frames, trial};
use craft_ui::testing::layout::Block;
use craft_ui::testing::{Fixture, Frames, pointer, trial};
use craft_ui_test_kit::body::Slab;
use craft_ui_test_kit::contract::{Panel, Probe, Seen};
use egui::Rect;
use egui::{Pos2, Rect, Vec2};
use std::cell::{Cell, RefCell};
use std::rc::Rc;
/// The region, the first probe's rect and the second's, in a full-width row whose first member has `sizing`.
fn row(sizing: Sizing) -> (Rect, Rect, Rect) {
@@ -31,3 +35,40 @@ fn a_fill_width_leaf_takes_the_room_its_row_leaves_and_a_content_leaf_does_not()
assert!((after_fill.width() - after_content.width()).abs() < 1.0, "the member after it keeps its size");
assert!(after_fill.min.x >= fill.max.x && after_fill.min.x > region.center().x, "the next member follows it at the row's end: {after_fill:?}");
}
/// The view, the fixed block, the leaf that fills and the container that fills (holding a probe at its least height),
/// in a scrolling `Stack` body that fills a 200 by 200 room and starts with a block `fixed` tall, scrolled to its end.
fn scrolled(fixed: f32) -> [Rect; 4] {
let seen = Rc::new(Cell::new([Rect::NOTHING; 4]));
let out = Rc::clone(&seen);
let mut fixture = Fixture::new(move |ui| {
let (leaf, inner) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()));
let body = Slab::new(ContainerProps::new().sizing(Sizing::Fill).scroll(Scroll::vertical())).id_salt("view").show(ui, |ui, _| {
let block = Block::new(Vec2::new(40.0, fixed)).show(ui).rect;
Probe::new(&leaf).sizing(Sizing::Fill).id_salt("leaf").show(ui);
let held = Panel::new().sizing(Sizing::Fill).id_salt("held").show(ui, |ui, _| {
Probe::new(&inner).id_salt("inner").show(ui);
});
(block, held.output.rect)
});
if let Some((block, held)) = body.inner {
out.set([body.output.rect, block, leaf.borrow().rect, held]);
}
});
fixture.room(Vec2::splat(200.0)).settle();
fixture.frame(vec![pointer::moved(Pos2::new(100.0, 100.0))]);
fixture.frame(vec![pointer::wheel(-10_000.0)]).settle();
seen.get()
}
#[trial]
fn a_scrolled_stack_gives_its_fill_children_what_the_view_leaves_and_scrolls_past_their_least() {
let [view, block, leaf, held] = scrolled(40.0);
let dump = format!("view {view:?}, block {block:?}, leaf {leaf:?}, held {held:?}");
assert!(leaf.height() > 40.0 && (leaf.height() - held.height()).abs() < 1.0, "shorter content: the fill children share the rest: {dump}");
assert!((held.bottom() - view.bottom()).abs() < 1.0, "shorter content: it reaches the view's end: {dump}");
let [view, _, leaf, held] = scrolled(400.0);
let dump = format!("view {view:?}, leaf {leaf:?}, held {held:?}");
assert!((leaf.height() - 20.0).abs() < 1.0 && (held.height() - 20.0).abs() < 1.0, "longer content: the fill children at their least: {dump}");
assert!((held.bottom() - view.bottom()).abs() < 1.0, "longer content: the view scrolls to the end of the last one: {dump}");
}