From f9e8433cf189fcfc642ffe6e182ad719cdf665e3 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Wed, 18 Nov 2026 03:54:14 -0500 Subject: [PATCH] pdf-chrome-w8: net change of 3 commits - pdf-chrome-w8: written from its sheet - pdf-chrome-w8: repair 1 - pdf-chrome-w8: review fixes (review-1) --- crates/automation/src/dock.rs | 30 ++++++++++--- crates/automation/src/window.rs | 21 ++++++++-- crates/automation/tests/it/dock.rs | 67 ++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 crates/automation/tests/it/dock.rs diff --git a/crates/automation/src/dock.rs b/crates/automation/src/dock.rs index 9c4dce156..0adb62170 100644 --- a/crates/automation/src/dock.rs +++ b/crates/automation/src/dock.rs @@ -2,11 +2,13 @@ use std::hash::Hash; -use craft_behavior::dock::Workspace; +use craft_behavior::dock::{Action, Workspace}; use craft_ui::docking::DockPanels; use serde::Serialize; use serde::de::DeserializeOwned; -use serde_json::Value; +use serde_json::{Value, json}; + +use crate::protocol::Params; /// `ui.dock.apply {action}`: deserializes one dock action and applies it to `workspace` with the permissions `panels` /// gives, as the dock area does. It returns `Ok({applied: [...]})`, the panels the action closed, in group order; an @@ -18,12 +20,28 @@ pub fn dock_method( method: &str, params: &Value, ) -> Option> { - let _ = (workspace, panels, locked, method, params); - todo!("CH-C22") + if method != "ui.dock.apply" { + return None; + } + Some(apply(workspace, panels, locked, params)) +} + +fn apply( + workspace: &mut Workspace

, + panels: &dyn DockPanels

, + locked: bool, + params: &Value, +) -> Result { + let params = Params(params).only(None, &["action"])?; + let action = params.get("action").cloned().ok_or("missing `action`")?; + let action: Action

= serde_json::from_value(action).map_err(|e| format!("`action` is not a dock action: {e}"))?; + let applied = workspace.apply(action, locked, |panel| panels.permissions(panel)).map_err(|e| e.to_string())?; + let applied = serde_json::to_value(applied).map_err(|e| e.to_string())?; + Ok(json!({ "applied": applied })) } /// The `dock` field of the app's one `ui.inspect`: the workspace as JSON. pub fn inspect(workspace: &Workspace

) -> Value { - let _ = workspace; - todo!("CH-C22") + // A panel id that cannot be written lists the dock as null rather than failing the whole `ui.inspect`. + serde_json::to_value(workspace).unwrap_or(Value::Null) } diff --git a/crates/automation/src/window.rs b/crates/automation/src/window.rs index 927788c48..ae5af54da 100644 --- a/crates/automation/src/window.rs +++ b/crates/automation/src/window.rs @@ -1,12 +1,25 @@ //! `ui.window`: window commands through the framework's one window-command path. -use craft_ui::runtime::windows::WindowHost; +use craft_ui::runtime::windows::{WindowAction, WindowHost}; use egui::ViewportId; -use serde_json::Value; +use serde_json::{Value, json}; + +use crate::protocol::Params; /// `ui.window {maximized: bool}` sends `WindowAction::SetMaximized` through `WindowHost::act` and returns `Ok({})`, or /// `Err` when `act` returns false (no window commands, as on wasm). `None` for any other method. It keeps no state. pub fn window_method(host: &WindowHost, viewport: ViewportId, method: &str, params: &Value) -> Option> { - let _ = (host, viewport, method, params); - todo!("CH-C23") + if method != "ui.window" { + return None; + } + Some(run(host, viewport, params)) +} + +fn run(host: &WindowHost, viewport: ViewportId, params: &Value) -> Result { + let params = Params(params).only(None, &["maximized"])?; + let maximized = Params(¶ms).bool_field("maximized")?.ok_or("missing `maximized`")?; + if !host.act(viewport, WindowAction::SetMaximized(maximized)) { + return Err("this platform has no window commands".to_owned()); + } + Ok(json!({})) } diff --git a/crates/automation/tests/it/dock.rs b/crates/automation/tests/it/dock.rs new file mode 100644 index 000000000..cf91cc0c8 --- /dev/null +++ b/crates/automation/tests/it/dock.rs @@ -0,0 +1,67 @@ +//! `ui.dock.apply` on a workspace, with the host's permissions and lock. + +use craft_automation::dock::{dock_method, inspect}; +use craft_behavior::DockSide; +use craft_behavior::dock::{Action, DockError, DockPlacement, Layout, Node, PanelState, Permissions, Workspace}; +use craft_ui::contract::BodyCx; +use craft_ui::docking::{DockPanels, PanelSpec}; +use craft_ui::testing::trial; +use egui::Ui; +use serde_json::{Value, json}; + +/// Two panels, the first protected as a canvas is. +struct Panels; + +impl DockPanels for Panels { + fn spec(&self, panel: &u8) -> PanelSpec { + PanelSpec::new(format!("Panel {panel}")) + } + + fn permissions(&self, panel: &u8) -> Permissions { + if *panel == 1 { Permissions::PROTECTED } else { Permissions::ALL } + } + + fn body(&mut self, _: &u8, _: &mut Ui, _: &mut BodyCx<'_>) {} +} + +fn call(workspace: &mut Workspace, locked: bool, params: Value) -> Result { + dock_method(workspace, &Panels, locked, "ui.dock.apply", ¶ms).expect("a ui.dock method") +} + +/// A JSON action applies to the workspace with the host's permissions and lock; a refused one replies the dock error +/// and changes nothing, and `inspect` lists the workspace. +#[trial] +fn a_dock_action_from_json_applies_to_the_workspace() { + let mut workspace = Workspace::new(Layout::new(Some(Node::tabs(vec![1u8, 2], 0)), Vec::new())); + assert_eq!(workspace.layout.panel_state(&2), PanelState::Open); + + assert_eq!(call(&mut workspace, false, json!({"action": {"Activate": {"panel": 2}}})), Ok(json!({"applied": []}))); + assert_eq!(workspace.layout.panel_state(&2), PanelState::Front, "the activate action brings panel 2 to the front"); + + let before = workspace.clone(); + assert_eq!( + call(&mut workspace, false, json!({"action": Action::close(1u8)})), + Err(DockError::NotAllowed.to_string()), + "the host's permissions protect panel 1" + ); + assert_eq!(workspace, before, "a refused action leaves the workspace as it was"); + assert!(call(&mut workspace, false, json!({"action": Action::activate(1u8), "panel": 1})).is_err(), "an unknown key is refused"); + assert!(call(&mut workspace, false, json!({})).is_err(), "a missing action is refused"); + assert!(call(&mut workspace, false, json!({"action": {"Activate": {}}})).is_err()); + assert_eq!(workspace, before); + + let split = Action::move_beside(2u8, 1, DockPlacement::Split(DockSide::End)); + assert_eq!(call(&mut workspace, true, json!({"action": split})), Err(DockError::NotAllowed.to_string()), "a locked workspace refuses a move"); + assert_eq!(workspace, before); + assert_eq!(call(&mut workspace, true, json!({"action": Action::activate(1u8)})), Ok(json!({"applied": []})), "a locked workspace activates"); + assert_eq!(workspace.layout.panel_state(&1), PanelState::Front); + + assert_eq!(call(&mut workspace, false, json!({"action": Action::close(2u8)})), Ok(json!({"applied": [2]})), "the reply lists the closed panel"); + assert_eq!(workspace.layout.panel_state(&2), PanelState::Hidden); + + assert!(dock_method(&mut workspace, &Panels, false, "ui.dock.close", &json!({})).is_none()); + + let listed = inspect(&workspace); + assert_eq!(serde_json::from_value::>(listed).ok(), Some(workspace.clone()), "inspect lists the whole workspace"); + assert_eq!(workspace.layout.panels(), vec![&1]); +}