mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
UiState saves its Tracked fields through the facade's tracked-serde feature and compares by value again
This commit is contained in:
1 parent
a8f21a383d
commit
c19d432dec
3 files changed
+6
-20
No files matched your search
@@ -16,7 +16,7 @@ suite = ["craft/suite"]
|
||||
[dependencies]
|
||||
chrono = { workspace = true }
|
||||
craft-print = { workspace = true }
|
||||
craft = { workspace = true, features = ["ui", "layers", "color-ui", "draw", "imaging", "window", "keymap", "automation", "theme-store", "print", "menu", "timeline", "text", "text-font-picker", "font-manager", "quit", "cms"] }
|
||||
craft = { workspace = true, features = ["ui", "layers", "color-ui", "draw", "imaging", "window", "keymap", "automation", "theme-store", "print", "menu", "timeline", "text", "text-font-picker", "font-manager", "quit", "cms", "tracked-serde"] }
|
||||
# Interface translations over the embedded craft-i18n catalogs (`build.rs`, `src/i18n.rs`).
|
||||
craft-i18n = { workspace = true }
|
||||
photocraft-geom = { workspace = true }
|
||||
|
||||
@@ -250,7 +250,7 @@ fn shape_stroke_defaults_are_drivable_and_validate_atomically() {
|
||||
let before = app.ui.clone();
|
||||
let bad = call(&mut app, &ctx, "ui.set", json!({"tool":"Rectangle","shapeStroke":{"dashes":[0,0]}}));
|
||||
assert_eq!(bad["ok"], false);
|
||||
assert_eq!(serde_json::to_value(&app.ui).unwrap(), serde_json::to_value(&before).unwrap());
|
||||
assert_eq!(app.ui, before);
|
||||
assert_eq!(call(&mut app, &ctx, "ui.set", json!({"shapeStroke":{"colour":"#ff0000"}}))["ok"], false);
|
||||
assert_eq!(call(&mut app, &ctx, "ui.set", json!({"shapeStroke":null}))["ok"], true);
|
||||
assert_eq!(app.ui.tool_options.stroke_width, 0.0);
|
||||
|
||||
@@ -160,7 +160,7 @@ pub struct DocWindow {
|
||||
pub position: Option<[f32; 2]>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
pub struct UiState {
|
||||
pub tool: Tool,
|
||||
/// The tools used so far, most recent first and each one once. The first tool of a shortcut
|
||||
@@ -413,7 +413,7 @@ pub struct UiState {
|
||||
#[serde(skip)]
|
||||
pub brush_curves_open: BTreeSet<&'static str>,
|
||||
/// Brushes panel: list or grid, zoom, collapsed groups and the search filter. Starts as a list.
|
||||
#[serde(default = "brushes_list", with = "tracked_serde")]
|
||||
#[serde(default = "brushes_list")]
|
||||
pub(crate) brushes_panel: Tracked<PresetState<BrushKey>>,
|
||||
/// Marquee options-bar mode: 0 new, 1 add, 2 subtract, 3 intersect (modifier keys override).
|
||||
#[serde(default)]
|
||||
@@ -460,20 +460,6 @@ fn brushes_list() -> Tracked<PresetState<BrushKey>> {
|
||||
Tracked::new(state)
|
||||
}
|
||||
|
||||
/// A `Tracked` field saved as its value; a loaded one starts a new revision source.
|
||||
mod tracked_serde {
|
||||
use craft::ui::contract::{PlainData, Tracked};
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
pub(super) fn serialize<T: PlainData + Serialize, S: Serializer>(value: &Tracked<T>, s: S) -> Result<S::Ok, S::Error> {
|
||||
(**value).serialize(s)
|
||||
}
|
||||
|
||||
pub(super) fn deserialize<'de, T: PlainData + Deserialize<'de>, D: Deserializer<'de>>(d: D) -> Result<Tracked<T>, D::Error> {
|
||||
T::deserialize(d).map(Tracked::new)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UiState {
|
||||
/// Creates the default UI state with default tools and empty drag state.
|
||||
fn default() -> Self {
|
||||
@@ -690,7 +676,7 @@ mod tests {
|
||||
let s = UiState::default();
|
||||
let v = serde_json::to_value(&s).unwrap();
|
||||
assert_eq!(v["tool"], "Move");
|
||||
let back: UiState = serde_json::from_value(v.clone()).unwrap();
|
||||
assert_eq!(serde_json::to_value(&back).unwrap(), v);
|
||||
let back: UiState = serde_json::from_value(v).unwrap();
|
||||
assert_eq!(back, s);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user