rioterm 0.3.8

Rio terminal is a hardware-accelerated GPU terminal emulator, focusing to run in desktops and browsers.
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
use crate::crosswords::pos::Column;
use crate::crosswords::pos::Line;
use crate::crosswords::pos::Side;
use crate::event::ClickState;
use rio_backend::crosswords::pos::Pos;
use rio_window::event::ElementState;
use rio_window::event::MouseButton;
use std::time::Instant;

#[derive(Default, Debug)]
pub struct AccumulatedScroll {
    /// Scroll we should perform along `x` axis.
    pub x: f64,

    /// Scroll we should perform along `y` axis.
    pub y: f64,
}

#[derive(Debug)]
pub struct Mouse {
    pub multiplier: f64,
    pub divider: f64,
    pub left_button_state: ElementState,
    pub middle_button_state: ElementState,
    pub right_button_state: ElementState,
    pub last_click_timestamp: Instant,
    pub last_click_button: MouseButton,
    pub click_state: ClickState,
    pub accumulated_scroll: AccumulatedScroll,
    pub square_side: Side,
    pub inside_text_area: bool,
    pub x: usize,
    pub y: usize,
    pub on_border: bool,
    /// Raw (unclamped) cursor Y in physical pixels, for selection scroll.
    pub raw_y: f64,
}

impl Default for Mouse {
    fn default() -> Mouse {
        Mouse {
            multiplier: 3.0,
            divider: 1.0,
            last_click_timestamp: Instant::now(),
            last_click_button: MouseButton::Left,
            left_button_state: ElementState::Released,
            middle_button_state: ElementState::Released,
            right_button_state: ElementState::Released,
            click_state: ClickState::None,
            square_side: Side::Left,
            inside_text_area: Default::default(),
            on_border: false,
            accumulated_scroll: AccumulatedScroll::default(),
            x: Default::default(),
            y: Default::default(),
            raw_y: 0.0,
        }
    }
}

impl Mouse {
    pub fn new(multiplier: f64, divider: f64) -> Self {
        Self {
            multiplier,
            divider,
            ..Default::default()
        }
    }

    #[inline]
    pub fn set_multiplier_and_divider(&mut self, multiplier: f64, divider: f64) {
        self.multiplier = multiplier;
        self.divider = divider;
    }
}

#[inline]
pub fn calculate_mouse_position(
    mouse: &Mouse,
    display_offset: usize,
    columns_rows: (usize, usize),
    margin_x_left: f32,
    margin_y_top: f32,
    cell_dimension: (f32, f32),
) -> Pos {
    // In case sugarloaf hasn't obtained the dimensions
    if cell_dimension.0 == 0.0 || cell_dimension.1 == 0.0 {
        return Pos::default();
    }

    let cell_width = cell_dimension.0;
    let cell_height = cell_dimension.1;
    // Margins are already pre-scaled (multiplied by scale_factor in
    // update_scaled_margin), so use them directly — do not scale again.
    let margin_x = margin_x_left as usize;
    let margin_y = margin_y_top as usize;

    let col = if (margin_x + cell_width as usize) > mouse.x {
        Column(0)
    } else {
        let col = ((mouse.x - margin_x) as f32 / cell_width) as usize;
        std::cmp::min(Column(col), Column(columns_rows.0 - 1))
    };

    let row = mouse.y.saturating_sub(margin_y) as f32 / cell_height;
    let calc_row = std::cmp::min(row as usize, columns_rows.1 - 1);
    let row = Line(calc_row as i32) - (display_offset);

    Pos::new(row, col)
}

/// Determine which side of a cell the mouse x-position falls on.
///
/// `margin_x` is already pre-scaled (physical pixels).
/// `cell_width` is the float cell width.
/// `grid_width` is the total width of the grid area (physical pixels).
#[inline]
pub fn calculate_side_by_pos(
    x: usize,
    margin_x: f32,
    cell_width: f32,
    grid_width: f32,
) -> Side {
    let x_in_grid = (x as f32 - margin_x).max(0.0);
    let cell_x = x_in_grid % cell_width;
    let half_cell_width = cell_width / 2.0;

    let additional_padding = (grid_width - margin_x) % cell_width;
    let end_of_grid = grid_width - margin_x - additional_padding;

    if cell_x > half_cell_width || x as f32 >= end_of_grid {
        Side::Right
    } else {
        Side::Left
    }
}

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

    /// Cell boundaries with width=9.4 and no margin: 0, 9.4, 18.8, 28.2, ...
    #[test]
    fn test_pos_calc_moving_mouse_x_with_scale_1() {
        let display_offset = 0;

        let columns = 10;
        let lines = 5;
        let margin_x_left = 0.0;
        let margin_y_top = 0.0;
        let cell_dimension_width = 9.4;
        let cell_dimension_height = 18.0;

        // x=8 → 8/9.4 = 0.85 → col 0
        let mouse = Mouse {
            x: 8,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=9 → 9/9.4 = 0.96 → col 0 (still within first cell)
        let mouse = Mouse {
            x: 9,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=10 → 10/9.4 = 1.06 → col 1
        let mouse = Mouse {
            x: 10,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=17 → 17/9.4 = 1.81 → col 1
        let mouse = Mouse {
            x: 17,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=19 → 19/9.4 = 2.02 → col 2
        let mouse = Mouse {
            x: 19,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(2)));
    }

    /// Same as scale_1 — scale_factor doesn't affect column math when margins
    /// are already pre-scaled (and here margin is 0).
    #[test]
    fn test_pos_calc_moving_mouse_x_with_scale_2() {
        let display_offset = 0;

        let columns = 10;
        let lines = 5;
        let margin_x_left = 0.0; // already scaled
        let margin_y_top = 0.0;
        let cell_dimension_width = 9.4;
        let cell_dimension_height = 18.0;

        let mouse = Mouse {
            x: 8,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=9 → 9/9.4 = 0.96 → col 0
        let mouse = Mouse {
            x: 9,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=10 → 10/9.4 = 1.06 → col 1
        let mouse = Mouse {
            x: 10,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        let mouse = Mouse {
            x: 17,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        let mouse = Mouse {
            x: 19,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(2)));
    }

    /// With pre-scaled margin=10, cell boundaries at: 10, 19.4, 28.8, 38.2, ...
    #[test]
    fn test_pos_calc_moving_mouse_x_with_scale_1_with_margin_10() {
        let display_offset = 0;

        let columns = 10;
        let lines = 5;
        let margin_x_left = 10.0; // already scaled (10 * 1.0)
        let margin_y_top = 0.0;
        let cell_dimension_width = 9.4;
        let cell_dimension_height = 18.0;

        // x=8 → before margin → col 0
        let mouse = Mouse {
            x: 8,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=9 → before margin → col 0
        let mouse = Mouse {
            x: 9,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=18 → (18-10)/9.4 = 0.85 → col 0
        let mouse = Mouse {
            x: 18,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=20 → (20-10)/9.4 = 1.06 → col 1
        let mouse = Mouse {
            x: 20,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=28 → (28-10)/9.4 = 1.91 → col 1
        let mouse = Mouse {
            x: 28,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=29 → (29-10)/9.4 = 2.02 → col 2
        let mouse = Mouse {
            x: 29,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(2)));

        // x=38 → (38-10)/9.4 = 2.98 → col 2
        let mouse = Mouse {
            x: 38,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(2)));

        // x=39 → (39-10)/9.4 = 3.08 → col 3
        let mouse = Mouse {
            x: 39,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(3)));
    }

    /// Margin=20 is already pre-scaled (e.g. config 10.0 * scale 2.0).
    /// Cell boundaries at: 20, 29.4, 38.8, ...
    #[test]
    fn test_pos_calc_moving_mouse_x_with_scale_2_with_margin_10() {
        let display_offset = 0;

        let columns = 10;
        let lines = 5;
        let margin_x_left = 20.0; // already scaled (10 * 2.0)
        let margin_y_top = 0.0;
        let cell_dimension_width = 9.4;
        let cell_dimension_height = 18.0;

        // x=9 → before margin → col 0
        let mouse = Mouse {
            x: 9,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=28 → (28-20)/9.4 = 0.85 → col 0
        let mouse = Mouse {
            x: 28,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=30 → (30-20)/9.4 = 1.06 → col 1
        let mouse = Mouse {
            x: 30,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));
    }

    /// Regression: margins passed to calculate_mouse_position are already
    /// pre-scaled (multiplied by scale_factor in update_scaled_margin), but the
    /// function multiplied them by scale_factor again.  With a 2× display and
    /// margin_y_top=72 (already 36*2), the double-scaling produces 144 instead
    /// of 72, shifting the row calculation by ~2 rows.
    ///
    /// Uses exact values observed on a Retina display:
    ///   cell 16.41×33, margin (4, 72) pre-scaled, scale 2.0, 96×27 grid.
    #[test]
    fn test_row_not_double_scaled() {
        let display_offset = 0;

        let columns = 96;
        let lines = 27;
        // These margins are ALREADY scaled (config margin * scale_factor).
        let margin_x_left = 4.0; // e.g. config 2.0 * scale 2.0
        let margin_y_top = 72.0; // e.g. config 36.0 * scale 2.0
        let cell_w = 16.41;
        let cell_h = 33.0;

        // Row 0 starts at y = margin_y_top = 72.
        // Row 6 spans y = [72 + 6*33, 72 + 7*33) = [270, 303).
        let mouse = Mouse {
            x: 100,
            y: 280,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_w, cell_h),
        );
        // (280 - 72) / 33 = 6.3 → row 6
        // Bug: (280 - 144) / 33 = 4.1 → row 4  (margin double-scaled)
        assert_eq!(pos.row, Line(6));

        // Row 22 spans y = [72 + 22*33, 72 + 23*33) = [798, 831).
        let mouse = Mouse {
            x: 100,
            y: 820,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_w, cell_h),
        );
        // (820 - 72) / 33 = 22.7 → row 22
        // Bug: (820 - 144) / 33 = 20.5 → row 20
        assert_eq!(pos.row, Line(22));
    }

    /// Same double-scaling issue on the X axis, but less visible with small
    /// margins.  With margin_x_left=20 (pre-scaled) and scale=2.0 the error
    /// is 20 extra pixels — enough to shift a column.
    #[test]
    fn test_col_not_double_scaled() {
        let display_offset = 0;

        let columns = 96;
        let lines = 27;
        let margin_x_left = 20.0; // already scaled
        let margin_y_top = 72.0;
        let cell_w = 16.41;
        let cell_h = 33.0;

        // Col 3 starts at x = 20 + 3*16.41 = 69.23.
        let mouse = Mouse {
            x: 70,
            y: 200,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_w, cell_h),
        );
        // (70 - 20) / 16.41 = 3.05 → col 3
        // Bug: (70 - 40) / 16 = 1.87 → col 1  (margin double-scaled + int truncation)
        assert_eq!(pos.col, Column(3));
    }

    /// Cell width=12.6, margin=10 (pre-scaled). Boundaries: 10, 22.6, 35.2, 47.8, ...
    #[test]
    fn test_pos_calc_font_size_12_6_moving_mouse_x_with_scale_1_with_margin_10() {
        let display_offset = 0;

        let columns = 10;
        let lines = 5;
        let margin_x_left = 10.0;
        let margin_y_top = 0.0;
        let cell_dimension_width = 12.6;
        let cell_dimension_height = 18.0;

        // x=10 → (10-10)/12.6 = 0 → col 0
        let mouse = Mouse {
            x: 10,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=22 → (22-10)/12.6 = 0.95 → col 0
        let mouse = Mouse {
            x: 22,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(0)));

        // x=23 → (23-10)/12.6 = 1.03 → col 1
        let mouse = Mouse {
            x: 23,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=35 → (35-10)/12.6 = 1.98 → col 1
        let mouse = Mouse {
            x: 35,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(1)));

        // x=36 → (36-10)/12.6 = 2.06 → col 2
        let mouse = Mouse {
            x: 36,
            ..Default::default()
        };
        let pos = calculate_mouse_position(
            &mouse,
            display_offset,
            (columns, lines),
            margin_x_left,
            margin_y_top,
            (cell_dimension_width, cell_dimension_height),
        );
        assert_eq!(pos, Pos::new(Line(0), Column(2)));
    }

    /// With cell_width=16.41, the left half is [0, 8.205) and right half
    /// is [8.205, 16.41).  Using float math avoids the truncation that
    /// would shift the boundary to 8 (half of 16).
    #[test]
    fn test_side_by_pos_float_precision() {
        let cell_width = 16.41_f32;
        let margin_x = 8.0; // pre-scaled
        let grid_width = 8.0 + 96.0 * cell_width;

        // Just left of center of first cell: margin + 8.0 = pixel 16
        // cell_x = 16 - 8 = 8.0, half = 8.205 → Left
        assert_eq!(
            calculate_side_by_pos(16, margin_x, cell_width, grid_width),
            Side::Left,
        );

        // Just right of center: margin + 8.41 ≈ pixel 17
        // cell_x = 17 - 8 = 9.0, half = 8.205 → Right
        assert_eq!(
            calculate_side_by_pos(17, margin_x, cell_width, grid_width),
            Side::Right,
        );
    }

    /// Regression: integer truncation of cell_width (16.41 → 16) caused
    /// the modulo to drift at higher pixel positions, making the side
    /// detection wrong for cells far from the left edge.
    #[test]
    fn test_side_by_pos_no_drift_at_high_columns() {
        let cell_width = 16.41_f32;
        let margin_x = 8.0;
        let grid_width = 8.0 + 96.0 * cell_width;

        // Column 76 starts at margin + 76 * 16.41 = 8 + 1247.16 = 1255.16
        // Left side: pixel 1256 → cell_x = (1256 - 8) % 16.41 = 1248 % 16.41 ≈ 0.66 → Left
        assert_eq!(
            calculate_side_by_pos(1256, margin_x, cell_width, grid_width),
            Side::Left,
        );

        // Right side of same cell: pixel 1265 → cell_x = (1265 - 8) % 16.41 = 1257 % 16.41 ≈ 9.48 → Right
        assert_eq!(
            calculate_side_by_pos(1265, margin_x, cell_width, grid_width),
            Side::Right,
        );
    }

    /// Margin must not be double-scaled in side calculation.
    #[test]
    fn test_side_by_pos_prescaled_margin() {
        let cell_width = 16.0;
        let margin_x = 40.0; // already scaled (e.g. 20 * 2.0)
        let grid_width = 40.0 + 80.0 * 16.0;

        // Pixel 41: just past margin, left side of cell 0
        // cell_x = (41 - 40) % 16 = 1.0, half = 8.0 → Left
        assert_eq!(
            calculate_side_by_pos(41, margin_x, cell_width, grid_width),
            Side::Left,
        );

        // Pixel 50: past center of cell 0
        // cell_x = (50 - 40) % 16 = 10.0, half = 8.0 → Right
        assert_eq!(
            calculate_side_by_pos(50, margin_x, cell_width, grid_width),
            Side::Right,
        );

        // Pixel 30: before margin → clamped to 0, Left
        assert_eq!(
            calculate_side_by_pos(30, margin_x, cell_width, grid_width),
            Side::Left,
        );
    }
}