strata-ui-platform 0.1.0

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
//! Window configuration, platform window traits, and mock implementations.

use crate::events::PlatformEvent;
use crate::surface::{FramePacer, RefreshRate, SurfaceConfig, SurfaceHandle};
use std::collections::VecDeque;
use std::time::Instant;

/// Operating mode of the platform window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WindowMode {
    #[default]
    Windowed,
    BorderlessFullscreen,
    ExclusiveFullscreen,
    Minimized,
    Maximized,
}

/// Window frame decoration style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum WindowDecoration {
    #[default]
    NativeOs,
    ClientSideDecorations,
    FramelessTransparent,
}

/// Operating system color scheme / theme preference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ThemeMode {
    #[default]
    System,
    Light,
    Dark,
}

/// Cursor icon style.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum CursorIcon {
    #[default]
    Default,
    Pointer,
    Text,
    Crosshair,
    Move,
    NotAllowed,
    Wait,
    Grab,
    Grabbing,
    ResizeN,
    ResizeS,
    ResizeE,
    ResizeW,
    ResizeNe,
    ResizeNw,
    ResizeSe,
    ResizeSw,
    ColResize,
    RowResize,
    Help,
    None,
}

/// Monitor workarea placement and sticky docking anchor for a platform window.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WindowAnchor {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
    Center,
}

impl WindowAnchor {
    /// Returns true if this anchor pins ("sticks") the window to a monitor workarea corner.
    pub const fn is_sticky_dock(self) -> bool {
        matches!(
            self,
            Self::TopLeft | Self::TopRight | Self::BottomLeft | Self::BottomRight
        )
    }

    /// Computes target `(x, y)` coordinates within a monitor workarea for a window of `(win_w, win_h)`.
    pub const fn compute_position(
        self,
        workarea_x: i32,
        workarea_y: i32,
        workarea_w: i32,
        workarea_h: i32,
        win_w: i32,
        win_h: i32,
    ) -> (i32, i32) {
        match self {
            Self::TopLeft => (workarea_x, workarea_y),
            Self::TopRight => (workarea_x + workarea_w - win_w, workarea_y),
            Self::BottomLeft => (workarea_x, workarea_y + workarea_h - win_h),
            Self::BottomRight => (
                workarea_x + workarea_w - win_w,
                workarea_y + workarea_h - win_h,
            ),
            Self::Center => (
                workarea_x + (workarea_w - win_w) / 2,
                workarea_y + (workarea_h - win_h) / 2,
            ),
        }
    }
}

/// Configuration used when creating or reconfiguring a platform window.
#[derive(Debug, Clone)]
pub struct WindowConfig {
    pub title: String,
    pub width: u32,
    pub height: u32,
    pub min_width: Option<u32>,
    pub min_height: Option<u32>,
    pub max_width: Option<u32>,
    pub max_height: Option<u32>,
    pub resizable: bool,
    pub mode: WindowMode,
    pub decoration: WindowDecoration,
    pub theme: ThemeMode,
    pub transparent: bool,
    pub always_on_top: bool,
    pub anchor: Option<WindowAnchor>,
    pub dock_bottom_right: bool,
    pub single_instance_id: Option<String>,
    pub vibrancy: crate::vibrancy::VibrancyEffect,
    /// Swapchain presentation mode (`Immediate` / `Mailbox` for uncapped 3D viewports, `Fifo` default).
    pub present_mode: crate::surface::PresentMode,
}

impl WindowConfig {
    /// Sets swapchain present mode (e.g. `PresentMode::Immediate` for uncapped framerate).
    pub fn with_present_mode(mut self, present_mode: crate::surface::PresentMode) -> Self {
        self.present_mode = present_mode;
        self
    }

    /// Configures the window to enforce a single-instance mutex and focus existing instance on launch.
    pub fn with_single_instance(mut self, app_id: impl Into<String>) -> Self {
        self.single_instance_id = Some(app_id.into());
        self
    }

    /// Configures window backdrop blur and vibrancy (Mica, Acrylic, NSVisualEffectView).
    pub fn with_vibrancy(mut self, vibrancy: crate::vibrancy::VibrancyEffect) -> Self {
        self.vibrancy = vibrancy;
        self
    }

    /// Configures monitor workarea placement or corner docking for this window.
    pub fn with_anchor(mut self, anchor: WindowAnchor) -> Self {
        self.anchor = Some(anchor);
        if anchor == WindowAnchor::BottomRight {
            self.dock_bottom_right = true;
        }
        self
    }

    /// Pins the window to the bottom-right corner of the primary monitor's workarea ("stuck" mode).
    pub fn with_dock_bottom_right(mut self, dock_bottom_right: bool) -> Self {
        self.dock_bottom_right = dock_bottom_right;
        if dock_bottom_right && self.anchor.is_none() {
            self.anchor = Some(WindowAnchor::BottomRight);
        } else if !dock_bottom_right && self.anchor == Some(WindowAnchor::BottomRight) {
            self.anchor = None;
        }
        self
    }

    /// Resolves the effective window anchor, honoring both `anchor` and `dock_bottom_right`.
    pub const fn effective_anchor(&self) -> Option<WindowAnchor> {
        if let Some(anchor) = self.anchor {
            Some(anchor)
        } else if self.dock_bottom_right {
            Some(WindowAnchor::BottomRight)
        } else {
            None
        }
    }

    /// Returns true when the window is docked ("stuck") to a monitor workarea corner.
    pub const fn is_docked(&self) -> bool {
        match self.effective_anchor() {
            Some(anchor) => anchor.is_sticky_dock(),
            None => false,
        }
    }

    /// Computes target `(x, y)` coordinates in the given monitor workarea if an anchor is configured.
    pub const fn compute_anchored_position(
        &self,
        workarea_x: i32,
        workarea_y: i32,
        workarea_w: i32,
        workarea_h: i32,
    ) -> Option<(i32, i32)> {
        match self.effective_anchor() {
            Some(anchor) => Some(anchor.compute_position(
                workarea_x,
                workarea_y,
                workarea_w,
                workarea_h,
                self.width as i32,
                self.height as i32,
            )),
            None => None,
        }
    }
}

impl Default for WindowConfig {
    fn default() -> Self {
        Self {
            title: "Strata Application".to_string(),
            width: 1280,
            height: 800,
            min_width: Some(320),
            min_height: Some(240),
            max_width: None,
            max_height: None,
            resizable: true,
            mode: WindowMode::Windowed,
            decoration: WindowDecoration::NativeOs,
            theme: ThemeMode::System,
            transparent: false,
            always_on_top: false,
            anchor: None,
            dock_bottom_right: false,
            single_instance_id: None,
            vibrancy: crate::vibrancy::VibrancyEffect::None,
            present_mode: crate::surface::PresentMode::Fifo,
        }
    }
}

/// Physical display monitor descriptor.
#[derive(Debug, Clone, PartialEq)]
pub struct MonitorInfo {
    pub id: u64,
    pub name: String,
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
    pub scale_factor: f32,
    pub refresh_rate: u32,
    pub is_primary: bool,
}

impl MonitorInfo {
    /// Creates a new monitor descriptor.
    pub fn new(
        id: u64,
        name: impl Into<String>,
        x: i32,
        y: i32,
        width: u32,
        height: u32,
        scale_factor: f32,
        refresh_rate: u32,
        is_primary: bool,
    ) -> Self {
        Self {
            id,
            name: name.into(),
            x,
            y,
            width,
            height,
            scale_factor,
            refresh_rate,
            is_primary,
        }
    }

    /// Default standard 1080p 1.0x display.
    pub fn standard_1080p() -> Self {
        Self::new(1, "Primary Display", 0, 0, 1920, 1080, 1.0, 60, true)
    }

    /// High-DPI 4K 2.0x display.
    pub fn hidpi_4k() -> Self {
        Self::new(2, "4K UHD Display", 1920, 0, 3840, 2160, 2.0, 120, false)
    }
}

/// Abstract platform window trait defining common cross-platform window operations.
pub trait PlatformWindow {
    fn title(&self) -> &str;
    fn set_title(&mut self, title: &str);
    fn size(&self) -> (u32, u32);
    fn set_size(&mut self, width: u32, height: u32);
    fn scale_factor(&self) -> f32;
    fn set_scale_factor(&mut self, scale_factor: f32);
    fn mode(&self) -> WindowMode;
    fn set_mode(&mut self, mode: WindowMode);
    fn is_focused(&self) -> bool;
    fn set_focused(&mut self, focused: bool);
    fn cursor(&self) -> CursorIcon;
    fn set_cursor(&mut self, cursor: CursorIcon);
    fn request_redraw(&mut self);
    fn is_redraw_requested(&self) -> bool;
    fn poll_events(&mut self) -> Vec<PlatformEvent>;
    fn push_event(&mut self, event: PlatformEvent);
    /// Returns the monitor containing the center of this window, if known.
    fn current_monitor(&self) -> Option<MonitorInfo> {
        None
    }
    /// Returns the list of currently connected physical display monitors.
    fn available_monitors(&self) -> Vec<MonitorInfo> {
        Vec::new()
    }
    /// Returns the primary system monitor.
    fn primary_monitor(&self) -> Option<MonitorInfo> {
        None
    }
    /// Transmits an incremental AccessKit/OS accessibility tree update to native screen readers.
    fn on_accessibility_update(&mut self, _json_payload: &str) {}
    /// Sets candidate window positioning following active text caret coordinate.
    fn set_ime_cursor_location(&mut self, _x: i32, _y: i32, _width: i32, _height: i32) {}
    /// Updates surrounding text buffer for context-aware IME prediction.
    fn set_ime_surrounding_text(&mut self, _text: &str, _cursor_index: i32) {}
    /// Resets active IME composition session.
    fn reset_ime(&mut self) {}
    /// Initiates an interactive window drag operation with the native window manager.
    fn begin_window_drag(&mut self, _x: f32, _y: f32) {}
    /// Initiates an interactive window resize operation with the native window manager.
    fn begin_window_resize(&mut self, _edge: crate::csd::CsdHitZone, _x: f32, _y: f32) {}
    /// Toggles the window maximized state and returns true if the window is now maximized.
    fn toggle_maximize(&mut self) -> bool {
        let current = self.mode();
        let new_mode = if current == WindowMode::Maximized {
            WindowMode::Windowed
        } else {
            WindowMode::Maximized
        };
        self.set_mode(new_mode);
        new_mode == WindowMode::Maximized
    }
}

/// Contract for platform windows supporting direct software/swapchain pixel blitting.
pub trait NativeSurfaceWindow: PlatformWindow {
    /// Blits a 32-bit BGRA uncompressed buffer to the window client area.
    fn present_surface(
        &mut self,
        pixels: &[u8],
        width: u32,
        height: u32,
    ) -> Result<(), crate::surface::PresentError>;
}

/// Headless mock platform window for deterministic testing, CI automation, and headless rendering.
#[derive(Debug, Clone)]
pub struct MockPlatformWindow {
    pub config: WindowConfig,
    pub scale_factor: f32,
    pub is_focused: bool,
    pub cursor: CursorIcon,
    pub redraw_requested: bool,
    pub event_queue: VecDeque<PlatformEvent>,
    pub last_accessibility_update: Option<String>,
    pub ime_cursor_location: Option<(i32, i32, i32, i32)>,
    pub ime_surrounding: Option<(String, i32)>,
    pub monitors: Vec<MonitorInfo>,
    pub current_monitor_id: Option<u64>,
}

impl MockPlatformWindow {
    /// Creates a new mock window with the specified configuration.
    pub fn new(config: WindowConfig) -> Self {
        Self {
            config,
            scale_factor: 1.0,
            is_focused: true,
            cursor: CursorIcon::Default,
            redraw_requested: false,
            event_queue: VecDeque::new(),
            last_accessibility_update: None,
            ime_cursor_location: None,
            ime_surrounding: None,
            monitors: vec![MonitorInfo::standard_1080p()],
            current_monitor_id: Some(1),
        }
    }

    /// Creates a mock window with default configuration.
    pub fn new_mock() -> Self {
        Self::new(WindowConfig::default())
    }

    /// Injects an event into the window's event queue.
    pub fn inject_event(&mut self, event: PlatformEvent) {
        self.push_event(event);
    }

    /// Consumes and clears the redraw request flag.
    pub fn take_redraw_request(&mut self) -> bool {
        let requested = self.redraw_requested;
        self.redraw_requested = false;
        requested
    }

    /// Adds a monitor to the mock topology.
    pub fn add_monitor(&mut self, monitor: MonitorInfo) {
        self.monitors.push(monitor);
    }

    /// Moves the window to another monitor, automatically updating high-DPI scaling.
    pub fn move_to_monitor(&mut self, monitor_id: u64) {
        if let Some(monitor) = self.monitors.iter().find(|m| m.id == monitor_id).cloned() {
            self.current_monitor_id = Some(monitor_id);
            if (self.scale_factor - monitor.scale_factor).abs() > 0.001 {
                self.set_scale_factor(monitor.scale_factor);
            }
        }
    }
}

impl PlatformWindow for MockPlatformWindow {
    fn title(&self) -> &str {
        &self.config.title
    }

    fn set_title(&mut self, title: &str) {
        self.config.title = title.to_string();
    }

    fn size(&self) -> (u32, u32) {
        (self.config.width, self.config.height)
    }

    fn set_size(&mut self, width: u32, height: u32) {
        self.config.width = width;
        self.config.height = height;
        self.push_event(PlatformEvent::WindowResized { width, height });
    }

    fn scale_factor(&self) -> f32 {
        self.scale_factor
    }

    fn set_scale_factor(&mut self, scale_factor: f32) {
        self.scale_factor = scale_factor;
        self.push_event(PlatformEvent::ScaleFactorChanged { scale_factor });
    }

    fn mode(&self) -> WindowMode {
        self.config.mode
    }

    fn set_mode(&mut self, mode: WindowMode) {
        let old = self.config.mode;
        self.config.mode = mode;
        if old != mode {
            self.push_event(PlatformEvent::WindowMaximized(
                mode == WindowMode::Maximized,
            ));
        }
    }

    fn is_focused(&self) -> bool {
        self.is_focused
    }

    fn set_focused(&mut self, focused: bool) {
        self.is_focused = focused;
        self.push_event(PlatformEvent::WindowFocused(focused));
    }

    fn cursor(&self) -> CursorIcon {
        self.cursor
    }

    fn set_cursor(&mut self, cursor: CursorIcon) {
        self.cursor = cursor;
    }

    fn request_redraw(&mut self) {
        self.redraw_requested = true;
    }

    fn is_redraw_requested(&self) -> bool {
        self.redraw_requested
    }

    fn poll_events(&mut self) -> Vec<PlatformEvent> {
        self.event_queue.drain(..).collect()
    }

    fn push_event(&mut self, event: PlatformEvent) {
        self.event_queue.push_back(event);
    }

    fn current_monitor(&self) -> Option<MonitorInfo> {
        self.current_monitor_id
            .and_then(|id| self.monitors.iter().find(|m| m.id == id).cloned())
    }

    fn available_monitors(&self) -> Vec<MonitorInfo> {
        self.monitors.clone()
    }

    fn primary_monitor(&self) -> Option<MonitorInfo> {
        self.monitors.iter().find(|m| m.is_primary).cloned()
    }

    fn on_accessibility_update(&mut self, json_payload: &str) {
        self.last_accessibility_update = Some(json_payload.to_string());
    }

    fn set_ime_cursor_location(&mut self, x: i32, y: i32, width: i32, height: i32) {
        self.ime_cursor_location = Some((x, y, width, height));
    }

    fn set_ime_surrounding_text(&mut self, text: &str, cursor_index: i32) {
        self.ime_surrounding = Some((text.to_string(), cursor_index));
    }

    fn reset_ime(&mut self) {
        self.ime_cursor_location = None;
        self.ime_surrounding = None;
    }
}

impl NativeSurfaceWindow for MockPlatformWindow {
    fn present_surface(
        &mut self,
        _pixels: &[u8],
        _width: u32,
        _height: u32,
    ) -> Result<(), crate::surface::PresentError> {
        self.redraw_requested = false;
        Ok(())
    }
}

/// Live interactive platform window with surface configuration, frame pacing, and event queuing.
#[derive(Debug, Clone)]
pub struct LivePlatformWindow {
    pub config: WindowConfig,
    pub surface_config: SurfaceConfig,
    pub surface_handle: SurfaceHandle,
    pub frame_pacer: FramePacer,
    pub scale_factor: f32,
    pub is_focused: bool,
    pub cursor: CursorIcon,
    pub redraw_requested: bool,
    pub event_queue: VecDeque<PlatformEvent>,
    pub monitors: Vec<MonitorInfo>,
    pub current_monitor_id: Option<u64>,
    pub display_buffer: Vec<u8>,
    pub is_native_presented: bool,
}

impl LivePlatformWindow {
    /// Creates a live platform window initialized with the given configuration.
    pub fn new(config: WindowConfig) -> Self {
        let scale_factor = 1.0;
        let surface_config = SurfaceConfig {
            width: config.width,
            height: config.height,
            scale_factor,
            ..Default::default()
        };
        let surface_handle = {
            #[cfg(target_os = "linux")]
            {
                if std::env::var_os("WAYLAND_DISPLAY").is_some() {
                    SurfaceHandle {
                        window_id: 1001,
                        raw_window_ptr: 0x574C4E44,  // "WLND"
                        raw_display_ptr: 0x574C4453, // "WLDS"
                        width: config.width,
                        height: config.height,
                        scale_factor_snapped: (scale_factor * 64.0) as u32,
                    }
                } else if std::env::var_os("DISPLAY").is_some() {
                    SurfaceHandle {
                        window_id: 1002,
                        raw_window_ptr: 0x58313157,  // "X11W"
                        raw_display_ptr: 0x58313144, // "X11D"
                        width: config.width,
                        height: config.height,
                        scale_factor_snapped: (scale_factor * 64.0) as u32,
                    }
                } else {
                    SurfaceHandle::new_headless(config.width, config.height, scale_factor)
                }
            }
            #[cfg(target_os = "windows")]
            {
                SurfaceHandle {
                    window_id: 2001,
                    raw_window_ptr: 0x57494E32,  // "WIN2"
                    raw_display_ptr: 0x47444931, // "GDI1"
                    width: config.width,
                    height: config.height,
                    scale_factor_snapped: (scale_factor * 64.0) as u32,
                }
            }
            #[cfg(target_os = "macos")]
            {
                SurfaceHandle {
                    window_id: 3001,
                    raw_window_ptr: 0x434F4341,  // "COCA"
                    raw_display_ptr: 0x4150504C, // "APPL"
                    width: config.width,
                    height: config.height,
                    scale_factor_snapped: (scale_factor * 64.0) as u32,
                }
            }
            #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
            {
                SurfaceHandle::new_headless(config.width, config.height, scale_factor)
            }
        };
        let frame_pacer = FramePacer::new(RefreshRate::Hz60);
        Self {
            config,
            surface_config,
            surface_handle,
            frame_pacer,
            scale_factor,
            is_focused: true,
            cursor: CursorIcon::Default,
            redraw_requested: true,
            event_queue: VecDeque::new(),
            monitors: vec![MonitorInfo::standard_1080p()],
            current_monitor_id: Some(1),
            display_buffer: Vec::new(),
            is_native_presented: false,
        }
    }

    /// Access surface configuration descriptor.
    pub fn surface_config(&self) -> &SurfaceConfig {
        &self.surface_config
    }

    /// Mutable access to frame pacer.
    pub fn frame_pacer_mut(&mut self) -> &mut FramePacer {
        &mut self.frame_pacer
    }

    /// Returns abstract surface handle.
    pub fn surface_handle(&self) -> SurfaceHandle {
        self.surface_handle
    }

    /// Consumes and clears redraw request flag.
    pub fn take_redraw_request(&mut self) -> bool {
        let requested = self.redraw_requested;
        self.redraw_requested = false;
        requested
    }
}

impl PlatformWindow for LivePlatformWindow {
    fn title(&self) -> &str {
        &self.config.title
    }

    fn set_title(&mut self, title: &str) {
        self.config.title = title.to_string();
    }

    fn size(&self) -> (u32, u32) {
        (self.config.width, self.config.height)
    }

    fn set_size(&mut self, width: u32, height: u32) {
        self.config.width = width;
        self.config.height = height;
        self.surface_config.width = width;
        self.surface_config.height = height;
        self.surface_handle.width = width;
        self.surface_handle.height = height;
        self.request_redraw();
        self.push_event(PlatformEvent::WindowResized { width, height });
    }

    fn scale_factor(&self) -> f32 {
        self.scale_factor
    }

    fn set_scale_factor(&mut self, scale_factor: f32) {
        self.scale_factor = scale_factor;
        self.surface_config.scale_factor = scale_factor;
        self.surface_handle.scale_factor_snapped = (scale_factor * 64.0) as u32;
        self.request_redraw();
        self.push_event(PlatformEvent::ScaleFactorChanged { scale_factor });
    }

    fn mode(&self) -> WindowMode {
        self.config.mode
    }

    fn set_mode(&mut self, mode: WindowMode) {
        self.config.mode = mode;
    }

    fn is_focused(&self) -> bool {
        self.is_focused
    }

    fn set_focused(&mut self, focused: bool) {
        self.is_focused = focused;
        self.push_event(PlatformEvent::WindowFocused(focused));
    }

    fn cursor(&self) -> CursorIcon {
        self.cursor
    }

    fn set_cursor(&mut self, cursor: CursorIcon) {
        self.cursor = cursor;
    }

    fn request_redraw(&mut self) {
        self.redraw_requested = true;
    }

    fn is_redraw_requested(&self) -> bool {
        self.redraw_requested
    }

    fn poll_events(&mut self) -> Vec<PlatformEvent> {
        self.event_queue.drain(..).collect()
    }

    fn push_event(&mut self, event: PlatformEvent) {
        self.event_queue.push_back(event);
    }

    fn current_monitor(&self) -> Option<MonitorInfo> {
        self.current_monitor_id
            .and_then(|id| self.monitors.iter().find(|m| m.id == id).cloned())
    }

    fn available_monitors(&self) -> Vec<MonitorInfo> {
        self.monitors.clone()
    }

    fn primary_monitor(&self) -> Option<MonitorInfo> {
        self.monitors.iter().find(|m| m.is_primary).cloned()
    }

    fn set_ime_cursor_location(&mut self, x: i32, y: i32, width: i32, height: i32) {
        #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
        crate::gtk_window::gtk_set_ime_cursor_location(x, y, width, height);
        #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
        let _ = (x, y, width, height);
    }

    fn set_ime_surrounding_text(&mut self, text: &str, cursor_index: i32) {
        #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
        crate::gtk_window::gtk_set_ime_surrounding_text(text, cursor_index);
        #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
        let _ = (text, cursor_index);
    }

    fn reset_ime(&mut self) {
        #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
        crate::gtk_window::gtk_reset_ime();
    }
}

impl NativeSurfaceWindow for LivePlatformWindow {
    fn present_surface(
        &mut self,
        pixels: &[u8],
        width: u32,
        height: u32,
    ) -> Result<(), crate::surface::PresentError> {
        self.redraw_requested = false;
        let expected_bytes = (width as usize) * (height as usize) * 4;
        if pixels.len() < expected_bytes {
            return Err(crate::surface::PresentError::BufferTooSmall);
        }
        self.display_buffer.resize(expected_bytes, 0);
        self.display_buffer
            .copy_from_slice(&pixels[..expected_bytes]);
        self.is_native_presented = true;
        Ok(())
    }
}

/// Control flow returned by event loop handlers.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum EventLoopControl {
    #[default]
    Poll,
    Wait,
    WaitUntil(Instant),
    Exit,
}

/// Abstract platform event loop trait.
pub trait EventLoop {
    fn run<F>(&mut self, handler: F)
    where
        F: FnMut(PlatformEvent, &mut EventLoopControl);
}

/// Headless mock event loop for simulated frame dispatching and testing.
#[derive(Debug, Default)]
pub struct MockEventLoop {
    pending_events: VecDeque<PlatformEvent>,
    control: EventLoopControl,
}

impl MockEventLoop {
    pub fn new() -> Self {
        Self {
            pending_events: VecDeque::new(),
            control: EventLoopControl::Poll,
        }
    }

    pub fn queue_event(&mut self, event: PlatformEvent) {
        self.pending_events.push_back(event);
    }

    pub fn step<F>(&mut self, mut handler: F) -> EventLoopControl
    where
        F: FnMut(PlatformEvent, &mut EventLoopControl),
    {
        while let Some(event) = self.pending_events.pop_front() {
            handler(event, &mut self.control);
            if self.control == EventLoopControl::Exit {
                return EventLoopControl::Exit;
            }
        }
        self.control
    }
}

impl EventLoop for MockEventLoop {
    fn run<F>(&mut self, mut handler: F)
    where
        F: FnMut(PlatformEvent, &mut EventLoopControl),
    {
        while self.control != EventLoopControl::Exit {
            if self.pending_events.is_empty() {
                break;
            }
            if self.step(&mut handler) == EventLoopControl::Exit {
                break;
            }
        }
    }
}