Merge branch 'red/look-chrome' into area/ribbon-fw

This commit is contained in:
Martin Ball
2026-11-19 13:38:02 +00:00
8 changed files with 147 additions and 7 deletions
+3 -2
View File
@@ -177,8 +177,9 @@ impl Component for VerticalRuler<'_> {
VerticalRulerStyle {
metrics: Metrics::new(),
focus: cx.focus_look(),
fill: cx.color(colors.surface.chrome),
margin: cx.color(colors.surface.sunken),
// As `TextRuler`'s: the text range is the light well, the margins are shaded as the desk round the page.
fill: cx.color(colors.surface.sunken),
margin: cx.color(colors.surface.canvas),
tick: cx.color(colors.text.muted),
marker: cx.color(colors.text.normal),
label: TextLook::new(TypeRole::Caption, craft_ui::style::Stateful::new(cx.color(colors.text.muted))),
+4 -2
View File
@@ -491,8 +491,10 @@ impl Component for TextRuler<'_> {
TextRulerStyle {
metrics: Metrics::new(),
focus: cx.focus_look(),
fill: cx.color(colors.surface.chrome),
margin: cx.color(colors.surface.sunken),
// The column is the light well the text goes in; the margins are shaded as the desk round the page, so the
// column reads lighter than the margins in both modes.
fill: cx.color(colors.surface.sunken),
margin: cx.color(colors.surface.canvas),
tick: cx.color(colors.text.muted),
marker_fill: cx.color(colors.surface.raised),
marker_off: cx.color(colors.text.faint),
+9 -1
View File
@@ -118,7 +118,15 @@ impl TextRuler<'_> {
f64::from(g.scale),
(f64::from(lo) * per_doc)..=(f64::from(hi) * per_doc),
);
let marks: Vec<Tick> = ticks.iter().map(|tick| Tick::new(g.x((tick.value / per_doc) as f32) - g.strip.min.x, tick.rank)).collect();
// The tick rule adds a labelled step beyond each end; a ruler cut to its extent (a page's) shows none past it.
let covered = g.visible().x_range();
let marks: Vec<Tick> = ticks
.iter()
.map(|tick| g.x((tick.value / per_doc) as f32))
.zip(&ticks)
.filter(|(x, _)| covered.contains(*x))
.map(|(x, tick)| Tick::new(x - g.strip.min.x, tick.rank))
.collect();
ui.content_painter(cx).ticks(g.strip, TickEdge::Bottom, &marks);
let draw = ui.draw();
let color: StyleColor = cx.resolve(&style.label.color);
+78
View File
@@ -195,6 +195,84 @@ fn ruler_numbers_skip_margins_and_markers_thin_out_mirror_for_rtl_and_keep_clear
}
}
/// A ruler cut to an extent (a page's x range) paints its ticks over that extent only: the tick rule's labelled step
/// beyond each end stays off the strip, as nothing of the ruler shows past the page.
#[trial]
fn a_ruler_cut_to_an_extent_paints_no_tick_past_it() {
const FROM: f32 = 100.0;
const TO: f32 = 300.0;
let values = Rc::new(RefCell::new(RulerValues::default()));
let strip_at: Rc<RefCell<Option<egui::Rect>>> = Rc::new(RefCell::new(None));
let seen = strip_at.clone();
let mut fixture = Fixture::new(move |ui| {
let stage = craft_ui::testing::stage(ui);
let (strip, _) = Rulers::strips(ui, stage);
*seen.borrow_mut() = Some(strip);
TextRuler::new("Ruler", Bind::bound(&mut *values.borrow_mut()), 140.0)
.unit(DocUnit::new(UnitKind::Inches, 96.0))
.origin(strip.min.x + 130.0)
.extent(egui::Rangef::new(strip.min.x + FROM, strip.min.x + TO))
.scale(1.0)
.show_at(ui, strip);
});
fixture.settle();
let shapes = fixture.frame_shapes();
let strip = strip_at.borrow().expect("the ruler showed");
let (from, to) = (strip.min.x + FROM - 0.5, strip.min.x + TO + 0.5);
let ticks: Vec<egui::Rect> = shapes
.iter()
.map(egui::Shape::visual_bounding_rect)
.filter(|rect| rect.is_finite() && rect.width() <= 2.0 && rect.height() > 0.0)
.filter(|rect| rect.min.y >= strip.min.y - 0.5 && rect.max.y <= strip.max.y + 0.5)
.collect();
assert!(ticks.iter().any(|rect| rect.center().x > from && rect.center().x < to), "the ruler paints ticks over its extent");
let past: Vec<&egui::Rect> = ticks.iter().filter(|rect| rect.center().x < from || rect.center().x > to).collect();
assert!(past.is_empty(), "no tick past the extent {from}..{to}: {past:?}");
}
/// The ruler's column (the text's width) is lighter than its margins, as a word processor's ruler reads: the column
/// takes the light well colour and the margins the shade of the desk round the page.
#[trial]
fn the_ruler_column_is_lighter_than_its_margins() {
const FROM: f32 = 100.0;
const TO: f32 = 300.0;
const ORIGIN: f32 = 130.0;
const WIDTH: f32 = 140.0;
let values = Rc::new(RefCell::new(RulerValues::default()));
let strip_at: Rc<RefCell<Option<egui::Rect>>> = Rc::new(RefCell::new(None));
let seen = strip_at.clone();
let mut fixture = Fixture::new(move |ui| {
let stage = craft_ui::testing::stage(ui);
let (strip, _) = Rulers::strips(ui, stage);
*seen.borrow_mut() = Some(strip);
TextRuler::new("Ruler", Bind::bound(&mut *values.borrow_mut()), WIDTH)
.origin(strip.min.x + ORIGIN)
.extent(egui::Rangef::new(strip.min.x + FROM, strip.min.x + TO))
.scale(1.0)
.show_at(ui, strip);
});
fixture.settle();
let shapes = fixture.frame_shapes();
let strip = strip_at.borrow().expect("the ruler showed");
// The fill of the filled rect that spans the strip's depth over exactly `from..to` (strip x).
let fill_over = |from: f32, to: f32| {
shapes.iter().find_map(|shape| match shape {
egui::Shape::Rect(r)
if (r.rect.min.x - (strip.min.x + from)).abs() < 0.6
&& (r.rect.max.x - (strip.min.x + to)).abs() < 0.6
&& (r.rect.height() - strip.height()).abs() < 0.6 =>
{
Some(r.fill)
}
_ => None,
})
};
let luma = |c: egui::Color32| 0.2126 * f32::from(c.r()) + 0.7152 * f32::from(c.g()) + 0.0722 * f32::from(c.b());
let column = fill_over(ORIGIN, ORIGIN + WIDTH).expect("the column is filled");
let margins = fill_over(FROM, TO).expect("the ruler's extent is filled with the margin shade");
assert!(luma(column) > luma(margins), "the column {column:?} is lighter than the margins {margins:?}");
}
/// Only the primary button starts a stop drag; Escape cancels it; a release well away from the strip clears the stop and
/// one near the strip keeps it.
#[trial]
+2
View File
@@ -66,6 +66,7 @@
"accent.soft": "#dde3f8",
"accent.solid": "#3b5bdb",
"accent.text": "#2b47b5",
"surface.canvas": "#e6e6e6",
"surface.chrome": "#f0f0f0",
"text.on_accent": "#ffffff"
},
@@ -73,6 +74,7 @@
"accent.soft": "#2e3a66",
"accent.solid": "#7a93f0",
"accent.text": "#9cb0f5",
"surface.canvas": "#141414",
"surface.chrome": "#1f1f1f",
"text.on_accent": "#11162e"
}
+2 -2
View File
@@ -73,12 +73,12 @@ impl<'a> StripEnd<'a> {
self.command_ref().is_none_or(|command| cx.resolve_command(command.clone()).is_none_or(|resolved| resolved.enabled))
}
/// The face with the label beside the icon.
/// The face with the label beside the icon; a menu item's ends with the chevron of a button that opens a menu.
fn full_face(&self) -> Button<'a> {
let face = Button::new(self.label.clone()).icon(self.icon.clone()).variant(self.variant);
match self.command_ref() {
Some(command) => face.command(command.clone()).label(self.label.clone()),
None => face,
None => face.trailing_icon(crate::controls::disclosure::DisclosureGlyph::menu_button()),
}
}
@@ -174,6 +174,18 @@ fn tab_strip_ribbon_end_items_narrow(ui: &mut Ui, data: &mut StoryData) -> Story
})
}
/// End items as framed buttons: two outlined, the menu one with its chevron, and the last filled with the accent.
#[story(TabStrip<'static, usize>, covers = [], drive = Drive::Never)]
fn tab_strip_ribbon_end_buttons(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
let rows = comment_rows();
let items = [
StripEnd::command("Comments", IconId::new("list").into(), EditCommands::Paste).variant(craft_theme::Variant::SECONDARY),
StripEnd::menu("Editing", IconId::new("list").into(), &rows).variant(craft_theme::Variant::SECONDARY),
StripEnd::command("Share", IconId::new("link").into(), EditCommands::Copy).variant(craft_theme::Variant::PRIMARY),
];
ribbon(ui, data, TabStrip::new("Ribbon").trailing(&items), &PLAIN)
}
/// The contextual tab is listed for the first frames and then dropped by the source: it fades out at its slot. The
/// story reads the frame it is in rather than counting its runs, so a measuring run or a second pass of one frame
/// lists the same tabs as the frame's other runs.
+37
View File
@@ -537,6 +537,43 @@ fn end_items_drop_their_labels_before_a_tab_overflows() {
assert!(kept, "a room keeps the contextual tab while a plain tab overflows");
}
/// The width of the one end item, named "Comments", of a ribbon strip in a wide room: a menu item when `menu`, else a
/// command item with the same label and icon.
fn comments_width(menu: bool) -> f32 {
use craft_behavior::IconId;
use craft_ui::contract::Entry;
use craft_ui::runtime::commands::{CommandRef, EditCommands};
use craft_ui::tabs::StripEnd;
let rows: [Entry<CommandRef>; 1] = [Entry::new(EditCommands::Paste.into(), "Show comments")];
let item = if menu {
StripEnd::menu("Comments", IconId::new("list").into(), &rows)
} else {
StripEnd::command("Comments", IconId::new("list").into(), EditCommands::Copy)
};
let items = [item];
let mut frames = Frames::new();
let mut show = |ui: &mut Ui| {
ui.set_max_width(2000.0);
let keys: Vec<usize> = (0..RIBBON.len()).collect();
let source = keyed(&keys, |key: &usize| *key);
let strip = TabStrip::new("Ribbon").kind(TabKind::Ribbon).shown(1).roles(ribbon_role).trailing(&items);
strip.show_items(ui, &source, &mut |key, _| Tab::new(RIBBON[*key]).role(ribbon_role(key)));
};
frames.idle(3, &mut show);
let tree = frames.tree();
let buttons = tree.with_role(Role::BUTTON);
buttons.iter().find(|key| tree.name_of(**key) == Some("Comments")).and_then(|key| tree.bounds_of(*key)).map_or(0.0, |rect| rect.width())
}
/// An end item that opens a menu shows the chevron of a menu button after its label, as every button that opens a
/// menu does; an item that runs a command shows none, so the menu item is wider by the chevron.
#[trial]
fn a_menu_end_item_shows_the_menu_chevron() {
let (menu, command) = (comments_width(true), comments_width(false));
assert!(command > 0.0, "the command item shows");
assert!(menu > command + 4.0, "the menu item is wider by its chevron: {menu} against {command}");
}
/// A contextual tab the source drops fades out at its slot: while it fades it has no node, a click on its slot
/// selects nothing and the overflow menu does not list it; once the fade ends it is not painted. A frame of two
/// passes paints the fade as one does. A contextual tab that comes back is hit from its first frame.