mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 00:57:26 +00:00
title bar: show_fitted puts the trailing content after the body on its line, so ranked members leave before the leading content narrows
This commit is contained in:
@@ -11,6 +11,7 @@ use crate::contract::{
|
||||
PartsCx, Role, ShowContainer as _, Sizing, WordSetCx as _,
|
||||
};
|
||||
use crate::layout::{Placed, arrange};
|
||||
use crate::layout_components::Row;
|
||||
use crate::runtime::access::Name;
|
||||
use crate::runtime::egui::pass;
|
||||
use crate::runtime::env::Env;
|
||||
@@ -67,7 +68,8 @@ impl Outcome for TitleBarOutput {
|
||||
/// In an embedded shell (an app preview, a story) the bar is a picture of a window: it acts on no window and reads
|
||||
/// none. The captions show hover and report their clicks, but send no window command, and the bar has no move band. The body is the leading content; the
|
||||
/// trailing content ([`trailing`](TitleBar::trailing)) sits before the end's captions. [`show_titled`](TitleBar::show_titled)
|
||||
/// adds the title in the gap between them. Close never closes the window; see [`TitleBarOutput`].
|
||||
/// adds the title in the gap between them; [`show_fitted`](TitleBar::show_fitted) puts the trailing content on the
|
||||
/// body's line, after the leading content. Close never closes the window; see [`TitleBarOutput`].
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(container)]
|
||||
pub struct TitleBar<'a> {
|
||||
@@ -246,6 +248,25 @@ impl<'a> TitleBar<'a> {
|
||||
out
|
||||
}
|
||||
|
||||
/// Shows the bar with `leading` content at its start and `trailing` content after it on the same line, at the end
|
||||
/// edge before the end's captions. The trailing content gets the room the leading content's natural width leaves:
|
||||
/// when the line is short, its members that carry a `fit_rank` leave whole, lowest rank first, before the leading
|
||||
/// content narrows (an app bar whose menus stay whole while its end controls go).
|
||||
pub fn show_fitted(
|
||||
self,
|
||||
ui: &mut Ui,
|
||||
mut leading: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a,
|
||||
mut trailing: impl FnMut(&mut Ui, &mut BodyCx<'_>) + 'a,
|
||||
) -> TitleBarOutput {
|
||||
self.show(ui, |ui, cx| {
|
||||
leading(ui, cx);
|
||||
// An empty row that fills the line takes what the members leave, so the trailing content sits at the end.
|
||||
let _ = Row::new().fill_width().show(ui, |_, _| {});
|
||||
trailing(ui, cx);
|
||||
})
|
||||
.output
|
||||
}
|
||||
|
||||
/// Shows one end of the bar and records its clicked caption: Close is reported, never acted on.
|
||||
fn end_of(&mut self, ui: &mut Ui, cx: &BodyCx<'_>, end: &End, window: &Window) {
|
||||
if let Some(caption) = show_end(ui, cx, end, window) {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
//! Stories of `TitleBar`: one per frame mode, and a maximized window.
|
||||
|
||||
use super::TitleBar;
|
||||
use crate::contract::Show as _;
|
||||
use crate::buttons::Button;
|
||||
use crate::contract::{Show as _, ShowContainer as _};
|
||||
use crate::display::Text;
|
||||
use crate::layout_components::Row;
|
||||
use crate::shell::Title;
|
||||
use crate::shell::{Caption, CaptionFamily, Captions, Frame, MacInset};
|
||||
use crate::testing::{Drive, StoryData, StoryOutput, story};
|
||||
@@ -21,6 +23,20 @@ fn tools(ui: &mut Ui, _: &mut crate::contract::BodyCx<'_>) {
|
||||
Text::new("Share").show(ui);
|
||||
}
|
||||
|
||||
/// The width of the narrow fitted bar: the menu and Share fit, the ranked end controls do not.
|
||||
const FITTED_NARROW: f32 = 420.0;
|
||||
|
||||
/// End controls that leave a short bar by rank, then one that stays.
|
||||
fn ranked_tools(ui: &mut Ui, _: &mut crate::contract::BodyCx<'_>) {
|
||||
let _ = Row::new().fit_rank(1).show(ui, |ui, _| {
|
||||
Button::new("Community").show(ui);
|
||||
});
|
||||
let _ = Row::new().fit_rank(2).show(ui, |ui, _| {
|
||||
Button::new("Learn").show(ui);
|
||||
});
|
||||
Text::new("Share").show(ui);
|
||||
}
|
||||
|
||||
#[story(TitleBar<'static>, covers = [Default], drive = Drive::Never)]
|
||||
fn title_bar_windows(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
let frame = captions(CaptionFamily::Windows, &[], &[Caption::Minimize, Caption::Maximize, Caption::Close]);
|
||||
@@ -57,3 +73,12 @@ fn title_bar_action_title(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
fn title_bar_native(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
TitleBar::new().frame(Frame::Native).title_when_native().show_titled(ui, Title::new("Landscape.psd"), menu, tools).into()
|
||||
}
|
||||
|
||||
/// A bar whose end controls fit after its menus: in a narrow window the menus stay whole and the end controls leave by
|
||||
/// rank, the lowest first (Community, then Learn), while the unranked Share stays.
|
||||
#[story(TitleBar<'static>, covers = [], drive = Drive::Never)]
|
||||
fn title_bar_fitted_narrow(ui: &mut Ui, _: &mut StoryData) -> StoryOutput {
|
||||
ui.set_max_width(FITTED_NARROW);
|
||||
let frame = captions(CaptionFamily::Windows, &[], &[Caption::Minimize, Caption::Maximize, Caption::Close]);
|
||||
TitleBar::new().frame(frame).show_fitted(ui, menu, ranked_tools).into()
|
||||
}
|
||||
|
||||
@@ -353,3 +353,52 @@ fn the_lights_room_follows_the_lights_without_mirroring_the_bar() {
|
||||
assert!(rect("Home").left() >= lights - 0.5, "Home {:?} after the lights at the left", rect("Home"));
|
||||
assert!(rect("Preferences").right() >= width - 20.0, "Preferences {:?} at the right end", rect("Preferences"));
|
||||
}
|
||||
|
||||
/// What a fitted bar `width` wide shows: the leading button's rect, and whether the trailing buttons ranked 1 and 2
|
||||
/// are there.
|
||||
fn fitted(width: f32) -> (Rect, [bool; 2]) {
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{Show as _, ShowContainer as _};
|
||||
use craft_ui::layout_components::Row;
|
||||
let mut fixture = Fixture::new(|ui| {
|
||||
let _ = TitleBar::new().frame(Frame::Native).show_fitted(
|
||||
ui,
|
||||
|ui, _| {
|
||||
Button::new("File Edit Object Type Select Effect View Window Help").show(ui);
|
||||
},
|
||||
|ui, _| {
|
||||
let _ = Row::new().fit_rank(1).show(ui, |ui, _| {
|
||||
Button::new("Community").show(ui);
|
||||
});
|
||||
let _ = Row::new().fit_rank(2).show(ui, |ui, _| {
|
||||
Button::new("Learn").show(ui);
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
fixture.room(vec2(width, 80.0)).settle();
|
||||
let leading = fixture.by_label("File Edit Object Type Select Effect View Window Help").expect("the leading button").rect;
|
||||
(leading, ["Community", "Learn"].map(|label| fixture.by_label(label).is_some()))
|
||||
}
|
||||
|
||||
/// The trailing content of a fitted bar takes what the leading content leaves: as the bar narrows, the trailing
|
||||
/// members leave by rank, lowest first, while the leading content keeps its width.
|
||||
#[trial]
|
||||
fn a_fitted_bar_drops_its_trailing_members_by_rank_before_the_leading_content_narrows() {
|
||||
let (whole, all) = fitted(1400.0);
|
||||
assert_eq!(all, [true; 2], "a wide bar shows every trailing member");
|
||||
let shown: Vec<(f32, Rect, [bool; 2])> = (300..=800)
|
||||
.step_by(20)
|
||||
.map(|width| {
|
||||
let (leading, trailing) = fitted(width as f32);
|
||||
(width as f32, leading, trailing)
|
||||
})
|
||||
.collect();
|
||||
for (width, leading, [low, high]) in &shown {
|
||||
assert!(!(*low && !*high), "rank 2 left before rank 1 at {width}");
|
||||
if *low || *high {
|
||||
assert!(leading.width() >= whole.width() - 0.5, "the leading content narrowed to {leading:?} at {width} while a trailing member stayed");
|
||||
}
|
||||
}
|
||||
assert!(shown.iter().any(|(_, _, [low, high])| !low && *high), "rank 1 never left alone: {shown:?}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user