A memoized view in a Stack fits its content to its room, as in a shrinking row; the menu bar folds in any Stack

This commit is contained in:
aiongg
2026-11-21 10:18:40 +00:00
parent bb2e565a2e
commit 8db22341b4
7 changed files with 51 additions and 15 deletions
+6 -2
View File
@@ -132,7 +132,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 in a top panel and returns the titles' rect (`MenuBarOutput`).
`MenuBarUi` draws a laid-out bar and returns the titles' rect (`MenuBarOutput`). 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
runtime's command state and keymap, and a choice is a `Fired` with `FireSource::Menu` in the
frame's output. `.help_search(true)` starts the Help menu with a search field over every command
@@ -141,7 +143,9 @@ frame's output. `.help_search(true)` starts the Help menu with a search field ov
```rust
let laid_out = craft_menu::layout(&menu_bar, Target::current(), &LayoutOptions::new("Sample")).bar;
egui::Panel::top("menu-bar").show(ui, |ui| {
MenuBarUi::new(&laid_out).keyboard(!dialog_open).help_search(true).show(ui);
Stack::new().show(ui, |ui, _| {
MenuBarUi::new(&laid_out).keyboard(!dialog_open).help_search(true).show(ui);
});
});
// The commands chosen arrive in `FrameOutput.fired`.
```
+2 -1
View File
@@ -81,7 +81,8 @@ pub struct MenuBarOutput {
/// The English-label hook a bar takes by default: none is set, so this type only names the slot.
pub type EnglishFn = fn(Labeled<'_>) -> Option<String>;
/// The bar for one frame: `MenuBarUi::new(&bar).show(ui)`. Draw it at the top of the window, in a top panel.
/// The bar for one frame: `MenuBarUi::new(&bar).show(ui)`. Draw it at the top of the window, inside a `Stack` (a top
/// panel's body) or the title bar's row: there it folds the titles that don't fit its room into "…".
/// The commands chosen come back through the command queue, not from `show`.
///
/// The bar is a memoized view: while its model, options, place and environment are those of its last run and nothing
+30 -2
View File
@@ -1,12 +1,15 @@
//! 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 "…".
//! 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.
use std::cell::Cell;
use std::rc::Rc;
use craft_host::target::{Os, WindowChrome};
use craft_menu::{CommandRef, MenuBar, MenuBarUi, Row, Target, from_items};
use craft_ui::contract::{Role, Show as _, Tracked};
use craft_ui::contract::{Role, Show as _, ShowContainer as _, Tracked};
use craft_ui::display::Picture;
use craft_ui::layout_components::Stack;
use craft_ui::runtime::env::WindowKey;
use craft_ui::shell::{AppShell, Caption, CaptionFamily, Captions, Frame, Title, TitleBar};
use craft_ui::testing::{Fixture, trial};
@@ -103,3 +106,28 @@ fn a_narrow_title_bar_folds_the_last_menus_into_more() {
assert!(fixture.by_label("Help").is_some(), "{pixels_per_point}x: Help is a row of the \"…\" menu");
}
}
#[trial]
fn a_bar_in_a_stack_narrower_than_its_titles_folds_them_and_draws_nothing_past_its_edge() {
let bar = Rc::new(model());
let stack = Rc::new(Cell::new(egui::Rect::NOTHING));
let seen = Rc::clone(&stack);
let mut fixture = Fixture::new(move |ui| {
let bar = Rc::clone(&bar);
let shown = Stack::new().show(ui, |ui, _| {
MenuBarUi::new(&bar).target(Target::Windows).show(ui);
});
seen.set(shown.output.rect);
});
fixture.os(Os::Windows).room(vec2(240.0, 600.0)).settle();
let shown = titles(&fixture);
assert_eq!(shown.last().map(String::as_str), Some("…"), "{shown:?}");
assert!(!shown.iter().any(|title| title == "Help"), "Help is not a title: {shown:?}");
let edge = stack.get().right();
for run in fixture.texts().iter().filter(|run| MENUS.contains(&run.text.as_str()) || run.text == "…") {
assert!(run.rect.right() <= edge + 0.5, "{:?} at {:?} reaches past the stack's edge {edge}", run.text, run.rect);
}
let more = fixture.by_label("…").expect("the \"…\" title");
fixture.click(&more).settle();
assert!(fixture.by_label("Help").is_some(), "Help is a row of the \"…\" menu");
}
+1 -1
View File
@@ -122,7 +122,7 @@ impl Lazy {
pixels_per_point: ctx.pixels_per_point(),
atlas: ctx.fonts(|fonts| fonts.atlas_generation()),
env: Env::of(ui).key,
fits: Env::of(ui).row_unit(),
fits: Env::of(ui).view_fits(),
};
if real && memo {
crate::runtime::memory::claim_id(&ctx, id, "two memoized views share an id in one pass");
+2 -1
View File
@@ -53,7 +53,8 @@ use std::sync::Arc;
/// cursor) does not reach in, so the room it is given reaches it only through its place. To the body
/// around it the view is one unit, sized by its content's measure. In a shrinking `Row` it is a unit the row narrows
/// as it narrows a body unit: from what its content takes in no room down to what it takes with every leaf at its
/// least. The width the row gives it is the room of its place, and the leaves of its content fit that room.
/// least. The width the row gives it is the room of its place, and the leaves of its content fit that room, as they
/// fit the room of a `Stack` the view is shown in.
///
/// What `show` returns holds layout facts only (a size, a rect, which part took the room). A replay returns the kept
/// value again, moved with the record when the room moved (`Shift`), so an event returned through it would arrive once
+8 -6
View File
@@ -409,19 +409,21 @@ impl Scoped {
self.offered = None;
}
/// Starts the layout room of a memoized view's content (`start_room`). A view in a shrinking `Row` is one of the
/// units the row narrows: its content's leaves fit the room the view is given (`Shrink::Fit`).
/// Starts the layout room of a memoized view's content (`start_room`). Where a leaf shown here would fit its room
/// (`view_fits`), the leaves of the view's content fit the room the view is given (`Shrink::Fit`).
pub(crate) fn start_view_room(&mut self) {
let fits = self.row_unit();
let fits = self.view_fits();
self.start_room();
if fits {
self.shrink = Some(Shrink::Fit);
}
}
/// Whether a child shown in flow here is a unit of the shrinking `Row` around it.
pub(crate) const fn row_unit(&self) -> bool {
matches!(self.shrink, Some(Shrink::Count(_) | Shrink::Least(_) | Shrink::Apply(_)))
/// Whether a memoized view shown in flow here fits its content to its room, as a leaf here would: in a `Stack` body,
/// whose leaves show at most its room, and as one of the units a shrinking `Row` narrows. In a `Grid` cell, a
/// wrapping `Row` or a `Ui` with no body around it a leaf keeps its measured width, and so does the view's content.
pub(crate) const fn view_fits(&self) -> bool {
matches!(self.shrink, Some(Shrink::Count(_) | Shrink::Least(_) | Shrink::Apply(_) | Shrink::Fit))
}
/// The key of everything a memoized view shown in this scope inherits beyond `key` (the style inputs, which its
+2 -2
View File
@@ -182,8 +182,8 @@ pub(crate) struct Place {
/// The scope's environment (`Env::key`): its tokens (theme, density), scale, direction, language and whether the
/// window is active.
pub(crate) env: EnvKey,
/// Whether the view is a unit of the shrinking `Row` around it, so the leaves of its content fit the view's room
/// (`Scoped::start_view_room`).
/// Whether the leaves of the view's content fit the view's room, as in a `Stack` or a shrinking `Row`
/// (`Scoped::view_fits`).
pub(crate) fits: bool,
}