Files
craft-libs/crates/behavior/src/dock/workspaces.rs
T
aiongg 42739aaa2d Dock on Derived: tracked panel specs, workspace revisions, layout-owned window ids
- `Workspace` fields are private. `revision()` moves with every write (a clone is a
  new source); `generation()` moves only on `replace` and ends drags as the old
  revision did. A trial walks every `&mut self` method, read from the source.
- Panel specs and permissions leave `DockPanels` for `PanelSpecs`, a plain-data model
  the host keeps in a `Tracked`. `DockArea` binds `Arc<Workspace>` and derives specs,
  permissions and keys from the workspace revision, the specs revision and the lock:
  an idle pass reads no spec, read-only docks included.
- `DockGrid::rail(cells)` is sized by the dock from its own tokens.
- Floating window ids come from a counter the layout owns; `replace` gives incoming
  windows ids the replaced layout never gave out. The dock's global id counter and its
  `no-global-state` allowlist entry are gone.

Part of #2336.
2026-11-20 18:16:52 +00:00

307 lines
12 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Named workspaces (Window › Workspace). A workspace is a saved [`Workspace`] layout plus which bars show;
//! the set holds the app's built-in workspaces, the user's saved ones, the current one,
//! Lock Workspace and Reset.
use std::{collections::BTreeMap, hash::Hash};
use super::{layout::Layout, workspace::Workspace};
/// Chrome a workspace remembers beside the dock layout: which bars show. The default shows every bar.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct WorkspaceChrome {
/// The menu bar shows.
pub menu_bar: bool,
/// The tool rail shows.
pub tool_rail: bool,
/// The options bar shows.
pub options_bar: bool,
/// The status bar shows.
pub status_bar: bool,
}
impl Default for WorkspaceChrome {
fn default() -> Self {
Self { menu_bar: true, tool_rail: true, options_bar: true, status_bar: true }
}
}
/// One named workspace.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(bound(deserialize = "P: serde::Deserialize<'de>"))]
#[non_exhaustive]
pub struct NamedWorkspace<P> {
/// Stable id (`"essentials"`, or a slug of a saved name).
pub id: String,
/// What the menus and the switcher show.
pub name: String,
/// The panel layout.
pub layout: Workspace<P>,
/// Which bars show.
pub chrome: WorkspaceChrome,
/// What it captured beside the panel layout.
#[serde(flatten)]
pub keeps: WorkspaceKeeps,
}
/// What a saved workspace captures beside the panel layout (the Capture boxes of New Workspace). The application
/// stores and restores what each flag names; the set only records the choice.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[non_exhaustive]
pub struct WorkspaceKeeps {
/// Keyboard shortcuts.
#[serde(rename = "keeps_shortcuts", default)]
pub shortcuts: bool,
/// Menu customizations.
#[serde(rename = "keeps_menus", default)]
pub menus: bool,
/// Toolbar customizations.
#[serde(rename = "keeps_toolbar", default)]
pub toolbar: bool,
}
impl WorkspaceKeeps {
/// Captures nothing beside the layout.
pub const NONE: WorkspaceKeeps = WorkspaceKeeps { shortcuts: false, menus: false, toolbar: false };
/// Captures shortcuts, menus and toolbar.
pub const ALL: WorkspaceKeeps = WorkspaceKeeps { shortcuts: true, menus: true, toolbar: true };
/// The keeps with shortcuts, menus and toolbar as given.
pub const fn new(shortcuts: bool, menus: bool, toolbar: bool) -> Self {
WorkspaceKeeps { shortcuts, menus, toolbar }
}
}
/// The app's workspaces: built-ins first, saved ones after, one current. It is a settings
/// section (serialized as [`Workspace`] is); the layouts of built-ins are
/// code, and only their user edits ([`edited`](Self::edited)) are stored. Files of an older
/// [`Workspace::VERSION`] load too.
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(from = "StoredSet<P>", bound(deserialize = "P: serde::Deserialize<'de>"))]
#[non_exhaustive]
pub struct WorkspaceSet<P> {
/// Workspaces the app defines in code; the app supplies them again on every start, so they
/// are not stored.
#[serde(skip)]
pub builtin: Vec<NamedWorkspace<P>>,
/// Workspaces the user saved.
pub saved: Vec<NamedWorkspace<P>>,
/// Id of the current workspace.
pub current: String,
/// Lock Workspace: pass it to [`Workspace::apply`] and [`Workspace::set_visible`]. Moves,
/// floating and closes are refused; activating, opening, minimizing, resizing and
/// recording native windows still work.
pub locked: bool,
/// The live layout each workspace was left with, by id, until Reset or Delete. Photoshop
/// remembers the state of each workspace this way.
pub edited: BTreeMap<String, Workspace<P>>,
}
impl<P> Default for WorkspaceSet<P> {
fn default() -> Self {
Self { builtin: Vec::new(), saved: Vec::new(), current: String::new(), locked: false, edited: BTreeMap::new() }
}
}
impl<P> NamedWorkspace<P> {
/// A workspace named `name` with the stable id `id`, showing every bar and capturing nothing beside the layout.
pub fn new(id: impl Into<String>, name: impl Into<String>, layout: Workspace<P>) -> Self {
Self { id: id.into(), name: name.into(), layout, chrome: WorkspaceChrome::default(), keeps: WorkspaceKeeps::NONE }
}
}
impl<P: Clone + Eq + Hash> WorkspaceSet<P> {
/// A set of `builtin` workspaces with the first one current.
pub fn new(builtin: Vec<NamedWorkspace<P>>) -> Self {
let current = builtin.first().map(|w| w.id.clone()).unwrap_or_default();
Self { builtin, current, ..Self::default() }
}
/// The current workspace, or `None` when `current` names none.
pub fn current(&self) -> Option<&NamedWorkspace<P>> {
self.find(&self.current)
}
/// The workspace with this id.
pub fn find(&self, id: &str) -> Option<&NamedWorkspace<P>> {
self.builtin.iter().chain(&self.saved).find(|w| w.id == id)
}
/// `base`, or `base-2`, `base-3`… when another workspace has the id.
fn free_id(&self, base: &str) -> String {
let taken = |id: &str| self.find(id).is_some();
if !taken(base) {
return base.to_string();
}
(2..).map(|n| format!("{base}-{n}")).find(|id| !taken(id)).unwrap_or_default()
}
/// The layout the current workspace shows: what it was left with, else its stored one.
pub fn layout(&self) -> Option<&Workspace<P>> {
self.edited.get(&self.current).or_else(|| self.current().map(|w| &w.layout))
}
/// Switch to `id` and replace `live` with the layout it shows; the current workspace's live
/// layout is kept under its id first (Photoshop remembers the state of each workspace until
/// Reset). Selecting the current workspace changes nothing.
pub fn select(&mut self, id: &str, live: &mut Workspace<P>) -> Result<(), WorkspaceError> {
if self.find(id).is_none() {
return Err(WorkspaceError::UnknownId);
}
if id == self.current {
return Ok(());
}
if self.find(&self.current).is_some() {
self.edited.insert(self.current.clone(), live.clone());
}
self.current = id.to_string();
live.replace(self.layout().cloned().unwrap_or_default());
Ok(())
}
/// Whether `name` can be saved: the trimmed name, and the id of the saved workspace it would replace (any case),
/// if any. An empty name and a built-in's name or id are refused.
pub fn check_name<'n>(&self, name: &'n str) -> Result<(&'n str, Option<&str>), WorkspaceError> {
let name = name.trim();
if name.is_empty() {
return Err(WorkspaceError::EmptyName);
}
let same = |other: &str| other.to_lowercase() == name.to_lowercase();
if self.builtin.iter().any(|w| same(&w.name) || same(&w.id)) {
return Err(WorkspaceError::NameTaken);
}
Ok((name, self.saved.iter().find(|w| same(&w.name)).map(|w| w.id.as_str())))
}
/// Save `live` under `name`, capturing `keeps`, and make it current; [`check_name`](Self::check_name) refuses
/// the name or names the saved workspace it replaces.
pub fn save(&mut self, name: &str, live: Workspace<P>, chrome: WorkspaceChrome, keeps: WorkspaceKeeps) -> Result<String, WorkspaceError> {
let (name, existing) = self.check_name(name)?;
let existing = existing.map(str::to_owned);
let id = existing.clone().unwrap_or_else(|| self.free_id(&slug(name)));
let workspace = NamedWorkspace { id: id.clone(), name: name.to_string(), layout: live, chrome, keeps };
match self.saved.iter().position(|w| Some(&w.id) == existing.as_ref()) {
Some(i) => self.saved[i] = workspace,
None => self.saved.push(workspace),
}
self.edited.remove(&id);
self.current = id.clone();
Ok(id)
}
/// Delete a saved workspace; the current one can't be deleted (Photoshop greys it).
pub fn delete(&mut self, id: &str) -> Result<(), WorkspaceError> {
let Some(i) = self.saved.iter().position(|w| w.id == id) else {
return Err(WorkspaceError::UnknownId);
};
if self.current == id {
return Err(WorkspaceError::CurrentCannotBeDeleted);
}
self.saved.remove(i);
self.edited.remove(id);
Ok(())
}
/// Reset the current workspace: drop what it was left with and replace `live` with its stored
/// layout (a built-in's code layout, or the layout it was saved with).
pub fn reset(&mut self, live: &mut Workspace<P>) -> Result<(), WorkspaceError> {
let stored = self.current().map(|w| w.layout.clone()).ok_or(WorkspaceError::UnknownId)?;
self.edited.remove(&self.current);
live.replace(stored);
Ok(())
}
}
/// Lower-case alphanumerics joined by single dashes; the name itself when it has none.
fn slug(name: &str) -> String {
let words: Vec<String> = name.split(|c: char| !c.is_alphanumeric()).filter(|w| !w.is_empty()).map(str::to_lowercase).collect();
if words.is_empty() { name.to_string() } else { words.join("-") }
}
/// Why a [`WorkspaceSet`] edit was refused.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum WorkspaceError {
/// No workspace has the id.
UnknownId,
/// A built-in workspace already has the name.
NameTaken,
/// The name is empty after trimming.
EmptyName,
/// The current workspace cannot be deleted.
CurrentCannotBeDeleted,
}
/// The file shape of a [`WorkspaceSet`], which also reads the files of format versions 1 and 2.
/// Those carried a `locked` and a `saved` layout on every [`Workspace`]; the set owns both now.
#[derive(serde::Deserialize)]
#[serde(bound(deserialize = "P: serde::Deserialize<'de>"))]
struct StoredSet<P> {
#[serde(default = "Vec::new")]
saved: Vec<StoredNamed<P>>,
#[serde(default)]
current: String,
#[serde(default)]
locked: bool,
#[serde(default = "BTreeMap::new")]
edited: BTreeMap<String, StoredWorkspace<P>>,
}
#[derive(serde::Deserialize)]
#[serde(bound(deserialize = "P: serde::Deserialize<'de>"))]
struct StoredNamed<P> {
id: String,
name: String,
layout: StoredWorkspace<P>,
chrome: WorkspaceChrome,
#[serde(flatten)]
keeps: WorkspaceKeeps,
}
/// A [`Workspace`] with the fields of format version 2 it no longer has.
#[derive(serde::Deserialize)]
#[serde(bound(deserialize = "P: serde::Deserialize<'de>"))]
struct StoredWorkspace<P> {
#[serde(flatten)]
workspace: Workspace<P>,
#[serde(default)]
locked: bool,
#[serde(default = "no_layout")]
saved: Option<Layout<P>>,
}
fn no_layout<P>() -> Option<Layout<P>> {
None
}
impl<P> From<StoredSet<P>> for WorkspaceSet<P> {
/// A lock on any stored workspace locks the set. A saved arrangement becomes the stored
/// layout Reset returns to; one on a built-in is dropped, since the code supplies it.
fn from(stored: StoredSet<P>) -> Self {
let mut locked = stored.locked;
let mut saved: Vec<NamedWorkspace<P>> = stored
.saved
.into_iter()
.map(|named| {
let StoredWorkspace { mut workspace, locked: lock, saved } = named.layout;
locked |= lock;
if let Some(layout) = saved {
workspace = workspace.with_layout(layout);
}
NamedWorkspace { id: named.id, name: named.name, layout: workspace, chrome: named.chrome, keeps: named.keeps }
})
.collect();
let mut edited = BTreeMap::new();
for (id, stored) in stored.edited {
locked |= stored.locked;
if let Some(layout) = stored.saved
&& let Some(named) = saved.iter_mut().find(|w| w.id == id)
{
named.layout = std::mem::take(&mut named.layout).with_layout(layout);
}
edited.insert(id, stored.workspace);
}
Self { builtin: Vec::new(), saved, current: stored.current, locked, edited }
}
}