mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 18:41:27 +01:00
ui-patterns: WorkspaceSwitcher::commands replaces the switcher's header menu rows
This commit is contained in:
1 parent
d059b92889
commit
e2dca317ec
3 files changed
+58
-11
No files matched your search
@@ -42,35 +42,44 @@ impl Outcome for WorkspaceSwitcherOutput {
|
||||
#[pattern(probe(WorkspaceSwitcher<'static, u32>))]
|
||||
pub struct WorkspaceSwitcher<'a, P> {
|
||||
set: &'a WorkspaceSet<P>,
|
||||
/// The header menu's command rows in place of the default ones ([`WorkspaceSwitcher::commands`]).
|
||||
commands: Option<Vec<CommandRef>>,
|
||||
}
|
||||
|
||||
impl<'a, P> WorkspaceSwitcher<'a, P> {
|
||||
/// A switcher over `set`.
|
||||
pub fn new(set: &'a WorkspaceSet<P>) -> 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<CommandRef>) -> Self {
|
||||
let _ = (self, rows);
|
||||
todo!("ch.K40")
|
||||
pub fn commands(mut self, rows: Vec<CommandRef>) -> 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::<DockWords>();
|
||||
let entries: Vec<Entry<String>> = 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);
|
||||
|
||||
@@ -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::<u32>::default());
|
||||
let set = WorkspaceSet::new(vec![essentials]);
|
||||
WorkspaceSwitcher::new(&set).commands(vec![WorkspaceCommands::New.into(), WorkspaceCommands::Reset.into()]).show(ui).into()
|
||||
}
|
||||
@@ -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<Vec<CommandRef>>) -> (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");
|
||||
}
|
||||
Reference in new issue
Block a user