mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 03:21:34 +01:00
BarItems: a strip short of room moves whole groups into More; AlignBar's More lists every command
Neighbouring items of one section are a group the strip shows or moves whole, as the old toolbar moved its groups. MoreMenu::Every lists every item, as the old align bar's menu did. A vertical strip sets its groups apart with space instead of a rule.
This commit is contained in:
1 parent
8a99049ed8
commit
38787f5598
5 files changed
+172
-47
No files matched your search
@@ -1,8 +1,8 @@
|
||||
//! `AlignBar`: the Align and Distribute controls of the Move tool's options bar. A pattern over `BarItems`: the six
|
||||
//! align commands, then the eight distribute commands after a rule, which overflow into the "More" menu or live in
|
||||
//! it.
|
||||
//! align commands, then the eight distribute commands after a rule or only in the "More" menu, which lists all
|
||||
//! fourteen.
|
||||
|
||||
use super::items::{BarItem, BarItems};
|
||||
use super::items::{BarItem, BarItems, MoreMenu};
|
||||
use super::words::BarWords;
|
||||
use crate::contract::{NoValue, Outcome, Props as _, Show as _};
|
||||
use crate::runtime::env::Env;
|
||||
@@ -113,7 +113,8 @@ impl Outcome for AlignBarOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// The align buttons, the optional distribute buttons and the menu button, shown as children of a `Bar`.
|
||||
/// The align buttons, the optional distribute buttons and the menu button, shown as children of a `Bar`. The menu
|
||||
/// lists every command, the align commands, a rule, then the distribute commands.
|
||||
#[derive(crate::contract::Pattern)]
|
||||
pub struct AlignBar {
|
||||
distribute: DistributeButtons,
|
||||
@@ -138,7 +139,7 @@ impl AlignBar {
|
||||
let distribute_keep = if self.distribute == DistributeButtons::Row { FitKeep::Free } else { FitKeep::Menu };
|
||||
let align = AlignAction::ALIGN.into_iter().map(BarItem::new);
|
||||
let distribute = AlignAction::DISTRIBUTE.into_iter().map(|action| BarItem::new(action).section(1).keep(distribute_keep));
|
||||
let response = BarItems::new(align.chain(distribute).collect()).label(name).show(ui);
|
||||
let response = BarItems::new(align.chain(distribute).collect()).label(name).more(MoreMenu::Every).show(ui);
|
||||
AlignBarOutput { response }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ use crate::primitives::IconSpec;
|
||||
use crate::runtime::commands::{CommandRef, ResolvedCommand};
|
||||
use crate::runtime::env::Label;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use craft_behavior::fit::{FitItem, FitKeep, fit_items, run_extent};
|
||||
use craft_behavior::fit::{FitItem, FitKeep, fit_items};
|
||||
use craft_behavior::{Axis, IconId, Metrics, NavShape, PartKey, PartRef, Proposal, Roving, Sense};
|
||||
use craft_theme::Variant;
|
||||
use egui::{Rect, Response, Vec2, pos2};
|
||||
@@ -33,13 +33,25 @@ pub enum ItemDisplay {
|
||||
IconText,
|
||||
}
|
||||
|
||||
/// What the "More" menu of a `BarItems` strip lists.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
#[non_exhaustive]
|
||||
pub enum MoreMenu {
|
||||
/// The items the strip has no room for, and the `Menu` items; "More" shows only when there are any.
|
||||
#[default]
|
||||
Overflow,
|
||||
/// Every item, shown on the strip or not; "More" always shows.
|
||||
Every,
|
||||
}
|
||||
|
||||
/// One command of a `BarItems` strip.
|
||||
#[derive(Clone, Debug)]
|
||||
#[non_exhaustive]
|
||||
pub struct BarItem {
|
||||
/// The command the item shows and runs.
|
||||
pub command: CommandRef,
|
||||
/// The section: a rule separates neighbouring items of different sections.
|
||||
/// The section: neighbouring items of one section are a group, which the strip shows or moves into "More" whole.
|
||||
/// A rule separates the groups of a horizontal strip, extra space those of a vertical one.
|
||||
pub section: u16,
|
||||
/// What the item shows.
|
||||
pub display: ItemDisplay,
|
||||
@@ -75,7 +87,8 @@ impl BarItem {
|
||||
}
|
||||
}
|
||||
|
||||
/// The style of a bar item strip: the gap between items and the space around the rule of a section change.
|
||||
/// The style of a bar item strip: the gap between items, the space around the rule of a section change and the
|
||||
/// space between the groups of a vertical strip.
|
||||
#[derive(Clone, Debug, Default, PartialEq, ComponentStyle)]
|
||||
#[non_exhaustive]
|
||||
pub struct BarItemsStyle {
|
||||
@@ -85,6 +98,8 @@ pub struct BarItemsStyle {
|
||||
joint: f32,
|
||||
/// The clear space of a section change, on both sides of its rule together.
|
||||
section_gap: f32,
|
||||
/// Between the groups of a vertical strip, which draws no rule: an item gap and `space.gap.stack`.
|
||||
stack_gap: f32,
|
||||
}
|
||||
|
||||
/// An item whose command resolved this pass.
|
||||
@@ -110,6 +125,13 @@ struct Sizes {
|
||||
more: Vec2,
|
||||
}
|
||||
|
||||
/// A run of neighbouring items of one section: the items it shows on the strip, in order, and its length there.
|
||||
struct Group {
|
||||
items: Vec<usize>,
|
||||
extent: f32,
|
||||
keep: FitKeep,
|
||||
}
|
||||
|
||||
/// What an item's part wears.
|
||||
enum Face {
|
||||
Action(IconButton<'static>),
|
||||
@@ -187,8 +209,9 @@ fn slot(rect: Rect, axis: Axis, rtl: bool, from: f32, length: f32) -> Rect {
|
||||
/// A strip of command items. Each item is a part that wears the face of a button resolved from its command: an
|
||||
/// icon button, a toggle for a command with a checked state, or a text button. The items are one Tab stop with
|
||||
/// arrow keys between them, and the strip joins the roving group of the body it is shown in. An icon item's tooltip
|
||||
/// is the command's label and its chord. When the room is too short, the last `Free` items move into a menu behind
|
||||
/// a "More" part, as rows of the same commands; `Menu` items are always there.
|
||||
/// is the command's label and its chord. Neighbouring items of one section are a group. When the room is too short,
|
||||
/// the last group with no `Pinned` item moves into a menu behind a "More" part, whole, as rows of the same commands,
|
||||
/// a rule between the groups; `Menu` items are always there. With `MoreMenu::Every` the menu lists every item.
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(overlay)]
|
||||
pub struct BarItems {
|
||||
@@ -198,13 +221,16 @@ pub struct BarItems {
|
||||
/// The axis the strip runs along.
|
||||
#[prop]
|
||||
axis: Axis,
|
||||
/// What the "More" menu lists.
|
||||
#[prop]
|
||||
more: MoreMenu,
|
||||
laid: Option<Laid>,
|
||||
}
|
||||
|
||||
impl BarItems {
|
||||
/// A strip of `items`, in the order given, running along the horizontal axis.
|
||||
pub fn new(items: Vec<BarItem>) -> Self {
|
||||
BarItems { common: Common::new(), items, axis: Axis::X, laid: None }
|
||||
BarItems { common: Common::new(), items, axis: Axis::X, more: MoreMenu::Overflow, laid: None }
|
||||
}
|
||||
|
||||
/// The items whose command the registry knows.
|
||||
@@ -221,12 +247,44 @@ impl BarItems {
|
||||
Sizes { items, rule, more }
|
||||
}
|
||||
|
||||
fn fit_items_of(&self, sizes: &Sizes, present: &[Present]) -> Vec<FitItem> {
|
||||
present
|
||||
.iter()
|
||||
.zip(&sizes.items)
|
||||
.map(|(present, size)| FitItem::new(along(self.axis, *size), present.item.section, present.item.keep))
|
||||
.collect()
|
||||
/// The space between two groups on the strip: around the rule of a horizontal strip, or the stack gap of a
|
||||
/// vertical one.
|
||||
fn group_gap(&self, style: &BarItemsStyle, sizes: &Sizes) -> f32 {
|
||||
if self.axis == Axis::Y { style.stack_gap } else { style.section_gap + sizes.rule }
|
||||
}
|
||||
|
||||
/// The groups of the items the strip can show, in order. `Menu` items are in no group.
|
||||
fn groups(&self, sizes: &Sizes, present: &[Present], gap: f32) -> Vec<Group> {
|
||||
let mut groups: Vec<(u16, Group)> = Vec::new();
|
||||
for (at, present) in present.iter().enumerate().filter(|(_, present)| present.item.keep != FitKeep::Menu) {
|
||||
let length = along(self.axis, sizes.items[at]);
|
||||
match groups.last_mut() {
|
||||
Some((section, group)) if *section == present.item.section => {
|
||||
group.items.push(at);
|
||||
group.extent += gap + length;
|
||||
}
|
||||
_ => groups.push((present.item.section, Group { items: vec![at], extent: length, keep: FitKeep::Free })),
|
||||
}
|
||||
if present.item.keep == FitKeep::Pinned {
|
||||
groups.last_mut().expect("just pushed").1.keep = FitKeep::Pinned;
|
||||
}
|
||||
}
|
||||
groups.into_iter().map(|(_, group)| group).collect()
|
||||
}
|
||||
|
||||
/// Whether "More" shows whatever the room.
|
||||
fn more_always(&self, present: &[Present]) -> bool {
|
||||
self.more == MoreMenu::Every || present.iter().any(|present| present.item.keep == FitKeep::Menu)
|
||||
}
|
||||
|
||||
/// The groups the strip shows, by index, fitted into `available`.
|
||||
fn fit(&self, groups: &[Group], present: &[Present], available: f32, gap: f32, group_gap: f32, more: f32) -> Vec<usize> {
|
||||
// Each group is one fit item of a section of its own; a `Menu` item at the end keeps "More" on the strip.
|
||||
let mut items: Vec<FitItem> = groups.iter().enumerate().map(|(at, group)| FitItem::new(group.extent, at as u16, group.keep)).collect();
|
||||
if self.more_always(present) {
|
||||
items.push(FitItem::new(0.0, u16::MAX, FitKeep::Menu));
|
||||
}
|
||||
fit_items(&items, available, gap, group_gap, more).shown.into_iter().filter(|&at| at < groups.len()).collect()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -237,30 +295,34 @@ impl Component for BarItems {
|
||||
|
||||
fn style(cx: &StyleCx<'_>, _: BarItemsStyleKey) -> BarItemsStyle {
|
||||
let gap = cx.tokens.space.gap.inline;
|
||||
BarItemsStyle { metrics: Metrics::new().gap(gap), joint: gap, section_gap: gap * 2.0 }
|
||||
BarItemsStyle { metrics: Metrics::new().gap(gap), joint: gap, section_gap: gap * 2.0, stack_gap: gap + cx.tokens.space.gap.stack }
|
||||
}
|
||||
|
||||
/// The whole strip, or the room along the strip when that is shorter. Asked for the narrowest it takes (a
|
||||
/// `Proposal::Min`), it gives the least that shows an item: the first item and the "More" part. The real run fits
|
||||
/// the items into the width it is given, so the strip moves items into "More" only when that is narrower than
|
||||
/// the whole strip. The strip with no item to move into "More" is as long as its items.
|
||||
/// `Proposal::Min`), it gives the least that shows a group: the first group and the "More" part. The real run fits
|
||||
/// the groups into the width it is given, so the strip moves groups into "More" only when that is narrower than
|
||||
/// the whole strip. The strip with one group and no "More" is as long as its items.
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
let present = self.present(cx);
|
||||
if present.is_empty() {
|
||||
return Vec2::ZERO;
|
||||
}
|
||||
let sizes = self.sizes(cx, &present);
|
||||
let all = vec![true; present.len()];
|
||||
let section_gap = cx.style.section_gap + sizes.rule;
|
||||
let natural = run_extent(&self.fit_items_of(&sizes, &present), &all, f64::from(cx.gap()), f64::from(section_gap)) as f32;
|
||||
let cross = sizes.items.iter().map(|size| across(self.axis, *size)).fold(across(self.axis, sizes.more), f32::max);
|
||||
let gap = cx.gap();
|
||||
let groups = self.groups(&sizes, &present, gap);
|
||||
let group_gap = self.group_gap(cx.style, &sizes);
|
||||
let more = along(self.axis, sizes.more);
|
||||
let run: f32 = groups.iter().map(|group| group.extent).sum::<f32>() + group_gap * groups.len().saturating_sub(1) as f32;
|
||||
let more_after = |run: f32| if run > 0.0 { run + gap + more } else { more };
|
||||
let natural = if self.more_always(&present) { more_after(run) } else { run };
|
||||
let least = if cx.proposal == Proposal::Min {
|
||||
let first = sizes.items.first().map_or(0.0, |size| along(self.axis, *size));
|
||||
(present.len() > 1).then(|| (first + cx.gap() + along(self.axis, sizes.more)).min(natural as f32))
|
||||
let first = groups.first().map_or(0.0, |group| group.extent);
|
||||
(groups.len() > 1 || self.more_always(&present)).then(|| more_after(first).min(natural))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let natural = least.unwrap_or(natural as f32);
|
||||
let natural = least.unwrap_or(natural);
|
||||
let cross = sizes.items.iter().map(|size| across(self.axis, *size)).fold(across(self.axis, sizes.more), f32::max);
|
||||
match self.axis {
|
||||
Axis::Y => Vec2::new(cross, natural),
|
||||
_ => Vec2::new(cx.room.map_or(natural, |room| natural.min(room)), cross),
|
||||
@@ -278,28 +340,35 @@ impl Component for BarItems {
|
||||
let present = self.present(cx);
|
||||
let sizes = self.sizes(cx, &present);
|
||||
let (gap, joint, rtl, os) = (cx.gap(), cx.style.joint, cx.flow.is_rtl(), cx.env.shell().os);
|
||||
let section_gap = cx.style.section_gap + sizes.rule;
|
||||
let group_gap = self.group_gap(cx.style, &sizes);
|
||||
let more_length = along(axis, sizes.more);
|
||||
let fit = fit_items(&self.fit_items_of(&sizes, &present), along(axis, rect.size()), gap, section_gap, more_length);
|
||||
let (mut at, mut previous) = (0.0, None::<u16>);
|
||||
let groups = self.groups(&sizes, &present, gap);
|
||||
let fitted = self.fit(&groups, &present, along(axis, rect.size()), gap, group_gap, more_length);
|
||||
let mut at = 0.0;
|
||||
let (mut shown, mut rules) = (Vec::new(), Vec::new());
|
||||
for &index in &fit.shown {
|
||||
let section = present[index].item.section;
|
||||
if let Some(before) = previous {
|
||||
if before == section {
|
||||
at += gap;
|
||||
} else {
|
||||
for (n, group) in fitted.iter().map(|&index| &groups[index]).enumerate() {
|
||||
if n > 0 {
|
||||
if axis != Axis::Y {
|
||||
rules.push(slot(rect, axis, rtl, at + joint, sizes.rule));
|
||||
at += section_gap;
|
||||
}
|
||||
at += group_gap;
|
||||
}
|
||||
for (k, &index) in group.items.iter().enumerate() {
|
||||
if k > 0 {
|
||||
at += gap;
|
||||
}
|
||||
let length = along(axis, sizes.items[index]);
|
||||
shown.push((index, slot(rect, axis, rtl, at, length)));
|
||||
at += length;
|
||||
}
|
||||
let length = along(axis, sizes.items[index]);
|
||||
shown.push((index, slot(rect, axis, rtl, at, length)));
|
||||
at += length;
|
||||
previous = Some(section);
|
||||
}
|
||||
let more = (!fit.overflow.is_empty()).then(|| {
|
||||
let from = if previous.is_some() { at + gap } else { at };
|
||||
let on_strip: Vec<usize> = shown.iter().map(|&(index, _)| index).collect();
|
||||
let overflow: Vec<usize> = match self.more {
|
||||
MoreMenu::Every => (0..present.len()).collect(),
|
||||
MoreMenu::Overflow => (0..present.len()).filter(|index| !on_strip.contains(index)).collect(),
|
||||
};
|
||||
let more = (!overflow.is_empty()).then(|| {
|
||||
let from = if shown.is_empty() { at } else { at + gap };
|
||||
slot(rect, axis, rtl, from, more_length)
|
||||
});
|
||||
for &(index, cell) in &shown {
|
||||
@@ -316,7 +385,7 @@ impl Component for BarItems {
|
||||
cx.part(MORE, more).sense(Sense::CLICK | Sense::FOCUS).focusable().toggles("menu");
|
||||
}
|
||||
cx.roving(Roving::new(NavShape::Flat));
|
||||
self.laid = Some(Laid { present, shown, rules, overflow: fit.overflow, more });
|
||||
self.laid = Some(Laid { present, shown, rules, overflow, more });
|
||||
}
|
||||
|
||||
/// The strip joins the body's roving group; a click or the activation key on an item runs its command.
|
||||
@@ -330,7 +399,7 @@ impl Component for BarItems {
|
||||
}
|
||||
}
|
||||
|
||||
/// The overflow menu: one command row per overflowed item, a rule between sections.
|
||||
/// The "More" menu: one command row per item it lists, a rule between sections.
|
||||
fn overlay(&mut self, ui: &mut OverlayUi<'_>, cx: &mut Cx<'_, Self>) {
|
||||
let Some(laid) = self.laid.as_ref().filter(|laid| laid.more.is_some()) else { return };
|
||||
let popup = cx.popup("menu");
|
||||
|
||||
@@ -17,6 +17,6 @@ pub use breadcrumb::{BreadcrumbBar, BreadcrumbBarStyle, BreadcrumbOutput, path_c
|
||||
pub use cell::BarCell;
|
||||
pub use constraint_fields::{ConstraintFields, ConstraintFieldsOutput};
|
||||
pub use craft_behavior::fit::FitKeep;
|
||||
pub use items::{BarItem, BarItems, BarItemsStyle, ItemDisplay};
|
||||
pub use items::{BarItem, BarItems, BarItemsStyle, ItemDisplay, MoreMenu};
|
||||
pub use status_readout::{StatusItem, StatusReadout, StatusReadoutOutput, StatusValues};
|
||||
pub use words::BarWords;
|
||||
@@ -0,0 +1,54 @@
|
||||
//! `BarItems`: a strip too short for its items moves whole groups into "More"; the every-item menu lists them all.
|
||||
|
||||
use craft_ui::bars::{BarItem, BarItems, MoreMenu};
|
||||
use craft_ui::contract::{Props as _, Role, Show as _};
|
||||
use craft_ui::runtime::commands::EditCommands;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::Vec2;
|
||||
|
||||
fn strip(width: f32, more: MoreMenu) -> Fixture {
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let items = vec![
|
||||
BarItem::new(EditCommands::Undo),
|
||||
BarItem::new(EditCommands::Redo),
|
||||
BarItem::new(EditCommands::Cut).section(1),
|
||||
BarItem::new(EditCommands::Copy).section(1),
|
||||
BarItem::new(EditCommands::Paste).section(1),
|
||||
];
|
||||
BarItems::new(items).more(more).show(ui);
|
||||
});
|
||||
fixture.room(Vec2::new(width, 80.0)).settle();
|
||||
fixture
|
||||
}
|
||||
|
||||
/// How many of `labels` the strip shows.
|
||||
fn shown(fixture: &Fixture, labels: &[&str]) -> usize {
|
||||
labels.iter().filter(|label| fixture.by_label(label).is_some()).count()
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_narrowing_strip_moves_whole_groups_into_more() {
|
||||
let (history, clipboard) = (["Undo", "Redo"], ["Cut", "Copy", "Paste"]);
|
||||
let wide = strip(600.0, MoreMenu::Overflow);
|
||||
assert_eq!(shown(&wide, &clipboard) + shown(&wide, &history), 5, "a wide strip shows every item");
|
||||
assert!(wide.by_label("More").is_none(), "a strip with room has no More");
|
||||
let mut split = false;
|
||||
for width in (40..=240).rev().step_by(4) {
|
||||
let fixture = strip(width as f32, MoreMenu::Overflow);
|
||||
let (h, c) = (shown(&fixture, &history), shown(&fixture, &clipboard));
|
||||
assert!(c == 0 || c == clipboard.len(), "{c} of the clipboard group show at {width}");
|
||||
assert!(h == 0 || h == history.len(), "{h} of the history group show at {width}");
|
||||
split |= h == history.len() && c == 0;
|
||||
}
|
||||
assert!(split, "no width shows the first group without the second");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_every_item_menu_lists_the_items_on_the_strip_too() {
|
||||
let mut fixture = strip(600.0, MoreMenu::Every);
|
||||
assert!(fixture.by_label("Undo").is_some(), "a wide strip shows its items");
|
||||
let more = fixture.by_label("More").expect("More shows with the every-item menu");
|
||||
fixture.click(&more);
|
||||
fixture.settle();
|
||||
assert_eq!(fixture.by_role(Role::MENU_ITEM).len(), 5, "the menu lists every item");
|
||||
}
|
||||
@@ -3,3 +3,4 @@
|
||||
mod bar;
|
||||
mod breadcrumb;
|
||||
mod constraint_fields;
|
||||
mod items;
|
||||
Reference in new issue
Block a user