From e2dca317ec4ce4712fbf65ec01aaa68998eae043 Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 04:58:37 +0700 Subject: [PATCH] ui-patterns: WorkspaceSwitcher::commands replaces the switcher's header menu rows --- .../ui-patterns/src/patterns/dock/switcher.rs | 29 +++++++++++------ .../src/patterns/dock/switcher/stories.rs | 9 ++++++ .../tests/it/docking/workspace_dialogs.rs | 31 ++++++++++++++++++- 3 files changed, 58 insertions(+), 11 deletions(-) diff --git a/crates/ui-patterns/src/patterns/dock/switcher.rs b/crates/ui-patterns/src/patterns/dock/switcher.rs index c4ed88136..773c5e65e 100644 --- a/crates/ui-patterns/src/patterns/dock/switcher.rs +++ b/crates/ui-patterns/src/patterns/dock/switcher.rs @@ -42,35 +42,44 @@ impl Outcome for WorkspaceSwitcherOutput { #[pattern(probe(WorkspaceSwitcher<'static, u32>))] pub struct WorkspaceSwitcher<'a, P> { set: &'a WorkspaceSet

, + /// The header menu's command rows in place of the default ones ([`WorkspaceSwitcher::commands`]). + commands: Option>, } impl<'a, P> WorkspaceSwitcher<'a, P> { /// A switcher over `set`. pub fn new(set: &'a WorkspaceSet

) -> Self { - WorkspaceSwitcher { set } + WorkspaceSwitcher { set, commands: None } } /// The header menu's command rows, in place of the default `WorkspaceCommands` rows. #[must_use] - pub fn commands(self, rows: Vec) -> Self { - let _ = (self, rows); - todo!("ch.K40") + pub fn commands(mut self, rows: Vec) -> Self { + self.commands = Some(rows); + self } /// Shows the switcher and reports the workspace picked. pub fn show(self, ui: &mut Ui) -> WorkspaceSwitcherOutput { - let set = self.set; + let WorkspaceSwitcher { set, commands } = self; let words = Env::of(ui).words::(); let entries: Vec> = set.builtin.iter().chain(&set.saved).map(|w| Entry::new(w.id.clone(), w.name.clone())).collect(); let shown = Select::new(words.choose_workspace.clone(), Bind::given(set.current.clone()), &entries) .size(ControlSize::Sm) .sizing(Sizing::Content) - .header_menu(|ui, _| { - for ran in [WorkspaceCommands::Reset, WorkspaceCommands::New, WorkspaceCommands::Delete, WorkspaceCommands::Lock] { - if ran == WorkspaceCommands::Lock { - Divider::horizontal().show(ui); + .header_menu(|ui, _| match &commands { + Some(rows) => { + for row in rows { + MenuItem::command(row.clone()).show(ui); + } + } + None => { + for ran in [WorkspaceCommands::Reset, WorkspaceCommands::New, WorkspaceCommands::Delete, WorkspaceCommands::Lock] { + if ran == WorkspaceCommands::Lock { + Divider::horizontal().show(ui); + } + MenuItem::command(ran).show(ui); } - MenuItem::command(ran).show(ui); } }) .show(ui); diff --git a/crates/ui-patterns/src/patterns/dock/switcher/stories.rs b/crates/ui-patterns/src/patterns/dock/switcher/stories.rs index b72a6ac1d..03e917f8b 100644 --- a/crates/ui-patterns/src/patterns/dock/switcher/stories.rs +++ b/crates/ui-patterns/src/patterns/dock/switcher/stories.rs @@ -1,6 +1,7 @@ //! Stories of `WorkspaceSwitcher`. use super::WorkspaceSwitcher; +use crate::docking::WorkspaceCommands; use crate::testing::{Drive, StoryData, StoryOutput, story}; use craft_behavior::dock::{NamedWorkspace, Workspace, WorkspaceSet}; use egui::Ui; @@ -12,3 +13,11 @@ fn workspace_switcher(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { let set = WorkspaceSet::new(vec![essentials, painting]); WorkspaceSwitcher::new(&set).show(ui).into() } + +/// A switcher whose list offers the app's own command rows in place of the default ones. +#[story(WorkspaceSwitcher<'static, u32>, covers = [], drive = Drive::Never)] +fn workspace_switcher_commands(ui: &mut Ui, _: &mut StoryData) -> StoryOutput { + let essentials = NamedWorkspace::new("essentials", "Essentials", Workspace::::default()); + let set = WorkspaceSet::new(vec![essentials]); + WorkspaceSwitcher::new(&set).commands(vec![WorkspaceCommands::New.into(), WorkspaceCommands::Reset.into()]).show(ui).into() +} diff --git a/crates/ui-patterns/tests/it/docking/workspace_dialogs.rs b/crates/ui-patterns/tests/it/docking/workspace_dialogs.rs index db9b3c911..44dd0c5b2 100644 --- a/crates/ui-patterns/tests/it/docking/workspace_dialogs.rs +++ b/crates/ui-patterns/tests/it/docking/workspace_dialogs.rs @@ -2,7 +2,9 @@ use craft_behavior::dock::{NamedWorkspace, Workspace, WorkspaceChrome, WorkspaceKeeps, WorkspaceSet}; use craft_ui::dialog::{DialogHost, DialogResult, StackDialog}; -use craft_ui::patterns::dock::{DeleteWorkspaceDialog, HostedNewWorkspace, NewWorkspace, NewWorkspaceDialog}; +use craft_ui::docking::WorkspaceCommands; +use craft_ui::patterns::dock::{DeleteWorkspaceDialog, HostedNewWorkspace, NewWorkspace, NewWorkspaceDialog, WorkspaceSwitcher}; +use craft_ui::runtime::commands::CommandRef; use craft_ui::testing::{Fixture, trial}; use egui::Vec2; use serde_json::json; @@ -97,3 +99,30 @@ fn the_hosted_new_workspace_refuses_a_built_in_name_and_answers_through_the_host assert_eq!(answer.name, "Mine 2"); assert_eq!(answer.keeps, WorkspaceKeeps::new(false, false, true), "the boxes set through the fields are the captures"); } + +/// Whether the switcher's header menu shows Reset Workspace and New Workspace…: the default workspace commands, or the +/// rows the app gives in their place. +fn switcher_rows(commands: Option>) -> (bool, bool) { + let set = workspaces(); + let mut fixture = Fixture::new(move |ui| { + let switcher = WorkspaceSwitcher::new(&set); + let _ = match commands.clone() { + Some(rows) => switcher.commands(rows), + None => switcher, + } + .show(ui); + }); + fixture.settle(); + let select = fixture.by_label("Choose a workspace").expect("the switcher"); + fixture.click(&select).settle(); + let more = fixture.by_label("More options").expect("the list's header menu button"); + fixture.click(&more).settle(); + (shows(&fixture, "Reset Workspace"), shows(&fixture, "New Workspace…")) +} + +// The switcher's list has the default workspace commands, and only the app's rows when it gives its own. +#[trial] +fn the_switcher_lists_the_apps_command_rows_in_place_of_the_default_ones() { + assert_eq!(switcher_rows(None), (true, true), "the default rows"); + assert_eq!(switcher_rows(Some(vec![WorkspaceCommands::New.into()])), (false, true), "only the app's rows"); +}