look-ribbon-4: a ribbon group sizes the spacer under its content and its separator from the content height the last real run kept, so the measuring run and the real run place the same sizes: the group no longer measures one body too tall (the band under the titles goes) and a settled ribbon measures no group again; the flat-button trial checks the outlined toggle by its edge (a toggle's style has one variant, so a ghost toggle draws it too)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Martin Ball
2026-11-19 14:45:54 +00:00
co-authored by Claude Opus 5.5
parent 596a204cc1
commit 3b5496df6b
2 changed files with 49 additions and 14 deletions
+40 -12
View File
@@ -12,7 +12,8 @@ use craft_ui::layout_components::{Row, Stack};
use craft_ui::popover::Popover;
use craft_ui::primitives::IconSpec;
use craft_ui::runtime::env::{Env, Label, with_env};
use craft_ui::runtime::memory::key_salt;
use craft_ui::runtime::measure;
use craft_ui::runtime::memory::{key_salt, with_session};
use craft_ui::style::TypeRole;
use egui::{Id, Pos2, Rect, Ui, Vec2};
@@ -53,29 +54,50 @@ impl<'a> RibbonGroup<'a> {
/// The group's content over its title row, with no gap between them: the title sits directly under the content,
/// `body` under the content's top whatever the content's height, so every group's title shares one line at the foot
/// of the row; a taller content pushes it down. Returns the content's height, which sets `body` for the row's next
/// pass, and the height of the whole group, which its separator takes.
fn open(&self, ui: &mut Ui, add: &mut dyn FnMut(&mut Ui), body: f32, line: f32) -> (f32, f32) {
let mut took = 0.0;
/// of the row; a taller content pushes it down. `kept` is the slot of this group's content height at this level.
///
/// The spacer under the content and the separator's length come from the content height the last real run kept,
/// never from the content's output in this run: a measuring run has no real size for the content yet, so a spacer
/// sized from it there took the whole `body`, the group measured its content's height too tall (the ribbon stood
/// one body too high under its titles), and the real run, placing another spacer, never matched what was measured,
/// so the row measured again on every pass. Both runs of a pass now place the same sizes; a changed content height
/// is kept by the real run and shows in the next pass.
///
/// Returns the content's height as the real run found it (none in a measuring run), which sets `body` for the
/// row's next pass, and the height of the whole group, which its separator takes.
fn open(&self, ui: &mut Ui, add: &mut dyn FnMut(&mut Ui), room: TitleRoom, kept: Id) -> (Option<f32>, f32) {
let ctx = ui.ctx().clone();
let line = if room.line.is_finite() { room.line.max(0.0) } else { 0.0 };
let body = if room.body.is_finite() { room.body.max(0.0) } else { 0.0 };
let held = with_session(&ctx, kept, |kept: &mut ContentHeight| kept.0);
let held = if held.is_finite() { held.max(0.0) } else { 0.0 };
let side = if self.launcher.is_some() { line } else { 0.0 };
let mut took = f32::NAN;
let mut title = Rect::NOTHING;
let side = if self.launcher.is_some() && line.is_finite() { line.max(0.0) } else { 0.0 };
let shown = Stack::new().gap(Gap::None).show(ui, |ui, _| {
took = Row::new().show(ui, |ui, _| add(ui)).output.rect.height();
// Every group places this spacer, an empty one too, so each has the same gaps above its title.
let rest = if (body - took).is_finite() { (body - took).max(0.0) } else { 0.0 };
craft_ui::layout::place_in_flow(ui, Vec2::new(0.0, rest));
craft_ui::layout::place_in_flow(ui, Vec2::new(0.0, (body - held).max(0.0)));
// The title on a line `line` high in every group, so the titles of groups with and without a launcher
// share one line; the row keeps room for the launcher after the title.
title = Row::new()
.gap(Gap::None)
.show(ui, |ui, _| {
craft_ui::layout::place_in_flow(ui, Vec2::new(0.0, line.max(0.0)));
craft_ui::layout::place_in_flow(ui, Vec2::new(0.0, line));
Text::new(self.title.clone()).role(TypeRole::Caption).shade(Shade::Muted).show(ui);
craft_ui::layout::place_in_flow(ui, Vec2::new(side, 0.0));
})
.output
.rect;
});
// Only the real run keeps what the content took; a measuring run's output is no size of it.
let found = (!measure::active(&ctx) && took.is_finite()).then_some(took.max(0.0));
if let Some(found) = found
&& (found - held).abs() >= 0.5
{
with_session(&ctx, kept, |kept: &mut ContentHeight| kept.0 = found);
ctx.request_repaint();
}
let group = shown.output.rect;
// The dialog launcher: a square as high as the title line, at the group's end edge on that line, as the old
// ribbon placed it. It shows at its place, not in the flow, so it never makes its group's title line taller.
@@ -90,7 +112,8 @@ impl<'a> RibbonGroup<'a> {
let name = words.launcher.fill(&[("title", self.title.as_str())]);
IconButton::command(IconId::new("maximize_2"), command.clone()).label(name).show_at(ui, at);
}
(took, group.height())
// What the stack places: the content or the body, whichever is taller, and the title line.
(found, held.max(body) + line)
}
/// One button with the group's icon and title; its popover holds the content at the full level. A press in a
@@ -126,8 +149,8 @@ impl<'a> RibbonGroup<'a> {
self.collapsed(ui, add);
room.body + room.line
} else {
let (took, high) = self.open(ui, add, room.body, room.line);
content = Some(took);
let (took, high) = self.open(ui, add, room, id.with(("content", level)));
content = took;
high
};
Divider::vertical().length(high).show(ui);
@@ -143,6 +166,11 @@ impl<'a> RibbonGroup<'a> {
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(super) struct BodyHeight(pub(super) f32);
/// The height of one open group's content at one level, as the last real run found it: what the spacer under it and
/// the group's separator are sized from in every run of the next pass. Kept in the ribbon's session memory.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct ContentHeight(f32);
/// Where a group's title goes: `body` under the group's top, on a line `line` high.
#[derive(Clone, Copy, Debug)]
struct TitleRoom {
+9 -2
View File
@@ -434,7 +434,7 @@ fn a_ribbons_buttons_are_flat_at_rest_unless_they_ask_for_a_variant() {
let _ = ToggleButton::new("Track Changes", Bind::bound(&mut on)).show(ui);
});
});
let (ghost, wide) = seen(toggle, "Track Changes", |ui| {
let (_, wide) = seen(toggle, "Track Changes", |ui| {
let mut on = false;
let _ = ToggleButton::new("Track Changes", Bind::bound(&mut on)).variant(Variant::GHOST).show(ui);
});
@@ -448,7 +448,14 @@ fn a_ribbons_buttons_are_flat_at_rest_unless_they_ask_for_a_variant() {
let edge = ribbon.border.as_ref().map(|border| border.off.rest);
assert!(edge.is_none() || edge == Some(ribbon.fill.off.rest), "a ribbon toggle shows no edge at rest: {edge:?}");
assert!(ribbon.fill.on.is_some(), "a ribbon toggle shows the on plate while on");
assert_ne!(standard, ghost, "outside a ribbon a toggle keeps its outlined default");
// A toggle's style has one variant (`ToggleButtonStyle` lists only `SECONDARY`), so a ghost toggle outside a ribbon
// draws that same outlined look: the default is checked by its edge, not against the ghost toggle.
let outlined = standard.clone().expect("a toggle has a surface");
let standard_edge = outlined.border.as_ref().map(|border| border.off.rest);
assert!(
standard_edge.is_some_and(|edge| edge != outlined.fill.off.rest),
"outside a ribbon a toggle keeps its outlined default: {standard_edge:?}"
);
assert_ne!(Some(ribbon), standard, "a ribbon toggle is not the outlined default");
assert!(narrow < wide - 0.5, "a ribbon's flat toggle sits close: {narrow} against {wide}");
}