fluor 0.0.0

First-principles GUI compositor library: center-origin RU coordinates, harmonic-mean span scaling, CPU softbuffer rendering, ARM-first.
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
//! Window chrome — verbatim port of photon's window controls. **Do not "improve" or "simplify" the math here.** The function below is photon's [draw_window_controls](/mnt/Octopus/Code/photon/src/ui/compositing.rs) with `Self::` prefixes stripped to free-function calls. The squircle bottom-left corner, the per-pixel `hit_test_map` write, the symbol placement at `bw/4` offset — all unchanged.
//!
//! Symbol rendering and `blend_rgb_only` live in [`crate::paint`]. Hit IDs are integers so the desktop host can index `hit_test_map[y * w + x]` directly on click.
//!
//! Chrome here = window controls strip + edges. Title-bar text, scroll bars, status bar, tooltips, context menus, per-pane chrome — all not yet built.

use crate::paint;
use crate::theme;

/// Hit-test IDs written into the per-pixel map by [`draw_window_controls`]. Same numbering as photon.
pub const HIT_NONE: u8 = 0;
pub const HIT_MINIMIZE_BUTTON: u8 = 1;
pub const HIT_MAXIMIZE_BUTTON: u8 = 2;
pub const HIT_CLOSE_BUTTON: u8 = 3;

/// Resize edge classification, photon's enum verbatim.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ResizeEdge {
    None,
    Top,
    Bottom,
    Left,
    Right,
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

/// Photon's `get_resize_edge`. Edge thickness derived from harmonic-mean span.
pub fn get_resize_edge(window_width: u32, window_height: u32, x: f32, y: f32) -> ResizeEdge {
    let span = 2.0 * window_width as f32 * window_height as f32 / (window_width as f32 + window_height as f32);
    let resize_border = (span / 32.0).ceil();

    let at_left = x < resize_border;
    let at_right = x > (window_width as f32 - resize_border);
    let at_top = y < resize_border;
    let at_bottom = y > (window_height as f32 - resize_border);

    if at_top && at_left { ResizeEdge::TopLeft }
    else if at_top && at_right { ResizeEdge::TopRight }
    else if at_bottom && at_left { ResizeEdge::BottomLeft }
    else if at_bottom && at_right { ResizeEdge::BottomRight }
    else if at_top { ResizeEdge::Top }
    else if at_bottom { ResizeEdge::Bottom }
    else if at_left { ResizeEdge::Left }
    else if at_right { ResizeEdge::Right }
    else { ResizeEdge::None }
}

/// Verbatim port of photon's `draw_window_controls`. Renders the top-right control strip — grey background, two-tone hairline edge, squircle bottom-left corner, plus three glyph buttons (minimize, maximize, close) — and writes per-pixel button IDs into `hit_test_map` for downstream click routing.
///
/// `ru` is the resolution-units multiplier (default `1.0`); button height = `ceil(span / 32 * ru)`.
pub fn draw_window_controls(
    pixels: &mut [u32],
    hit_test_map: &mut [u8],
    window_width: u32,
    window_height: u32,
    ru: f32,
) -> (usize, Vec<(u16, u8, u8)>, usize, usize) {
    let window_width = window_width as usize;
    let window_height = window_height as usize;

    // Calculate button dimensions using harmonic mean (span) scaled by ru span = 2wh/(w+h), base button size = span/32, scaled by ru (zoom multiplier)
    let span = 2.0 * window_width as f32 * window_height as f32
        / (window_width as f32 + window_height as f32);
    let button_height = (span / 32.0 * ru).ceil() as usize;
    let button_width = button_height;
    let total_width = button_width * 7 / 2;

    // Buttons extend to top-right corner of window
    let mut x_start = window_width - total_width;
    let y_start = 0;

    // Build squircle crossings for bottom-left corner
    let radius = span * ru / 4.;
    let squirdleyness = 24;

    let mut crossings: Vec<(u16, u8, u8)> = Vec::new();
    let mut y = 1f32;
    loop {
        let y_norm = y / radius;
        let x_norm = (1.0 - y_norm.powi(squirdleyness)).powf(1.0 / squirdleyness as f32);
        let x = x_norm * radius;
        let inset = radius - x;
        if inset > 0. {
            crossings.push((
                inset as u16,
                (inset.fract().sqrt() * 256.) as u8,
                ((1. - inset.fract()).sqrt() * 256.) as u8,
            ));
        }
        if x < y {
            break;
        }
        y += 1.;
    }
    let start = (radius - y) as usize;
    let crossings: Vec<(u16, u8, u8)> = crossings.into_iter().rev().collect();

    let edge_colour = theme::WINDOW_LIGHT_EDGE;
    let bg_colour = theme::WINDOW_CONTROLS_BG;

    // Left edge (vertical) - draw light hairline following squircle curve
    let mut y_offset = start;
    for (inset, l, h) in &crossings {
        if y_offset >= button_height {
            break;
        }
        let py = y_start + button_height - 1 - y_offset;

        // Fill grey to the right of the curve and populate hit test map
        let col_end = total_width.min(window_width - x_start);
        for col in (*inset as usize + 2)..col_end - 1 {
            let px = x_start + col;
            let pixel_idx = py * window_width + px;

            pixels[pixel_idx] = bg_colour;

            // Determine which button this pixel belongs to. Buttons are drawn with a button_width / 4 offset.
            let button_area_x_start = x_start + button_width / 4;
            let button_id = if px < button_area_x_start {
                HIT_MINIMIZE_BUTTON
            } else {
                let x_in_button_area = px - button_area_x_start;
                if x_in_button_area < button_width {
                    HIT_MINIMIZE_BUTTON
                } else if x_in_button_area < button_width * 2 {
                    HIT_MAXIMIZE_BUTTON
                } else {
                    HIT_CLOSE_BUTTON
                }
            };
            hit_test_map[pixel_idx] = button_id;
        }

        let px = x_start + *inset as usize;
        let pixel_idx = py * window_width + px;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], edge_colour, *l, *h);

        let px = x_start + *inset as usize + 1;
        let pixel_idx = py * window_width + px;
        pixels[pixel_idx] = paint::blend_rgb_only(bg_colour, edge_colour, *h, *l);

        let button_area_x_start = x_start + button_width / 4;
        let button_id = if px < button_area_x_start {
            HIT_MINIMIZE_BUTTON
        } else {
            let x_in_button_area = px - button_area_x_start;
            if x_in_button_area < button_width {
                HIT_MINIMIZE_BUTTON
            } else if x_in_button_area < button_width * 2 {
                HIT_MAXIMIZE_BUTTON
            } else {
                HIT_CLOSE_BUTTON
            }
        };
        hit_test_map[pixel_idx] = button_id;

        y_offset += 1;
    }

    // Bottom edge (horizontal)
    let mut x_offset = start;
    let crossing_limit = crossings.len().min(window_width - (x_start + start));
    for &(inset, l, h) in &crossings[..crossing_limit] {
        let i = inset as usize;
        let px = x_start + x_offset;

        let py = y_start + button_height - 1 - i;
        let pixel_idx = py * window_width + px;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], edge_colour, l, h);

        for row in (i + 2)..start {
            let py = y_start + button_height - 1 - row;
            let pixel_idx = py * window_width + px;

            pixels[pixel_idx] = bg_colour;

            let button_area_x_start = x_start + button_width / 4;
            let button_id = if px < button_area_x_start {
                HIT_MINIMIZE_BUTTON
            } else {
                let x_in_button_area = px - button_area_x_start;
                if x_in_button_area < button_width {
                    HIT_MINIMIZE_BUTTON
                } else if x_in_button_area < button_width * 2 {
                    HIT_MAXIMIZE_BUTTON
                } else {
                    HIT_CLOSE_BUTTON
                }
            };
            hit_test_map[pixel_idx] = button_id;
        }

        let py = y_start + button_height - 1 - (i + 1);
        let pixel_idx = py * window_width + px;
        pixels[pixel_idx] = paint::blend_rgb_only(bg_colour, edge_colour, h, l);

        let button_area_x_start = x_start + button_width / 4;
        let button_id = if px < button_area_x_start {
            HIT_MINIMIZE_BUTTON
        } else {
            let x_in_button_area = px - button_area_x_start;
            if x_in_button_area < button_width {
                HIT_MINIMIZE_BUTTON
            } else if x_in_button_area < button_width * 2 {
                HIT_MAXIMIZE_BUTTON
            } else {
                HIT_CLOSE_BUTTON
            }
        };
        hit_test_map[pixel_idx] = button_id;

        x_offset += 1;
    }

    // Continue bottom edge linearly from where squircle ends to window edge
    let linear_start_x = x_start + start + crossings.len();
    let edge_y = y_start + button_height - 1;

    for px in linear_start_x..window_width {
        let pixel_idx = edge_y * window_width + px;
        pixels[pixel_idx] = edge_colour;

        for row in 1..start {
            let py = edge_y - row;
            let pixel_idx = py * window_width + px;
            pixels[pixel_idx] = bg_colour;

            // All pixels past the squircle belong to close button
            hit_test_map[pixel_idx] = HIT_CLOSE_BUTTON;
        }
    }

    x_start += button_width / 4;

    // Draw button symbols using glyph colours
    paint::glyph::minimize_symbol(
        pixels,
        window_width,
        x_start + button_width / 2,
        y_start + button_width / 2,
        button_width / 4,
        theme::MINIMIZE_GLYPH,
    );

    paint::glyph::maximize_symbol(
        pixels,
        window_width,
        x_start + button_width + button_width / 2,
        y_start + button_width / 2,
        button_width / 4,
        theme::MAXIMIZE_GLYPH,
        theme::MAXIMIZE_GLYPH_INTERIOR,
    );

    paint::glyph::close_symbol(
        pixels,
        window_width,
        x_start + button_width * 2 + button_width / 2,
        y_start + button_width / 2,
        button_width / 4,
        theme::CLOSE_GLYPH,
    );
    (start, crossings, x_start, button_height)
}

/// Verbatim port of photon's `draw_window_edges_and_mask`. Paints the two-tone window perimeter (light top/left, shadow bottom/right) and clips out squircle corners by zeroing pixels outside the squircle (those pixels become fully transparent under `with_transparent(true)`). Takes `start` and `crossings` from the matching call to [`draw_window_controls`].
pub fn draw_window_edges_and_mask(
    pixels: &mut [u32],
    hit_test_map: &mut [u8],
    width: u32,
    height: u32,
    start: usize,
    crossings: &[(u16, u8, u8)],
) {
    let light_colour = theme::WINDOW_LIGHT_EDGE;
    let shadow_colour = theme::WINDOW_SHADOW_EDGE;

    // Fill all four edges with white before squircle clipping
    // Top edge
    for x in 0..width {
        let idx = 0 * width + x;
        pixels[idx as usize] = light_colour;
    }

    // Bottom edge
    for x in 0..width {
        let idx = (height - 1) * width + x;
        pixels[idx as usize] = shadow_colour;
    }

    // Left edge
    for y in 0..height {
        let idx = y * width + 0;
        pixels[idx as usize] = light_colour;
    }

    // Right edge
    for y in 0..height {
        let idx = y * width + (width - 1);
        pixels[idx as usize] = shadow_colour;
    }

    // Fill four corner squares and clear hitmap
    for row in 0..start {
        for col in 0..start {
            let idx = row * width as usize + col;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }
    }
    for row in 0..start {
        for col in (width as usize - start)..width as usize {
            let idx = row * width as usize + col;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }
    }
    for row in (height as usize - start)..height as usize {
        for col in 0..start {
            let idx = row * width as usize + col;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }
    }
    for row in (height as usize - start)..height as usize {
        for col in (width as usize - start)..width as usize {
            let idx = row * width as usize + col;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }
    }

    // Top left/right edges
    let mut y_top = start;
    for crossing in 0..crossings.len() {
        let (inset, l, h) = crossings[crossing];
        // Left edge fill
        for idx in y_top * width as usize..y_top * width as usize + inset as usize {
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        // Left edge outer pixel
        let pixel_idx = y_top * width as usize + inset as usize;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(light_colour, h);
        } else {
            pixels[pixel_idx] = (light_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE; // NEEDS FIXED!!!
        }

        // Left edge inner pixel
        let pixel_idx = pixel_idx + 1;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], light_colour, h, l);

        // Right edge inner pixel
        let pixel_idx = y_top * width as usize + width as usize - 2 - inset as usize;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], shadow_colour, h, l);

        // Right edge outer pixel
        let pixel_idx = pixel_idx + 1;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(shadow_colour, h);
        } else {
            pixels[pixel_idx] = (shadow_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Right edge fill
        for idx in (y_top * width as usize + width as usize - inset as usize)
            ..((y_top + 1) * width as usize)
        {
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }
        y_top += 1;
    }

    // Bottom left/right edges
    let mut y_bottom = height as usize - start - 1;
    for crossing in 0..crossings.len() {
        let (inset, l, h) = crossings[crossing];

        // Left edge fill
        for idx in y_bottom * width as usize..y_bottom * width as usize + inset as usize {
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        // Left outer edge pixel
        let pixel_idx = y_bottom * width as usize + inset as usize;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(light_colour, h);
        } else {
            pixels[pixel_idx] = (light_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Left inner edge pixel
        let pixel_idx = pixel_idx + 1;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], light_colour, h, l);

        // Right edge inner pixel
        let pixel_idx = y_bottom * width as usize + width as usize - 2 - inset as usize;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], shadow_colour, h, l);

        // Right edge outer pixel
        let pixel_idx = pixel_idx + 1;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(shadow_colour, h);
        } else {
            pixels[pixel_idx] = (shadow_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Right edge fill
        for idx in (y_bottom * width as usize + width as usize - inset as usize)
            ..((y_bottom + 1) * width as usize)
        {
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        y_bottom -= 1;
    }

    // Left side top/bottom edges
    let mut x_left = start;
    for crossing in 0..crossings.len() {
        let (inset, l, h) = crossings[crossing];

        // Top edge fill
        for row in 0..inset as usize {
            let idx = row * width as usize + x_left;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        // Top outer edge pixel
        let pixel_idx = inset as usize * width as usize + x_left;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(light_colour, h);
        } else {
            pixels[pixel_idx] = (light_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Top inner edge pixel
        let pixel_idx = (inset as usize + 1) * width as usize + x_left;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], light_colour, h, l);

        // Bottom outer edge pixel
        let pixel_idx = (height as usize - 1 - inset as usize) * width as usize + x_left;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(shadow_colour, h);
        } else {
            pixels[pixel_idx] = (shadow_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Bottom inner edge pixel
        let pixel_idx = (height as usize - 2 - inset as usize) * width as usize + x_left;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], shadow_colour, h, l);

        // Bottom edge fill
        for row in (height as usize - inset as usize)..height as usize {
            let idx = row * width as usize + x_left;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        x_left += 1;
    }

    // Right side top/bottom edges
    let mut x_right = width as usize - start - 1;
    for crossing in 0..crossings.len() {
        let (inset, l, h) = crossings[crossing];

        // Top edge fill
        for row in 0..inset as usize {
            let idx = row * width as usize + x_right;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        // Top outer edge pixel
        let pixel_idx = inset as usize * width as usize + x_right;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(light_colour, h);
        } else {
            pixels[pixel_idx] = (light_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Top inner edge pixel
        let pixel_idx = (inset as usize + 1) * width as usize + x_right;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], light_colour, h, l);

        // Bottom outer edge pixel
        let pixel_idx = (height as usize - 1 - inset as usize) * width as usize + x_right;
        if paint::PREMULTIPLIED {
            pixels[pixel_idx] = paint::scale_alpha(shadow_colour, h);
        } else {
            pixels[pixel_idx] = (shadow_colour & 0x00FFFFFF) | ((h as u32) << 24);
        }
        if h < 255 {
            hit_test_map[pixel_idx] = HIT_NONE;
        }

        // Bottom inner edge pixel
        let pixel_idx = (height as usize - 2 - inset as usize) * width as usize + x_right;
        pixels[pixel_idx] = paint::blend_rgb_only(pixels[pixel_idx], shadow_colour, h, l);

        // Bottom edge fill
        for row in (height as usize - inset as usize)..height as usize {
            let idx = row * width as usize + x_right;
            pixels[idx] = 0;
            hit_test_map[idx] = HIT_NONE;
        }

        x_right -= 1;
    }
}

/// Verbatim port of photon's `draw_button_hairlines`. Vertical 1-px hairlines between minimize/maximize and maximize/close, drawn from the strip's vertical center outward until they hit a pixel of a different color (so they stop cleanly at the squircle edge above and the bottom edge below).
pub fn draw_button_hairlines(
    pixels: &mut [u32],
    hit_test_map: &mut [u8],
    window_width: u32,
    _window_height: u32,
    button_x_start: usize,
    button_height: usize,
    _start: usize,
    _crossings: &[(u16, u8, u8)],
) {
    let width = window_width as usize;
    let y_start = 0;

    // button_width equals button_height (passed in, already scaled with span * ru)
    let button_width = button_height;

    // Two hairlines: at 1.0 and 2.0 button widths from button area start
    let left_px = button_x_start + button_width;
    let right_px = button_x_start + button_width * 2;

    let center_y = y_start + button_height / 2;
    let edge_colour = theme::WINDOW_CONTROLS_HAIRLINE;

    // Left hairline — upward from center
    let center_colour = pixels[center_y * width + left_px];
    for py in (y_start..=center_y).rev() {
        let idx = py * width + left_px;
        let diff = pixels[idx] != center_colour;
        pixels[idx] = edge_colour;
        hit_test_map[idx] = HIT_NONE;
        if diff {
            break;
        }
    }
    // Left hairline — downward from center+1
    for py in (center_y + 1)..(y_start + button_height) {
        let idx = py * width + left_px;
        let diff = pixels[idx] != center_colour;
        pixels[idx] = edge_colour;
        hit_test_map[idx] = HIT_NONE;
        if diff {
            break;
        }
    }

    // Right hairline — upward from center
    let center_colour_right = pixels[center_y * width + right_px];
    for py in (y_start..=center_y).rev() {
        let idx = py * width + right_px;
        let diff = pixels[idx] != center_colour_right;
        pixels[idx] = edge_colour;
        hit_test_map[idx] = HIT_NONE;
        if diff {
            break;
        }
    }
    // Right hairline — downward from center+1
    for py in (center_y + 1)..(y_start + button_height) {
        let idx = py * width + right_px;
        let diff = pixels[idx] != center_colour_right;
        pixels[idx] = edge_colour;
        hit_test_map[idx] = HIT_NONE;
        if diff {
            break;
        }
    }
}

/// Verbatim port of photon's `draw_button_hover_by_pixels`. Add or subtract a packed-u32 hover delta over a precomputed pixel list (deltas chosen so RGB wraps and alpha absorbs the carry).
pub fn draw_button_hover_by_pixels(pixels: &mut [u32], pixel_list: &[usize], hover: bool, button_id: u8) {
    let hover_delta = match button_id {
        HIT_CLOSE_BUTTON => theme::CLOSE_HOVER,
        HIT_MAXIMIZE_BUTTON => theme::MAXIMIZE_HOVER,
        HIT_MINIMIZE_BUTTON => theme::MINIMIZE_HOVER,
        _ => return,
    };
    for &hit_idx in pixel_list {
        pixels[hit_idx] = if hover {
            pixels[hit_idx].wrapping_add(hover_delta)
        } else {
            pixels[hit_idx].wrapping_sub(hover_delta)
        };
    }
}

/// Build a pixel-index list for `button_id` by scanning `hit_test_map`. Photon caches these lists across frames in its `Compositor`; for v0 we recompute on hover-state change (still O(width*height) but only once per hover event, not per frame).
pub fn pixels_for_button(hit_test_map: &[u8], button_id: u8) -> Vec<usize> {
    if button_id == HIT_NONE { return Vec::new(); }
    let mut out = Vec::new();
    for (i, &id) in hit_test_map.iter().enumerate() {
        if id == button_id { out.push(i); }
    }
    out
}

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

    #[test]
    fn pixels_for_button_returns_matching_indices() {
        let mut map = vec![HIT_NONE; 16];
        map[3] = HIT_CLOSE_BUTTON;
        map[7] = HIT_CLOSE_BUTTON;
        map[10] = HIT_MAXIMIZE_BUTTON;
        let close = pixels_for_button(&map, HIT_CLOSE_BUTTON);
        assert_eq!(close, vec![3, 7]);
        let max = pixels_for_button(&map, HIT_MAXIMIZE_BUTTON);
        assert_eq!(max, vec![10]);
    }

    #[test]
    fn hover_by_pixels_shifts_listed_pixels_only() {
        let mut pixels = vec![0xFF20_2024u32; 8];
        draw_button_hover_by_pixels(&mut pixels, &[2, 5], true, HIT_CLOSE_BUTTON);
        assert_ne!(pixels[2], 0xFF20_2024);
        assert_ne!(pixels[5], 0xFF20_2024);
        assert_eq!(pixels[0], 0xFF20_2024);
        assert_eq!(pixels[7], 0xFF20_2024);
    }

    #[test]
    fn resize_edge_corners_win() {
        assert_eq!(get_resize_edge(1920, 1080, 0.0, 0.0), ResizeEdge::TopLeft);
        assert_eq!(get_resize_edge(1920, 1080, 1919.9, 0.0), ResizeEdge::TopRight);
        assert_eq!(get_resize_edge(1920, 1080, 0.0, 1079.9), ResizeEdge::BottomLeft);
        assert_eq!(get_resize_edge(1920, 1080, 1919.9, 1079.9), ResizeEdge::BottomRight);
    }

    #[test]
    fn resize_edge_none_in_center() {
        assert_eq!(get_resize_edge(1920, 1080, 960.0, 540.0), ResizeEdge::None);
    }

    #[test]
    fn draw_window_controls_writes_hit_map() {
        let w = 800u32;
        let h = 600u32;
        let mut pixels = vec![0u32; (w * h) as usize];
        let mut hit_map = vec![HIT_NONE; (w * h) as usize];
        let _ = draw_window_controls(&mut pixels, &mut hit_map, w, h, 1.0);
        // Some pixels in the top-right strip should have button IDs.
        let close_count = hit_map.iter().filter(|&&id| id == HIT_CLOSE_BUTTON).count();
        let max_count = hit_map.iter().filter(|&&id| id == HIT_MAXIMIZE_BUTTON).count();
        let min_count = hit_map.iter().filter(|&&id| id == HIT_MINIMIZE_BUTTON).count();
        assert!(close_count > 0, "no close pixels");
        assert!(max_count > 0, "no maximize pixels");
        assert!(min_count > 0, "no minimize pixels");
    }
}