mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 20:14:31 +01:00
craft-ui: a levelled unit stands on its row's line
A levelled unit's content ran in a room of its own with the row's line cleared, so a child of it that fills its height took its own measured height instead of the line the row settled on. The unit keeps the row's line; only the probes that measure each level run off it.
This commit is contained in:
1 parent
463c634dd6
commit
7a430a8e4f
3 files changed
+64
-5
No files matched your search
@@ -398,19 +398,30 @@ pub fn plan_levels(units: &[Vec<f32>], room: f32) -> Vec<usize> {
|
||||
/// continuous units leave at their least), and the continuous units narrow by their own rule in what is left. The level
|
||||
/// is kept with the row's measuring run, so a pass without one (nothing the row measures changed) shows each unit once,
|
||||
/// at its kept level. Outside a shrinking row the unit shows at level 0.
|
||||
///
|
||||
/// The unit shows on the row's line: a child of it that fills its height takes the line's height, as a direct child of
|
||||
/// the row does (`fill_size`).
|
||||
pub fn levelled_unit(ui: &mut Ui, key: Id, levels: usize, mut show: impl FnMut(&mut Ui, usize)) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let shrink = Env::of(ui).shrink;
|
||||
// The unit's content is no unit of the row: it runs in a room of its own.
|
||||
let own_room = Env::of(ui).derive_local(|scoped| scoped.start_room());
|
||||
// The unit's content is no unit of the row: it runs in a room of its own, on the row's line.
|
||||
let line = Env::of(ui).row_line;
|
||||
let own_room = Env::of(ui).derive_local(|scoped| {
|
||||
scoped.start_room();
|
||||
scoped.row_line = line;
|
||||
});
|
||||
match shrink {
|
||||
Some(Shrink::Count(row)) => {
|
||||
let rtl = Env::of(ui).direction.is_rtl();
|
||||
let mut widths = Vec::with_capacity(levels);
|
||||
let mut widest = Vec2::ZERO;
|
||||
// Each level measures at what it takes in no room: unbounded along x, in a region as wide as the row's.
|
||||
// An infinite egui region would put right-to-left content's start edge at infinity.
|
||||
let no_room = own_room.derive_local(|scoped| scoped.unbounded.0 = true);
|
||||
// Each level measures at what it takes in no room: unbounded along x, in a region as wide as the row's,
|
||||
// and off the row's line, which a probe is not placed on. An infinite egui region would put
|
||||
// right-to-left content's start edge at infinity.
|
||||
let no_room = own_room.derive_local(|scoped| {
|
||||
scoped.unbounded.0 = true;
|
||||
scoped.row_line = None;
|
||||
});
|
||||
let width = ui.max_rect().width();
|
||||
for level in 0..levels.max(1) {
|
||||
let installed = Env::install_for(ui, no_room.clone());
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
//! Filling the line of a row that takes its height from its content: a levelled unit stands on the row's line.
|
||||
|
||||
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::layout::{BodyLayout, levelled_unit};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use craft_ui_test_kit::contract::{Panel, Probe, Seen};
|
||||
use egui::{Id, Rect, Vec2};
|
||||
use std::cell::RefCell;
|
||||
|
||||
const SNAP: f32 = 1.0;
|
||||
|
||||
/// The height of the tall member that sets each row's line.
|
||||
const TALL: f32 = 100.0;
|
||||
|
||||
/// The rect of the row and of the probe that fills, after the first frame and once settled, for a row of a tall probe
|
||||
/// beside `filler`, which shows the filling probe into `seen`.
|
||||
fn line_and_fill(filler: impl Fn(&mut egui::Ui, &RefCell<Seen>)) -> [(Rect, Rect); 2] {
|
||||
let (tall, fill, row) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()), RefCell::new(Seen::default()));
|
||||
let mut frames = Frames::new();
|
||||
let mut show = |ui: &mut egui::Ui| {
|
||||
Panel::new().layout(BodyLayout::row()).seen(&row).id_salt("row").show(ui, |ui, _| {
|
||||
Probe::new(&tall).size(Vec2::new(40.0, TALL)).id_salt("tall").show(ui);
|
||||
filler(ui, &fill);
|
||||
});
|
||||
};
|
||||
frames.frame(Vec::new(), &mut show);
|
||||
let first = (row.borrow().rect, fill.borrow().rect);
|
||||
frames.idle(4, &mut show);
|
||||
let settled = (row.borrow().rect, fill.borrow().rect);
|
||||
[first, settled]
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_levelled_unit_stands_on_its_rows_line() {
|
||||
let seen = line_and_fill(|ui, fill| {
|
||||
levelled_unit(ui, Id::new("unit"), 2, |ui, _| {
|
||||
Probe::new(fill).sizing(Sizing::Fill).id_salt("fill").show(ui);
|
||||
});
|
||||
});
|
||||
for ((row, fill), when) in seen.into_iter().zip(["in the first frame", "once settled"]) {
|
||||
assert!((row.height() - TALL).abs() <= SNAP, "{when}: the row {row:?} is as tall as its tall member");
|
||||
assert!(
|
||||
(fill.top() - row.top()).abs() <= SNAP && (fill.bottom() - row.bottom()).abs() <= SNAP,
|
||||
"{when}: the unit's filling child {fill:?} spans the row's line {row:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub(crate) mod body;
|
||||
pub(crate) mod fill;
|
||||
pub(crate) mod line_fill;
|
||||
pub(crate) mod memory;
|
||||
pub(crate) mod overlay_bar;
|
||||
Reference in new issue
Block a user