mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 13:57:06 +00:00
look-ribbon-1b: review of the look-ribbon-1 WIP (ribbon place, title line, captioned gallery)
- RibbonGroup::open returns the content height it measured (the WIP left it unreturned). - A ribbon's font box is a short field (size.length.short, FieldStyle::shortest), not the small field length, which at the ribbon's small controls was the width it already had. - Trials: ribbon buttons resolve the ghost look unless they ask for a variant; a ribbon combo box fits its options; a ribbon font box is short and keeps its width; every open group's title sits on one line; a 2:1 captioned gallery names its tiles under wide pictures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
049765a32d
commit
dd7a3fe52c
@@ -588,10 +588,11 @@ impl Component for FontPicker<'_> {
|
||||
type Style = craft_ui::text::FieldStyle;
|
||||
type Output = FontPickerOutput;
|
||||
|
||||
/// A field; in a ribbon (`ControlPlace::Ribbon`) the small field length, as a ribbon's font box is.
|
||||
/// A field; in a ribbon (`ControlPlace::Ribbon`) at its shortest width (`size.length.short`), as a ribbon's font
|
||||
/// box is.
|
||||
fn style(cx: &craft_ui::style::StyleCx<'_>, _: FontPickerStyleKey) -> craft_ui::text::FieldStyle {
|
||||
let field = craft_ui::text::FieldStyle::at(cx);
|
||||
if cx.env::<ControlPlace>() == ControlPlace::Ribbon { field.small_length(cx) } else { field }
|
||||
if cx.env::<ControlPlace>() == ControlPlace::Ribbon { field.shortest() } else { field }
|
||||
}
|
||||
|
||||
/// The wheel steps the family through the list.
|
||||
@@ -600,7 +601,7 @@ impl Component for FontPicker<'_> {
|
||||
}
|
||||
|
||||
/// The width of the longest family names in common use, so the field keeps its size whichever family is picked.
|
||||
/// In a ribbon the field's length alone sets its width (the style's small field length): a longer name scrolls
|
||||
/// In a ribbon the style alone sets its width (`FieldStyle::shortest`): a longer name scrolls
|
||||
/// in the field, as in a word processor's font box.
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let current = cx.value().map_or("", String::as_str);
|
||||
|
||||
@@ -14,7 +14,8 @@ use craft_font_manager::picker::{
|
||||
use craft_font_manager::{Catalog, FontEnv};
|
||||
use craft_ui::contract::tooltip::DELAY;
|
||||
use craft_ui::contract::{Bind, Show as _};
|
||||
use craft_ui::runtime::env::Label;
|
||||
use craft_ui::buttons::ControlPlace;
|
||||
use craft_ui::runtime::env::{Label, with_env};
|
||||
use craft_ui::testing::focus::AccessActionKind;
|
||||
use craft_ui::testing::pointer::moved;
|
||||
use craft_ui::testing::{Fixture, Room, trial};
|
||||
@@ -287,4 +288,37 @@ fn an_action_row_is_reported_not_committed_and_a_missing_family_shows_the_note_n
|
||||
assert!(fx.part_rect(picker, PartKey::new("missing")).is_none(), "an installed family has no mark");
|
||||
}
|
||||
|
||||
/// The width of a plain picker over `family`, in a ribbon group's place or not.
|
||||
fn picker_width(family: &'static str, ribbon: bool) -> f32 {
|
||||
let catalog = catalog_of(&[INTER_REGULAR]);
|
||||
let mut fx = Fixture::new(move |ui| {
|
||||
let mut family = family.to_owned();
|
||||
let mut show = |ui: &mut egui::Ui| {
|
||||
let _ = FontPicker::new("Font family", Bind::bound(&mut family), &catalog).kind(FontPickerKind::Plain).show(ui);
|
||||
};
|
||||
if ribbon {
|
||||
with_env(
|
||||
ui,
|
||||
|overrides| {
|
||||
overrides.values.set(ControlPlace::Ribbon);
|
||||
},
|
||||
show,
|
||||
);
|
||||
} else {
|
||||
show(ui);
|
||||
}
|
||||
});
|
||||
fx.in_room(Room::Window);
|
||||
fx.settle();
|
||||
fx.by_label("Font family").expect("the picker").rect.width()
|
||||
}
|
||||
|
||||
/// In a ribbon the font box is a short field, narrower than in a form, and keeps its width whichever family it shows.
|
||||
#[trial]
|
||||
fn a_ribbons_font_box_is_short_and_keeps_its_width() {
|
||||
let (ribbon, standard) = (picker_width("Inter", true), picker_width("Inter", false));
|
||||
assert!(ribbon < standard - 0.5, "the ribbon's font box is narrower: {ribbon} against {standard}");
|
||||
let long = picker_width("Source Sans Pro Semibold Condensed Extra", true);
|
||||
assert!((long - ribbon).abs() < 0.5, "a long family name leaves its width: {long} against {ribbon}");
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ impl<'a> RibbonGroup<'a> {
|
||||
}
|
||||
});
|
||||
});
|
||||
took
|
||||
}
|
||||
|
||||
/// One button with the group's icon and title; its popover holds the content at the full level. A press in a
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//! The ribbon: the fit rule, keyed group levels, a collapsed group's popover, the tab-change slide, the run model and
|
||||
//! the ribbon's own context menu.
|
||||
//! The ribbon: the fit rule, keyed group levels, a collapsed group's popover, the tab-change slide, the run model, the
|
||||
//! line group titles share and the ribbon's own context menu.
|
||||
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::collections::HashMap;
|
||||
@@ -10,7 +10,8 @@ use craft_behavior::{IconId, MotionMode};
|
||||
use craft_keymap::CommandRef;
|
||||
use craft_menu::{GroupLevel, GroupWidths, Ribbon, RibbonGroup, RibbonMenuRequest, RibbonRow, RibbonTab, plan};
|
||||
use craft_ui::buttons::{Arrangement, Button, SplitButton};
|
||||
use craft_ui::contract::{Bind, BodyCx, Entry, Props as _, Show as _};
|
||||
use craft_ui::contract::{Bind, BodyCx, Entry, Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::layout_components::Stack;
|
||||
use craft_ui::menu::MenuItem;
|
||||
use craft_ui::runtime::env::{Direction, Env};
|
||||
use craft_ui::runtime::inspect::{Element, Recorded};
|
||||
@@ -302,6 +303,34 @@ fn a_settled_ribbon_runs_each_group_body_once() {
|
||||
assert!(last_pass_measuring_runs(fixture.ctx()).is_empty(), "a pass with unchanged inputs runs no measuring run");
|
||||
}
|
||||
|
||||
/// Every open group's title sits on one line at the foot of the row whatever its content's height: a group of one
|
||||
/// button, a group of three stacked rows and a group of one stacked button.
|
||||
#[trial]
|
||||
fn every_open_groups_title_sits_on_one_line() {
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let tabs = [RibbonTab::new(0_usize, "Home")];
|
||||
let _ = Ribbon::new("Ribbon", &tabs, Bind::given(0_usize)).show(ui, |row, _| {
|
||||
row.group(RibbonGroup::new("short", "Short", IconId::new("list").into()), |ui| {
|
||||
Button::new("One").show(ui);
|
||||
});
|
||||
row.group(RibbonGroup::new("rows", "Rows", IconId::new("list").into()), |ui| {
|
||||
Stack::new().show(ui, |ui, _| {
|
||||
for label in ["First", "Second", "Third"] {
|
||||
Button::new(label).show(ui);
|
||||
}
|
||||
});
|
||||
});
|
||||
group(row, "big", "Big", &["Large"]);
|
||||
});
|
||||
});
|
||||
fixture.room(vec2(900.0, 400.0));
|
||||
fixture.settle();
|
||||
let baseline = |title: &str| fixture.texts().iter().find(|run| run.text == title).map_or(f32::NAN, |run| run.baseline);
|
||||
let (short, rows, big) = (baseline("Short"), baseline("Rows"), baseline("Big"));
|
||||
assert!((short - rows).abs() < 0.5, "a one-button group's title is on the line of a three-row group's: {short} against {rows}");
|
||||
assert!((big - rows).abs() < 0.5, "a stacked button group's title is on the same line: {big} against {rows}");
|
||||
}
|
||||
|
||||
/// Presses and releases the secondary button at `at`, a frame each.
|
||||
fn right_click(fixture: &mut Fixture, at: Pos2) {
|
||||
let button = |pressed| Event::PointerButton { pos: at, button: PointerButton::Secondary, pressed, modifiers: Modifiers::NONE };
|
||||
|
||||
@@ -5,7 +5,9 @@ use std::rc::Rc;
|
||||
|
||||
use craft_menu::{Gallery, GalleryFace, GalleryItem, GroupLevel};
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::list::ThumbAspect;
|
||||
use craft_ui::primitives::ContentPainterCx;
|
||||
use craft_ui::tile_grid::CaptionPlace;
|
||||
use craft_ui::runtime::env::{Label, with_env};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::Rect;
|
||||
@@ -68,3 +70,33 @@ fn the_tile_count_follows_the_group_level_and_stepping_stays_in_range_when_the_i
|
||||
fixture.settle();
|
||||
assert_eq!(*picks.borrow(), vec![1], "a click on a tile picks its key");
|
||||
}
|
||||
|
||||
/// The tiles' rects and the painted texts of a gallery of three, square and uncaptioned or two to one and named under
|
||||
/// each picture.
|
||||
fn tiles(wide: bool) -> (Vec<Rect>, Vec<String>) {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let paint = |_: &ContentPainterCx, _: Rect| {};
|
||||
let items: Vec<GalleryItem<'_, usize>> =
|
||||
(0..3).map(|at| GalleryItem { key: at, label: Label::from(format!("Tile {at}")), face: GalleryFace::Paint(&paint) }).collect();
|
||||
let gallery = Gallery::new("Styles", &items, None).tiles(3, 3);
|
||||
let gallery = if wide { gallery.aspect(ThumbAspect::new(2, 1)).caption(CaptionPlace::Below) } else { gallery };
|
||||
let _ = gallery.show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let rects = (0..3).filter_map(|at| fixture.by_label(&format!("Tile {at}")).map(|tile| tile.rect)).collect();
|
||||
(rects, fixture.texts().iter().map(|run| run.text.clone()).collect())
|
||||
}
|
||||
|
||||
/// A style gallery's tiles are twice as wide as a square one's and name their items under the pictures; a plain
|
||||
/// gallery's tiles are square and show no names.
|
||||
#[trial]
|
||||
fn a_wide_captioned_gallery_names_its_tiles_under_wide_pictures() {
|
||||
let (square, plain) = tiles(false);
|
||||
let (wide, named) = tiles(true);
|
||||
assert_eq!((square.len(), wide.len()), (3, 3), "every tile shows");
|
||||
let (square, wide) = (square[0], wide[0]);
|
||||
assert!(wide.width() > 1.5 * square.width(), "a two to one tile is wider: {wide:?} against {square:?}");
|
||||
assert!(wide.height() > square.height() + 0.5, "a captioned tile keeps a line for its name: {wide:?} against {square:?}");
|
||||
assert!(named.iter().any(|text| text == "Tile 0"), "a captioned tile paints its name: {named:?}");
|
||||
assert!(!plain.iter().any(|text| text == "Tile 0"), "a plain tile paints no name: {plain:?}");
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ pub enum GroupLevel {
|
||||
///
|
||||
/// - a `Button` or `SplitButton` that asks for no variant (or for the default `Variant::SECONDARY`) draws flat at
|
||||
/// rest, as the ghost variant: no edge and no fill until the pointer is over it or presses it;
|
||||
/// - a `ComboBox` is as wide as its widest option and its chevron, not a field length, and a font picker takes the
|
||||
/// small field length (`size.length.field.sm`).
|
||||
/// - a `ComboBox` is as wide as its widest option and its chevron, not a field length, and a font picker is a short
|
||||
/// field (`size.length.short`) whichever family it shows.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, EnvValue)]
|
||||
#[env(style)]
|
||||
#[non_exhaustive]
|
||||
|
||||
@@ -74,11 +74,11 @@ impl FieldStyle {
|
||||
self
|
||||
}
|
||||
|
||||
/// The style of a field as wide as the small field length (`size.length.field.sm`) whatever its control size,
|
||||
/// where the room allows: a ribbon's font box, which keeps that width whichever family it shows.
|
||||
/// The style of a field with no field length to widen it past its shortest width (`size.length.short`): a
|
||||
/// ribbon's font box, which keeps that width whichever family it shows, as a word processor's does.
|
||||
#[must_use]
|
||||
pub fn small_length(mut self, cx: &StyleCx<'_>) -> FieldStyle {
|
||||
self.metrics = self.metrics.ideal_width(cx.tokens.size.length.field.sm);
|
||||
pub fn shortest(mut self) -> FieldStyle {
|
||||
self.metrics = self.metrics.ideal_width(self.metrics.min_size.x);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
|
||||
use craft_behavior::{IconId, PartKey};
|
||||
use craft_theme::ControlSize;
|
||||
use craft_ui::buttons::{Arrangement, Button, GroupLevel, SplitButton, SplitMenu, ToggleButton};
|
||||
use craft_theme::Variant;
|
||||
use craft_ui::buttons::{Arrangement, Button, ControlPlace, GroupLevel, SplitButton, SplitMenu, ToggleButton};
|
||||
use craft_ui::contract::tooltip::DELAY;
|
||||
use craft_ui::contract::{Bind, CommandSet, Entry, Hover, Props as _, Role, Show as _};
|
||||
use craft_ui::pickers::ComboBox;
|
||||
use craft_ui::display::Picture;
|
||||
use craft_ui::list::ThumbAspect;
|
||||
use craft_ui::runtime::commands::CommandState;
|
||||
@@ -325,3 +327,83 @@ fn a_click_on_a_buttons_picture_activates_the_button_which_stays_one_access_node
|
||||
assert!(fixture.tree().children_of(button.node).is_empty(), "the picture adds no node under the button");
|
||||
assert!(fixture.by_role(Role::IMAGE).is_empty(), "the picture is not a node of its own");
|
||||
}
|
||||
|
||||
/// The first style of `type_name` resolved when `show` runs alone.
|
||||
fn style_seen(type_name: &str, show: impl Fn(&mut Ui) + 'static) -> std::sync::Arc<craft_ui::testing::style::StyleSeen> {
|
||||
let mut fixture = Fixture::new(move |ui| show(ui));
|
||||
fixture.settle();
|
||||
fixture.styles_of(type_name).into_iter().next().expect("the control resolved its style")
|
||||
}
|
||||
|
||||
/// Shows `show` where a ribbon group shows its content.
|
||||
fn in_ribbon(ui: &mut Ui, show: impl FnOnce(&mut Ui)) {
|
||||
with_env(
|
||||
ui,
|
||||
|overrides| {
|
||||
overrides.values.set(ControlPlace::Ribbon);
|
||||
},
|
||||
show,
|
||||
);
|
||||
}
|
||||
|
||||
/// In a ribbon a push or split button that asks for no variant is flat at rest: it resolves the ghost look. Anywhere
|
||||
/// else it keeps its outlined default, and in a ribbon a variant it asks for stays.
|
||||
#[trial]
|
||||
fn a_ribbons_buttons_are_flat_at_rest_unless_they_ask_for_a_variant() {
|
||||
let button = std::any::type_name::<Button<'static>>();
|
||||
let ribbon = style_seen(button, |ui| {
|
||||
in_ribbon(ui, |ui| {
|
||||
let _ = Button::new("Copy").show(ui);
|
||||
});
|
||||
});
|
||||
let ghost = style_seen(button, |ui| {
|
||||
let _ = Button::new("Copy").variant(Variant::GHOST).show(ui);
|
||||
});
|
||||
let standard = style_seen(button, |ui| {
|
||||
let _ = Button::new("Copy").show(ui);
|
||||
});
|
||||
let primary = style_seen(button, |ui| {
|
||||
in_ribbon(ui, |ui| {
|
||||
let _ = Button::new("Copy").variant(Variant::PRIMARY).show(ui);
|
||||
});
|
||||
});
|
||||
assert!(ribbon.looks.same_as(&ghost.looks), "a ribbon button takes the ghost look");
|
||||
assert!(!standard.looks.same_as(&ghost.looks), "outside a ribbon the default look stays");
|
||||
assert!(!primary.looks.same_as(&ghost.looks), "a variant asked for in a ribbon stays");
|
||||
|
||||
let split = std::any::type_name::<SplitButton<'static, usize>>();
|
||||
let ribbon = style_seen(split, |ui| {
|
||||
let entries = [Entry::new(1_usize, "Keep text only")];
|
||||
in_ribbon(ui, |ui| {
|
||||
let _ = SplitButton::new("Paste", &entries).show(ui);
|
||||
});
|
||||
});
|
||||
let ghost = style_seen(split, |ui| {
|
||||
let entries = [Entry::new(1_usize, "Keep text only")];
|
||||
let _ = SplitButton::new("Paste", &entries).variant(Variant::GHOST).show(ui);
|
||||
});
|
||||
assert!(ribbon.looks.same_as(&ghost.looks), "a ribbon split button takes the ghost look");
|
||||
}
|
||||
|
||||
/// In a ribbon a combo box is as wide as its widest option and its chevron, not a field length.
|
||||
#[trial]
|
||||
fn a_ribbons_combo_box_is_as_wide_as_its_options() {
|
||||
let width = |ribbon: bool| {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let options = [Entry::new("8".to_owned(), "8"), Entry::new("72".to_owned(), "72")];
|
||||
let mut size = "11".to_owned();
|
||||
let mut show = |ui: &mut Ui| {
|
||||
let _ = ComboBox::new("Font Size", Bind::bound(&mut size), &options).show(ui);
|
||||
};
|
||||
if ribbon {
|
||||
in_ribbon(ui, show);
|
||||
} else {
|
||||
show(ui);
|
||||
}
|
||||
});
|
||||
fixture.settle();
|
||||
fixture.by_label("Font Size").expect("the combo box").rect.width()
|
||||
};
|
||||
let (ribbon, standard) = (width(true), width(false));
|
||||
assert!(ribbon < standard / 2.0, "the ribbon's combo box fits two digits and its chevron: {ribbon} against {standard}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user