mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
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)
This commit is contained in:
@@ -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<P: Clone + Eq + Hash + Serialize + DeserializeOwned>(
|
||||
method: &str,
|
||||
params: &Value,
|
||||
) -> Option<Result<Value, String>> {
|
||||
let _ = (workspace, panels, locked, method, params);
|
||||
todo!("CH-C22")
|
||||
if method != "ui.dock.apply" {
|
||||
return None;
|
||||
}
|
||||
Some(apply(workspace, panels, locked, params))
|
||||
}
|
||||
|
||||
fn apply<P: Clone + Eq + Hash + Serialize + DeserializeOwned>(
|
||||
workspace: &mut Workspace<P>,
|
||||
panels: &dyn DockPanels<P>,
|
||||
locked: bool,
|
||||
params: &Value,
|
||||
) -> Result<Value, String> {
|
||||
let params = Params(params).only(None, &["action"])?;
|
||||
let action = params.get("action").cloned().ok_or("missing `action`")?;
|
||||
let action: Action<P> = 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<P: Serialize>(workspace: &Workspace<P>) -> 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)
|
||||
}
|
||||
|
||||
@@ -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<Result<Value, String>> {
|
||||
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<Value, String> {
|
||||
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!({}))
|
||||
}
|
||||
|
||||
@@ -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<u8> 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<u8>, locked: bool, params: Value) -> Result<Value, String> {
|
||||
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::<Workspace<u8>>(listed).ok(), Some(workspace.clone()), "inspect lists the whole workspace");
|
||||
assert_eq!(workspace.layout.panels(), vec![&1]);
|
||||
}
|
||||
Reference in New Issue
Block a user