mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
ui-contract, title bar: one way for an end group whose ranked members leave first: TitleBar::show_fitted turns on its end region's fit, as Bar::trailing does, and layout::end_group goes; the fit's env fact carries the body's shortfall, which the region's first row gives up off its ideal width
This commit is contained in:
@@ -128,8 +128,8 @@ pub(super) struct BodyHooks<'b> {
|
||||
/// found it has to run again at (`ColumnGroup::scope_measured`).
|
||||
settle: Option<crate::layout::Columns>,
|
||||
unsettled: Option<crate::layout::Columns>,
|
||||
/// The room the ranked units of an end region that joins the body's fit (`EndRegion::fit`) fit into in the
|
||||
/// measuring run's second sweep, and the room a first sweep found they have to fit into.
|
||||
/// How far the ranked units of an end region that joins the body's fit (`EndRegion::fit`) give way in the
|
||||
/// measuring run's second sweep, and the body's shortfall a first sweep found.
|
||||
end_fit: Option<f32>,
|
||||
end_unfit: Option<f32>,
|
||||
extent: Option<Vec2>,
|
||||
|
||||
@@ -634,9 +634,9 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
layout::ask_shortfall(&plan.ctx, plan.layout_id);
|
||||
}
|
||||
self.body_step(comp, ui, env, cx.core, &mut plan);
|
||||
// The end region's ranked units take the body's shortfall off what the region took.
|
||||
if fits_end && let (Some(short), Some(end)) = (layout::take_shortfall(&plan.ctx, plan.layout_id), self.end_size) {
|
||||
self.end_unfit = Some(end.x - short);
|
||||
// The end region's ranked units give up the body's shortfall.
|
||||
if fits_end && let Some(short) = layout::take_shortfall(&plan.ctx, plan.layout_id) {
|
||||
self.end_unfit = Some(short);
|
||||
}
|
||||
self.after_body(ui, env, &mut plan);
|
||||
if plan.real && props.end.is_some() {
|
||||
@@ -839,8 +839,8 @@ impl BodyHooks<'_> {
|
||||
|
||||
/// An end region that joins the body's fit (`EndRegion::fit`) gives way before the body: when the body's units ask
|
||||
/// for more than the line the region leaves, the sweep is taken back and the content runs once more with the
|
||||
/// region's ranked units fitted into what it took less the body's shortfall, so they leave before a body unit
|
||||
/// narrows or leaves.
|
||||
/// region's first row giving up the body's shortfall by its ranked units, so they leave before a body unit narrows
|
||||
/// or leaves.
|
||||
#[inline(never)]
|
||||
fn fitting_sweeps<C: Container>(
|
||||
&mut self,
|
||||
@@ -859,8 +859,8 @@ impl BodyHooks<'_> {
|
||||
},
|
||||
Option::is_none,
|
||||
);
|
||||
if let Some(room) = unfit {
|
||||
self.end_fit = Some(room);
|
||||
if let Some(short) = unfit {
|
||||
self.end_fit = Some(short);
|
||||
self.sweep(comp, at, cx, (layer, rect), values);
|
||||
(self.end_fit, self.end_unfit) = (None, None);
|
||||
}
|
||||
@@ -1330,7 +1330,7 @@ impl BodyHooks<'_> {
|
||||
..line
|
||||
});
|
||||
}
|
||||
let fitted = self.end_fit.filter(|_| !plan.real).map(|room| plan.line_env.derive_local(|scoped| scoped.rank_room = Some(room)));
|
||||
let fitted = self.end_fit.filter(|_| !plan.real).map(|short| plan.line_env.derive_local(|scoped| scoped.rank_short = Some(short)));
|
||||
let frame = RegionFrame { id: plan.id, rtl: plan.rtl, node: plan.node, env: fitted.as_ref().unwrap_or(&plan.line_env), gap: plan.gap };
|
||||
let closed = self.end_region(comp, ui, &mut end_cx, &frame, props, end, line);
|
||||
plan.close = plan.close.or(closed);
|
||||
|
||||
@@ -57,7 +57,7 @@ pub(crate) struct EnvFacts {
|
||||
given_column: bool,
|
||||
unbounded: (bool, bool),
|
||||
row_room: Option<f32>,
|
||||
rank_room: Option<f32>,
|
||||
rank_short: Option<f32>,
|
||||
row_line: Option<crate::runtime::env::RowLine>,
|
||||
offered: Option<f32>,
|
||||
measure: Option<f32>,
|
||||
@@ -78,7 +78,7 @@ impl EnvFacts {
|
||||
given_column: env.given_column,
|
||||
unbounded: env.unbounded,
|
||||
row_room: env.row_room,
|
||||
rank_room: env.rank_room,
|
||||
rank_short: env.rank_short,
|
||||
row_line: env.row_line,
|
||||
offered: env.offered,
|
||||
measure: env.measure,
|
||||
|
||||
@@ -695,41 +695,6 @@ 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)))
|
||||
}
|
||||
|
||||
/// Shows `show`, the end group of the unwrapped `Row` body around it, at the row's end edge: the room the row's members
|
||||
/// leave lies between the content before the group and the group. Show it last in the row.
|
||||
///
|
||||
/// The group's children are members of the row as any other child is: they sit on its line and are units of its shrink
|
||||
/// step, so those that carry a rank (`ContainerProps::fit_rank`) leave whole, the lowest rank first, before the row's
|
||||
/// other units narrow or drop, and the room they leave goes to the free room. The group is placed from the width it
|
||||
/// took in this pass's measuring run; without one, and outside an unwrapped row, it follows the content before it.
|
||||
pub fn end_group<R>(ui: &mut Ui, show: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let env = Env::of(ui);
|
||||
let Some(row) = env.flow_body.filter(|_| env.row_room.is_some() && env.row_line.is_some()) else { return show(ui) };
|
||||
let (ctx, rtl, key) = (ui.ctx().clone(), env.direction.is_rtl(), sub(row, "end_group"));
|
||||
let sizing = measure::active(&ctx);
|
||||
let room = ui.available_rect_before_wrap().width();
|
||||
if let Some(took) = this_pass(&ctx, sizing, key).filter(|took| took.x > 0.0 && room.is_finite()) {
|
||||
ui.add_space((room - took.x).max(0.0));
|
||||
}
|
||||
// The cursor's edge, which stays finite in a room unbounded at its end.
|
||||
let cursor = |ui: &Ui| {
|
||||
let rest = ui.available_rect_before_wrap();
|
||||
if rtl { rest.max.x } else { rest.min.x }
|
||||
};
|
||||
let start = cursor(ui);
|
||||
// In the row's counting sweep each member measures as in no room: one that the content before it crowds still
|
||||
// reports its whole width, and leaves by its rank instead of narrowing.
|
||||
let inner = if matches!(env.shrink, Some(Shrink::Count(_))) {
|
||||
Env::run_in(ui, env.derive_local(|scoped| scoped.unbounded.0 = true), show)
|
||||
} else {
|
||||
show(ui)
|
||||
};
|
||||
// The cursor stands a gap past the group's last child.
|
||||
let took = ((cursor(ui) - start).abs() - ui.spacing().item_spacing.x).max(0.0);
|
||||
found(&ctx, sizing, key, Vec2::new(room, 0.0), Vec2::new(took, 0.0));
|
||||
inner
|
||||
}
|
||||
|
||||
/// 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| {
|
||||
@@ -924,10 +889,10 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
let layout = *ui.layout();
|
||||
let mut env = Env::of(ui);
|
||||
let inherited = (env.unbounded.0, env.least);
|
||||
let rank_room = env.rank_room.unwrap_or(f32::INFINITY);
|
||||
let rank_short = env.rank_short;
|
||||
let mut sweep = |ui: &mut Ui, shrink: Shrink, body: &mut dyn FnMut(&mut Ui) -> R| {
|
||||
let scoped = env.local_mut();
|
||||
scoped.rank_room = None;
|
||||
scoped.rank_short = None;
|
||||
scoped.shrink = Some(shrink);
|
||||
let least = matches!(shrink, Shrink::Least(_));
|
||||
scoped.unbounded.0 = inherited.0 || least;
|
||||
@@ -960,7 +925,10 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
|
||||
};
|
||||
let width_at = |leaf: &Reported, level: usize| leaf.levels.get(level).copied().unwrap_or_default();
|
||||
let held: f32 = levelled.iter().zip(&levels).map(|(leaf, level)| width_at(leaf, *level)).sum();
|
||||
// A row offered no bound (a content-sized row's measuring run) still fits its ranked units into its rank room.
|
||||
// The ranked units give up `rank_short` of the row's ideal width; a row offered no bound (a content-sized row's
|
||||
// measuring run) still drops them for it.
|
||||
let ideal = fixed + held + units.iter().map(|unit| unit.ideal).sum::<f32>();
|
||||
let rank_room = rank_short.map_or(f32::INFINITY, |short| ideal - short);
|
||||
let widths = if room.is_finite() || rank_room.is_finite() {
|
||||
shrink_or_drop(&units, &whole, (&first, &ranks), (fixed + held, gap), (room, rank_room), order, drops)
|
||||
} else {
|
||||
|
||||
@@ -7,7 +7,7 @@ mod label;
|
||||
mod slot;
|
||||
|
||||
pub use arrange::{Placed, arrange};
|
||||
pub use body::{BodyLayout, CellAlign, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, end_group, levelled_unit, plan_levels, shrink_widths};
|
||||
pub use body::{BodyLayout, CellAlign, GridFit, RowAlign, ShrinkOrder, ShrinkUnit, empty_cell, levelled_unit, plan_levels, shrink_widths};
|
||||
pub use column_group::ColumnGroup;
|
||||
#[doc(hidden)]
|
||||
pub use column_group::ambient_width;
|
||||
|
||||
@@ -153,9 +153,10 @@ pub struct Scoped {
|
||||
/// The width a `Row` body was offered this pass, before its `End` or `Center` lead narrowed the block: the room
|
||||
/// its direct children share. Only the row's own children read it.
|
||||
pub(crate) row_room: Option<f32>,
|
||||
/// The room the ranked units of the first `Row` body below fit into before its other drops, when less than the
|
||||
/// row's room: an end region that joins its body's fit (`EndRegion::fit`). That row's children do not read it.
|
||||
pub(crate) rank_room: Option<f32>,
|
||||
/// How far the ranked units of the first `Row` body below give way before its other drops: the row drops them
|
||||
/// until the kept units fit in its ideal width less this, an end region that joins its body's fit
|
||||
/// (`EndRegion::fit`) giving up the body's shortfall. That row's children do not read it.
|
||||
pub(crate) rank_short: Option<f32>,
|
||||
/// The line of the unwrapped `Row` body whose direct children the scope places in flow (`RowLine`).
|
||||
pub(crate) row_line: Option<RowLine>,
|
||||
/// Where the first child the scope places in flow reports its first line: a container's header or body
|
||||
@@ -695,7 +696,7 @@ impl Env {
|
||||
given_column: false,
|
||||
content: (false, false),
|
||||
row_room: None,
|
||||
rank_room: None,
|
||||
rank_short: None,
|
||||
row_line: None,
|
||||
first_line: None,
|
||||
flow_body: None,
|
||||
|
||||
@@ -10,7 +10,8 @@ use crate::contract::{
|
||||
Access, BodyCx, BodyLayout, Common, Component, Container, ContainerProps, Cx, DragBand, EndRegion, Gap, HeaderRegion, NoValue, Outcome, Pad,
|
||||
PartsCx, Role, ShowContainer as _, Sizing, WordSetCx as _,
|
||||
};
|
||||
use crate::layout::{Placed, arrange, end_group};
|
||||
use crate::layout::{Placed, arrange};
|
||||
use crate::layout_components::Row;
|
||||
use crate::runtime::access::Name;
|
||||
use crate::runtime::egui::pass;
|
||||
use crate::runtime::env::Env;
|
||||
@@ -70,8 +71,8 @@ impl Outcome for TitleBarOutput {
|
||||
/// In an embedded shell (an app preview, a story) the bar is a picture of a window: it acts on no window and reads
|
||||
/// none. The captions show hover and report their clicks, but send no window command, and the bar has no move band. The body is the leading content; the
|
||||
/// trailing content ([`trailing`](TitleBar::trailing)) sits before the end's captions. [`show_titled`](TitleBar::show_titled)
|
||||
/// adds the title in the gap between them; [`show_fitted`](TitleBar::show_fitted) puts the trailing content on the
|
||||
/// body's line, where its ranked members leave before the leading content narrows. Close never closes the window; see
|
||||
/// adds the title in the gap between them; [`show_fitted`](TitleBar::show_fitted) fits the trailing content after the
|
||||
/// leading content: its ranked members leave before the leading content narrows. Close never closes the window; see
|
||||
/// [`TitleBarOutput`].
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(container)]
|
||||
@@ -251,21 +252,22 @@ impl<'a> TitleBar<'a> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Shows the bar with `leading` content at its start and `trailing` content in its place of
|
||||
/// [`trailing`](TitleBar::trailing), before the end's captions, both on the body's line: the trailing content is
|
||||
/// the body's end group (`layout::end_group`). It takes the room the leading content's natural width leaves, so on
|
||||
/// a short bar its members that carry a `fit_rank` leave whole, lowest rank first, before the leading content
|
||||
/// narrows: an app bar whose menus stay whole while its end controls go.
|
||||
/// Shows the bar with `leading` content at its start and `trailing` content before the end's captions, where the
|
||||
/// end region joins the body's fit (`EndRegion::fit`): on a short bar the trailing members that carry a `fit_rank`
|
||||
/// leave whole, lowest rank first, before the leading content narrows: an app bar whose menus stay whole while its
|
||||
/// end controls go.
|
||||
pub fn show_fitted(
|
||||
self,
|
||||
mut self,
|
||||
ui: &mut Ui,
|
||||
mut leading: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a,
|
||||
leading: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a,
|
||||
mut trailing: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a,
|
||||
) -> TitleBarOutput {
|
||||
self.show(ui, |ui, cx| {
|
||||
leading(ui, cx);
|
||||
end_group(ui, |ui| trailing(ui, cx));
|
||||
self.props = self.props.end(EndRegion::new().fit(true));
|
||||
// The ranked members fit as units of the first row in the end region.
|
||||
self.trailing(move |ui: &mut Ui, cx: &mut BodyCx<'_>| {
|
||||
let _ = Row::new().show(ui, |ui, _| trailing(ui, cx));
|
||||
})
|
||||
.show(ui, leading)
|
||||
.output
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user