pixelcoords-core 0.4.0

Platform-free core of pixelcoords: screen geometry, HiDPI and multi-monitor coordinate spaces, the session.json schema, template relocation, point verdicts, click-point resolution, region diffing, and click-code emitters. Cross-platform (macOS, Windows, Linux), no unsafe
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
//! Configuration types (serde) and their resolution into validated values.
//!
//! The structs here are plain data the binary deserializes from TOML; this
//! crate stays parser-agnostic. Resolution is strict: bad colors, silly
//! thicknesses, and unknown hotkey pieces are errors, never silently
//! defaulted (the predecessor's silent numeric fallbacks were a bug class).

use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::draw::Color;
use crate::hotkeys::{Binding, HotkeyError, default_bindings};

#[derive(Debug, Error, PartialEq, Eq)]
pub enum ConfigError {
    #[error("invalid color '{0}': expected hex RGB, 3 or 6 digits, optional '#'")]
    Color(String),
    #[error("thickness {0} is out of range (0-512)")]
    Thickness(u32),
    #[error(transparent)]
    Hotkey(#[from] HotkeyError),
    #[error(
        "[capture] monitors: {0} — expected \"all\", or a monitor query \
         (an index, \"primary\", or part of a display name), or a list of them"
    )]
    Monitors(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct Config {
    pub style: StyleConfig,
    pub hotkeys: Vec<HotkeyEntry>,
    pub capture: CaptureConfig,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(default, deny_unknown_fields)]
pub struct CaptureConfig {
    /// Which monitors a no-flag launch freezes. `None` (the table absent,
    /// or the key absent) means all of them — the launch default, and the
    /// product's thesis. This is the answer for a double-clicked binary,
    /// which has no terminal to pass `--monitor` on.
    pub monitors: Option<MonitorsSetting>,
}

/// `monitors = "primary"` and `monitors = ["DELL", "Built-in"]` are both
/// natural to write, so both parse.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MonitorsSetting {
    One(String),
    Many(Vec<String>),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct StyleConfig {
    /// Outline color while dragging out a shape.
    pub preview_color: String,
    /// Outline color of committed shapes.
    pub complete_color: String,
    /// Label and HUD text color.
    pub label_color: String,
    /// Border color drawn around the `--target` window.
    pub target_color: String,
    /// Outline thickness in pixels; 0 hides outlines.
    pub thickness: u32,
    /// Fill shapes instead of outlining them.
    pub fill: bool,
}

impl Default for StyleConfig {
    fn default() -> Self {
        Self {
            preview_color: "#00A0FF".into(),
            complete_color: "#00FF66".into(),
            label_color: "#FFFFFF".into(),
            target_color: "#FFB000".into(),
            thickness: 2,
            fill: false,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HotkeyEntry {
    pub key: String,
    pub action: String,
    pub edge: Option<String>,
    pub when: Option<String>,
}

/// Validated, ready-to-use style values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
    pub preview: Color,
    pub complete: Color,
    pub label: Color,
    pub target: Color,
    pub thickness: i32,
    pub fill: bool,
}

impl Config {
    pub fn resolve_style(&self) -> Result<Style, ConfigError> {
        let s = &self.style;
        if s.thickness > 512 {
            return Err(ConfigError::Thickness(s.thickness));
        }
        Ok(Style {
            preview: parse_hex_color(&s.preview_color)?,
            complete: parse_hex_color(&s.complete_color)?,
            label: parse_hex_color(&s.label_color)?,
            target: parse_hex_color(&s.target_color)?,
            thickness: s.thickness as i32,
            fill: s.fill,
        })
    }

    /// The monitor queries a no-flag launch should use, or an empty vec
    /// meaning all of them.
    ///
    /// Only the *shape* is checked here — this crate has no idea what is
    /// plugged in. A query that names no attached display fails at launch
    /// with the same message `--monitor` gives, which is the honest place
    /// for it: the answer depends on the hardware, not the file. What is
    /// rejected here is a value that could never mean anything on any
    /// machine, because a silent default is the bug class this module
    /// exists to avoid.
    pub fn resolve_monitors(&self) -> Result<Vec<String>, ConfigError> {
        let raw = match &self.capture.monitors {
            None => return Ok(Vec::new()),
            Some(MonitorsSetting::One(one)) => vec![one.clone()],
            Some(MonitorsSetting::Many(many)) => {
                if many.is_empty() {
                    return Err(ConfigError::Monitors("the list is empty".into()));
                }
                many.clone()
            }
        };
        // "all" is the launch default said out loud, and only means that on
        // its own — in a list it would be a display name, which is a
        // contradiction worth naming rather than resolving.
        if raw.len() == 1 && raw[0].trim().eq_ignore_ascii_case("all") {
            return Ok(Vec::new());
        }
        for query in &raw {
            if query.trim().is_empty() {
                return Err(ConfigError::Monitors("an entry is empty".into()));
            }
            if query.trim().eq_ignore_ascii_case("all") {
                return Err(ConfigError::Monitors(
                    "\"all\" cannot be combined with other monitors".into(),
                ));
            }
        }
        Ok(raw)
    }

    /// Defaults, then config-file entries, then `extra` (CLI `--bind`).
    /// Binding any key removes ALL default bindings for that key (every
    /// edge), so rebinding `[` doesn't leave the default's repeat-edge
    /// rotation alive; among user bindings, later entries shadow earlier
    /// ones per key + edge.
    pub fn resolve_bindings(&self, extra: &[String]) -> Result<Vec<Binding>, ConfigError> {
        let mut user: Vec<Binding> = Vec::new();
        for entry in &self.hotkeys {
            let mut spec = format!("{}={}", entry.key, entry.action);
            for part in [&entry.edge, &entry.when].into_iter().flatten() {
                spec.push(',');
                spec.push_str(part);
            }
            user.push(Binding::parse(&spec)?);
        }
        for spec in extra {
            user.push(Binding::parse(spec)?);
        }
        let user_keys: std::collections::HashSet<_> = user.iter().map(|b| b.key).collect();
        let mut bindings: Vec<Binding> = default_bindings()
            .into_iter()
            .filter(|b| !user_keys.contains(&b.key))
            .collect();
        bindings.extend(user);
        Ok(bindings)
    }
}

/// Parse `RGB`/`RRGGBB` with optional `#`, matching the predecessor's rules.
pub fn parse_hex_color(input: &str) -> Result<Color, ConfigError> {
    let s = input
        .trim()
        .strip_prefix('#')
        .unwrap_or_else(|| input.trim());
    let expanded: String = match s.len() {
        3 => s.chars().flat_map(|c| [c, c]).collect(),
        6 => s.to_string(),
        _ => return Err(ConfigError::Color(input.to_string())),
    };
    if !expanded.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err(ConfigError::Color(input.to_string()));
    }
    let channel = |range| u8::from_str_radix(&expanded[range], 16).unwrap_or_default();
    Ok(Color {
        r: channel(0..2),
        g: channel(2..4),
        b: channel(4..6),
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hotkeys::{Action, Edge, KeyName, OverlayState, match_event};

    #[test]
    fn hex_six_digit_with_hash() {
        assert_eq!(
            parse_hex_color("#FF8000").unwrap(),
            Color {
                r: 255,
                g: 128,
                b: 0
            }
        );
    }

    #[test]
    fn hex_without_hash_and_lowercase() {
        assert_eq!(
            parse_hex_color("00a0ff").unwrap(),
            Color {
                r: 0,
                g: 160,
                b: 255
            }
        );
    }

    #[test]
    fn hex_three_digit_expands() {
        assert_eq!(
            parse_hex_color("#F80").unwrap(),
            Color {
                r: 255,
                g: 136,
                b: 0
            }
        );
    }

    #[test]
    fn hex_rejects_bad_input() {
        for bad in ["", "#", "12345", "1234567", "GGGGGG", "#12 456"] {
            assert!(parse_hex_color(bad).is_err(), "{bad:?} should be rejected");
        }
    }

    fn capture(toml: &str) -> Result<Vec<String>, ConfigError> {
        let cfg: Config = ::toml::from_str(toml).expect("parses");
        cfg.resolve_monitors()
    }

    #[test]
    fn no_capture_table_means_every_monitor() {
        assert!(capture("").unwrap().is_empty());
        assert!(capture("[capture]\n").unwrap().is_empty());
    }

    #[test]
    fn all_is_the_launch_default_said_out_loud() {
        assert!(
            capture("[capture]\nmonitors = \"all\"\n")
                .unwrap()
                .is_empty()
        );
        assert!(
            capture("[capture]\nmonitors = \"ALL\"\n")
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn a_single_query_and_a_list_both_parse() {
        assert_eq!(
            capture("[capture]\nmonitors = \"primary\"\n").unwrap(),
            vec!["primary".to_string()]
        );
        assert_eq!(
            capture("[capture]\nmonitors = [\"DELL\", \"Built-in\"]\n").unwrap(),
            vec!["DELL".to_string(), "Built-in".to_string()]
        );
    }

    #[test]
    fn empty_and_contradictory_values_are_errors_not_silent_defaults() {
        // The bug class this module exists to avoid: a value that means
        // nothing quietly becoming "freeze everything".
        assert!(capture("[capture]\nmonitors = \"\"\n").is_err());
        assert!(capture("[capture]\nmonitors = \"   \"\n").is_err());
        assert!(capture("[capture]\nmonitors = []\n").is_err());
        assert!(capture("[capture]\nmonitors = [\"DELL\", \"\"]\n").is_err());
        // "all" is only the default on its own; alongside a name it is a
        // contradiction rather than a display.
        assert!(capture("[capture]\nmonitors = [\"all\", \"DELL\"]\n").is_err());
    }

    #[test]
    fn an_unknown_capture_key_is_refused_like_every_other_table() {
        assert!(::toml::from_str::<Config>("[capture]\nmonitor = \"primary\"\n").is_err());
    }

    #[test]
    fn default_config_resolves() {
        let cfg = Config::default();
        let style = cfg.resolve_style().unwrap();
        assert_eq!(style.thickness, 2);
        assert!(!style.fill);
        assert_eq!(
            style.label,
            Color {
                r: 255,
                g: 255,
                b: 255
            }
        );
    }

    #[test]
    fn thickness_out_of_range_errors() {
        let mut cfg = Config::default();
        cfg.style.thickness = 513;
        assert_eq!(
            cfg.resolve_style().unwrap_err(),
            ConfigError::Thickness(513)
        );
    }

    #[test]
    fn toml_round_trip_and_hotkey_merge() {
        let toml_src = r##"
            [style]
            preview_color = "#F00"
            thickness = 4

            [[hotkeys]]
            key = "x"
            action = "save"
            when = "has_selection"
        "##;
        let cfg: Config = toml::from_str(toml_src).unwrap();
        let style = cfg.resolve_style().unwrap();
        assert_eq!(style.preview, Color { r: 255, g: 0, b: 0 });
        assert_eq!(style.thickness, 4);
        // Unspecified fields keep defaults.
        assert!(!style.fill);

        let bindings = cfg.resolve_bindings(&[]).unwrap();
        let state = OverlayState {
            has_selection: true,
            cursor_in_shape: false,
        };
        assert_eq!(
            match_event(&bindings, KeyName::Character('X'), Edge::Press, state),
            Some(Action::Save)
        );
    }

    #[test]
    fn unknown_toml_field_is_rejected() {
        let err = toml::from_str::<Config>("[style]\npreview_colour = \"#F00\"\n");
        assert!(err.is_err());
    }

    #[test]
    fn rebinding_a_key_removes_all_its_default_edges() {
        // 'Q' has press AND repeat defaults for rotate_ccw; rebinding it
        // must silence both, not leave the repeat default alive.
        let cfg = Config::default();
        let bindings = cfg.resolve_bindings(&["q=next_tool".to_string()]).unwrap();
        let state = OverlayState {
            cursor_in_shape: true,
            ..OverlayState::default()
        };
        assert_eq!(
            match_event(&bindings, KeyName::Character('Q'), Edge::Press, state),
            Some(Action::NextTool)
        );
        assert_eq!(
            match_event(&bindings, KeyName::Character('Q'), Edge::Repeat, state),
            None,
            "repeat-edge default must be gone"
        );
        // Untouched keys keep their defaults.
        assert_eq!(
            match_event(&bindings, KeyName::Character('E'), Edge::Repeat, state),
            Some(Action::RotateCw)
        );
    }

    #[test]
    fn cli_bind_shadows_defaults() {
        let cfg = Config::default();
        let bindings = cfg.resolve_bindings(&["q=undo".to_string()]).unwrap();
        assert_eq!(
            match_event(
                &bindings,
                KeyName::Character('Q'),
                Edge::Press,
                OverlayState::default()
            ),
            Some(Action::Undo)
        );
    }

    #[test]
    fn bad_hotkey_entry_is_an_error() {
        let mut cfg = Config::default();
        cfg.hotkeys.push(HotkeyEntry {
            key: "z".into(),
            action: "teleport".into(),
            edge: None,
            when: None,
        });
        assert!(matches!(
            cfg.resolve_bindings(&[]),
            Err(ConfigError::Hotkey(_))
        ));
    }
}