mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 01:08:38 +00:00
Merge pull request #2955 from dotCooCoo/feat/menu-bar-natural-width
Menu bar: the output reports the width every title takes side by side
This commit is contained in:
@@ -137,7 +137,9 @@ app translates. The dialog never opens a file dialog or touches the file system.
|
||||
|
||||
## Windows, Linux and web: the in-window bar
|
||||
|
||||
`MenuBarUi` draws a laid-out bar and returns the titles' rect (`MenuBarOutput`). Show it inside a
|
||||
`MenuBarUi` draws a laid-out bar and returns the titles' rect and the width every title takes side by
|
||||
side (`MenuBarOutput`). A host that puts controls after the titles on the same row compares that
|
||||
width with the room it leaves the bar. Show it inside a
|
||||
`Stack` (a top panel's body) or the title bar's row: there the titles that don't fit its room fold
|
||||
into "…".
|
||||
Each item is a command row: its enabled state, check mark and chord follow the command in the
|
||||
|
||||
@@ -48,11 +48,11 @@ pub(crate) fn keymap_platform(target: Target, os: Platform) -> Platform {
|
||||
/// A replay at another place moves the titles' rect with the bar.
|
||||
impl craft_ui::runtime::Shift for MenuBarOutput {
|
||||
fn shifted(&self, by: &egui::RecordShift) -> Self {
|
||||
MenuBarOutput { titles: by.rect(self.titles), opened: self.opened }
|
||||
MenuBarOutput { titles: by.rect(self.titles), natural: self.natural, opened: self.opened }
|
||||
}
|
||||
|
||||
fn snapped(&self, to: &Self, within: f32) -> Self {
|
||||
MenuBarOutput { titles: craft_ui::runtime::Shift::snapped(&self.titles, &to.titles, within), opened: self.opened }
|
||||
MenuBarOutput { titles: craft_ui::runtime::Shift::snapped(&self.titles, &to.titles, within), natural: self.natural, opened: self.opened }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,6 +63,9 @@ pub struct MenuBarOutput {
|
||||
/// The menu titles alone, without the room the bar leaves after them
|
||||
/// ([`craft_ui::menu::MenuBarOutput::titles`]).
|
||||
pub titles: Rect,
|
||||
/// The width every title takes side by side with none in "…", whatever room the bar had
|
||||
/// ([`craft_ui::menu::MenuBarOutput::natural`]). Zero when the bar shows nothing.
|
||||
pub natural: f32,
|
||||
/// True in the one pass where a menu of the bar opened while none was open
|
||||
/// ([`craft_ui::menu::MenuBarOutput::opened`]).
|
||||
pub opened: bool,
|
||||
@@ -167,7 +170,7 @@ impl<'a> MenuBarUi<'a> {
|
||||
icons: self.icons,
|
||||
compact: self.compact,
|
||||
};
|
||||
MemoView::new("menu-bar", props, show_bar).show(ui).unwrap_or(MenuBarOutput { titles: Rect::NOTHING, opened: false })
|
||||
MemoView::new("menu-bar", props, show_bar).show(ui).unwrap_or(MenuBarOutput { titles: Rect::NOTHING, natural: 0.0, opened: false })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -214,8 +217,8 @@ fn show_bar(props: &mut BarProps<'_>, ui: &mut Ui) -> MenuBarOutput {
|
||||
if let Some(menu) = props.help {
|
||||
bar = bar.help(MenuHelp::new(menu, &entries));
|
||||
}
|
||||
let out = bar.show(ui);
|
||||
MenuBarOutput { titles: out.titles, opened: out.opened }
|
||||
let shown = bar.show(ui);
|
||||
MenuBarOutput { titles: shown.titles, natural: shown.natural, opened: shown.opened }
|
||||
}
|
||||
|
||||
/// What the kept text of a bar is made of: the model, the platform that names its shortcuts, the preferred mnemonics,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! The in-window menu bar in an app's title bar: every title shows while the bar has room for them, the document title
|
||||
//! takes the rest of the bar, and only a bar too narrow for every title folds the last into "…". In a `Stack`
|
||||
//! narrower than its titles the bar folds the same way and draws nothing past the stack's edge.
|
||||
//! narrower than its titles the bar folds the same way and draws nothing past the stack's edge. In any room the bar
|
||||
//! reports the width its titles take side by side.
|
||||
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
@@ -131,3 +132,30 @@ fn a_bar_in_a_stack_narrower_than_its_titles_folds_them_and_draws_nothing_past_i
|
||||
fixture.click(&more).settle();
|
||||
assert!(fixture.by_label("Help").is_some(), "Help is a row of the \"…\" menu");
|
||||
}
|
||||
|
||||
/// The bar reports the width every title takes side by side: in a stack that holds them all it is the titles' width,
|
||||
/// and in a stack too narrow for them it is the same width while the drawn titles are narrower.
|
||||
#[trial]
|
||||
fn the_bar_reports_the_width_every_title_takes_in_a_wide_and_a_narrow_stack() {
|
||||
let mut reports = Vec::new();
|
||||
for width in [1520.0, 240.0] {
|
||||
let bar = Rc::new(model());
|
||||
let seen = Rc::new(Cell::new((0.0, 0.0)));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let bar = Rc::clone(&bar);
|
||||
let out = Rc::clone(&out);
|
||||
Stack::new().show(ui, move |ui, _| {
|
||||
let shown = MenuBarUi::new(&bar).target(Target::Windows).show(ui);
|
||||
out.set((shown.natural, shown.titles.width()));
|
||||
});
|
||||
});
|
||||
fixture.os(Os::Windows).room(vec2(width, 600.0)).settle();
|
||||
reports.push(seen.get());
|
||||
}
|
||||
let [(wide, wide_titles), (narrow, narrow_titles)] = [reports[0], reports[1]];
|
||||
assert!(wide > 0.0, "the bar reports a width");
|
||||
assert!((wide - wide_titles).abs() < 0.5, "with room for every title the natural width {wide} is the titles' width {wide_titles}");
|
||||
assert!((narrow - wide).abs() < 0.5, "a narrow bar reports the same natural width {narrow} as a wide one {wide}");
|
||||
assert!(narrow_titles < narrow, "a narrow bar draws titles {narrow_titles} narrower than its natural width {narrow}");
|
||||
}
|
||||
|
||||
@@ -267,6 +267,10 @@ pub struct MenuBarOutput {
|
||||
pub open: bool,
|
||||
/// The titles alone (with "…" when it shows), from the first title's reading start to the last title's end.
|
||||
pub titles: Rect,
|
||||
/// The width every title takes side by side with none in "…", whatever room the bar had: a host that places
|
||||
/// controls after the titles on the same row compares it with the room it leaves the bar. Zero when the bar
|
||||
/// shows nothing (`MenuPlacement::ScreenTop`).
|
||||
pub natural: f32,
|
||||
/// True in the one pass where a menu of the bar opened while no menu of the bar was open when the pass began. A
|
||||
/// rerun pass in the same frame and a switch from one open title to another report false.
|
||||
pub opened: bool,
|
||||
@@ -365,6 +369,8 @@ pub struct MenuBar<'a> {
|
||||
#[prop]
|
||||
keyboard: bool,
|
||||
open: bool,
|
||||
/// The width of every title side by side, from this pass's measure or parts step.
|
||||
natural: f32,
|
||||
/// A menu opened in this pass while none was open when it began.
|
||||
opened: bool,
|
||||
}
|
||||
@@ -372,7 +378,7 @@ pub struct MenuBar<'a> {
|
||||
impl<'a> MenuBar<'a> {
|
||||
/// A bar named `label` with one title per menu, in reading order.
|
||||
pub fn new(label: impl Into<Label>, titles: Vec<MenuTitle<'a>>) -> Self {
|
||||
MenuBar { common: Common::new(), titles, help: None, keyboard: true, open: false, opened: false }.label(label)
|
||||
MenuBar { common: Common::new(), titles, help: None, keyboard: true, open: false, natural: 0.0, opened: false }.label(label)
|
||||
}
|
||||
|
||||
fn in_window(env: &EnvView<'_>) -> bool {
|
||||
@@ -508,7 +514,8 @@ impl Component for MenuBar<'_> {
|
||||
return Vec2::ZERO;
|
||||
}
|
||||
let (widths, height, more) = self.widths(cx);
|
||||
let width = if cx.proposal == Proposal::Min { more } else { widths.iter().sum() };
|
||||
self.natural = widths.iter().sum();
|
||||
let width = if cx.proposal == Proposal::Min { more } else { self.natural };
|
||||
Vec2::new(width, height)
|
||||
}
|
||||
|
||||
@@ -521,6 +528,7 @@ impl Component for MenuBar<'_> {
|
||||
return;
|
||||
}
|
||||
let (widths, _, more) = self.widths(cx);
|
||||
self.natural = widths.iter().sum();
|
||||
let fit = fit_titles(&widths, more, rect.width());
|
||||
let gap = Self::gap(cx);
|
||||
let mut flow = Flow::new(rect, cx.flow.is_rtl());
|
||||
@@ -763,6 +771,6 @@ impl Component for MenuBar<'_> {
|
||||
let rects = Self::slots(found).into_iter().filter_map(|slot| cx.part_if_declared(Self::slot_part(slot)).map(|result| result.rect));
|
||||
let start = if cx.flow.is_rtl() { cx.rect.right_top() } else { cx.rect.left_top() };
|
||||
let titles = rects.reduce(Rect::union).unwrap_or_else(|| Rect::from_min_size(start, Vec2::ZERO));
|
||||
MenuBarOutput { response: cx.response.clone(), open: self.open, titles, opened: self.opened }
|
||||
MenuBarOutput { response: cx.response.clone(), open: self.open, titles, natural: self.natural, opened: self.opened }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! The menu bar: what the help search finds.
|
||||
//! The menu bar: what the help search finds, and the width its titles take.
|
||||
|
||||
use craft_host::target::Os;
|
||||
use craft_i18n::Direction;
|
||||
@@ -97,6 +97,40 @@ fn a_letter_runs_the_row_it_marks_in_a_menu_a_title_click_opened() {
|
||||
assert!(fixture.by_label("Save").is_none(), "and the run closes the menu");
|
||||
}
|
||||
|
||||
/// The output's `natural` is the width of every title side by side: the titles' width in a room that holds them all, and
|
||||
/// the same width in a room that folds some into "…", where the drawn titles are narrower.
|
||||
#[trial]
|
||||
fn the_output_reports_the_width_every_title_takes_in_any_room() {
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
let seen = Rc::new(Cell::new([(0.0, 0.0); 2]));
|
||||
let out = Rc::clone(&seen);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let titles = || {
|
||||
["&File", "&Edit", "&Image", "&Layer", "&Select", "&View", "&Window", "&Help"]
|
||||
.into_iter()
|
||||
.map(|title| {
|
||||
MenuTitle::new(title, |ui: &mut egui::Ui| {
|
||||
MenuItem::new("&New").show(ui);
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
let top = ui.max_rect().left_top();
|
||||
let wide = MenuBar::new("Wide menu", titles()).show_at(ui, egui::Rect::from_min_size(top, egui::vec2(800.0, 24.0)));
|
||||
let below = top + egui::vec2(0.0, 40.0);
|
||||
let narrow = MenuBar::new("Narrow menu", titles()).show_at(ui, egui::Rect::from_min_size(below, egui::vec2(120.0, 24.0)));
|
||||
out.set([(wide.natural, wide.titles.width()), (narrow.natural, narrow.titles.width())]);
|
||||
});
|
||||
fixture.os(Os::Linux);
|
||||
fixture.settle();
|
||||
let [(wide, wide_titles), (narrow, narrow_titles)] = seen.get();
|
||||
assert!(wide > 0.0, "the bar reports a width");
|
||||
assert!((wide - wide_titles).abs() < 0.5, "with room for every title the natural width {wide} is the titles' width {wide_titles}");
|
||||
assert!((narrow - wide).abs() < 0.5, "a narrow bar reports the same natural width {narrow} as a wide one {wide}");
|
||||
assert!(narrow_titles < narrow, "a narrow bar draws titles {narrow_titles} narrower than its natural width {narrow}");
|
||||
}
|
||||
|
||||
/// The pass a pointer move over a menu's rows runs paints the row under the pointer as the active row and no other,
|
||||
/// moving down or up, at 1x and at a fractional scale. The two frames egui runs after any frame with input change
|
||||
/// nothing, and then the menu asks for none.
|
||||
|
||||
Reference in New Issue
Block a user