mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 09:08:11 +00:00
Merge pull request #3469 from dotCooCoo/fix/field-stacks-when-control-squeezed
ui-contract: a field puts a row of controls under its label when the row would fall below its least width
This commit is contained in:
@@ -1798,6 +1798,13 @@ fn measure_step(
|
||||
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(ctx, row, id, size.x),
|
||||
_ => {}
|
||||
}
|
||||
// A leaf that fills a shrinking row is no unit of it, but a row under a label counts its least among the controls'.
|
||||
let fills_row = t.fills() == Some(craft_behavior::Axis::X) || begun.common.sizing == Some(super::Sizing::Fill);
|
||||
if let (Some(least), Some(crate::layout::Shrink::Count(row))) = (min_took.filter(|_| fills_row), env.shrink)
|
||||
&& shrink.is_none()
|
||||
{
|
||||
crate::layout::report_fill_least(ctx, row, size.x, least.x);
|
||||
}
|
||||
// A body that fills the rest of a shrinking row needs at least its content: the row counts it at that width, so
|
||||
// the units narrow to leave it, and it takes what they leave.
|
||||
if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
use super::column_group::{ColumnGroup, Columns};
|
||||
use crate::contract::InBar;
|
||||
use crate::runtime::access::scope;
|
||||
use crate::runtime::egui::{alloc, child};
|
||||
use crate::runtime::env::{ColumnSweep, Env, RowLine, Shrink};
|
||||
use crate::runtime::layout_memory;
|
||||
@@ -546,6 +547,12 @@ pub(crate) struct ShrinkReports(IdMap<Vec<Reported>>);
|
||||
#[derive(Default)]
|
||||
pub(crate) struct LeastReports(IdMap<IdMap<f32>>);
|
||||
|
||||
/// How much the leaves that fill each `Row` body under a label need at their least beyond what they took in its first
|
||||
/// sweep (less where they took more), by row id: the row adds it to its units' least widths as the least its label
|
||||
/// leaves it.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct FillLeasts(IdMap<f32>);
|
||||
|
||||
/// How far the units of each `Row` body asked for one this pass reach past the row's room at their ideal widths, by
|
||||
/// row id: `None` for a row whose units fit.
|
||||
#[derive(Default)]
|
||||
@@ -727,6 +734,16 @@ fn level_of(ctx: &egui::Context, row: Id, unit: Id) -> (usize, Option<f32>) {
|
||||
.map_or((0, None), |(found, _)| (found.took.y.max(0.0) as usize, Some(found.took.x).filter(|width| width.is_finite() && *width > 0.0)))
|
||||
}
|
||||
|
||||
/// Reports the least width `least` of the leaf `leaf` that fills the row `row`, which took `took` in the row's first
|
||||
/// sweep, to a row that sums its controls' least widths for its label (`FillLeasts`).
|
||||
pub(crate) fn report_fill_least(ctx: &egui::Context, row: Id, took: f32, least: f32) {
|
||||
pass_state::with::<FillLeasts, _>(ctx, |reports| {
|
||||
if let Some(sum) = reports.0.get_mut(&row) {
|
||||
*sum += least - took;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/// Reports the least width of the body unit `body` of the row `row` that asks for it.
|
||||
pub(crate) fn report_least(ctx: &egui::Context, row: Id, body: Id, least: f32) {
|
||||
pass_state::with::<LeastReports, _>(ctx, |reports| {
|
||||
@@ -984,11 +1001,17 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
}
|
||||
again
|
||||
};
|
||||
let (inner, rect, mut leaves, fixed, probe, again) = crate::contract::keeping_ids(&ctx, || {
|
||||
// A row that is the first control under a label holds every control the label's placement must leave room for.
|
||||
let labelled = scope::label_awaiting_min(&ctx);
|
||||
let (inner, rect, mut leaves, fixed, fills, probe, again) = crate::contract::keeping_ids(&ctx, || {
|
||||
let mut first = laid_out_child(ui, layout);
|
||||
pass_state::with::<ShrinkReports, _>(&ctx, |reports| reports.0.insert(id, Vec::new()));
|
||||
if labelled.is_some() {
|
||||
pass_state::with::<FillLeasts, _>(&ctx, |reports| reports.0.insert(id, 0.0));
|
||||
}
|
||||
let inner = sweep(&mut first, Shrink::Count(id), &mut body);
|
||||
let leaves = pass_state::with::<ShrinkReports, _>(&ctx, |reports| reports.0.remove(&id)).unwrap_or_default();
|
||||
let fills = pass_state::with::<FillLeasts, _>(&ctx, |reports| reports.0.remove(&id)).unwrap_or_default();
|
||||
let rect = first.min_rect();
|
||||
let fixed = rect.width() - leaves.iter().map(|leaf| leaf.took).sum::<f32>();
|
||||
let shortfall = fixed + leaves.iter().map(|leaf| leaf.widths.ideal).sum::<f32>() - room;
|
||||
@@ -996,9 +1019,10 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
if short {
|
||||
pass_state::with::<RowShortfalls, _>(&ctx, |asked| asked.0.get_mut(&id).map(|slot| *slot = Some(shortfall)));
|
||||
}
|
||||
let probe = room.is_finite() && short && leaves.iter().any(|leaf| leaf.body);
|
||||
// A row under a label asks its bodies for their least whatever its room: the label's placement needs it.
|
||||
let probe = (labelled.is_some() || (room.is_finite() && short)) && leaves.iter().any(|leaf| leaf.body);
|
||||
let again = !probe && settle(&leaves, fixed);
|
||||
((inner, rect, leaves, fixed, probe, again), probe || again)
|
||||
((inner, rect, leaves, fixed, fills, probe, again), probe || again)
|
||||
});
|
||||
let again = again || probe;
|
||||
if probe {
|
||||
@@ -1020,6 +1044,9 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
}
|
||||
settle(&leaves, fixed);
|
||||
}
|
||||
if let Some(container) = labelled {
|
||||
scope::record_row_min(&ctx, container, fixed + fills + leaves.iter().map(|leaf| leaf.widths.least).sum::<f32>());
|
||||
}
|
||||
if again {
|
||||
child::block(ui, None, layout, |ui| sweep(ui, Shrink::Apply(id), &mut body)).0
|
||||
} else {
|
||||
|
||||
@@ -26,6 +26,7 @@ pub(crate) use body::run_body;
|
||||
pub(crate) use body::{
|
||||
BodyLine, BracketTook, HeldKind, InFlow, ask_shortfall, at_flow_start, close_first_line, close_flow_start_in, count_fill_view, counting_fills,
|
||||
fill_share, fill_size, flow_fill_room, flow_region, flow_room, flow_start, grid_cell, held_kind, in_grid_content_column, leaf_fill_room,
|
||||
members_line, open_first_lines, open_view_fill, place_in_cell, report_bracket_control, report_held, report_least, report_shrink, row_alone_room,
|
||||
run_body_measured, shares_y, shrink_cap, take_bracket, take_cell, take_in_flow, take_shortfall, take_view_fill, view_room,
|
||||
members_line, open_first_lines, open_view_fill, place_in_cell, report_bracket_control, report_fill_least, report_held, report_least,
|
||||
report_shrink, row_alone_room, run_body_measured, shares_y, shrink_cap, take_bracket, take_cell, take_in_flow, take_shortfall, take_view_fill,
|
||||
view_room,
|
||||
};
|
||||
|
||||
@@ -366,27 +366,54 @@ pub(crate) fn record_control_min(ctx: &Context, width: f32) {
|
||||
stack.0.iter().rev().find(|scope| matches!(scope.kind, Kind::Label(_))).map(|scope| scope.container)
|
||||
});
|
||||
if let Some(container) = container {
|
||||
layout_memory::contribute(ctx, std::sync::Arc::new(ControlMin { container, width }));
|
||||
layout_memory::contribute(ctx, std::sync::Arc::new(ControlMin { container, width, whole: false }));
|
||||
}
|
||||
}
|
||||
|
||||
/// The innermost open label scope, when no control inside it has reported its narrowest width yet.
|
||||
pub(crate) fn label_awaiting_min(ctx: &Context) -> Option<Id> {
|
||||
pass_state::with::<Stack, _>(ctx, |stack| {
|
||||
stack
|
||||
.0
|
||||
.iter()
|
||||
.rev()
|
||||
.find(|scope| matches!(scope.kind, Kind::Label(_)))
|
||||
.filter(|scope| scope.control_min.is_none())
|
||||
.map(|scope| scope.container)
|
||||
})
|
||||
}
|
||||
|
||||
/// Records `width` as the narrowest width of the controls of `container`'s label scope, in place of what the first of
|
||||
/// them reported: the least of a row that holds them side by side, the first control of the scope
|
||||
/// (`label_awaiting_min`). Only a measuring run records it.
|
||||
pub(crate) fn record_row_min(ctx: &Context, container: Id, width: f32) {
|
||||
layout_memory::contribute(ctx, std::sync::Arc::new(ControlMin { container, width, whole: true }));
|
||||
}
|
||||
|
||||
/// A control's narrowest width reported to the label scope of `container`, as a measuring run hands it forward.
|
||||
struct ControlMin {
|
||||
container: Id,
|
||||
width: f32,
|
||||
/// The width is the least of a row of controls (`record_row_min`): it replaces the first control's.
|
||||
whole: bool,
|
||||
}
|
||||
|
||||
impl layout_memory::Contribution for ControlMin {
|
||||
fn apply(&self, ctx: &Context) {
|
||||
pass_state::with::<Stack, _>(ctx, |stack| {
|
||||
if let Some(scope) = stack.0.iter_mut().rev().find(|scope| scope.container == self.container && matches!(scope.kind, Kind::Label(_))) {
|
||||
scope.control_min.get_or_insert(self.width);
|
||||
if self.whole {
|
||||
scope.control_min = Some(self.width);
|
||||
} else {
|
||||
scope.control_min.get_or_insert(self.width);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
fn report(&self) -> Option<(Id, f32)> {
|
||||
Some((sub(self.container, "control_min"), self.width))
|
||||
// A row's shrink step sweeps only in a measuring run, so a real run has no row least to compare.
|
||||
(!self.whole).then(|| (sub(self.container, "control_min"), self.width))
|
||||
}
|
||||
|
||||
fn undo(&self, ctx: &Context) -> layout_memory::TakeBack {
|
||||
|
||||
@@ -1266,6 +1266,7 @@ pub(crate) mod guard {
|
||||
("layout::body::Cursors", "grid and spread cursors: opened and closed by the body's own run"),
|
||||
("layout::body::ShrinkReports", "shrink reports: opened and closed by the row's own first sweep; the widths go through emit_record"),
|
||||
("layout::body::LeastReports", "least reports: opened and closed by the row's own least sweep; the widths go through emit_record"),
|
||||
("layout::body::FillLeasts", "fill leasts: opened and closed by the row's own first sweep; the sum goes through contribute"),
|
||||
("layout::body::RowShortfalls", "shortfalls: asked and taken by the container around the row in the same measuring sweep"),
|
||||
("runtime::reads::Runs", "measuring runs of the read record: opened and closed by each run; reads go through contribute"),
|
||||
("contract::steps::id::IdPass", "id registration: a measuring run keeps the ids it uses out of the pass (keeping_ids)"),
|
||||
|
||||
@@ -91,6 +91,7 @@ pass_state! {
|
||||
jumps: crate::contract::body::scroll::Jumps,
|
||||
shrink_reports: crate::layout::body::ShrinkReports,
|
||||
least_reports: crate::layout::body::LeastReports,
|
||||
fill_leasts: crate::layout::body::FillLeasts,
|
||||
row_shortfalls: crate::layout::body::RowShortfalls,
|
||||
bar_rules: crate::contract::BarRules,
|
||||
surface_frames: crate::primitives::SurfaceFrames,
|
||||
|
||||
@@ -11,6 +11,7 @@ pub mod kit;
|
||||
pub(crate) mod link;
|
||||
pub(crate) mod reach;
|
||||
pub(crate) mod reset;
|
||||
pub(crate) mod row_least;
|
||||
pub(crate) mod scheme;
|
||||
pub(crate) mod scrub_drags;
|
||||
pub(crate) mod short;
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
//! A field whose control is a row of several controls (a path field and its Browse button) puts the row under its
|
||||
//! label when the label beside it would leave the row less than its least width.
|
||||
|
||||
use craft_theme::Size;
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Bind, Props as _, Show as _, ShowContainer as _, Sizing};
|
||||
use craft_ui::form::Field;
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use craft_ui::text::TextField;
|
||||
use egui::{Rect, Vec2};
|
||||
|
||||
/// How far two edges may differ and still line up, in points.
|
||||
const SLACK: f32 = 0.5;
|
||||
const BROWSE: &str = "Browse…";
|
||||
|
||||
/// A field labelled `label` over a fill-width path field and a Browse button, in a room `width` wide.
|
||||
fn path_field(label: &'static str, width: f32) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
Field::new(label).show(ui, |ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
TextField::new(label, Bind::given(String::from("/photos/agents"))).limit(None).sizing(Sizing::FillWidth).show(ui);
|
||||
Button::new(BROWSE).show(ui);
|
||||
});
|
||||
});
|
||||
});
|
||||
fixture.room(Vec2::new(width, 300.0)).density(Size::Compact);
|
||||
fixture.settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
fn caption(fixture: &Fixture, text: &str) -> Rect {
|
||||
fixture.texts().iter().find(|run| run.text == text).map(|run| run.rect).unwrap_or_else(|| panic!("{text:?} is painted"))
|
||||
}
|
||||
|
||||
/// In a room 240 pt wide, the long label beside the row would leave it less than the path field's and the button's
|
||||
/// least widths together. The row shows under the label, and the button's text shows whole.
|
||||
#[trial]
|
||||
fn a_long_label_puts_a_row_of_controls_under_it_when_the_row_would_fall_below_its_least() {
|
||||
const LABEL: &str = "Folder agents can open from";
|
||||
let fixture = path_field(LABEL, 240.0);
|
||||
let (label, field) = (caption(&fixture, LABEL), fixture.by_label(LABEL).expect("the label names the path field").rect);
|
||||
assert!(label.max.y <= field.min.y + SLACK, "the path field shows under its label: {label:?} {field:?}");
|
||||
let texts = fixture.texts();
|
||||
let browse = texts.iter().find(|run| run.text == BROWSE).unwrap_or_else(|| panic!("{BROWSE:?} shows whole: {texts:?}"));
|
||||
assert!(browse.clip.min.x <= browse.rect.min.x + SLACK && browse.clip.max.x >= browse.rect.max.x - SLACK, "Browse shows whole: {browse:?}");
|
||||
}
|
||||
|
||||
/// A short label leaves the row its least width beside it, so the label stays beside the row.
|
||||
#[trial]
|
||||
fn a_short_label_stays_beside_a_row_of_controls_that_fits() {
|
||||
const LABEL: &str = "Folder";
|
||||
let fixture = path_field(LABEL, 240.0);
|
||||
let (label, field) = (caption(&fixture, LABEL), fixture.by_label(LABEL).expect("the label names the path field").rect);
|
||||
assert!(label.max.x <= field.min.x + SLACK && label.max.y > field.min.y, "the label stays beside the path field: {label:?} {field:?}");
|
||||
}
|
||||
Reference in New Issue
Block a user