darkly 0.5.0

A GPU-native paint engine on wgpu: brushes, layers, blend modes, masks, selections, and undo.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
pub mod schema;
pub mod sections;

#[allow(dead_code)]
mod presets_gen {
    include!(concat!(env!("OUT_DIR"), "/presets_gen.rs"));
}

pub use presets_gen::{BASE_SETTINGS_OPTIONS, DEFAULTS_YAML, OVERLAYS};

use std::cell::RefCell;
use std::collections::HashMap;

/// On-disk schema version for `user_settings.json`. Bump whenever a change
/// to the schema or YAML layers cannot be auto-cleaned by
/// [`super::schema`]-driven validation — e.g. a pref key is renamed, a
/// pref's kind changes shape (str→int, scalar→list), or the file's
/// envelope itself changes. Pre-release we just discard mismatched files
/// (per CLAUDE.md "No Migrations"); post-release this is the discriminator
/// migrations key off.
///
/// Forward-compatible changes don't need a bump: new prefs get default
/// values, removed pref keys are dropped by `validateOverrides`, and
/// numeric range changes are clamped.
pub const CONFIG_VERSION: u32 = 1;

/// A configuration value.
#[derive(Clone, Debug, PartialEq)]
pub enum ConfigValue {
    Float(f64),
    Int(i64),
    Bool(bool),
    Str(String),
}

/// Three-layer config store:
///
/// ```text
/// user override → overlay[active editor] → defaults (defaults.yaml)
/// ```
///
/// All three layers are sourced from YAML at startup: `defaults.yaml` is the
/// editor-AGNOSTIC baseline (always applied), each `<editor>.yaml` is one
/// equal-status overlay, and the user layer collects personal overrides.
///
/// The active editor is whatever `app.baseSettings` resolves to in the user
/// layer. The startup PresetPicker writes it before any consumer reads a
/// resolved value, so `get_*` getters can panic on a missing setting just
/// as before.
struct Config {
    defaults: HashMap<String, ConfigValue>,
    overlays: HashMap<String, HashMap<String, ConfigValue>>,
    user: HashMap<String, ConfigValue>,
}

thread_local! {
    static CONFIG: RefCell<Config> = RefCell::new(Config::new());
}

impl Config {
    fn new() -> Self {
        let defaults = parse_yaml_preset(presets_gen::DEFAULTS_YAML)
            .unwrap_or_else(|e| panic!("failed to parse defaults.yaml: {e}"));
        let mut overlays = HashMap::new();
        for (name, yaml) in presets_gen::OVERLAYS {
            let map = parse_yaml_preset(yaml)
                .unwrap_or_else(|e| panic!("failed to parse overlay {name}: {e}"));
            overlays.insert((*name).to_string(), map);
        }
        Config {
            defaults,
            overlays,
            user: HashMap::new(),
        }
    }

    /// Resolve a key down the layer stack.
    fn get(&self, key: &str) -> Option<&ConfigValue> {
        if let Some(v) = self.user.get(key) {
            return Some(v);
        }
        if let Some(ConfigValue::Str(name)) = self.user.get("app.baseSettings") {
            if let Some(v) = self.overlays.get(name).and_then(|m| m.get(key)) {
                return Some(v);
            }
        }
        self.defaults.get(key)
    }

    /// What "Reset override on this key" would reveal — the layer below
    /// the user layer. Drives the Settings UI's "displayed default" and
    /// the Reset-affordance disabled state.
    fn base_value(&self, key: &str) -> Option<&ConfigValue> {
        if let Some(ConfigValue::Str(name)) = self.user.get("app.baseSettings") {
            if let Some(v) = self.overlays.get(name).and_then(|m| m.get(key)) {
                return Some(v);
            }
        }
        self.defaults.get(key)
    }
}

// ---------------------------------------------------------------------------
// YAML parsing — flattens the `{ hotkeys, mouse_clicks, settings }` shape
// into a dot-path key/value map, mirroring the legacy on-disk JSON model.
// ---------------------------------------------------------------------------

fn parse_yaml_preset(yaml: &str) -> Result<HashMap<String, ConfigValue>, String> {
    let value: serde_yaml_ng::Value = serde_yaml_ng::from_str(yaml).map_err(|e| e.to_string())?;
    let map = match value {
        serde_yaml_ng::Value::Mapping(m) => m,
        // Empty doc: zero entries (legitimately allowed for overlays that
        // only define a `name:` field and nothing else).
        serde_yaml_ng::Value::Null => return Ok(HashMap::new()),
        other => return Err(format!("expected top-level mapping, got {other:?}")),
    };

    let mut out: HashMap<String, ConfigValue> = HashMap::new();

    for (k, v) in map {
        let Some(key) = k.as_str() else { continue };
        match key {
            "name" | "description" => {
                // Metadata: not a config key.
                continue;
            }
            "hotkeys" => collect_string_facet(&v, "hotkeys.", &mut out)?,
            "mouse_clicks" => collect_string_facet(&v, "mouseclicks.", &mut out)?,
            "settings" => collect_settings_facet(&v, &mut out)?,
            _ => {
                // Tolerate top-level scalar entries by treating the key as a
                // settings key (handy for future hand-written YAML).
                if let Some(cv) = yaml_to_config_value(&v) {
                    out.insert(key.to_string(), cv);
                }
            }
        }
    }

    Ok(out)
}

/// `hotkeys` / `mouse_clicks` facets: keys map to either a single string
/// (one binding) or a list of strings (multiple alternative bindings).
/// Multi-binding entries are joined with a `|` separator — consumers know
/// to split on it. (Legacy: the only known multi-binding action is
/// `isolateLayer` with `[layerThumb:alt+click, maskThumb:alt+click]`.)
fn collect_string_facet(
    v: &serde_yaml_ng::Value,
    prefix: &str,
    out: &mut HashMap<String, ConfigValue>,
) -> Result<(), String> {
    let m = match v {
        serde_yaml_ng::Value::Mapping(m) => m,
        serde_yaml_ng::Value::Null => return Ok(()),
        other => return Err(format!("{prefix} expected a mapping, got {other:?}")),
    };
    for (k, v) in m {
        let Some(key) = k.as_str() else { continue };
        let full_key = format!("{prefix}{key}");
        match v {
            serde_yaml_ng::Value::String(s) => {
                out.insert(full_key, ConfigValue::Str(s.clone()));
            }
            serde_yaml_ng::Value::Sequence(seq) => {
                let mut parts: Vec<String> = Vec::with_capacity(seq.len());
                for item in seq {
                    if let serde_yaml_ng::Value::String(s) = item {
                        parts.push(s.clone());
                    } else {
                        return Err(format!("{full_key}: list item is not a string"));
                    }
                }
                out.insert(full_key, ConfigValue::Str(parts.join("|")));
            }
            serde_yaml_ng::Value::Null => {
                // Tolerate `key: ` with no value as "empty string" — a key
                // explicitly unbinds the action.
                out.insert(full_key, ConfigValue::Str(String::new()));
            }
            other => return Err(format!("{full_key}: unexpected value {other:?}")),
        }
    }
    Ok(())
}

/// `settings` facet: keys are already fully-qualified dot-paths; values are
/// bool/int/float/string.
fn collect_settings_facet(
    v: &serde_yaml_ng::Value,
    out: &mut HashMap<String, ConfigValue>,
) -> Result<(), String> {
    let m = match v {
        serde_yaml_ng::Value::Mapping(m) => m,
        serde_yaml_ng::Value::Null => return Ok(()),
        other => return Err(format!("settings expected a mapping, got {other:?}")),
    };
    for (k, v) in m {
        let Some(key) = k.as_str() else { continue };
        if let Some(cv) = yaml_to_config_value(v) {
            out.insert(key.to_string(), cv);
        } else {
            return Err(format!("settings.{key}: unsupported value {v:?}"));
        }
    }
    Ok(())
}

fn yaml_to_config_value(v: &serde_yaml_ng::Value) -> Option<ConfigValue> {
    match v {
        serde_yaml_ng::Value::Bool(b) => Some(ConfigValue::Bool(*b)),
        serde_yaml_ng::Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                Some(ConfigValue::Int(i))
            } else {
                n.as_f64().map(ConfigValue::Float)
            }
        }
        serde_yaml_ng::Value::String(s) => Some(ConfigValue::Str(s.clone())),
        serde_yaml_ng::Value::Null => None,
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Public module-level API (delegates to thread-local)
// ---------------------------------------------------------------------------

/// Get a config value by dot-path key. Returns `None` only if the key is
/// absent from every layer.
pub fn get(key: &str) -> Option<ConfigValue> {
    CONFIG.with(|c| c.borrow().get(key).cloned())
}

/// Get a float value. Coerces Int → f64. Panics if the key is missing.
pub fn get_f64(key: &str) -> f64 {
    match get(key) {
        Some(ConfigValue::Float(f)) => f,
        Some(ConfigValue::Int(i)) => i as f64,
        other => panic!("config key {key:?}: expected numeric, got {other:?}"),
    }
}

/// Get an integer value. Panics if the key is missing or wrong type.
pub fn get_i64(key: &str) -> i64 {
    match get(key) {
        Some(ConfigValue::Int(i)) => i,
        other => panic!("config key {key:?}: expected int, got {other:?}"),
    }
}

/// Get a string value. Panics if the key is missing or wrong type.
pub fn get_str(key: &str) -> String {
    match get(key) {
        Some(ConfigValue::Str(s)) => s,
        other => panic!("config key {key:?}: expected string, got {other:?}"),
    }
}

/// Get a boolean value. Panics if the key is missing or wrong type.
pub fn get_bool(key: &str) -> bool {
    match get(key) {
        Some(ConfigValue::Bool(b)) => b,
        other => panic!("config key {key:?}: expected bool, got {other:?}"),
    }
}

/// Layer-below-user value for a key (overlay → defaults). Drives "Reset"
/// affordances and the Settings UI's displayed-default text.
pub fn base_value(key: &str) -> Option<ConfigValue> {
    CONFIG.with(|c| c.borrow().base_value(key).cloned())
}

/// Set a value in the user layer.
pub fn set(key: &str, value: ConfigValue) {
    CONFIG.with(|c| {
        c.borrow_mut().user.insert(key.to_string(), value);
    });
}

/// Remove a user override for a key. Reveals the overlay/default below.
pub fn reset(key: &str) {
    CONFIG.with(|c| {
        c.borrow_mut().user.remove(key);
    });
}

/// Clear every user override **except** `app.baseSettings` — the picker
/// choice survives a global reset so the user isn't bumped back to the
/// first-run picker by clicking "Reset everything".
pub fn reset_all() {
    CONFIG.with(|c| {
        let mut cfg = c.borrow_mut();
        let base = cfg.user.remove("app.baseSettings");
        cfg.user.clear();
        if let Some(v) = base {
            cfg.user.insert("app.baseSettings".to_string(), v);
        }
    });
}

/// Equal-status overlay display names (alphabetical order).
pub fn base_names() -> Vec<String> {
    presets_gen::OVERLAYS
        .iter()
        .map(|(name, _)| (*name).to_string())
        .collect()
}

/// True if the declared `PrefKind` for `key` is `Int` (used by the WASM
/// bridge to disambiguate JS numbers when serializing back to Rust).
pub fn kind_is_int(key: &str) -> bool {
    for section in sections::registrations() {
        for pref in section.prefs {
            if pref.key == key {
                return matches!(pref.kind, schema::PrefKind::Int { .. });
            }
        }
    }
    false
}

/// Return the full schema as a serializable view. Used by the WASM bridge to
/// feed the Settings UI.
pub fn schema_info() -> Vec<schema::SectionInfo> {
    let mut out: Vec<_> = sections::registrations()
        .iter()
        .map(schema::SectionInfo::from_section)
        .collect();
    out.sort_by(|a, b| a.order.cmp(&b.order).then_with(|| a.id.cmp(b.id)));
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn reset_state() {
        CONFIG.with(|c| {
            c.borrow_mut().user.clear();
        });
    }

    fn pick(name: &str) {
        set("app.baseSettings", ConfigValue::Str(name.to_string()));
    }

    #[test]
    fn defaults_from_yaml() {
        reset_state();
        // Agnostic defaults present without picking an editor.
        assert_eq!(get_i64("animation.veil_divisor"), 2);
        assert_eq!(get_i64("canvas.width"), 1920);
        // Autosave section (schema in sections/autosave.rs, defaults in yaml).
        assert!(get_bool("autosave.enabled"));
        assert_eq!(get_i64("autosave.intervalSeconds"), 120);
        assert!(kind_is_int("autosave.intervalSeconds"));
        assert_eq!(get_str("hotkeys.nav.trigger"), "Space");
        assert!(get_bool("input.fingerPainting"));
        // Darkly-original hotkey defined in defaults.yaml (no reference-editor
        // prior art, so it stays in the agnostic baseline).
        assert_eq!(get_str("hotkeys.addBrushNode"), "Shift+KeyA");
    }

    #[test]
    fn overlay_resolves_above_defaults() {
        reset_state();
        pick("Krita");
        // Krita-specific override (defined only in krita.yaml).
        assert_eq!(get_str("hotkeys.brushTool"), "KeyB");
        // Defaults still show through where the overlay is silent —
        // addBrushNode is Darkly-original and no overlay defines it.
        assert_eq!(get_str("hotkeys.addBrushNode"), "Shift+KeyA");

        // Switching to Photoshop swaps the overlay live.
        pick("Photoshop");
        assert_eq!(get_str("hotkeys.rectSelectTool"), "KeyM");
        assert_eq!(get_str("hotkeys.addBrushNode"), "Shift+KeyA");
    }

    #[test]
    fn user_wins_over_overlay_and_defaults() {
        reset_state();
        pick("Krita");
        set("hotkeys.brushTool", ConfigValue::Str("KeyZ".into()));
        assert_eq!(get_str("hotkeys.brushTool"), "KeyZ");
        reset("hotkeys.brushTool");
        // Falls back to overlay value, not defaults.
        assert_eq!(get_str("hotkeys.brushTool"), "KeyB");
    }

    #[test]
    fn reset_all_preserves_base_choice() {
        reset_state();
        pick("Photoshop");
        set("hotkeys.brushTool", ConfigValue::Str("KeyZ".into()));
        reset_all();
        // Override is gone…
        assert_eq!(get_str("hotkeys.brushTool"), "KeyB");
        // …but the base choice survives.
        assert_eq!(get_str("app.baseSettings"), "Photoshop");
    }

    #[test]
    fn base_value_skips_user_layer() {
        reset_state();
        pick("Krita");
        set("hotkeys.brushTool", ConfigValue::Str("KeyZ".into()));
        // `base_value` is what a Reset would reveal — the overlay value.
        match base_value("hotkeys.brushTool") {
            Some(ConfigValue::Str(s)) => assert_eq!(s, "KeyB"),
            other => panic!("expected overlay value, got {other:?}"),
        }
    }

    #[test]
    fn base_names_lists_overlays_alphabetically() {
        let names = base_names();
        assert!(!names.is_empty(), "expected at least one overlay");
        let mut sorted = names.clone();
        sorted.sort();
        assert_eq!(names, sorted);
    }

    #[test]
    fn kind_is_int_uses_schema() {
        // `canvas.width` is an int pref → true.
        assert!(kind_is_int("canvas.width"));
        // `nav.panSensitivity` is a float pref → false.
        assert!(!kind_is_int("nav.panSensitivity"));
        // Unknown key → false (defensive).
        assert!(!kind_is_int("bogus.key"));
    }
}