mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Panels: load layouts saved before workspaces as the default
A saved docking value that is not a craft-ui workspace, such as the bare layout with dockingHidden written before workspaces, no longer migrates: the window opens with the default layout built from the saved panels, dockTabs and dock, and its collapsed custom groups are dropped. Workspaces load as saved. The width a user dragged the native dock to, which egui remembers under the native dock's panel id, sizes the default column once.
This commit is contained in:
1 parent
cfbefc5dd1
commit
bedcab596f
4 files changed
+63
-48
No files matched your search
@@ -856,10 +856,14 @@ pub fn restore(app: &mut PhotocraftApp) {
|
||||
/// (a workspace or `panelLayout`). Missing or invalid dock parts are left alone; old
|
||||
/// layouts without Timeline visibility restore it closed.
|
||||
pub fn apply(app: &mut PhotocraftApp, v: &Value) {
|
||||
app.ui.docking_collapsed = v.get("dockingCollapsed").and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default();
|
||||
app.ui.docking_reveal = None;
|
||||
app.ui.docking_generation = app.ui.docking_generation.wrapping_add(1);
|
||||
(app.ui.docking, app.ui.docking_returns) = crate::panel_docking::load(v).map_or((None, Default::default()), |(w, r)| (Some(w), r));
|
||||
// Collapsed custom groups belong to the workspace; the native dock keeps its own flags.
|
||||
app.ui.docking_collapsed = match app.ui.docking {
|
||||
Some(_) => v.get("dockingCollapsed").and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
if let Some(mut p) = v.get("panels").and_then(|p| serde_json::from_value::<Panels>(p.clone()).ok()) {
|
||||
// The embedding host owns these flags, including for layouts that serialize them.
|
||||
p.menu_bar = app.ui.panels.menu_bar;
|
||||
|
||||
@@ -154,29 +154,14 @@ pub(crate) fn valid_workspace(workspace: &Workspace) -> bool {
|
||||
&& workspace.hidden.iter().all(|(id, at)| registered(id) && at.anchor.as_deref().is_none_or(registered))
|
||||
}
|
||||
|
||||
/// Read a saved `docking` value: a workspace, or a bare layout written before workspaces, whose
|
||||
/// hidden panels are in `dockingHidden` (panel to return place). There, entries for panels the
|
||||
/// layout shows are where a floated panel docks back.
|
||||
/// Read a saved `docking` workspace and where its floating panels dock back. Anything else,
|
||||
/// such as the bare layouts written before workspaces, gives `None`: the native dock's default.
|
||||
pub(crate) fn load(saved: &Value) -> Option<(Workspace, Returns)> {
|
||||
let docking = saved.get("docking").filter(|v| !v.is_null())?;
|
||||
let bare = docking.get("layout").is_none();
|
||||
let mut workspace: Workspace = if bare {
|
||||
Workspace { version: 1, ..Workspace::new(serde_json::from_value(docking.clone()).ok()?) }
|
||||
} else {
|
||||
serde_json::from_value(docking.clone()).ok()?
|
||||
};
|
||||
let docking = saved.get("docking").filter(|v| v.get("layout").is_some())?;
|
||||
let mut workspace: Workspace = serde_json::from_value(docking.clone()).ok()?;
|
||||
workspace.layout = beside_canvas(workspace.layout);
|
||||
workspace.saved = workspace.saved.map(beside_canvas);
|
||||
let mut returns: Returns = saved.get("dockingReturns").and_then(|v| serde_json::from_value(v.clone()).ok()).unwrap_or_default();
|
||||
if bare && let Some(hidden) = saved.get("dockingHidden").and_then(|v| serde_json::from_value::<Returns>(v.clone()).ok()) {
|
||||
for (panel, at) in hidden {
|
||||
if workspace.layout.contains(&panel) {
|
||||
returns.insert(panel, at);
|
||||
} else {
|
||||
workspace.hidden.push((panel, at));
|
||||
}
|
||||
}
|
||||
}
|
||||
returns.retain(|panel, at| registered(panel) && at.floating.is_none() && floats(&workspace.layout, panel));
|
||||
valid_workspace(&workspace).then_some((workspace, returns))
|
||||
}
|
||||
@@ -567,6 +552,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
|
||||
let range = crate::panels::dock_width_range(ui.ctx().content_rect().width());
|
||||
set_dock_width(app, width.clamp(*range.start(), *range.end()));
|
||||
}
|
||||
native_dock_width(app, ui.ctx());
|
||||
let layout = match &app.ui.docking {
|
||||
Some(workspace) => workspace.layout.clone(),
|
||||
None => legacy(app),
|
||||
@@ -672,6 +658,24 @@ fn set_dock_width(app: &mut PhotocraftApp, width: f32) {
|
||||
}
|
||||
}
|
||||
|
||||
/// The width the user dragged the native dock to is in egui's remembered memory under the native
|
||||
/// dock's panel id. The default layout's column takes it once, after the canvas has been laid
|
||||
/// out so the Studio Properties card keeps its place.
|
||||
fn native_dock_width(app: &mut PhotocraftApp, ctx: &egui::Context) {
|
||||
let id = egui::Id::new("dock");
|
||||
let Some(state) = ctx.data_mut(|data| data.get_persisted::<egui::PanelState>(id)) else { return };
|
||||
if app.ui.docking.is_none() && !app.last_canvas_rect.is_positive() {
|
||||
return;
|
||||
}
|
||||
ctx.data_mut(|data| data.remove::<egui::PanelState>(id));
|
||||
let pro = matches!(app.ui.theme, crate::theme::ThemeKind::Pro | crate::theme::ThemeKind::ProMedium);
|
||||
let range = crate::panels::dock_width_range(ctx.content_rect().width());
|
||||
let width = state.size().x;
|
||||
if app.ui.docking.is_none() && width.is_finite() && (width - default_width(pro)).abs() >= 1.0 {
|
||||
set_dock_width(app, width.clamp(*range.start(), *range.end()));
|
||||
}
|
||||
}
|
||||
|
||||
/// `layout` as drawn: a dock group whose panels are all collapsed shows only its tab strip.
|
||||
fn presented(mut layout: Layout<String>, collapsed: &std::collections::HashSet<String>) -> Layout<String> {
|
||||
let mut pending: Vec<&mut Node<String>> = layout.root.iter_mut().collect();
|
||||
|
||||
@@ -909,43 +909,49 @@ fn the_dock_can_be_as_wide_as_the_window_allows() {
|
||||
}
|
||||
}
|
||||
|
||||
/// A workspace file written by main before workspaces had a version: a bare `docking` layout
|
||||
/// with one-group floating windows, `dockingHidden` (closed panels and a floated panel's dock
|
||||
/// place) and `dockingCollapsed`.
|
||||
/// A workspace file written by main: a bare `docking` layout with `dockingHidden` and
|
||||
/// `dockingCollapsed` beside the native keys. It loads as the default built from the native
|
||||
/// keys, and the first change saves a versioned workspace.
|
||||
#[test]
|
||||
fn main_workspace_file_keeps_its_arrangement_and_saves_as_a_workspace() {
|
||||
fn main_workspace_file_loads_as_the_default_and_saves_as_a_workspace() {
|
||||
let saved: Value = serde_json::from_str(include_str!("../tests/fixtures/main-docking-workspace.json")).unwrap();
|
||||
let mut app = app();
|
||||
app.ui.theme = crate::theme::ThemeKind::ProMedium;
|
||||
crate::dock::apply(&mut app, &saved);
|
||||
let workspace = app.ui.docking.clone().expect("the custom layout loads");
|
||||
assert_eq!(workspace.version, 1);
|
||||
assert!(app.ui.docking.is_none() && app.ui.docking_returns.is_empty() && app.ui.docking_collapsed.is_empty());
|
||||
let workspace = legacy_workspace(&app);
|
||||
let layout = &workspace.layout;
|
||||
assert_eq!(root_groups(layout), [vec!["swatches"], vec!["color", "gradients", "patterns"], vec!["history", "actions"], vec!["layers", "channels"]]);
|
||||
assert!(panel_active(layout, "actions") && panel_active(layout, "color"));
|
||||
let window = layout.floating.first().unwrap();
|
||||
assert_eq!((window.rect, group_members(layout, "adjustments")), ([420.0, 150.0, 320.0, 440.0], vec!["properties".into(), "adjustments".into()]));
|
||||
let hidden: Vec<_> = workspace.hidden.iter().map(|(panel, at)| (panel.as_str(), at.anchor.as_deref())).collect();
|
||||
assert_eq!(hidden, [("layerComps", Some("actions")), ("paths", Some("channels"))]);
|
||||
assert_eq!(app.ui.docking_returns.get("properties").and_then(|at| at.anchor.as_deref()), Some("layers"));
|
||||
assert!(collapsed_members(&app, layout).contains("history"));
|
||||
assert_eq!(
|
||||
root_groups(layout),
|
||||
[vec!["color", "swatches", "gradients", "patterns"], vec!["properties", "adjustments"], vec!["history", "actions"], vec!["layers", "channels"]]
|
||||
);
|
||||
assert!(in_dock_column(layout, "layers") && panel_active(layout, "actions"));
|
||||
for (panel, _, _) in PANELS {
|
||||
let panel = panel.to_string();
|
||||
assert!(layout.contains(&panel) || workspace.hidden.iter().any(|(id, _)| *id == panel), "{panel} is shown or can be reopened");
|
||||
}
|
||||
|
||||
app.run("window.panel.float", json!({"panel":"properties"})).unwrap();
|
||||
let written = crate::dock::snapshot(&app);
|
||||
assert!(written.get("dockingHidden").is_none());
|
||||
assert_eq!(written["docking"]["version"], 2);
|
||||
assert_eq!(written["docking"]["layout"]["floating"][0]["panels"], json!(["properties", "adjustments"]));
|
||||
let mut reloaded = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
|
||||
crate::dock::apply(&mut reloaded, &written);
|
||||
let again = reloaded.ui.docking.as_ref().unwrap();
|
||||
assert_eq!((&again.layout, &again.hidden), (&workspace.layout, &workspace.hidden));
|
||||
assert_eq!((&reloaded.ui.docking_returns, &reloaded.ui.docking_collapsed), (&app.ui.docking_returns, &app.ui.docking_collapsed));
|
||||
let again = reloaded.ui.docking.as_ref().expect("the workspace loads as saved");
|
||||
let now = app.ui.docking.as_ref().unwrap();
|
||||
assert_eq!((&again.layout, &again.hidden), (&now.layout, &now.hidden));
|
||||
assert_eq!(reloaded.ui.docking_returns, app.ui.docking_returns);
|
||||
}
|
||||
|
||||
app.session.prefs.edit(|prefs| prefs.workspace_locked = true);
|
||||
assert!(app.run("window.panel.move", json!({"panel":"swatches","anchor":"layers"})).is_err(), "Lock Workspace still applies");
|
||||
app.session.prefs.edit(|prefs| prefs.workspace_locked = false);
|
||||
app.run("window.panel.activate", json!({"panel":"paths"})).unwrap();
|
||||
assert_eq!(group_members(lay(&app), "paths"), ["layers", "channels", "paths"]);
|
||||
app.run("window.panel.dock", json!({"panel":"properties"})).unwrap();
|
||||
let location = lay(&app).location(&"properties".to_string()).unwrap();
|
||||
assert_eq!((location.floating, location.anchor.as_deref()), (None, Some("layers")), "a floated panel docks back where it was");
|
||||
/// The width a user gave the native dock, kept in egui's memory, sizes the default column once.
|
||||
#[test]
|
||||
fn the_native_dock_width_sizes_the_default_column() {
|
||||
let mut h = harness(app(), vec2(1280.0, 800.0));
|
||||
let state = egui::PanelState { outer_rect: egui::Rect::from_min_size(egui::pos2(900.0, 0.0), vec2(380.0, 700.0)) };
|
||||
h.ctx.data_mut(|data| data.insert_persisted(egui::Id::new("dock"), state));
|
||||
h.run_steps(2);
|
||||
let Some(Node::Split { second, .. }) = &lay(h.state()).root else { panic!("expected the canvas split") };
|
||||
let Node::Dock { columns, .. } = second.as_ref() else { panic!("expected the dock") };
|
||||
assert_eq!(columns[0].width, 380.0);
|
||||
assert!(h.ctx.data_mut(|data| data.get_persisted::<egui::PanelState>(egui::Id::new("dock"))).is_none(), "taken once");
|
||||
}
|
||||
@@ -690,8 +690,9 @@ split paths return an error without changing the workspace.
|
||||
The `docking` object in `ui.state` and in saved workspaces is craft-ui's `Workspace`:
|
||||
`version` (2), `layout`, `hidden` (each closed panel with its return place), `saved` and
|
||||
`locked`. `dockingReturns` holds where each floating panel docks back, and
|
||||
`dockingCollapsed` the panels of collapsed custom groups. Workspaces saved before the version
|
||||
field (a bare `docking` layout with `dockingHidden`) load unchanged.
|
||||
`dockingCollapsed` the panels of collapsed custom groups. Any other saved `docking` value,
|
||||
such as a bare layout with `dockingHidden`, loads as the default layout, built from the
|
||||
saved `panels`, `dockTabs` and `dock`.
|
||||
|
||||
The existing `ui.set` panel visibility, `dockTabs` selection and `dockWidth` controls
|
||||
remain available after customization. Width keeps its 250–520 point clamp. Named
|
||||
|
||||
Reference in new issue
Block a user