systemless 0.76.0

High-Level Emulation for classic Macintosh applications
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! Architecture-neutral Control Manager records and list operations.

/// Tagged property associated with a ControlRef in Appearance Manager / Carbon.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct ProcessControlProperty {
    pub(crate) creator: u32,
    pub(crate) tag: u32,
    pub(crate) attributes: u32,
    pub(crate) data: Vec<u8>,
}

/// Host metadata for one guest `ControlRecord`.
///
/// The relocatable record and its window-list link remain canonical guest
/// memory. This process-owned entry retains only information that the HLE
/// cannot recover reliably from the record, including the original control
/// definition ID, pop-up definition private values, embedding hierarchy,
/// and Carbon custom properties. Inside Macintosh Volume I (1985),
/// pp. I-316--I-319 and I-328--I-333; Mac OS 8.5 Appearance Manager (1998).
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct ProcessControlRecord {
    pub(crate) handle: u32,
    pub(crate) pointer: u32,
    pub(crate) proc_id: i16,
    pub(crate) popup_menu_id: i16,
    pub(crate) popup_title_width: Option<i16>,
    pub(crate) active: bool,
    pub(crate) font_style: Option<ControlFontStyle>,
    pub(crate) is_root: bool,
    pub(crate) parent: u32,
    pub(crate) sub_controls: Vec<u32>,
    pub(crate) properties: Vec<ProcessControlProperty>,
    pub(crate) color_proc: u32,
    pub(crate) control_id: (u32, i32),
    pub(crate) command_id: u32,
    pub(crate) has_focus: bool,
    pub(crate) focus_part: i16,
    pub(crate) drag_tracking_enabled: bool,
}

/// The Appearance Manager style override associated with a ControlRef.
/// RGBColor components are stored in guest byte order as decoded host words.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct ControlFontStyle {
    pub(crate) flags: i16,
    pub(crate) font: i16,
    pub(crate) size: i16,
    pub(crate) style: i16,
    pub(crate) mode: i16,
    pub(crate) justification: i16,
    pub(crate) foreground: [u16; 3],
    pub(crate) background: [u16; 3],
}

/// Canonical Control Manager metadata for one Macintosh process.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub(crate) struct ProcessControlManagerState {
    records: Vec<ProcessControlRecord>,
}

impl ProcessControlManagerState {
    pub(crate) fn is_pristine(&self) -> bool {
        self.records.is_empty()
    }

    pub(crate) fn register(&mut self, handle: u32, pointer: u32, proc_id: i16, popup_menu_id: i16) {
        self.records
            .retain(|record| handle == 0 || record.handle != handle || record.pointer == pointer);
        if let Some(record) = self
            .records
            .iter_mut()
            .find(|record| record.pointer == pointer && pointer != 0)
        {
            if handle != 0 {
                record.handle = handle;
            }
            record.proc_id = proc_id;
            record.popup_menu_id = popup_menu_id;
            return;
        }
        self.records.push(ProcessControlRecord {
            handle,
            pointer,
            proc_id,
            popup_menu_id,
            popup_title_width: None,
            active: true,
            font_style: None,
            is_root: false,
            parent: 0,
            sub_controls: Vec::new(),
            properties: Vec::new(),
            color_proc: 0,
            control_id: (0, 0),
            command_id: 0,
            has_focus: false,
            focus_part: 0,
            drag_tracking_enabled: false,
        });
    }

    #[allow(dead_code)]
    pub(crate) fn color_proc(&self, handle: u32) -> u32 {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or(0, |record| record.color_proc)
    }

    #[allow(dead_code)]
    pub(crate) fn set_color_proc(&mut self, handle: u32, proc: u32) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.color_proc = proc;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn control_id(&self, handle: u32) -> (u32, i32) {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or((0, 0), |record| record.control_id)
    }

    #[allow(dead_code)]
    pub(crate) fn set_control_id(&mut self, handle: u32, signature: u32, id: i32) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.control_id = (signature, id);
        }
    }

    #[allow(dead_code)]
    pub(crate) fn command_id(&self, handle: u32) -> u32 {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or(0, |record| record.command_id)
    }

    #[allow(dead_code)]
    pub(crate) fn set_command_id(&mut self, handle: u32, command_id: u32) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.command_id = command_id;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn has_focus(&self, handle: u32) -> bool {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .is_some_and(|record| record.has_focus)
    }

    #[allow(dead_code)]
    pub(crate) fn set_has_focus(&mut self, handle: u32, focus: bool) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.has_focus = focus;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn focus_part(&self, handle: u32) -> i16 {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or(0, |record| record.focus_part)
    }

    #[allow(dead_code)]
    pub(crate) fn set_focus_part(&mut self, handle: u32, part: i16) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.focus_part = part;
            record.has_focus = part != 0;
        }
    }

    #[allow(dead_code)]
    pub(crate) fn drag_tracking_enabled(&self, handle: u32) -> bool {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .is_some_and(|record| record.drag_tracking_enabled)
    }

    #[allow(dead_code)]
    pub(crate) fn set_drag_tracking_enabled(&mut self, handle: u32, enabled: bool) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.drag_tracking_enabled = enabled;
        }
    }

    pub(crate) fn proc_id(&self, pointer: u32) -> i16 {
        self.records
            .iter()
            .find(|record| record.pointer == pointer)
            .map_or(0, |record| record.proc_id)
    }

    pub(crate) fn contains_pointer(&self, pointer: u32) -> bool {
        self.records.iter().any(|record| record.pointer == pointer)
    }

    pub(crate) fn set_font_style(&mut self, pointer: u32, style: Option<ControlFontStyle>) {
        if let Some(record) = self.records.iter_mut().find(|record| record.pointer == pointer) {
            record.font_style = style;
        }
    }

    pub(crate) fn set_proc_id(&mut self, pointer: u32, proc_id: i16) {
        if let Some(record) = self
            .records
            .iter_mut()
            .find(|record| record.pointer == pointer)
        {
            record.proc_id = proc_id;
        } else {
            self.register(0, pointer, proc_id, 0);
        }
    }

    pub(crate) fn associate_handle(&mut self, handle: u32, pointer: u32) {
        self.records
            .retain(|record| record.handle != handle || record.pointer == pointer);
        if let Some(record) = self
            .records
            .iter_mut()
            .find(|record| record.pointer == pointer)
        {
            record.handle = handle;
        } else {
            self.register(handle, pointer, 0, 0);
        }
    }

    pub(crate) fn set_popup_title_width(&mut self, pointer: u32, width: i16) {
        if !self.records.iter().any(|record| record.pointer == pointer) {
            self.register(0, pointer, 0, 0);
        }
        if let Some(record) = self
            .records
            .iter_mut()
            .find(|record| record.pointer == pointer)
        {
            record.popup_title_width = Some(width);
        }
    }

    pub(crate) fn popup_title_width(&self, pointer: u32, fallback: i16) -> i16 {
        self.records
            .iter()
            .find(|record| record.pointer == pointer)
            .and_then(|record| record.popup_title_width)
            .unwrap_or(fallback)
    }

    pub(crate) fn remove_pointer(&mut self, pointer: u32) {
        let handle = self
            .records
            .iter()
            .find(|record| record.pointer == pointer)
            .map_or(0, |record| record.handle);
        if handle != 0 {
            self.remove_handle(handle);
        } else {
            self.records.retain(|record| record.pointer != pointer);
        }
    }

    pub(crate) fn remove_handle(&mut self, handle: u32) {
        let parent = self.parent(handle);
        if parent != 0 {
            if let Some(parent_rec) = self.records.iter_mut().find(|r| r.handle == parent) {
                parent_rec.sub_controls.retain(|h| *h != handle);
            }
        }
        for record in self.records.iter_mut() {
            if record.parent == handle {
                record.parent = 0;
            }
            record.sub_controls.retain(|h| *h != handle);
        }
        self.records.retain(|record| record.handle != handle);
    }

    #[allow(dead_code)]
    pub(crate) fn is_root(&self, handle: u32) -> bool {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .is_some_and(|record| record.is_root)
    }

    #[allow(dead_code)]
    pub(crate) fn set_is_root(&mut self, handle: u32, is_root: bool) {
        if let Some(record) = self.records.iter_mut().find(|record| record.handle == handle) {
            record.is_root = is_root;
        }
    }

    pub(crate) fn parent(&self, handle: u32) -> u32 {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or(0, |record| record.parent)
    }

    #[allow(dead_code)]
    pub(crate) fn count_sub_controls(&self, handle: u32) -> u16 {
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .map_or(0, |record| record.sub_controls.len() as u16)
    }

    #[allow(dead_code)]
    pub(crate) fn indexed_sub_control(&self, handle: u32, index: u16) -> Option<u32> {
        if index == 0 {
            return None;
        }
        self.records
            .iter()
            .find(|record| record.handle == handle)
            .and_then(|record| record.sub_controls.get((index - 1) as usize).copied())
    }

    #[allow(dead_code)]
    pub(crate) fn embed_control(&mut self, control: u32, container: u32) -> Result<(), i16> {
        if control == 0 {
            return Err(-30582);
        }
        if control == container {
            return Err(-30594);
        }
        let mut curr = container;
        while curr != 0 {
            if curr == control {
                return Err(-30594);
            }
            curr = self.parent(curr);
        }

        let old_parent = self.parent(control);
        if old_parent != 0 {
            if let Some(old_rec) = self.records.iter_mut().find(|r| r.handle == old_parent) {
                old_rec.sub_controls.retain(|h| *h != control);
            }
        }

        if let Some(rec) = self.records.iter_mut().find(|r| r.handle == control) {
            rec.parent = container;
        } else {
            self.records.push(ProcessControlRecord {
                handle: control,
                parent: container,
                ..Default::default()
            });
        }

        if container != 0 {
            if let Some(container_rec) = self.records.iter_mut().find(|r| r.handle == container) {
                if !container_rec.sub_controls.contains(&control) {
                    container_rec.sub_controls.push(control);
                }
            } else {
                self.records.push(ProcessControlRecord {
                    handle: container,
                    sub_controls: vec![control],
                    ..Default::default()
                });
            }
        }
        Ok(())
    }

    #[allow(dead_code)]
    pub(crate) fn set_property(
        &mut self,
        control: u32,
        creator: u32,
        tag: u32,
        attributes: u32,
        data: Vec<u8>,
    ) {
        if control == 0 {
            return;
        }
        let record = if let Some(rec) = self.records.iter_mut().find(|r| r.handle == control) {
            rec
        } else {
            self.records.push(ProcessControlRecord {
                handle: control,
                ..Default::default()
            });
            self.records.last_mut().unwrap()
        };

        if let Some(prop) = record
            .properties
            .iter_mut()
            .find(|p| p.creator == creator && p.tag == tag)
        {
            prop.attributes = attributes;
            prop.data = data;
        } else {
            record.properties.push(ProcessControlProperty {
                creator,
                tag,
                attributes,
                data,
            });
        }
    }

    #[allow(dead_code)]
    pub(crate) fn get_property(
        &self,
        control: u32,
        creator: u32,
        tag: u32,
    ) -> Option<&ProcessControlProperty> {
        self.records
            .iter()
            .find(|r| r.handle == control)
            .and_then(|r| r.properties.iter().find(|p| p.creator == creator && p.tag == tag))
    }

    #[allow(dead_code)]
    pub(crate) fn remove_property(&mut self, control: u32, creator: u32, tag: u32) -> bool {
        if let Some(rec) = self.records.iter_mut().find(|r| r.handle == control) {
            let before = rec.properties.len();
            rec.properties.retain(|p| !(p.creator == creator && p.tag == tag));
            rec.properties.len() < before
        } else {
            false
        }
    }

    #[allow(dead_code)]
    pub(crate) fn change_property_attributes(
        &mut self,
        control: u32,
        creator: u32,
        tag: u32,
        set: u32,
        clear: u32,
    ) -> Result<u32, i16> {
        if let Some(rec) = self.records.iter_mut().find(|r| r.handle == control) {
            if let Some(prop) = rec
                .properties
                .iter_mut()
                .find(|p| p.creator == creator && p.tag == tag)
            {
                prop.attributes = (prop.attributes | set) & !clear;
                return Ok(prop.attributes);
            }
        }
        Err(-5604)
    }

    #[allow(dead_code)]
    pub(crate) fn set_control_data(
        &mut self,
        control: u32,
        _part: i16,
        tag: u32,
        data: Vec<u8>,
    ) {
        if control == 0 {
            return;
        }
        let record = if let Some(rec) = self.records.iter_mut().find(|r| r.handle == control) {
            rec
        } else {
            self.records.push(ProcessControlRecord {
                handle: control,
                ..Default::default()
            });
            self.records.last_mut().unwrap()
        };

        if let Some(prop) = record.properties.iter_mut().find(|p| p.tag == tag) {
            prop.data = data;
        } else {
            record.properties.push(ProcessControlProperty {
                creator: 0,
                tag,
                attributes: 0,
                data,
            });
        }
    }

    #[allow(dead_code)]
    pub(crate) fn get_control_data(
        &self,
        control: u32,
        _part: i16,
        tag: u32,
    ) -> Option<&[u8]> {
        self.records
            .iter()
            .find(|r| r.handle == control)
            .and_then(|r| {
                r.properties
                    .iter()
                    .find(|p| p.creator == 0 && p.tag == tag)
                    .or_else(|| r.properties.iter().find(|p| p.tag == tag))
            })
            .map(|p| p.data.as_slice())
    }

    #[allow(dead_code)]
    pub(crate) fn get_control_data_size(
        &self,
        control: u32,
        part: i16,
        tag: u32,
    ) -> Option<usize> {
        self.get_control_data(control, part, tag).map(|d| d.len())
    }
}

impl std::ops::Deref for ProcessControlManagerState {
    type Target = Vec<ProcessControlRecord>;

    fn deref(&self) -> &Self::Target {
        &self.records
    }
}

impl std::ops::DerefMut for ProcessControlManagerState {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.records
    }
}

/// Maximum number of controls accepted from one live guest `wControlList`.
///
/// This is a defensive corruption bound, not a guest-visible Control Manager
/// limit. A repeated handle terminates traversal before this bound is reached.
const MAX_CONTROL_LIST_ENTRIES: usize = 4096;

/// Standard `pushButProc` corner oval used by the classic control definition.
pub(crate) const STANDARD_BUTTON_OVAL: i16 = 10;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct CheckboxLayout {
    pub(crate) indicator: (i16, i16, i16, i16),
    pub(crate) label_left: i16,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct RadioButtonLayout {
    pub(crate) indicator: (i16, i16, i16, i16),
    pub(crate) label_left: i16,
}

/// Resolve standard checkbox indicator and label geometry from `contrlRect`.
///
/// The checkbox CDEF owns a 12-pixel indicator, inset two pixels from the
/// control's leading edge, with four pixels before its title. Macintosh
/// Toolbox Essentials (1992), pp. 5-15--5-16.
pub(crate) fn standard_checkbox_layout(
    (top, left, bottom, _right): (i16, i16, i16, i16),
) -> CheckboxLayout {
    let indicator_size = 12.min(bottom.saturating_sub(top).max(1));
    let indicator_top =
        top.saturating_add(bottom.saturating_sub(top).saturating_sub(indicator_size) / 2);
    let indicator_left = left.saturating_add(2);
    CheckboxLayout {
        indicator: (
            indicator_top,
            indicator_left,
            indicator_top.saturating_add(indicator_size),
            indicator_left.saturating_add(indicator_size),
        ),
        label_left: indicator_left
            .saturating_add(indicator_size)
            .saturating_add(4),
    }
}

/// Resolve standard radio-button indicator and label geometry from
/// `contrlRect`.
///
/// The standard radio-button CDEF uses a 12-pixel round indicator, inset two
/// pixels from the control's leading edge, with four pixels before its title.
/// Inside Macintosh Volume I (1985), p. I-322.
pub(crate) fn standard_radio_button_layout(
    (top, left, bottom, _right): (i16, i16, i16, i16),
) -> RadioButtonLayout {
    let indicator_size = 12.min(bottom.saturating_sub(top).max(1));
    let indicator_top =
        top.saturating_add(bottom.saturating_sub(top).saturating_sub(indicator_size) / 2);
    let indicator_left = left.saturating_add(2);
    RadioButtonLayout {
        indicator: (
            indicator_top,
            indicator_left,
            indicator_top.saturating_add(indicator_size),
            indicator_left.saturating_add(indicator_size),
        ),
        label_left: indicator_left
            .saturating_add(indicator_size)
            .saturating_add(4),
    }
}

/// Visit the two diagonal pixels for each row of a selected checkbox mark.
/// Inside Macintosh Volume I (1985), p. I-322, Figure 27.
pub(crate) fn for_each_standard_checkbox_mark_pixel(
    size: i16,
    mut visit: impl FnMut(i16, i16),
) {
    for offset in 1..size.saturating_sub(1) {
        visit(offset, offset);
        visit(size.saturating_sub(1).saturating_sub(offset), offset);
    }
}

/// Center one system-font label inside a standard control rectangle.
pub(crate) fn centered_control_label_origin(
    (top, left, bottom, right): (i16, i16, i16, i16),
    text_advance: i16,
    ascent: i16,
    descent: i16,
) -> (i16, i16) {
    (
        left.saturating_add(right.saturating_sub(left).saturating_sub(text_advance) / 2),
        top.saturating_add(
            bottom
                .saturating_sub(top)
                .saturating_sub(ascent.saturating_add(descent))
                / 2,
        )
        .saturating_add(ascent),
    )
}

/// Resolve the handles that `DrawControls` must present, in draw order.
///
/// `NewControl` prepends records to `wControlList`. `DrawControls` draws in
/// reverse order of creation: the newest-first guest chain is already in
/// draw order, leaving the first-created overlapping control frontmost.
/// CPU adapters remain responsible only for reading the live next-handle field
/// and presenting or invoking the resulting control. Macintosh Toolbox
/// Essentials (1992), pp. 5-82 and 5-87--5-88.
pub(crate) fn control_draw_order<Handle>(
    head: Handle,
    mut next: impl FnMut(Handle) -> Option<Handle>,
) -> Vec<Handle>
where
    Handle: Copy + Eq + Default,
{
    let nil = Handle::default();
    let mut newest_first = Vec::new();
    let mut handle = head;
    while handle != nil
        && newest_first.len() < MAX_CONTROL_LIST_ENTRIES
        && !newest_first.contains(&handle)
    {
        newest_first.push(handle);
        handle = next(handle).unwrap_or(nil);
    }
    newest_first
}

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

    #[test]
    fn draw_order_preserves_the_newest_first_guest_chain() {
        let next = HashMap::from([(3u32, 2u32), (2, 1), (1, 0)]);
        assert_eq!(
            control_draw_order(3, |handle| next.get(&handle).copied()),
            [3, 2, 1]
        );
    }

    #[test]
    fn draw_order_stops_at_a_corrupt_cycle() {
        let next = HashMap::from([(3u32, 2u32), (2, 3)]);
        assert_eq!(
            control_draw_order(3, |handle| next.get(&handle).copied()),
            [3, 2]
        );
    }

    #[test]
    fn standard_checkbox_layout_is_shared_guest_geometry() {
        assert_eq!(
            standard_checkbox_layout((255, 185, 279, 315)),
            CheckboxLayout {
                indicator: (261, 187, 273, 199),
                label_left: 203,
            }
        );
    }

    #[test]
    fn standard_checkbox_mark_is_the_two_interior_diagonals() {
        let mut pixels = Vec::new();
        for_each_standard_checkbox_mark_pixel(5, |x, y| pixels.push((x, y)));
        assert_eq!(pixels, [(1, 1), (3, 1), (2, 2), (2, 2), (3, 3), (1, 3)]);
    }

    #[test]
    fn standard_radio_button_layout_is_shared_guest_geometry() {
        assert_eq!(
            standard_radio_button_layout((70, 250, 90, 390)),
            RadioButtonLayout {
                indicator: (74, 252, 86, 264),
                label_left: 268,
            }
        );
    }

    #[test]
    fn process_control_manager_state_evaluates_embedding_hierarchy() {
        let mut state = ProcessControlManagerState::default();
        state.register(10, 0x1000, 0, 0);
        state.register(20, 0x2000, 0, 0);
        state.register(30, 0x3000, 0, 0);

        state.set_is_root(10, true);
        assert!(state.is_root(10));
        assert!(!state.is_root(20));

        assert_eq!(state.count_sub_controls(10), 0);
        assert_eq!(state.embed_control(20, 10), Ok(()));
        assert_eq!(state.embed_control(30, 10), Ok(()));
        assert_eq!(state.embed_control(20, 20), Err(-30594));

        assert_eq!(state.count_sub_controls(10), 2);
        assert_eq!(state.indexed_sub_control(10, 1), Some(20));
        assert_eq!(state.indexed_sub_control(10, 2), Some(30));
        assert_eq!(state.indexed_sub_control(10, 3), None);

        assert_eq!(state.parent(20), 10);
        assert_eq!(state.parent(10), 0);

        state.remove_handle(20);
        assert_eq!(state.count_sub_controls(10), 1);
        assert_eq!(state.indexed_sub_control(10, 1), Some(30));
    }

    #[test]
    fn process_control_manager_state_evaluates_tagged_properties() {
        let mut state = ProcessControlManagerState::default();
        state.register(10, 0x1000, 0, 0);

        let creator = 0x5445_5354;
        let tag = 0x5441_4731;
        state.set_property(10, creator, tag, 0x01, vec![1, 2, 3]);

        let prop = state.get_property(10, creator, tag).unwrap();
        assert_eq!(prop.creator, creator);
        assert_eq!(prop.tag, tag);
        assert_eq!(prop.attributes, 0x01);
        assert_eq!(prop.data, vec![1, 2, 3]);

        assert_eq!(
            state.change_property_attributes(10, creator, tag, 0x10, 0x01),
            Ok(0x10)
        );
        let prop = state.get_property(10, creator, tag).unwrap();
        assert_eq!(prop.attributes, 0x10);

        assert!(state.remove_property(10, creator, tag));
        assert!(state.get_property(10, creator, tag).is_none());
        assert!(!state.remove_property(10, creator, tag));
    }

    #[test]
    fn process_control_manager_state_evaluates_focus_part_and_drag_tracking() {
        let mut state = ProcessControlManagerState::default();
        state.register(10, 0x1000, 0, 0);

        assert_eq!(state.focus_part(10), 0);
        assert!(!state.has_focus(10));
        assert!(!state.drag_tracking_enabled(10));

        state.set_focus_part(10, 1);
        assert_eq!(state.focus_part(10), 1);
        assert!(state.has_focus(10));

        state.set_focus_part(10, 0);
        assert_eq!(state.focus_part(10), 0);
        assert!(!state.has_focus(10));

        state.set_drag_tracking_enabled(10, true);
        assert!(state.drag_tracking_enabled(10));
    }
}