pixelcoords-core 0.1.0

Platform-free core logic for pixelcoords: geometry, selections, session schema, point verdicts, code emitters, template relocation, hotkeys, config, rasterizer
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! Point-in-region verdicts against a saved session — the logic behind
//! `pixelcoords assert`.
//!
//! A verdict answers "does this point land inside a marked region?" for a
//! point expressed in any of the session's coordinate spaces. It exists so
//! automation — a computer-use agent, a click script under test — can be
//! scored against regions a human marked once, without reimplementing the
//! session's geometry.

use serde::Serialize;
use thiserror::Error;

use crate::geometry::{Point, Rect, Shape, ToolKind};
use crate::session::{SelectionRecord, SessionFile};

pub const VERDICT_SCHEMA_VERSION: u32 = 1;

/// The coordinate space an incoming point is expressed in. Each maps to the
/// coordinates the session already stores; nothing is derived at test time.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PointSpace {
    /// Global desktop physical pixels (`global_px`).
    Global,
    /// Monitor-local physical pixels (`px`) of the given monitor index.
    Monitor(usize),
    /// Physical pixels relative to the target window's top-left
    /// (`window_px`); needs a `--target` session.
    Window,
}

impl PointSpace {
    pub const fn label(self) -> &'static str {
        match self {
            Self::Global => "global",
            Self::Monitor(_) => "monitor",
            Self::Window => "window",
        }
    }
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum VerdictError {
    #[error(
        "the session has no target window — window-relative points need a \
         session captured with --target"
    )]
    NoTarget,
    #[error("monitor {requested} is not in this session; it has monitors {available:?}")]
    UnknownMonitor {
        requested: usize,
        available: Vec<usize>,
    },
    #[error("no selection is labeled {requested:?}; labels in this space: {available:?}")]
    UnknownLabel {
        requested: String,
        available: Vec<String>,
    },
    #[error("the session has no selections in {0} space to test against")]
    NoCandidates(&'static str),
}

/// The result of testing one point, ready to serialize as the `assert`
/// subcommand's JSON output.
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Verdict {
    pub schema: u32,
    pub point: Point,
    pub space: &'static str,
    /// The monitor index the point is local to; present only in monitor
    /// space.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub monitor: Option<usize>,
    pub hit: bool,
    /// Every region containing the point, in session (stacking) order —
    /// last is topmost. A miss against `--label` still lists what the
    /// point *did* land in.
    pub contained_in: Vec<RegionRef>,
    /// Present on a miss: the closest relevant region, for partial credit.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nearest: Option<Nearest>,
}

/// A selection referenced by its position in the session file.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RegionRef {
    pub index: usize,
    pub label: String,
    pub shape: ToolKind,
    pub monitor: usize,
}

#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Nearest {
    pub region: RegionRef,
    /// Distance in pixels from the point to the region's rotated bounding
    /// box. 0 means inside the bbox but outside the shape itself.
    pub bbox_distance_px: f64,
}

/// One selection viewed through the requested coordinate space.
struct Candidate<'a> {
    index: usize,
    record: &'a SelectionRecord,
    shape: Shape,
}

impl Candidate<'_> {
    fn rot_deg(&self) -> i32 {
        self.record.rot_deg.unwrap_or(0)
    }

    fn region_ref(&self) -> RegionRef {
        RegionRef {
            index: self.index,
            label: self.record.label.clone(),
            shape: self.record.shape,
            monitor: self.record.monitor,
        }
    }
}

/// Test `point` against the session's regions. `label` restricts what
/// counts as a hit to selections carrying that label (ASCII
/// case-insensitive, matching the window matcher's behavior).
pub fn assess(
    session: &SessionFile,
    point: Point,
    space: PointSpace,
    label: Option<&str>,
) -> Result<Verdict, VerdictError> {
    let candidates = candidates(session, space)?;
    if candidates.is_empty() {
        return Err(VerdictError::NoCandidates(space.label()));
    }
    if let Some(wanted) = label {
        let known = candidates
            .iter()
            .any(|c| c.record.label.eq_ignore_ascii_case(wanted));
        if !known {
            return Err(VerdictError::UnknownLabel {
                requested: wanted.to_string(),
                available: distinct_labels(&candidates),
            });
        }
    }

    let contained: Vec<&Candidate> = candidates
        .iter()
        .filter(|c| c.shape.hit_test_rotated(c.rot_deg(), point))
        .collect();
    let hit = label.map_or(!contained.is_empty(), |wanted| {
        contained
            .iter()
            .any(|c| c.record.label.eq_ignore_ascii_case(wanted))
    });
    let nearest = if hit {
        None
    } else {
        nearest_relevant(&candidates, point, label)
    };
    Ok(Verdict {
        schema: VERDICT_SCHEMA_VERSION,
        point,
        space: space.label(),
        monitor: match space {
            PointSpace::Monitor(index) => Some(index),
            _ => None,
        },
        hit,
        contained_in: contained.iter().map(|c| c.region_ref()).collect(),
        nearest,
    })
}

/// The selections testable in `space`, each carrying the shape already
/// stored for that space.
fn candidates(
    session: &SessionFile,
    space: PointSpace,
) -> Result<Vec<Candidate<'_>>, VerdictError> {
    let records = session.selections.iter().enumerate();
    match space {
        PointSpace::Global => Ok(records
            .map(|(index, record)| Candidate {
                index,
                record,
                shape: record.global_px.clone(),
            })
            .collect()),
        PointSpace::Monitor(wanted) => {
            let available: Vec<usize> = session.monitors.iter().map(|m| m.index).collect();
            if !available.contains(&wanted) {
                return Err(VerdictError::UnknownMonitor {
                    requested: wanted,
                    available,
                });
            }
            Ok(records
                .filter(|(_, record)| record.monitor == wanted)
                .map(|(index, record)| Candidate {
                    index,
                    record,
                    shape: record.px.clone(),
                })
                .collect())
        }
        PointSpace::Window => {
            if session.target.is_none() {
                return Err(VerdictError::NoTarget);
            }
            Ok(records
                .filter_map(|(index, record)| {
                    record.window_px.clone().map(|shape| Candidate {
                        index,
                        record,
                        shape,
                    })
                })
                .collect())
        }
    }
}

/// The labels a `--label` filter could have matched, deduplicated in
/// session order; unlabeled selections contribute nothing.
fn distinct_labels(candidates: &[Candidate]) -> Vec<String> {
    let mut labels: Vec<String> = Vec::new();
    for c in candidates {
        if c.record.label.is_empty() {
            continue;
        }
        if labels
            .iter()
            .any(|l| l.eq_ignore_ascii_case(&c.record.label))
        {
            continue;
        }
        labels.push(c.record.label.clone());
    }
    labels
}

/// The closest region the miss was measured against: the labeled ones when
/// a label was requested, otherwise all of them.
fn nearest_relevant(
    candidates: &[Candidate],
    point: Point,
    label: Option<&str>,
) -> Option<Nearest> {
    candidates
        .iter()
        .filter(|c| label.is_none_or(|wanted| c.record.label.eq_ignore_ascii_case(wanted)))
        .map(|c| Nearest {
            region: c.region_ref(),
            bbox_distance_px: bbox_distance(c.shape.rotated_bbox(c.rot_deg()), point),
        })
        .min_by(|a, b| a.bbox_distance_px.total_cmp(&b.bbox_distance_px))
}

/// Euclidean distance from `p` to the nearest pixel of `rect`, 0 inside.
/// i64/f64 throughout so extreme deserialized coordinates cannot overflow.
fn bbox_distance(rect: Rect, p: Point) -> f64 {
    let dx = axis_distance(p.x, rect.x, rect.w);
    let dy = axis_distance(p.y, rect.y, rect.h);
    f64::hypot(dx as f64, dy as f64)
}

/// Distance from `v` to the half-open interval `[start, start + len)` on
/// one axis, 0 inside — the same inclusion rule as `Rect::contains`.
fn axis_distance(v: i32, start: i32, len: i32) -> i64 {
    let v = i64::from(v);
    let start = i64::from(start);
    let end = start + i64::from(len) - 1;
    if v < start {
        return start - v;
    }
    if v > end {
        return v - end;
    }
    0
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::geometry::{Rect, Size};
    use crate::selection::Selection;
    use crate::session::{MonitorRecord, TargetRecord};

    fn monitor(index: usize, ox: i32, oy: i32) -> MonitorRecord {
        MonitorRecord {
            index,
            name: format!("Display {index}"),
            primary: index == 0,
            origin_px: Point::new(ox, oy),
            size_px: Size::new(1920, 1080),
            scale: 2.0,
        }
    }

    fn labeled(shape: Shape, monitor: usize, label: &str) -> Selection {
        let mut sel = Selection::new(shape, monitor);
        sel.label = label.into();
        sel
    }

    fn session(selections: &[Selection], target: Option<TargetRecord>) -> SessionFile {
        let crops: Vec<String> = (0..selections.len()).map(|i| format!("c{i}.png")).collect();
        SessionFile::build(
            "test",
            "2026-07-27T00:00:00Z".into(),
            vec![monitor(0, 0, 0), monitor(1, 1920, 0)],
            selections,
            &crops,
            target,
        )
    }

    #[test]
    fn global_space_hits_through_the_monitor_origin() {
        // Local (10, 20) on monitor 1 sits at global (1930, 20).
        let file = session(
            &[labeled(Shape::Rect(Rect::new(10, 20, 30, 40)), 1, "submit")],
            None,
        );
        let hit = assess(&file, Point::new(1935, 25), PointSpace::Global, None).unwrap();
        assert!(hit.hit);
        assert_eq!(hit.contained_in[0].label, "submit");
        assert!(hit.nearest.is_none());
        let miss = assess(&file, Point::new(15, 25), PointSpace::Global, None).unwrap();
        assert!(!miss.hit);
    }

    #[test]
    fn monitor_space_tests_local_pixels_of_that_monitor_only() {
        let file = session(
            &[
                labeled(Shape::Rect(Rect::new(10, 20, 30, 40)), 1, "right"),
                labeled(Shape::Rect(Rect::new(10, 20, 30, 40)), 0, "left"),
            ],
            None,
        );
        let v = assess(&file, Point::new(15, 25), PointSpace::Monitor(1), None).unwrap();
        assert!(v.hit);
        assert_eq!(v.monitor, Some(1));
        // Both monitors hold an identical local rect; only monitor 1's is
        // eligible, so exactly one region contains the point.
        assert_eq!(v.contained_in.len(), 1);
        assert_eq!(v.contained_in[0].label, "right");
    }

    #[test]
    fn unknown_monitor_is_an_error_naming_the_real_ones() {
        let file = session(&[labeled(Shape::Rect(Rect::new(0, 0, 5, 5)), 0, "a")], None);
        let err = assess(&file, Point::new(0, 0), PointSpace::Monitor(7), None).unwrap_err();
        assert_eq!(
            err,
            VerdictError::UnknownMonitor {
                requested: 7,
                available: vec![0, 1],
            }
        );
    }

    #[test]
    fn monitor_with_no_selections_is_no_candidates_not_a_miss() {
        let file = session(&[labeled(Shape::Rect(Rect::new(0, 0, 5, 5)), 0, "a")], None);
        let err = assess(&file, Point::new(2, 2), PointSpace::Monitor(1), None).unwrap_err();
        assert_eq!(err, VerdictError::NoCandidates("monitor"));
    }

    #[test]
    fn window_space_needs_a_target_session() {
        let file = session(&[labeled(Shape::Rect(Rect::new(0, 0, 5, 5)), 0, "a")], None);
        let err = assess(&file, Point::new(2, 2), PointSpace::Window, None).unwrap_err();
        assert_eq!(err, VerdictError::NoTarget);
    }

    #[test]
    fn window_space_uses_window_relative_coords_and_skips_other_monitors() {
        let target = TargetRecord {
            app: "Editor".into(),
            title: "main.rs".into(),
            monitor: 0,
            origin_px: Point::new(400, 250),
            size_px: Size::new(800, 600),
        };
        let file = session(
            &[
                labeled(Shape::Rect(Rect::new(500, 300, 40, 20)), 0, "on target"),
                labeled(Shape::Rect(Rect::new(1, 1, 5, 5)), 1, "elsewhere"),
            ],
            Some(target),
        );
        // Window-relative: the rect sits at (100, 50) from the window origin.
        let v = assess(&file, Point::new(110, 55), PointSpace::Window, None).unwrap();
        assert!(v.hit);
        assert_eq!(v.contained_in.len(), 1);
        assert_eq!(v.contained_in[0].label, "on target");
    }

    #[test]
    fn label_filter_is_case_insensitive_and_sees_through_overlap() {
        let file = session(
            &[
                labeled(Shape::Rect(Rect::new(0, 0, 100, 100)), 0, "Cancel"),
                labeled(Shape::Rect(Rect::new(200, 0, 50, 50)), 0, "Submit"),
            ],
            None,
        );
        let v = assess(
            &file,
            Point::new(10, 10),
            PointSpace::Global,
            Some("SUBMIT"),
        )
        .unwrap();
        // The point is inside "Cancel", so the verdict is a labeled miss
        // that still reports what was actually hit — and how far the
        // wanted region is.
        assert!(!v.hit);
        assert_eq!(v.contained_in.len(), 1);
        assert_eq!(v.contained_in[0].label, "Cancel");
        let nearest = v.nearest.unwrap();
        assert_eq!(nearest.region.label, "Submit");
        assert!(nearest.bbox_distance_px > 0.0);

        let hit = assess(
            &file,
            Point::new(210, 10),
            PointSpace::Global,
            Some("submit"),
        )
        .unwrap();
        assert!(hit.hit);
    }

    #[test]
    fn unknown_label_is_an_error_listing_the_real_ones() {
        let file = session(
            &[
                labeled(Shape::Rect(Rect::new(0, 0, 5, 5)), 0, "Submit"),
                labeled(Shape::Rect(Rect::new(9, 9, 5, 5)), 0, "submit"),
                labeled(Shape::Rect(Rect::new(20, 20, 5, 5)), 0, ""),
            ],
            None,
        );
        let err = assess(&file, Point::new(0, 0), PointSpace::Global, Some("send")).unwrap_err();
        // Case-insensitive duplicates collapse; the unlabeled one is not
        // offered.
        assert_eq!(
            err,
            VerdictError::UnknownLabel {
                requested: "send".into(),
                available: vec!["Submit".into()],
            }
        );
    }

    #[test]
    fn empty_session_is_no_candidates() {
        let file = session(&[], None);
        let err = assess(&file, Point::new(0, 0), PointSpace::Global, None).unwrap_err();
        assert_eq!(err, VerdictError::NoCandidates("global"));
    }

    #[test]
    fn rotated_rect_is_tested_as_the_user_saw_it() {
        // A wide rect rotated 90° stands tall: x 25..35, y -5..35 visually.
        let mut sel = Selection::new(Shape::Rect(Rect::new(10, 10, 40, 10)), 0);
        sel.rot_deg = 90;
        let file = session(&[sel], None);
        let inside_rotated = Point::new(28, 27);
        let v = assess(&file, inside_rotated, PointSpace::Global, None).unwrap();
        assert!(v.hit, "point inside the rotated silhouette must hit");
        let inside_unrotated_only = Point::new(45, 15);
        let v = assess(&file, inside_unrotated_only, PointSpace::Global, None).unwrap();
        assert!(
            !v.hit,
            "the axis-aligned box no longer applies once rotated"
        );
    }

    #[test]
    fn circle_and_triangle_hits_use_their_own_geometry() {
        let file = session(
            &[
                labeled(
                    Shape::Circle {
                        cx: 50,
                        cy: 50,
                        r: 10,
                    },
                    0,
                    "dot",
                ),
                labeled(
                    Shape::Triangle {
                        ax: 200,
                        ay: 100,
                        bx: 150,
                        by: 200,
                        cx: 250,
                        cy: 200,
                    },
                    0,
                    "tri",
                ),
            ],
            None,
        );
        // On the circle's rim (inclusive), inside the triangle, and in each
        // shape's bbox corner — which its geometry excludes.
        assert!(
            assess(&file, Point::new(60, 50), PointSpace::Global, None)
                .unwrap()
                .hit
        );
        assert!(
            assess(&file, Point::new(200, 150), PointSpace::Global, None)
                .unwrap()
                .hit
        );
        let circle_corner = assess(&file, Point::new(41, 41), PointSpace::Global, None).unwrap();
        assert!(!circle_corner.hit);
        let tri_corner = assess(&file, Point::new(151, 101), PointSpace::Global, None).unwrap();
        assert!(!tri_corner.hit);
    }

    #[test]
    fn overlapping_hits_come_back_in_stacking_order() {
        let file = session(
            &[
                labeled(Shape::Rect(Rect::new(0, 0, 100, 100)), 0, "below"),
                labeled(Shape::Rect(Rect::new(0, 0, 50, 50)), 0, "above"),
            ],
            None,
        );
        let v = assess(&file, Point::new(10, 10), PointSpace::Global, None).unwrap();
        let labels: Vec<&str> = v.contained_in.iter().map(|r| r.label.as_str()).collect();
        assert_eq!(labels, ["below", "above"]);
        assert_eq!(v.contained_in[1].index, 1);
    }

    #[test]
    fn miss_distance_is_euclidean_to_the_bbox() {
        let file = session(
            &[labeled(Shape::Rect(Rect::new(10, 10, 20, 20)), 0, "box")],
            None,
        );
        // 3 left of x=10, 4 above y=10: a 3-4-5 triangle.
        let v = assess(&file, Point::new(7, 6), PointSpace::Global, None).unwrap();
        assert!(!v.hit);
        let nearest = v.nearest.unwrap();
        assert_eq!(nearest.region.label, "box");
        assert!((nearest.bbox_distance_px - 5.0).abs() < f64::EPSILON);
    }

    #[test]
    fn miss_distance_uses_the_rotated_bbox() {
        // Rotated 90°, the wide rect's silhouette spans x 25..35 — a point
        // just right of it measures against that tall box, not the
        // original wide one.
        let mut sel = Selection::new(Shape::Rect(Rect::new(10, 10, 40, 10)), 0);
        sel.rot_deg = 90;
        let file = session(&[sel], None);
        let v = assess(&file, Point::new(40, 0), PointSpace::Global, None).unwrap();
        assert!(!v.hit);
        // The rotated bbox reaches x=35 at y=0; distance is 40-34=6 in x
        // per half-open inclusion, 0 in y — well under the unrotated
        // bbox's answer (which would include a 10px y gap).
        let d = v.nearest.unwrap().bbox_distance_px;
        assert!(
            d < 8.0,
            "distance {d} should measure the rotated silhouette"
        );
    }

    #[test]
    fn verdict_json_shape_is_stable() {
        let file = session(
            &[labeled(Shape::Rect(Rect::new(10, 10, 20, 20)), 0, "box")],
            None,
        );
        let hit = assess(&file, Point::new(15, 15), PointSpace::Global, None).unwrap();
        let json = serde_json::to_value(&hit).unwrap();
        assert_eq!(json["schema"], 1);
        assert_eq!(json["space"], "global");
        assert_eq!(json["hit"], true);
        assert_eq!(json["point"]["x"], 15);
        assert_eq!(json["contained_in"][0]["label"], "box");
        assert!(json.get("monitor").is_none(), "global space has no monitor");
        assert!(json.get("nearest").is_none(), "hits carry no nearest");

        let miss = assess(&file, Point::new(500, 500), PointSpace::Monitor(0), None).unwrap();
        let json = serde_json::to_value(&miss).unwrap();
        assert_eq!(json["space"], "monitor");
        assert_eq!(json["monitor"], 0);
        assert_eq!(json["nearest"]["region"]["label"], "box");
    }

    #[test]
    fn space_labels_name_their_own_space() {
        assert_eq!(PointSpace::Global.label(), "global");
        assert_eq!(PointSpace::Monitor(3).label(), "monitor");
        assert_eq!(PointSpace::Window.label(), "window");
    }
}