FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Write the earlier workspace layout format alongside saved workspaces
aiongg3c8bb57ebe11
diff --git a/crates/ui-egui/README.md b/crates/ui-egui/README.mdindex 98799841f..ccc6a03e9 100644--- a/crates/ui-egui/README.md+++ b/crates/ui-egui/README.md@@ -94,7 +94,8 @@ be resolved, so a floating window docks back only through a command such as `winWhere egui embeds viewports (tests), floating windows stay inside the main window.Named workspaces save the layout, floating windows included; workspaces saved in the earlier-layout format still load. Automation IDs and workspace commands remain the integration boundary.+layout format still load. While every floating window is one tab group, a save also writes the+earlier format, so earlier versions still open the workspace. Automation IDs and workspace commands remain the integration boundary.Tools and dock tabs expose their actual egui widget IDs, paint keyboard focus, and supportTab/Shift-Tab traversal and Space/Enter activation. Control-injected keys can reach focuseddiff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rsindex d84b183bd..11cbecb3d 100644--- a/crates/ui-egui/src/dock.rs+++ b/crates/ui-egui/src/dock.rs@@ -162,7 +162,9 @@ pub struct WorkspacePrefs {/// A named workspace. `workspace` is a craft-ui [`Workspace`] kept as JSON, so one unreadable/// entry (a panel a newer version added) doesn't lose the others. Files written before/// FilmCraft used craft-ui workspaces hold a frame tree in `layout` and floating groups in-/// `floating` instead; [`saved_workspace`] reads them, and saving writes `workspace`.+/// `floating` instead; [`saved_workspace`] reads them. [`SavedWorkspace::new`] writes+/// `workspace`, and also `layout` and `floating` while every floating window is one tab+/// group, so that earlier versions still open the workspace.#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "camelCase", default)]pub struct SavedWorkspace {@@ -175,6 +177,16 @@ pub struct SavedWorkspace {pub floating: serde_json::Value,}+impl SavedWorkspace {+ pub fn new(name: &str, layout: &Layout) -> Result<Self, String> {+ let workspace = Workspace::new(layout.clone());+ workspace.validate().map_err(|e| e.to_string())?;+ let workspace = serde_json::to_value(&workspace).map_err(|e| e.to_string())?;+ let (layout, floating) = legacy::write(layout).unwrap_or_default();+ Ok(SavedWorkspace { name: name.to_string(), workspace, layout, floating })+ }+}+impl WorkspacePrefs {pub fn load(path: &std::path::Path) -> Self {use std::io::Read as _;@@ -298,11 +310,11 @@ pub fn saved_docking(prefs: &WorkspacePrefs, name: &str) -> Layout {/// [`Workspace`]s.mod legacy {use super::{Floating, Layout, Node, PanelKind, SplitAxis, SplitSize};- use serde::Deserialize;+ use serde::{Deserialize, Serialize};const MAX_DEPTH: usize = 32;- #[derive(Deserialize)]+ #[derive(Deserialize, Serialize)]enum Tree {/// `vertical`: children stacked top/bottom; else side by side.Split {@@ -317,14 +329,14 @@ mod legacy {},}- #[derive(Deserialize)]+ #[derive(Deserialize, Serialize)]enum Size {Ratio(f32),FixedA(f32),FixedB(f32),}- #[derive(Deserialize)]+ #[derive(Deserialize, Serialize)]struct Group {panels: Vec<PanelKind>,active: usize,@@ -350,6 +362,45 @@ mod legacy {})}+ fn tree(node: &Node<PanelKind>, depth: usize) -> Option<Tree> {+ if depth > MAX_DEPTH {+ return None;+ }+ Some(match node {+ Node::Tabs { panels, active } => Tree::Tabs { panels: panels.clone(), active: *active },+ Node::Split { axis, size, first, second } => Tree::Split {+ vertical: *axis == SplitAxis::Vertical,+ size: match *size {+ SplitSize::Ratio(value) => Size::Ratio(value),+ SplitSize::FixedFirst(value) => Size::FixedA(value),+ SplitSize::FixedSecond(value) => Size::FixedB(value),+ },+ a: Box::new(tree(first, depth + 1)?),+ b: Box::new(tree(second, depth + 1)?),+ },+ _ => return None,+ })+ }++ /// `layout` as the `layout` and `floating` fields earlier versions read; `None` when a+ /// floating window holds more than one tab group, which they can't represent.+ pub(super) fn write(layout: &Layout) -> Option<(serde_json::Value, serde_json::Value)> {+ let root = match &layout.root {+ Some(root) => tree(root, 0)?,+ None => Tree::Tabs { panels: Vec::new(), active: 0 },+ };+ let floating = layout+ .floating+ .iter()+ .map(|window| match &window.root {+ Node::Tabs { panels, active } => Some(Group { panels: panels.clone(), active: *active, rect: window.rect }),+ _ => None,+ })+ .collect::<Option<Vec<_>>>()?;+ let floating = if floating.is_empty() { serde_json::Value::Null } else { serde_json::to_value(floating).ok()? };+ Some((serde_json::to_value(root).ok()?, floating))+ }+pub(super) fn layout(root: &serde_json::Value, floating: &serde_json::Value) -> Option<Layout> {let tree: Tree = serde_json::from_value(root.clone()).ok()?;let floating: Vec<Group> = if floating.is_null() { vec![] } else { serde_json::from_value(floating.clone()).ok()? };@@ -876,4 +927,82 @@ mod tests {let back: WorkspacePrefs = serde_json::from_str(&text).unwrap();assert_eq!(saved_docking(&back, "Mine"), mine);}++ /// A saved workspace still opens in versions that read only the earlier `layout` and+ /// `floating` fields, as long as every floating window is one tab group.+ #[test]+ fn saved_workspaces_open_in_earlier_versions() {+ // The shapes earlier versions read the file into.+ #[allow(dead_code)]+ #[derive(Debug, PartialEq, Deserialize)]+ enum OldSize {+ Ratio(f32),+ FixedA(f32),+ FixedB(f32),+ }+ #[allow(dead_code)]+ #[derive(Debug, PartialEq, Deserialize)]+ enum OldNode {+ Split { vertical: bool, size: OldSize, a: Box<OldNode>, b: Box<OldNode> },+ Tabs { panels: Vec<PanelKind>, active: usize },+ }+ #[derive(Debug, PartialEq, Deserialize)]+ struct OldFloating {+ panels: Vec<PanelKind>,+ active: usize,+ rect: [f32; 4],+ }+ #[derive(Default, Deserialize)]+ #[serde(rename_all = "camelCase", default)]+ struct OldSaved {+ layout: serde_json::Value,+ floating: serde_json::Value,+ }+ #[derive(Default, Deserialize)]+ #[serde(rename_all = "camelCase", default)]+ struct OldPrefs {+ saved: Vec<OldSaved>,+ }+ let read = |layout: &Layout| {+ let prefs = WorkspacePrefs { saved: vec![SavedWorkspace::new("Mine", layout).unwrap()], current: "Mine".into() };+ let text = serde_json::to_string(&prefs).unwrap();+ assert_eq!(saved_docking(&serde_json::from_str(&text).unwrap(), "Mine"), *layout, "{text}");+ let old: OldPrefs = serde_json::from_str(&text).unwrap();+ let entry = old.saved.into_iter().next().unwrap();+ let floating: Vec<OldFloating> = if entry.floating.is_null() { vec![] } else { serde_json::from_value(entry.floating).unwrap() };+ (serde_json::from_value::<OldNode>(entry.layout).ok(), floating)+ };++ fn panels(node: &OldNode, out: &mut Vec<PanelKind>) {+ match node {+ OldNode::Split { a, b, .. } => {+ panels(a, out);+ panels(b, out);+ }+ OldNode::Tabs { panels, .. } => out.extend(panels),+ }+ }++ let mut layout = workspace("Editing");+ layout.apply(Action::Float { panel: PanelKind::Info, rect: [10.0, 20.0, 300.0, 200.0] }).unwrap();+ let (root, floating) = read(&layout);+ let mut old = Vec::new();+ panels(&root.unwrap(), &mut old);+ let docked = Layout { root: layout.root.clone(), floating: vec![] };+ assert_eq!(old, docked.panels().into_iter().copied().collect::<Vec<_>>());+ assert_eq!(floating, vec![OldFloating { panels: vec![PanelKind::Info], active: 0, rect: [10.0, 20.0, 300.0, 200.0] }]);++ // A floating window split in two has no earlier form: those versions use the default.+ let mut split = layout.clone();+ split.apply(Action::Float { panel: PanelKind::Effects, rect: [400.0, 20.0, 300.0, 200.0] }).unwrap();+ let window = split.floating.iter().position(|w| w.contains(&PanelKind::Effects)).unwrap();+ let effects = split.floating.remove(window);+ split.floating[0].root = Node::Split {+ axis: SplitAxis::Vertical,+ size: SplitSize::Ratio(0.5),+ first: Box::new(split.floating[0].root.clone()),+ second: Box::new(effects.root),+ };+ assert_eq!(read(&split), (None, vec![]));+ }}diff --git a/crates/ui-egui/src/panels/workspaces.rs b/crates/ui-egui/src/panels/workspaces.rsindex 89ac924a1..aed12891b 100644--- a/crates/ui-egui/src/panels/workspaces.rs+++ b/crates/ui-egui/src/panels/workspaces.rs@@ -56,14 +56,12 @@ pub fn route(app: &mut FilmcraftApp, id: &str, params: &Value) -> Option<Result</// Save the current layout as the saved layout of `name`.fn save(app: &mut FilmcraftApp, name: &str) -> Result<(), String> {- let workspace = crate::dock::Workspace::new(app.ui.dock.clone());- workspace.validate().map_err(|e| e.to_string())?;- let workspace = serde_json::to_value(&workspace).map_err(|e| e.to_string())?;+ let entry = dock::SavedWorkspace::new(name, &app.ui.dock)?;let mut next = app.workspaces.clone();let saved = &mut next.saved;match saved.iter_mut().find(|s| s.name == name) {- Some(s) => *s = dock::SavedWorkspace { name: name.to_string(), workspace, ..Default::default() },- None => saved.push(dock::SavedWorkspace { name: name.to_string(), workspace, ..Default::default() }),+ Some(s) => *s = entry,+ None => saved.push(entry),}next.current = name.to_string();app.set_workspaces(next)diff --git a/crates/ui-egui/tests/it/shared_docking_ui.rs b/crates/ui-egui/tests/it/shared_docking_ui.rsindex 5a7359266..1d2fa0a28 100644--- a/crates/ui-egui/tests/it/shared_docking_ui.rs+++ b/crates/ui-egui/tests/it/shared_docking_ui.rs@@ -91,7 +91,9 @@ fn builtin_and_legacy_workspace_shapes_roundtrip_and_save_floating() {let prefs = dock::WorkspacePrefs::load(&dir.join(dock::WORKSPACES_FILE));let restored = dock::saved_docking(&prefs, "Floating test");assert_eq!(restored, app.ui.dock);- assert!(prefs.saved[0].layout.is_null() && !prefs.saved[0].workspace.is_null());+ // Earlier versions read `layout` and `floating`.+ assert!(!prefs.saved[0].layout.is_null() && !prefs.saved[0].workspace.is_null());+ assert_eq!(prefs.saved[0].floating[0]["panels"], json!(["Info"]));let mut restarted_session = Session::default();restarted_session.prefs_path = Some(dir.join("preferences.json"));let restarted = FilmcraftApp::new(restarted_session);