diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index 0f9f07641..1ad267b23 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -5,7 +5,7 @@ use super::open::open_now; use super::placement::{Placement, measure_layer, overlay_of, shown_placement}; use super::regions::{ LabelColumns, LabelsFound, RegionFrame, RegionSizes, Regions, RuleBands, end_full_height, footer_in_body, footer_in_end, footer_rule_under, - gap_of, header_gap_of, label_extra_of, least_of, pad_of, paint_edge_rule, paint_rules, rect_of, shared_labels, shared_line, + gap_of, header_gap_of, label_extra_of, least_of, pad_of, paint_bracket, paint_edge_rule, paint_rules, rect_of, shared_labels, shared_line, }; use super::reorder::REORDER_BAND; use super::scroll::{ @@ -1305,12 +1305,12 @@ impl BodyHooks<'_> { && env.values.get::() == label::LabelScheme::Inspector; let (room, content_column, scroll, beside, label_width) = (plan.room, plan.content_column, plan.scroll, plan.regions.beside, plan.label_extra.x); - let first_body = plan.first.1; + let (first_body, layout_id) = (plan.first.1, plan.layout_id); // The slot of a footer the body places takes the size the measuring run's footer took. let footer_slot = footer_in_body(props) .then(|| FooterSlotOf { owner: id, size: if real { self.measured_footer } else { self.footer_size.unwrap_or(Vec2::ZERO) } }); plan.body_env = Some(plan.line_env.derive_local(|scoped| { - self.scope_body(scoped, props, id, BodyScope { room, real, content_column, scroll, label_width, beside }); + self.scope_body(scoped, props, id, BodyScope { room, real, content_column, scroll, label_width, beside, layout: layout_id }); scoped.inside_label = inside.map(|inside| inside as crate::runtime::env::Held); scoped.cell_fills = cell_fills; if footer_slot.is_some() { @@ -1569,6 +1569,12 @@ impl BodyHooks<'_> { /// size counts the label column. fn after_body(&mut self, ui: &mut Ui, env: &Env, plan: &mut BodyPlan<'_>) { let (props, ctx, real) = (plan.props, &plan.ctx, plan.real); + if let Some(bracket) = props.bracket { + let took = layout::take_bracket(ctx, plan.layout_id); + if let (true, crate::contract::Bracket::Linked, Some(took)) = (real, bracket, took) { + paint_bracket(ui, env, &took, plan.rtl); + } + } // A measuring run decided the label before the control reported its minimum width: it decides again // from the minimum this run recorded, so the extent it measures is the one the real run lays out. if let (false, Some(input)) = (real, plan.label_input.as_mut()) { @@ -1580,6 +1586,12 @@ impl BodyHooks<'_> { plan.label_extra = label_extra_of(plan.regions.body, plan.body_rect, plan.label_rect, plan.rtl); } if let Some(size) = &mut self.body_size { + // A labelled row of a bracketed row reports where its control lies: the bracket runs from it. + if let (Some(row), Some(_)) = (env.bracket, props.label.as_ref()) { + let body = plan.body_rect; + let left = if plan.rtl { body.max.x - size.x } else { body.min.x }; + layout::report_bracket_control(ctx, row, Rect::from_min_size(egui::pos2(left, body.min.y), *size), plan.rtl); + } let label_extra = plan.label_extra; // A body beside its label in a content-sized column of rows reports how far it reaches past the label // column, so that column's container measures its rows at the widest label. diff --git a/crates/ui-contract/src/contract/body/inherit.rs b/crates/ui-contract/src/contract/body/inherit.rs index 660e5ebc8..31bfd65eb 100644 --- a/crates/ui-contract/src/contract/body/inherit.rs +++ b/crates/ui-contract/src/contract/body/inherit.rs @@ -19,6 +19,8 @@ pub(super) struct BodyScope { pub(super) label_width: f32, /// The width the regions beside the body take on its line (`Regions::beside`). pub(super) beside: f32, + /// The id the body's layout runs under (`BodyPlan::layout_id`). + pub(super) layout: Id, } /// The surface a container paints under its regions: its props' surface, else its style's in its current state. @@ -96,10 +98,19 @@ impl BodyHooks<'_> { /// What the body inherits on top of the line's env: arena, transform, the room it is offered and its scroll axes. pub(super) fn scope_body(&self, scoped: &mut Scoped, props: &ContainerProps, id: Id, body: BodyScope) { - let BodyScope { room, real, content_column, scroll, label_width, beside } = body; + let BodyScope { room, real, content_column, scroll, label_width, beside, layout } = body; if props.arena { scoped.arena = Some(ArenaId::new(id)); } + scoped.flow_body = None; + scoped.column_row = + self.in_column && matches!(props.layout, crate::layout::BodyLayout::Row { wrap: false, align: crate::layout::RowAlign::Start }); + // A bracketed row's rows report to it up to the first labelled container, whose body is its own control. + if props.bracket.is_some() { + scoped.bracket = Some(layout); + } else if props.label.is_some() { + scoped.bracket = None; + } scoped.transform = props.transform; scoped.content_column = content_column; scoped.given_column = !content_column && matches!(props.layout, crate::layout::BodyLayout::Stack); diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index 810189cf3..c0ee61ec8 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -11,7 +11,7 @@ use crate::runtime::egui::{alloc, child, style}; use crate::runtime::env::{Env, RowLine}; use crate::runtime::memory::sub; use crate::runtime::overlay::CloseReason; -use crate::style::{RuleKind, UiTokens, rule_look}; +use crate::style::{RuleKind, RuleLook, RuleWidth, UiTokens, rule_look}; use egui::{Align, Id, Layout, Pos2, Rect, Ui, UiBuilder, Vec2}; /// The slots of a container's own header parts at the edges of the header's line (`Container::header_parts`). @@ -217,6 +217,30 @@ pub(super) fn paint_rules(ui: &mut Ui, env: &Env, regions: &Regions) { } } +/// The bracket of a linked `Row` (`Bracket::Linked`) from what its rows reported in `took`: a tick from the end of the +/// first and the last row's controls, `space.gap.tight` past them, to the middle child's centre line, and a stroke +/// along that line from each tick to the middle child's edge. `text.muted`, `stroke.thin` wide. +pub(super) fn paint_bracket(ui: &mut Ui, env: &Env, took: &crate::layout::BracketTook, rtl: bool) { + let (Some(first), Some(last), Some(end), Some(middle)) = (took.first, took.last, took.end, took.middle) else { return }; + if last <= first { + return; + } + let tokens = &env.tokens; + let tight = tokens.tokens().space.gap.tight; + let look = RuleLook { color: crate::style::rgba_color(tokens.color.text.muted), width: RuleWidth::Points(tokens.stroke.thin) }; + let ppp = ui.ctx().pixels_per_point(); + let (start, line) = if rtl { (-end - tight, middle.center().x) } else { (end + tight, middle.center().x) }; + let painter = ui.painter(); + for (from, to) in [ + (Pos2::new(start, first), Pos2::new(line, first)), + (Pos2::new(start, last), Pos2::new(line, last)), + (Pos2::new(line, first), Pos2::new(line, middle.top().max(first))), + (Pos2::new(line, middle.bottom().min(last)), Pos2::new(line, last)), + ] { + painter.extend(Rule::shapes(&look, from, to, ppp)); + } +} + impl BodyHooks<'_> { /// The footer's rect and how far its left edge lies past the container's, under the control column of a /// label-scoped container; along an axis the room leaves unbounded, the footer follows the body and the end region. diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index 1c0392f89..58aaacd29 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -29,6 +29,8 @@ pub(super) struct MeasureKey { /// Whether the body shows, and how much of it an animated collapse shows. shows: (bool, f32), env: EnvFacts, + /// Whether its parent flow showed the container as a row of a vertical flow (`label::in_column`). + in_column: bool, } /// The props a body was measured under, compared on the fields its measure reads. @@ -92,6 +94,7 @@ impl<'b> BodyHooks<'b> { width, shows: (self.open, self.presence.t), env: EnvFacts::of(env), + in_column: self.in_column, } } diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 142677f22..b7a8f1da8 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -727,6 +727,19 @@ impl EnvValue for InMenu { } } +/// The bracket a `Row` draws from the labelled rows of its first child to the child after them (Image Size's width and +/// height to their chain): a tick from each of the first and the last row's controls to the middle child's centre +/// line, joined along that line. The middle child sits halfway between the two controls. The value is the link's +/// state. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[non_exhaustive] +pub enum Bracket { + /// The rows are linked: the bracket is drawn. + Linked, + /// The rows are not linked: the middle child keeps its place, with no bracket. + Unlinked, +} + /// What a container or collection adds to `Common`. A builder added here is added to the forwarder list in /// `crates/ui-macros/src/props.rs` too. /// @@ -820,6 +833,9 @@ pub struct ContainerProps { pub label_column: bool, /// How the labelled rows under the container show their labels; `None` keeps the scheme of the scope. pub labels: Option, + /// The bracket an unwrapped `Row` placed from its start draws from its first child's labelled rows to the child + /// after them; any other body draws none. + pub bracket: Option, /// The component whose memory holds the container's shown memory and its layer's bookkeeping, when it is another: /// the body of a popup an `overlay` hook shows belongs to the component that shows it. pub(crate) owner: Option, @@ -860,6 +876,7 @@ impl ContainerProps { && self.outer_edit == other.outer_edit && self.collection_body == other.collection_body && self.labels == other.labels + && self.bracket == other.bracket } /// Props with every field at its default. @@ -901,6 +918,7 @@ impl ContainerProps { collection_body: false, label_column: false, labels: None, + bracket: None, owner: None, } } @@ -1122,6 +1140,12 @@ impl ContainerProps { self } + /// The bracket of a `Row` from its first child's labelled rows to the child after them ([`bracket`](Self::bracket)). + pub const fn bracket(mut self, bracket: Bracket) -> Self { + self.bracket = Some(bracket); + self + } + /// The labelled rows under the container show their labels in `scheme`: its regions, its body and every container /// inside, until one sets a scheme of its own. pub const fn labels(mut self, scheme: LabelScheme) -> Self { diff --git a/crates/ui-contract/src/contract/label/step.rs b/crates/ui-contract/src/contract/label/step.rs index e49a5c280..b2c5f1b18 100644 --- a/crates/ui-contract/src/contract/label/step.rs +++ b/crates/ui-contract/src/contract/label/step.rs @@ -116,6 +116,8 @@ impl LeafPlaced { pub(crate) fn leaf(ui: &mut Ui, env: &Env, at: &LeafLabel<'_>, child: &mut C, trailing: Option>) -> LeafPlaced { let scope = sub(at.id, "field"); let enabled = at.under.enabled; + // Read before the child measures: nothing it does there may count as a child placed in flow. + let in_column = in_column(ui); let control_min = crate::contract::run::measure_under(child, &*ui, Some(at.id), craft_behavior::Proposal::Min, Some(at.under)).x; let input = LabelIn { scope, @@ -131,7 +133,7 @@ pub(crate) fn leaf(ui: &mut Ui, env: &Env, at: &LeafLabel<'_>, child: & size: at.under.size, control_min, leaf: true, - in_column: in_column(ui), + in_column, claims: C::LABELLED_AFTER, status: false, trailing: trailing.as_ref().map_or(Vec2::ZERO, |trailing| trailing.size), @@ -227,9 +229,15 @@ pub(crate) fn run(ui: &mut Ui, env: &Env, input: &LabelIn<'_>, trailing: Option< placed } -/// Whether a row shown in `flow` is a row of a vertical flow, not a member of a row's line. +/// Whether a row shown in `flow` is a row of a vertical flow, not a member of a row's line. The first child of an +/// unwrapped row that is itself a row of a vertical flow (`Scoped::column_row`) starts where that flow's rows start, +/// so it counts as one of them: a check box that starts a row of a form lines up with the form's controls. pub(crate) fn in_column(flow: &Ui) -> bool { - !flow.layout().is_horizontal() + if !flow.layout().is_horizontal() { + return true; + } + let env = Env::of(flow); + env.column_row && crate::layout::at_flow_start(flow.ctx(), &env) } /// A control with its label after it that the label step moved into the control column (its `control` starts past diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index d0db84761..bb9ec4da3 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -176,6 +176,7 @@ impl Lazy { if memo { place_view(ui, rect); } else { + crate::layout::close_flow_start_in(ui); alloc::advance_past(ui, rect); } if view.is_some() || memo { @@ -268,6 +269,7 @@ fn at_start(region: Rect, rtl: bool, size: Vec2) -> Pos2 { /// starts a layout room of its own and takes none; elsewhere the flow moves past `rect`. fn place_view(ui: &mut Ui, rect: Rect) -> Rect { if take_cell(ui, rect.size()).is_none() { + crate::layout::close_flow_start_in(ui); alloc::advance_past(ui, rect); } rect diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index 14ea466f7..6642281f5 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -68,8 +68,9 @@ pub use container::InBar; #[doc(hidden)] pub use container::InMenu; pub use container::{ - BandFill, Collapse, ContainerProps, DragBand, EndRegion, FooterPlace, FooterRegion, Gap, HeaderEdge, HeaderParts, HeaderRegion, LeastWidth, - Mount, Notch, POPUP_PAD, Pad, PadEdges, PaneEdge, Scroll, ScrollAnchor, ScrollAxes, ScrollIndicator, ShellPane, Sizing, ThemeScope, WrapWidth, + BandFill, Bracket, Collapse, ContainerProps, DragBand, EndRegion, FooterPlace, FooterRegion, Gap, HeaderEdge, HeaderParts, HeaderRegion, + LeastWidth, Mount, Notch, POPUP_PAD, Pad, PadEdges, PaneEdge, Scroll, ScrollAnchor, ScrollAxes, ScrollIndicator, ShellPane, Sizing, ThemeScope, + WrapWidth, }; pub use craft_tracked::{PlainData, Revision, Tracked}; pub use craft_ui_macros::{CommandSet, Component, Pattern, PlainData, Props, register_contract}; diff --git a/crates/ui-contract/src/contract/run/typed.rs b/crates/ui-contract/src/contract/run/typed.rs index 0be4e115f..956401601 100644 --- a/crates/ui-contract/src/contract/run/typed.rs +++ b/crates/ui-contract/src/contract/run/typed.rs @@ -546,6 +546,7 @@ pub(crate) fn show_container_taking( body: &mut dyn FnMut(&mut Ui, &mut BodyCx<'_>) -> R, ) -> Inner { let shown = show_container(slot, ui, ShowAt::Rect(rect), body, None); + crate::layout::close_flow_start_in(ui); crate::runtime::egui::alloc::advance_past(ui, rect); shown } diff --git a/crates/ui-contract/src/layout/arrange.rs b/crates/ui-contract/src/layout/arrange.rs index de37411f3..8a6b9b170 100644 --- a/crates/ui-contract/src/layout/arrange.rs +++ b/crates/ui-contract/src/layout/arrange.rs @@ -71,6 +71,7 @@ pub fn arrange(ui: &mut Ui, layout: &dyn Arrangement, children: &mut [Placed<'_> placed.response = Some(placed.child.show_at(ui, *id, flow.to_physical(*rect))); } let whole = flow.to_physical(Rect::from_min_size(room.min, taken)); + super::close_flow_start_in(ui); alloc::advance_past(ui, whole); whole } diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index c32bee2cb..440476bb1 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -10,7 +10,7 @@ use crate::runtime::measure; use crate::runtime::memory::sub; use crate::runtime::pass_state; use craft_behavior::{Axis, ColumnWidth, Flow, GridCursor, GridMeasure}; -use egui::{Id, IdMap, Rect, Ui, Vec2}; +use egui::{Id, IdMap, IdSet, Rect, Ui, Vec2}; use std::borrow::Cow; /// How a body lays out its children, in reading order: mirrored under a right-to-left direction. @@ -211,6 +211,71 @@ fn report_first_line(ctx: &egui::Context, target: Id, at: Option) { }); } +/// The bodies whose flow has placed nothing yet in the sweep or run under way. +#[derive(Default)] +pub(crate) struct FlowStarts(IdSet); + +/// Whether the next child placed in flow under `env` starts its body's flow: the body that places it has placed +/// nothing yet in this sweep or run. False outside a body. +pub(crate) fn at_flow_start(ctx: &egui::Context, env: &Env) -> bool { + env.flow_body.is_some_and(|body| pass_state::with::(ctx, |starts| starts.0.contains(&body))) +} + +/// A child took room in the flow of the body that places the children under `env`: its flow start closes. +pub(crate) fn close_flow_start(ctx: &egui::Context, env: &Env) { + if let Some(body) = env.flow_body { + pass_state::with::(ctx, |starts| starts.0.remove(&body)); + } +} + +/// `close_flow_start` for a child placed in the flow of `ui` outside `place_in_cell` and `take_in_flow`. +pub(crate) fn close_flow_start_in(ui: &Ui) { + close_flow_start(ui.ctx(), &Env::of(ui)); +} + +/// What the labelled rows of a bracketed `Row` (`ContainerProps::bracket`) reported of their controls in the sweep or +/// run under way, and the rect of the row's last child in flow, by the row's layout id. +#[derive(Default)] +pub(crate) struct Brackets(IdMap); + +/// The controls of a bracket's rows and the child after them. +#[derive(Clone, Copy, Debug, Default)] +pub(crate) struct BracketTook { + /// The centre line of the first row's control. + pub(crate) first: Option, + /// The centre line of the last row's control. + pub(crate) last: Option, + /// How far the rows' controls reach toward the row's end edge, at most: right, or left under right-to-left. + pub(crate) end: Option, + /// The rect of the row's last child placed in flow: the control in the bracket's middle. + pub(crate) middle: Option, +} + +impl BracketTook { + /// The centre line between the first and the last row's controls, once two rows reported. + pub(crate) fn centre(&self) -> Option { + self.first.zip(self.last).filter(|(first, last)| last > first).map(|(first, last)| (first + last) / 2.0) + } +} + +/// Reports the `control` rect of a labelled row to the bracketed row `row`, its end edge `rtl`-aware. +pub(crate) fn report_bracket_control(ctx: &egui::Context, row: Id, control: Rect, rtl: bool) { + pass_state::with::(ctx, |brackets| { + if let Some(took) = brackets.0.get_mut(&row) { + let centre = control.center().y; + took.first.get_or_insert(centre); + took.last = Some(centre); + let end = if rtl { -control.min.x } else { control.max.x }; + took.end = Some(took.end.map_or(end, |was: f32| was.max(end))); + } + }); +} + +/// What the rows of the bracketed row `row` reported in the run that just ended; `None` outside a bracketed row. +pub(crate) fn take_bracket(ctx: &egui::Context, row: Id) -> Option { + pass_state::with::(ctx, |brackets| brackets.0.remove(&row)) +} + /// How a child that fills the main axis of the body it sits in sizes along it. enum MainFill { /// The body is being measured: the child takes no room along the axis and is counted. @@ -1115,9 +1180,16 @@ pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect { let env = Env::of(ui); let (line, target) = (env.row_line, env.first_line); let child = InFlow { first: if child.line { child.first } else { one_line(child.first, size.y) }, ..child }; - let above = line.and_then(|line| line.centre).zip(child.first).map_or(0.0, |(centre, first)| (centre - first).max(0.0)); + // A one-line child of a bracketed row sits with its centre on the bracket's middle, between the controls its + // rows reported before it. + let bracket = line.filter(|line| env.bracket == Some(line.body)).and_then(|line| { + let took = pass_state::with::(ui.ctx(), |brackets| brackets.0.get(&line.body).copied())?; + Some((line.body, took.centre().map(|centre| centre - ui.available_rect_before_wrap().top()))) + }); + let centre = bracket.and_then(|(_, centre)| centre).or_else(|| line.and_then(|line| line.centre)); + let above = centre.zip(child.first).map_or(0.0, |(centre, first)| (centre - first).max(0.0)); let slot = Vec2::new(size.x, line.and_then(|line| line.height).map_or(above + size.y, |height| height.max(above + size.y))); - let (rect, in_cell) = take_in(ui, env.grid, slot); + let (rect, in_cell) = take_in(ui, &env, slot); let placed = if child.fill && in_cell { rect } else { @@ -1130,6 +1202,9 @@ pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect { }; Rect::from_min_max(egui::pos2(rect.min.x, rect.min.y + above), egui::pos2(rect.max.x, rect.max.y - below)) }; + if let Some((row, _)) = bracket { + pass_state::with::(ui.ctx(), |brackets| brackets.0.get_mut(&row).map(|took| took.middle = Some(placed))); + } if measure::active(ui.ctx()) { if let Some(line) = line { pass_state::with::(ui.ctx(), |rows| { @@ -1148,7 +1223,7 @@ pub(crate) fn place_in_cell(ui: &mut Ui, size: Vec2, child: InFlow) -> Rect { /// Takes `size` in flow in `ui`: the next cell of the `Grid` or spread `Row` body that encloses it directly, else the /// next allocation of `ui`'s layout. Returns the rect taken and whether it is a cell. pub(crate) fn take_in_flow(ui: &mut Ui, size: Vec2) -> (Rect, bool) { - take_in(ui, Env::of(ui).grid, size) + take_in(ui, &Env::of(ui), size) } /// Leaves the next cell of the `Grid` body that encloses `ui` directly empty: the next child takes the cell after it. @@ -1158,13 +1233,15 @@ pub fn empty_cell(ui: &mut Ui) { let env = Env::of(ui); let grid = env.grid.filter(|body| pass_state::with::(ui.ctx(), |cursors| matches!(cursors.0.get(body), Some(Cursor::Grid(_))))); if grid.is_some() { - take_in(ui, grid, Vec2::ZERO); + take_in(ui, &env, Vec2::ZERO); } } -/// `take_in_flow`, with `grid` the body of `ui`'s env that places its children itself. -fn take_in(ui: &mut Ui, grid: Option, size: Vec2) -> (Rect, bool) { - match cell_in(ui, grid, size) { +/// `take_in_flow` under `env`, `ui`'s env: the next cell of its grid, else the next allocation; the body's flow start +/// closes. +fn take_in(ui: &mut Ui, env: &Env, size: Vec2) -> (Rect, bool) { + close_flow_start(ui.ctx(), env); + match cell_in(ui, env.grid, size) { Some(cell) => (cell, true), None => (alloc::space(ui, size), false), } @@ -1173,7 +1250,12 @@ fn take_in(ui: &mut Ui, grid: Option, size: Vec2) -> (Rect, bool) { /// Takes the next cell of the `Grid` or spread `Row` body that encloses `ui` directly for a child of `size`, and returns /// it; `None`, taking nothing, outside such a body. pub(crate) fn take_cell(ui: &mut Ui, size: Vec2) -> Option { - cell_in(ui, Env::of(ui).grid, size) + let env = Env::of(ui); + let cell = cell_in(ui, env.grid, size); + if cell.is_some() { + close_flow_start(ui.ctx(), &env); + } + cell } fn cell_in(ui: &mut Ui, grid: Option, size: Vec2) -> Option { @@ -1324,6 +1406,7 @@ fn run_grid( scoped.derive_local(|scoped| { scoped.grid = Some(id); scoped.grid_top = cells == CellAlign::Top; + scoped.flow_body = Some(id); }), ); let (inner, unsettled) = ColumnGroup::grid(ui, id, count, seed, &mut body); @@ -1598,8 +1681,14 @@ pub(crate) fn run_body_measured( // Each sweep of a measuring run finds the body's first line and an unwrapped row's line afresh. let target = env.first_line.filter(|_| sizing); let row = (sizing && matches!(layout, BodyLayout::Row { wrap: false, .. })).then_some(id); + // Each sweep and the real run start the body's flow, and a bracketed row's reports, afresh. + let bracket = env.bracket == Some(id); let body = |ui: &mut Ui| { open_sweep(&ctx, target, row); + pass_state::with::(&ctx, |starts| starts.0.insert(id)); + if bracket { + pass_state::with::(&ctx, |brackets| brackets.0.insert(id, BracketTook::default())); + } body(ui) }; // Each layout runs in its own function, so a body nested in bodies keeps only its own layout's frame on the stack. @@ -1627,7 +1716,10 @@ struct RowIn { /// A `Stack` body: each leaf shows at most the stack's room, never below its least. fn run_stack(ui: &mut Ui, env: &Env, at: &RowIn, body: impl FnMut(&mut Ui) -> R) -> R { let ctx = ui.ctx().clone(); - let env = env.derive_local(|scoped| scoped.shrink = Some(Shrink::Fit)); + let env = env.derive_local(|scoped| { + scoped.shrink = Some(Shrink::Fit); + scoped.flow_body = Some(at.id); + }); let installed = Env::install_for(ui, env.clone()); // A held child of a column whose own room is unbounded keeps the width it measured at. let inner = if env.content_column && flow_room(ui).x.is_finite() { @@ -1652,6 +1744,7 @@ fn run_spread_row(ui: &mut Ui, env: &Env, at: &RowIn, mut body: impl FnMut(&m scoped.grid_top = false; scoped.row_line = Some(line); scoped.first_line = None; + scoped.flow_body = Some(id); }), ); let inner = run_spread(ui, id, gap.x, rtl, &mut body); @@ -1675,6 +1768,7 @@ fn run_start_row(ui: &mut Ui, env: &Env, at: &RowIn, mut body: impl FnMut(&mu scoped.row_room = Some(room.width()); scoped.row_line = Some(line); scoped.first_line = None; + scoped.flow_body = Some(id); }), ); let inner = run_shrinking(ui, id, &mut body); @@ -1724,6 +1818,7 @@ fn run_row(ui: &mut Ui, env: &Env, at: &RowIn, (wrap, align): (bool, RowAlign env.derive_local(|scoped| { scoped.row_room = Some(room.width()); scoped.row_line = line; + scoped.flow_body = Some(id); if line.is_some() { scoped.first_line = None; } diff --git a/crates/ui-contract/src/layout/mod.rs b/crates/ui-contract/src/layout/mod.rs index 4f2654e97..fa1419ccd 100644 --- a/crates/ui-contract/src/layout/mod.rs +++ b/crates/ui-contract/src/layout/mod.rs @@ -24,7 +24,7 @@ pub use body::place_in_flow; #[cfg(feature = "testing")] pub(crate) use body::run_body; pub(crate) use body::{ - InFlow, close_first_line, counting_fills, fill_share, fill_size, flow_fill_room, flow_region, flow_room, grid_cell, in_grid_content_column, - leaf_fill_room, members_line, open_first_lines, place_in_cell, report_held, report_least, report_shrink, row_alone_room, run_body_measured, - shares_y, shrink_cap, take_cell, take_in_flow, view_room, + BracketTook, InFlow, at_flow_start, close_first_line, close_flow_start_in, counting_fills, fill_share, fill_size, flow_fill_room, flow_region, + flow_room, grid_cell, in_grid_content_column, leaf_fill_room, members_line, open_first_lines, 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, view_room, }; diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index 38a7d029b..c8857cd64 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -138,6 +138,15 @@ pub struct Scoped { /// Where the first child the scope places in flow reports its first line: a container's header or body /// (`layout::FirstLines`). `None` in the children of a `Row` body, which reports its line itself. pub(crate) first_line: Option, + /// The body whose flow places the scope's direct children: the flow start a child reads (`layout::flow_start`) + /// and the one a child placed in flow closes. + pub(crate) flow_body: Option, + /// The body is an unwrapped `Row` placed from its start whose container is a row of a vertical flow: its first + /// child in flow starts where the rows of that flow start (`label::in_column`). + pub(crate) column_row: bool, + /// The layout of the bracketed `Row` body (`ContainerProps::bracket`) whose labelled rows report their controls to + /// it: set for the row's body and kept through the containers inside it up to a labelled one. + pub(crate) bracket: Option, /// The container whose footer stands in its body (`FooterPlace::Body`) and the size that footer measured: a /// `FooterSlot` shown under it takes that size and is where the footer is drawn. pub(crate) footer_slot: Option, @@ -358,6 +367,7 @@ impl Scoped { /// layer is no line of a bar it was opened from. A layer's content measures and runs in this. pub(crate) fn start_layer(&mut self) { self.start_room(); + self.bracket = None; self.measure = None; self.arena = None; self.roving = None; @@ -387,6 +397,8 @@ impl Scoped { self.row_room = None; self.row_line = None; self.first_line = None; + self.flow_body = None; + self.column_row = false; self.fill_body = None; self.fill_share = None; self.shrink = None; @@ -555,6 +567,9 @@ impl Env { row_room: None, row_line: None, first_line: None, + flow_body: None, + column_row: false, + bracket: None, footer_slot: None, fill_body: None, fill_share: None, @@ -644,6 +659,7 @@ impl Env { pub(crate) fn new_room(&self) -> Env { self.derive_local(|scoped| { scoped.start_room(); + scoped.bracket = None; scoped.lazy = None; scoped.measure = None; // A pane opened from a bar is not the bar's line. diff --git a/crates/ui-contract/src/runtime/layout_memory.rs b/crates/ui-contract/src/runtime/layout_memory.rs index f58c68dd2..22e9b6838 100644 --- a/crates/ui-contract/src/runtime/layout_memory.rs +++ b/crates/ui-contract/src/runtime/layout_memory.rs @@ -1239,6 +1239,11 @@ pub(crate) mod guard { ("layout::body::HeldWidths", "held widths: opened and closed by the column's own first sweep"), ("layout::body::FirstLines", "first lines: opened and closed by the container's own measuring sweep; the line goes through emit_record"), ("layout::body::RowLines", "row lines: opened by each sweep of the row and closed after it; the line goes through emit_record"), + ( + "layout::body::FlowStarts", + "flow starts: opened by each sweep of the body and closed by its first child in flow; a child reads its body's at its own start, and its measure key holds what it read", + ), + ("layout::body::Brackets", "bracket reports: opened by each sweep of the bracketed row and taken after its body"), ("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"), diff --git a/crates/ui-contract/src/runtime/pass_state.rs b/crates/ui-contract/src/runtime/pass_state.rs index 2dc04e1d0..982cedf9e 100644 --- a/crates/ui-contract/src/runtime/pass_state.rs +++ b/crates/ui-contract/src/runtime/pass_state.rs @@ -96,6 +96,8 @@ pass_state! { held_widths: crate::layout::body::HeldWidths, first_lines: crate::layout::body::FirstLines, row_lines: crate::layout::body::RowLines, + flow_starts: crate::layout::body::FlowStarts, + brackets: crate::layout::body::Brackets, cursors: crate::layout::body::Cursors, footer_slots: crate::contract::footer_slot::Slots, column_groups: crate::layout::column_group::Open, diff --git a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs index 16d315d52..dfdb6a893 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/new_document/details.rs @@ -5,7 +5,7 @@ use super::presets::{ BackgroundContents, ColorMode, DocumentOptions, MAX_NAME, MAX_SAVED, Orientation, PIXEL_ASPECTS, RESOLUTION_UNITS, SIZE_UNITS, SQUARE_PIXELS, }; use super::{NewDocumentWords, Read, Session, background_rgb, depth_label, mode_label}; -use crate::buttons::Button; +use crate::buttons::{Button, IconButton}; use crate::choice::{Checkbox, Segmented, Swatch}; use crate::contract::{Bind, Entry, FieldLayout, Gap, MemoProps, Props as _, Show as _, ShowContainer as _, Sizing, Validity, clicked}; use crate::display::{Shade, Text}; @@ -138,7 +138,8 @@ pub(super) fn show(ui: &mut Ui, session: &mut Session, values: &mut DocumentOpti if std::mem::take(&mut session.focus_name) { focus::request(ui.ctx(), FocusTarget::Id(shown.response.id)); } - let save = Button::new(words.save_preset_title.clone()).painted(words.save_preset.clone()).id_salt("save-preset").show(ui); + let title = words.save_preset_title.clone(); + let save = IconButton::new(IconId::new("square_plus")).label(title.clone()).tooltip(title).id_salt("save-preset").show(ui); if clicked(&save) { session.naming = Some(proposed_name(read)); session.name_tried = false; diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs index 2e8f01b98..84e82f056 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing.rs @@ -3,7 +3,7 @@ use super::FormDialog; use super::size_model::{LinkedSize, MAX_DOCUMENT_SIDE}; -use crate::contract::{Bind, Entry, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, Sizing}; +use crate::contract::{Bind, Bracket, Entry, NoValue, Outcome, Props as _, Show as _, ShowContainer as _, Sizing}; use crate::dialog::{DialogResult, FieldRefusal, HeldAnswer}; use crate::display::Text; use crate::form::{Field, FieldGroup}; @@ -412,10 +412,11 @@ impl Kit { } } - /// The width and height rows, with the chain beside them when the model draws one. + /// The width and height rows, with the chain bracketed to them when the model draws one. pub(super) fn sides(&self, ui: &mut Ui, size: &mut LinkedSize) { let chain = size.has_chain(); - Row::new().show(ui, |ui, _| { + let row = if chain { Row::new().bracket(if size.is_linked() { Bracket::Linked } else { Bracket::Unlinked }) } else { Row::new() }; + row.show(ui, |ui, _| { Stack::new().show(ui, |ui, _| { self.row(ui, size, Dimension::Width); self.row(ui, size, Dimension::Height); diff --git a/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs b/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs index 8b47eb526..5b571c966 100644 --- a/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs +++ b/crates/ui-patterns/src/patterns/form_dialogs/sizing/image.rs @@ -4,7 +4,7 @@ use super::{Kit, LinkedSize, Resample, SizeForm, SizingOutput, set_size_json, sh use crate::choice::Checkbox; use crate::contract::{Bind, Props as _, Show as _, ShowContainer as _}; use crate::dialog::{DialogId, DialogResult, DialogStack, FieldRefusal, HeldAnswer, HeldOk, StackDialog, dialog_id}; -use crate::form::Field; +use crate::layout_components::Row; use crate::pickers::Select; use crate::runtime::env::{ColumnSize, Label, UnitKind}; use crate::runtime::{keys, overlay}; @@ -220,18 +220,19 @@ impl<'a> ImageSize<'a> { } } } - let on = Checkbox::new(Bind::given(state.size.is_resampling())).label(words.resample.clone()).id_salt("resample").show(ui); - if let Some(on) = on.value().copied() { - state.size.resampling(on); - } - if state.size.is_resampling() { - Field::new(words.resample_method.clone()).id_salt("method").show(ui, |ui, _| { - let picked = Select::new(words.resample_method.clone(), Bind::given(state.method), &entries).show(ui); + // Resample and its method share a row that starts in the control column. + Row::new().id_salt("resample-row").show(ui, |ui, _| { + let on = Checkbox::new(Bind::given(state.size.is_resampling())).label(words.resample.clone()).id_salt("resample").show(ui); + if let Some(on) = on.value().copied() { + state.size.resampling(on); + } + if state.size.is_resampling() { + let picked = Select::new(words.resample_method.clone(), Bind::given(state.method), &entries).id_salt("method").show(ui); if let Some(method) = picked.value().copied() { state.method = method; } - }); - } + } + }); let styles = Checkbox::new(Bind::given(state.scale_styles)) .label(words.scale_styles.clone()) .id_salt("scale-styles") diff --git a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs index b2adec9e8..59d1e9ece 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/hosted.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/hosted.rs @@ -521,3 +521,42 @@ fn sizing_dialogs_show_width_and_height_beside_their_labels_at_full_height() { } } } + +/// The horizontal line segments one frame of `fixture` paints, as (y, from x, to x). +fn horizontal_lines(fixture: &mut Fixture) -> Vec<(f32, f32, f32)> { + fixture + .frame_shapes() + .into_iter() + .filter_map(|shape| match shape { + egui::Shape::LineSegment { points: [a, b], .. } if (a.y - b.y).abs() < 0.5 => Some((a.y, a.x.min(b.x), a.x.max(b.x))), + _ => None, + }) + .collect() +} + +/// Image Size as 0.23.1 lays it out: the chain sits halfway between Width's and Height's controls with the bracket +/// drawn from each control to it, and Resample starts in the control column with the method on its line. +#[trial] +fn image_size_brackets_width_and_height_and_starts_resample_in_the_control_column() { + let host = Rc::new(RefCell::new(DialogHost::new())); + let mut fixture = fixture(&host); + host.borrow_mut().open(image_size()); + fixture.settle(); + let rect = |fixture: &Fixture, name: &str| fixture.by_label(name).unwrap_or_else(|| panic!("{name} is shown")).rect; + let (width, height, link) = (rect(&fixture, "Width unit"), rect(&fixture, "Height unit"), rect(&fixture, "Maintain aspect ratio")); + let middle = (width.center().y + height.center().y) / 2.0; + assert!((link.center().y - middle).abs() <= 0.5, "the chain sits halfway between Width and Height: {link:?} {middle}"); + let lines = horizontal_lines(&mut fixture); + for (name, row) in [("Width", width), ("Height", height)] { + assert!( + lines.iter().any(|(y, from, to)| (y - row.center().y).abs() <= 1.0 && *from >= row.max.x && *to >= link.center().x - 1.0), + "a tick runs from {name}'s control to the chain: {lines:?}" + ); + } + let (number, check, method) = (rect(&fixture, "Width"), rect(&fixture, "Resample"), rect(&fixture, "Resample method")); + assert!((check.min.x - number.min.x).abs() <= 0.5, "Resample starts at the control column: {check:?} {number:?}"); + assert!( + method.min.x >= check.max.x - 0.5 && method.min.y < check.max.y && method.max.y > check.min.y, + "the method follows Resample on its line: {method:?} {check:?}" + ); +} diff --git a/crates/ui-patterns/tests/it/form_dialogs/new_document.rs b/crates/ui-patterns/tests/it/form_dialogs/new_document.rs index 307ad8248..760b341b0 100644 --- a/crates/ui-patterns/tests/it/form_dialogs/new_document.rs +++ b/crates/ui-patterns/tests/it/form_dialogs/new_document.rs @@ -270,7 +270,7 @@ fn the_new_document_layout_places_presets_and_details_below_the_tabs() { } /// The fields run Name, size, Color Mode, Background Contents, then the advanced Color Profile and Pixel Aspect Ratio; the -/// save icon sits at the end of the Name row. +/// save icon (no text) sits at the end of the Name row. #[trial] fn the_new_document_fields_run_background_before_the_advanced_ones_and_save_sits_on_the_name_row() { let mut options = DocumentOptions::default(); @@ -287,6 +287,7 @@ fn the_new_document_fields_run_background_before_the_advanced_ones_and_save_sits let save = fixture.by_label("Save Document Preset").expect("the save action").rect; assert!(save.top() < name.bottom() && name.top() < save.bottom(), "the save icon shares the Name row"); assert!(name.right() <= save.left(), "the save icon is at the end of the Name field"); + assert!(!fixture.texts().iter().any(|run| run.text.starts_with("Save ")), "the save control is an icon, with no text"); } /// The caption role sets its text in capitals when it paints; the label stays as written. diff --git a/crates/ui-widgets/src/layout_components/mod.rs b/crates/ui-widgets/src/layout_components/mod.rs index e5c1c8e73..b0d1a52ba 100644 --- a/crates/ui-widgets/src/layout_components/mod.rs +++ b/crates/ui-widgets/src/layout_components/mod.rs @@ -2,7 +2,7 @@ //! lays its children out (6.3). use crate::contract::{ - Access, BodyCx, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, + Access, BodyCx, Bracket, Common, Component, Container, ContainerProps, Cx, EndRegion, FooterPlace, FooterRegion, Gap, HeaderRegion, LabelScheme, LeastWidth, Pad, Role, Scroll, Sizing, ThemeScope, WrapWidth, }; use crate::layout::{BodyLayout, CellAlign, GridFit, RowAlign}; @@ -325,6 +325,14 @@ impl Row { self.props.layout = std::mem::replace(&mut self.props.layout, BodyLayout::Stack).align(align); self } + + /// Links the labelled rows of the first child to the child after them (width and height to their chain): that + /// child sits halfway between the first and the last row's controls, and `Bracket::Linked` draws the bracket + /// from the controls to it. + pub fn bracket(mut self, bracket: Bracket) -> Self { + self.props = self.props.bracket(bracket); + self + } } impl Default for Row { diff --git a/crates/ui-widgets/src/layout_components/stories.rs b/crates/ui-widgets/src/layout_components/stories.rs index 30359b634..511f7f5db 100644 --- a/crates/ui-widgets/src/layout_components/stories.rs +++ b/crates/ui-widgets/src/layout_components/stories.rs @@ -1,11 +1,13 @@ //! Stories of the layout components `Row`, `Stack` and `Grid`. use super::{Grid, Row, Stack}; -use crate::contract::{Gap, LeastWidth, Pad, Props as _, Show as _, ShowContainer as _, Sizing, WrapWidth}; +use crate::contract::{Bind, Bracket, Gap, LeastWidth, Pad, Props as _, Show as _, ShowContainer as _, Sizing, WrapWidth}; use crate::display::Text; +use crate::form::{Field, FieldGroup}; use crate::layout::{CellAlign, GridFit, empty_cell}; use crate::story_kit::sample::{outlined, words}; use crate::testing::{StoryData, StoryOutput, story}; +use crate::text::{NumberField, link_toggle}; use craft_behavior::ColumnWidth; use egui::Ui; @@ -37,6 +39,29 @@ fn stack_footer(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { outlined(ui, |ui| Stack::new().gap(Gap::Stack).pad(Pad::Regular).footer(|ui, _| words(ui, 2..3)).show(ui, |ui, _| words(ui, 0..2)).into()) } +/// A bracketed row: the width and height of a form linked to their chain, which sits halfway between the two fields' +/// controls. The bracket is drawn while they are linked. +#[story(Row, covers = [], title = "Linked rows")] +fn row_bracket(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let linked = *data.choice("linked", true, &[("Linked", true), ("Unlinked", false)]); + outlined(ui, |ui| { + FieldGroup::new("Size") + .show(ui, |ui, _| { + Row::new().bracket(if linked { Bracket::Linked } else { Bracket::Unlinked }).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + for name in ["Width", "Height"] { + Field::new(name).show(ui, |ui, _| { + NumberField::new(name, Bind::given(Some(800.0))).sizing(Sizing::Content).show(ui); + }); + } + }); + link_toggle("Constrain proportions", Bind::given(linked)).show(ui); + }); + }) + .into() + }) +} + /// A side menu beside a page: the menu as wide as its widest item and as tall as the row, its footer at the bottom. #[story(Stack<'static>, covers = [])] fn stack_fill_height(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { diff --git a/crates/ui-widgets/tests/it/label/bracket.rs b/crates/ui-widgets/tests/it/label/bracket.rs new file mode 100644 index 000000000..107d7e8d8 --- /dev/null +++ b/crates/ui-widgets/tests/it/label/bracket.rs @@ -0,0 +1,116 @@ +//! A bracketed row and the row a check box starts: the chain halfway between its fields with the bracket drawn to +//! their controls, and a check box that starts a row of a form in the form's control column. + +use craft_ui::choice::Checkbox; +use craft_ui::contract::{Bind, Bracket, Entry, Props as _, Show as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::form::{Field, FieldGroup}; +use craft_ui::layout_components::{Row, Stack}; +use craft_ui::pickers::{Select, UnitSelect}; +use craft_ui::runtime::env::UnitKind; +use craft_ui::testing::{Fixture, trial}; +use craft_ui::text::{NumberField, link_toggle}; +use egui::{Rect, Shape, Vec2}; + +/// How far two edges may differ and still line up, in points. +const SLACK: f32 = 0.5; + +fn control(fixture: &Fixture, name: &str) -> Rect { + fixture.by_label(name).unwrap_or_else(|| panic!("{name:?} names its control")).rect +} + +/// The horizontal line segments of one frame, as (y, from x, to x). +fn ticks(fixture: &mut Fixture) -> Vec<(f32, f32, f32)> { + fixture + .frame_shapes() + .into_iter() + .filter_map(|shape| match shape { + Shape::LineSegment { points: [a, b], .. } if (a.y - b.y).abs() < SLACK => Some((a.y, a.x.min(b.x), a.x.max(b.x))), + _ => None, + }) + .collect() +} + +/// A form with Width and Height (unit selects) stacked in a row with `bracket`, beside their link. +fn linked(bracket: Bracket) -> Fixture { + let units = [Entry::new(UnitKind::Pixels, "Pixels"), Entry::new(UnitKind::Percent, "Percent")]; + Fixture::new(move |ui| { + Row::new().show(ui, |ui, _| { + FieldGroup::new("Image size").show(ui, |ui, _| { + Row::new().bracket(bracket).show(ui, |ui, _| { + Stack::new().show(ui, |ui, _| { + for name in ["Width", "Height"] { + Field::new(name).show(ui, |ui, _| { + UnitSelect::new(name, Bind::given(Some(8.0)), Bind::given(UnitKind::Pixels), &units) + .unit_label(format!("{name} unit")) + .show(ui); + }); + } + }); + link_toggle("Link", Bind::given(bracket == Bracket::Linked)).show(ui); + }); + }); + }); + }) +} + +/// A linked bracket runs from the end of Width's and Height's controls to the chain's centre line, at each control's +/// centre, and the chain sits halfway between the two; unlinked, the chain keeps its place and no bracket is drawn. +/// At 1x and 1.25x. +#[trial] +fn a_bracketed_row_centres_its_chain_between_the_rows_and_draws_the_bracket_while_linked() { + for scale in [1.0, 1.25] { + for bracket in [Bracket::Linked, Bracket::Unlinked] { + let mut fixture = linked(bracket); + fixture.room(Vec2::new(1000.0, 600.0)).pixels_per_point(scale); + fixture.settle(); + let (width, height, link) = (control(&fixture, "Width unit"), control(&fixture, "Height unit"), control(&fixture, "Link")); + let middle = (width.center().y + height.center().y) / 2.0; + assert!((link.center().y - middle).abs() <= SLACK, "{bracket:?} at {scale}x: the chain sits halfway between the rows: {link:?} {middle}"); + let drawn = ticks(&mut fixture); + for (name, row) in [("Width", width), ("Height", height)] { + let tick = drawn.iter().find(|(y, from, to)| (y - row.center().y).abs() <= 1.0 && *from >= row.max.x && *to >= link.center().x - 1.0); + match bracket { + Bracket::Linked => { + assert!(tick.is_some(), "at {scale}x a tick runs from {name}'s control to the chain: {drawn:?} {row:?} {link:?}"); + } + _ => assert!(tick.is_none(), "unlinked at {scale}x, no tick on {name}'s row: {tick:?}"), + } + } + } + } +} + +/// A check box that starts a row of a form (Resample with its method after it) starts in the form's control column; +/// one after another child of the row stays where the row puts it. +#[trial] +fn a_check_box_that_starts_a_row_of_a_form_starts_in_the_control_column() { + let methods = [Entry::new(0_u8, "Automatic"), Entry::new(1_u8, "Bicubic")]; + let mut fixture = Fixture::new(move |ui| { + Row::new().show(ui, |ui, _| { + FieldGroup::new("Size").show(ui, |ui, _| { + Field::new("Resolution").show(ui, |ui, _| { + NumberField::new("Resolution", Bind::given(Some(72.0))).show(ui); + }); + Row::new().show(ui, |ui, _| { + Checkbox::new(Bind::given(true)).label("Resample").show(ui); + Select::new("Method", Bind::given(0_u8), &methods).show(ui); + }); + Row::new().show(ui, |ui, _| { + Text::new("Then").show(ui); + Checkbox::new(Bind::given(false)).label("Second").show(ui); + }); + }); + }); + }); + fixture.room(Vec2::new(1000.0, 600.0)); + fixture.settle(); + let (field, check, method) = (control(&fixture, "Resolution"), control(&fixture, "Resample"), control(&fixture, "Method")); + assert!((check.min.x - field.min.x).abs() <= SLACK, "the check box starts at the form's control edge: {check:?} {field:?}"); + assert!( + method.min.x >= check.max.x - SLACK && method.min.y < check.max.y && method.max.y > check.min.y, + "the method follows on its line: {method:?} {check:?}" + ); + let second = control(&fixture, "Second"); + assert!(second.min.x < field.min.x, "a check box after another child of the row is not moved to the control column: {second:?} {field:?}"); +} diff --git a/crates/ui-widgets/tests/it/label/mod.rs b/crates/ui-widgets/tests/it/label/mod.rs index 6facfa093..00b7f7256 100644 --- a/crates/ui-widgets/tests/it/label/mod.rs +++ b/crates/ui-widgets/tests/it/label/mod.rs @@ -2,6 +2,7 @@ pub(crate) mod align; pub(crate) mod baseline; +pub(crate) mod bracket; pub(crate) mod caption; pub mod group; pub(crate) mod hit;