diff --git a/crates/ui-contract/src/contract/body/hooks.rs b/crates/ui-contract/src/contract/body/hooks.rs index fbb2783e3..a92e7820e 100644 --- a/crates/ui-contract/src/contract/body/hooks.rs +++ b/crates/ui-contract/src/contract/body/hooks.rs @@ -165,8 +165,9 @@ impl ShapeHooks for BodyHooks<'_> { self.region_gap = gap_of(&env.tokens, Gap::Tight); self.end_gap = gap_of(&env.tokens, props.end.map_or(Gap::Tight, |end| end.gap)); self.header_gap = header_gap_of(props, &env.tokens); - self.after_rule = layout::flow_start(&ctx, env) == Some(true); - self.rules = RuleBands::of(props, &env.tokens, crate::runtime::egui::scale::pixels_per_point(&ctx), self.after_rule); + let start = layout::flow_start(&ctx, env); + self.after_rule = start == Some(true); + self.rules = RuleBands::of(props, env, crate::runtime::egui::scale::pixels_per_point(&ctx), start); self.least_width = least_of(&env.tokens, props.least_width); let quiet = stage.is_peek() || crate::runtime::measure::active(&ctx); if !matches!(props.mount, Mount::Inline | Mount::Pane(_)) @@ -1900,8 +1901,9 @@ impl BodyHooks<'_> { self.region_gap = gap_of(&at.env.tokens, Gap::Tight); self.end_gap = gap_of(&at.env.tokens, props.end.map_or(Gap::Tight, |end| end.gap)); self.header_gap = header_gap_of(&props, &at.env.tokens); - self.after_rule = layout::flow_start(at.ui.ctx(), at.env) == Some(true); - self.rules = RuleBands::of(&props, &at.env.tokens, crate::runtime::egui::scale::pixels_per_point(at.ui.ctx()), self.after_rule); + let start = layout::flow_start(at.ui.ctx(), at.env); + self.after_rule = start == Some(true); + self.rules = RuleBands::of(&props, at.env, crate::runtime::egui::scale::pixels_per_point(at.ui.ctx()), start); self.least_width = least_of(&at.env.tokens, props.least_width); self.room = match at.width { Some(width) => Vec2::new(width, props.room_height.unwrap_or(f32::INFINITY)), diff --git a/crates/ui-contract/src/contract/body/regions.rs b/crates/ui-contract/src/contract/body/regions.rs index 1d74217e4..a02bea21b 100644 --- a/crates/ui-contract/src/contract/body/regions.rs +++ b/crates/ui-contract/src/contract/body/regions.rs @@ -3,7 +3,7 @@ use super::BodyHooks; use super::scroll::scroll_of; use crate::contract::cx::BodyCx; -use crate::contract::{Container, ContainerProps, FooterPlace, Gap, HeaderParts, LeastWidth, Mount, Pad, PadEdges, Sizing}; +use crate::contract::{Container, ContainerProps, FooterPlace, Gap, HeaderParts, LeastWidth, Mount, Pad, PadEdges, SectionRhythm, Sizing}; use crate::layout::{ColumnGroup, Columns, Reached}; use crate::primitives::{RectSide, Rule}; use crate::runtime::access::{self}; @@ -70,11 +70,16 @@ pub(super) fn pad_vec(tokens: &UiTokens, pad: Pad, edges: PadEdges) -> Vec2 { /// The room the rules between a container's regions take, in points: the divider rule's width on the display for each /// rule the container draws, none for one it doesn't. Each rule lies in a band of its own, so it shares no pixel with -/// a region's content at any scale. +/// a region's content at any scale. Under `SectionRhythm::Spaced` the rule above a header has room around it. #[derive(Clone, Copy, Debug, Default)] pub(super) struct RuleBands { + /// Before the rule above the header: what makes the gap the container's `Stack` leaves before it up to + /// `space.gap.section`. + pub(super) lead: f32, /// Above a header with `HeaderRegion::rule_above`. pub(super) above: f32, + /// Between the rule above and the header: `space.gap.row`. + pub(super) trail: f32, /// Under a stacked header with `HeaderRegion::rule`, before its gap. pub(super) under: f32, /// Between a footer with `FooterRegion::rule` and the content beside it: over the footer, or under it in the end @@ -85,18 +90,31 @@ pub(super) struct RuleBands { } impl RuleBands { - /// The bands of `props`' rules in `tokens` on a display of `pixels_per_point`. A rule above separates the - /// container from what precedes it in flow: right after a rule (`after_rule`) there is none. - pub(super) fn of(props: &ContainerProps, tokens: &UiTokens, pixels_per_point: f32, after_rule: bool) -> Self { + /// The bands of `props`' rules under `env` on a display of `pixels_per_point`, for a container whose flow start is + /// `start` (`layout::flow_start`). A rule above separates the container from what precedes it in flow: right after + /// a rule there is none, and at its flow's start nothing precedes it to stand off from. + pub(super) fn of(props: &ContainerProps, env: &Env, pixels_per_point: f32, start: Option) -> Self { + let tokens = &env.tokens; let width = rule_look(tokens, RuleKind::Divider).width.points(pixels_per_point); let on = |rule: bool| if rule { width } else { 0.0 }; + let above = on(start != Some(true) && props.header.is_some_and(|header| header.rule_above)); + let spaced = above > 0.0 && env.get::() == SectionRhythm::Spaced; + let gaps = &tokens.tokens().space.gap; + let stack_gap = env.stack_gap.filter(|(body, _)| env.flow_body == Some(*body)).map_or(0.0, |(_, gap)| gap); RuleBands { - above: on(!after_rule && props.header.is_some_and(|header| header.rule_above)), + lead: if spaced && start.is_none() { (gaps.section - stack_gap).max(0.0) } else { 0.0 }, + above, + trail: if spaced { gaps.row } else { 0.0 }, under: on(props.header.is_some_and(|header| header.rule && !header.inline)), footer: on(props.footer.is_some_and(|footer| footer.rule)), end: on(props.end.is_some_and(|end| end.rule)), } } + + /// The band of the rule above the header with the room around it. + pub(super) fn above_band(&self) -> f32 { + self.lead + self.above + self.trail + } } /// Whether a ruled footer's rule lies under it: a footer at the top of the end region separates itself from the end @@ -405,7 +423,7 @@ impl<'b> BodyHooks<'b> { let end_h = sizes.end.map_or(0.0, |end| end.y) + if in_end { footer_h } else { 0.0 }; // An inline header takes no height of its own: the line is as high as the taller of header and body. A rule // above the header takes its band either way. - let stacked_h = self.rules.above + if inline { 0.0 } else { header_h + self.header_space(header_h) }; + let stacked_h = self.rules.above_band() + if inline { 0.0 } else { header_h + self.header_space(header_h) }; let top = inner.min.y + stacked_h; // Along an axis the container is not given, its body is offered the room the container was offered and the // container takes what the body took. @@ -457,7 +475,7 @@ impl<'b> BodyHooks<'b> { let width = if rtl && !width.is_finite() { crate::contract::MAX_EXTENT } else { width }; let (left, right) = if rtl { (inner.max.x - width, inner.max.x) } else { (inner.min.x, inner.min.x + width) }; let band = |top: f32, bottom: f32| Rect::from_min_max(Pos2::new(left, top), Pos2::new(right, bottom.max(top))); - let header_top = inner.min.y + self.rules.above; + let header_top = inner.min.y + self.rules.above_band(); let header = band(header_top, if props.header.is_some() { header_top + header_h.max(0.0) } else { header_top }); let mut body = band(top, bottom); let header = if inline { @@ -476,7 +494,7 @@ impl<'b> BodyHooks<'b> { header }; let across = |top: f32, width: f32| (width > 0.0).then(|| (Rect::from_x_y_ranges(inner.x_range(), top..=top + width), RectSide::Top)); - let above = across(inner.min.y, self.rules.above); + let above = across(inner.min.y + self.rules.lead, self.rules.above); let under = if inline { None } else { across(header.max.y, self.rules.under) }; // The end region keeps the width it measured (with a footer in it, the wider of the two) and takes the end // edge; the body takes the rest. Before its first measure it spans the line and the body keeps its width. diff --git a/crates/ui-contract/src/contract/body/size.rs b/crates/ui-contract/src/contract/body/size.rs index fe3c8103f..fb634c843 100644 --- a/crates/ui-contract/src/contract/body/size.rs +++ b/crates/ui-contract/src/contract/body/size.rs @@ -271,7 +271,7 @@ impl<'b> BodyHooks<'b> { } else { ((line.x + gap(end.x, end_gap)).max(across.x), line.y.max(end.y) + across.y) }; - let above = self.rules.above; + let above = self.rules.above_band(); if inline { Vec2::new(width, height + above) } else { diff --git a/crates/ui-contract/src/contract/container.rs b/crates/ui-contract/src/contract/container.rs index 819ff2606..502851b41 100644 --- a/crates/ui-contract/src/contract/container.rs +++ b/crates/ui-contract/src/contract/container.rs @@ -307,6 +307,20 @@ pub enum HeaderEdge { End, } +/// The room around the rule above a header ([`HeaderRegion::rule_above`]), as a `Section` has. An app sets it once at its +/// root (`RootEnv::values`); the default is `Tight`. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, crate::runtime::env::EnvValue, crate::contract::MemoProps)] +#[env(style)] +#[non_exhaustive] +pub enum SectionRhythm { + /// The rule takes the gap its flow leaves before the container, and the header follows it at once. + #[default] + Tight, + /// The rule stands `space.gap.section` below what precedes it in its `Stack`, in place of the stack's gap (no room + /// at its flow's start), and the header `space.gap.row` below it: production VectorCraft's panel sections. + Spaced, +} + /// A header region, filled by `Container::header`. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[non_exhaustive] diff --git a/crates/ui-contract/src/contract/mod.rs b/crates/ui-contract/src/contract/mod.rs index cb309b921..03dd21d44 100644 --- a/crates/ui-contract/src/contract/mod.rs +++ b/crates/ui-contract/src/contract/mod.rs @@ -69,8 +69,8 @@ pub use container::InBar; pub use container::InMenu; pub use container::{ 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, + LeastWidth, Mount, Notch, POPUP_PAD, Pad, PadEdges, PaneEdge, Scroll, ScrollAnchor, ScrollAxes, ScrollIndicator, SectionRhythm, 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/layout/body.rs b/crates/ui-contract/src/layout/body.rs index ab377e08d..5ca08306c 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -1965,6 +1965,7 @@ fn run_stack(ui: &mut Ui, env: &Env, at: &RowIn, body: impl FnMut(&mut Ui) -> let env = env.derive_local(|scoped| { scoped.shrink = Some(Shrink::Fit); scoped.flow_body = Some(at.id); + scoped.stack_gap = Some((at.id, at.gap.y)); }); 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. diff --git a/crates/ui-contract/src/runtime/env.rs b/crates/ui-contract/src/runtime/env.rs index c655fb603..294f035af 100644 --- a/crates/ui-contract/src/runtime/env.rs +++ b/crates/ui-contract/src/runtime/env.rs @@ -165,6 +165,9 @@ pub struct Scoped { /// 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 `Stack` body that places the scope's direct children in flow and the gap it leaves between them; it holds + /// only while that body is `flow_body`. + pub(crate) stack_gap: Option<(Id, f32)>, /// A rule precedes the start of the body this scope opens: its container draws one under its header, or draws /// nothing of its own and itself follows one. The body's layout opens its flow start with it. pub(crate) after_rule: bool, @@ -700,6 +703,7 @@ impl Env { row_line: None, first_line: None, flow_body: None, + stack_gap: None, after_rule: false, column_row: false, bracket: None, diff --git a/crates/ui-panels/src/sections/section/stories.rs b/crates/ui-panels/src/sections/section/stories.rs index 1425532bc..fd33261ef 100644 --- a/crates/ui-panels/src/sections/section/stories.rs +++ b/crates/ui-panels/src/sections/section/stories.rs @@ -1,8 +1,10 @@ //! Stories of `Section`. use super::Section; -use crate::contract::{Role, Show as _, ShowContainer as _}; +use crate::contract::{Props as _, Role, SectionRhythm, Show as _, ShowContainer as _}; use crate::display::Text; +use crate::layout_components::Stack; +use crate::runtime::env::with_env; use crate::sections::sample::body; use crate::sections::{HeaderAction, HeaderStrength}; use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story}; @@ -61,3 +63,26 @@ fn section_with_controls(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { .show(ui, |ui, _| body(ui)); shown.output.into() } + +/// A row and two sections under each `SectionRhythm`: `Tight` (the default) gives each rule the stack's gap and its +/// header follows at once; `Spaced` stands each rule `space.gap.section` off what precedes it and its header +/// `space.gap.row` below it. +#[story(Section<'static>, covers = [], drive = Drive::Never)] +fn section_rhythm(ui: &mut Ui, data: &mut StoryData) -> StoryOutput { + let mut column = |ui: &mut Ui, key: &'static str| { + Stack::new().id_salt(key).show(ui, |ui, _| { + Text::new("Fill").show(ui); + Section::new("Stroke").open(data.bind(key, true)).show(ui, |ui, _| body(ui)); + Section::fixed("Effects").show(ui, |ui, _| body(ui)); + }); + }; + column(ui, "tight"); + with_env( + ui, + |overrides| { + overrides.values.set(SectionRhythm::Spaced); + }, + |ui| column(ui, "spaced"), + ); + StoryOutput::default() +} diff --git a/crates/ui-panels/tests/it/sections/mod.rs b/crates/ui-panels/tests/it/sections/mod.rs index c5c8b214d..9a71809d9 100644 --- a/crates/ui-panels/tests/it/sections/mod.rs +++ b/crates/ui-panels/tests/it/sections/mod.rs @@ -7,4 +7,5 @@ pub(crate) mod footer_row; pub(crate) mod layout; pub(crate) mod panel_header; pub(crate) mod reorderable; +pub(crate) mod rhythm; pub(crate) mod stack; diff --git a/crates/ui-panels/tests/it/sections/rhythm.rs b/crates/ui-panels/tests/it/sections/rhythm.rs new file mode 100644 index 000000000..4fe61c1b7 --- /dev/null +++ b/crates/ui-panels/tests/it/sections/rhythm.rs @@ -0,0 +1,48 @@ +//! `SectionRhythm`: the room around the rule above a section's header. + +use craft_ui::contract::{SectionRhythm, Show as _, ShowContainer as _}; +use craft_ui::display::Text; +use craft_ui::layout_components::Stack; +use craft_ui::runtime::env::{Env, RootEnv}; +use craft_ui::sections::Section; +use craft_ui::testing::{Frames, trial}; +use egui::{Rect, Ui}; + +/// What a stack of a row and a fixed section shows under `rhythm`: the room from the row to the section's rule, the room +/// from the rule to the section's body, and `space.gap.section`. +fn rule_room(rhythm: SectionRhythm) -> (f32, f32, f32) { + let mut frames = Frames::new(); + let mut root = RootEnv::default(); + root.values.set(rhythm); + frames.root(root); + let (mut row, mut body, mut section_gap) = (Rect::NOTHING, Rect::NOTHING, 0.0); + let mut show = |ui: &mut Ui| { + section_gap = Env::of(ui).tokens.space.gap.section; + Stack::new().show(ui, |ui, _| { + row = Text::new("Fill").show(ui).rect; + Section::fixed("Stroke").show(ui, |ui, _| body = Text::new("Width").show(ui).rect); + }); + }; + frames.idle(2, &mut show); + let rule = frames + .frame_shapes(&mut show) + .iter() + .map(egui::Shape::visual_bounding_rect) + .find(|line| { + line.is_finite() && line.height() <= 2.0 && line.width() > body.width() && line.top() >= row.bottom() && line.bottom() <= body.top() + }) + .expect("the section draws its rule between the row and its body"); + (rule.top() - row.bottom(), body.top() - rule.bottom(), section_gap) +} + +/// Regression: a ruled section in a stack took only the stack's gap above its rule and its header right under it, so an +/// app could not keep production's room around its panel dividers. `Spaced` stands the rule `space.gap.section` off the +/// row above and the header further from the rule; `Tight` keeps the stack's rhythm. +#[trial] +fn a_spaced_section_stands_its_rule_off_the_row_above_and_its_header() { + let (tight_above, tight_below, _) = rule_room(SectionRhythm::Tight); + let (spaced_above, spaced_below, section_gap) = rule_room(SectionRhythm::Spaced); + assert!(spaced_above > tight_above, "the spaced rule stands further off the row ({spaced_above}) than the tight one ({tight_above})"); + assert!(spaced_above >= section_gap - 0.5, "the spaced rule stands a section gap ({section_gap}) off the row, not {spaced_above}"); + assert!(spaced_below > tight_below, "the spaced header stands further below its rule ({spaced_below}) than the tight one ({tight_below})"); +}