1use crate::events::PlatformEvent;
4use crate::surface::{FramePacer, RefreshRate, SurfaceConfig, SurfaceHandle};
5use std::collections::VecDeque;
6use std::time::Instant;
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum WindowMode {
11 #[default]
12 Windowed,
13 BorderlessFullscreen,
14 ExclusiveFullscreen,
15 Minimized,
16 Maximized,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
21pub enum WindowDecoration {
22 #[default]
23 NativeOs,
24 ClientSideDecorations,
25 FramelessTransparent,
26}
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
30pub enum ThemeMode {
31 #[default]
32 System,
33 Light,
34 Dark,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
39pub enum CursorIcon {
40 #[default]
41 Default,
42 Pointer,
43 Text,
44 Crosshair,
45 Move,
46 NotAllowed,
47 Wait,
48 Grab,
49 Grabbing,
50 ResizeN,
51 ResizeS,
52 ResizeE,
53 ResizeW,
54 ResizeNe,
55 ResizeNw,
56 ResizeSe,
57 ResizeSw,
58 ColResize,
59 RowResize,
60 Help,
61 None,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
66pub enum WindowAnchor {
67 TopLeft,
68 TopRight,
69 BottomLeft,
70 BottomRight,
71 Center,
72}
73
74impl WindowAnchor {
75 pub const fn is_sticky_dock(self) -> bool {
77 matches!(
78 self,
79 Self::TopLeft | Self::TopRight | Self::BottomLeft | Self::BottomRight
80 )
81 }
82
83 pub const fn compute_position(
85 self,
86 workarea_x: i32,
87 workarea_y: i32,
88 workarea_w: i32,
89 workarea_h: i32,
90 win_w: i32,
91 win_h: i32,
92 ) -> (i32, i32) {
93 match self {
94 Self::TopLeft => (workarea_x, workarea_y),
95 Self::TopRight => (workarea_x + workarea_w - win_w, workarea_y),
96 Self::BottomLeft => (workarea_x, workarea_y + workarea_h - win_h),
97 Self::BottomRight => (
98 workarea_x + workarea_w - win_w,
99 workarea_y + workarea_h - win_h,
100 ),
101 Self::Center => (
102 workarea_x + (workarea_w - win_w) / 2,
103 workarea_y + (workarea_h - win_h) / 2,
104 ),
105 }
106 }
107}
108
109#[derive(Debug, Clone, Copy, PartialEq)]
112pub struct HoverTooltipStyle {
113 pub background_color: strata_style::types::Color,
115 pub border_color: strata_style::types::Color,
117 pub text_color: strata_style::types::Color,
119 pub shadow_color: strata_style::types::Color,
121 pub font_size: f32,
123 pub line_height: f32,
125 pub padding_x: f32,
127 pub padding_y: f32,
129 pub corner_radius: f32,
131 pub border_width: f32,
133 pub max_width: f32,
135 pub cursor_offset_x: f32,
137 pub cursor_offset_y: f32,
139}
140
141impl Default for HoverTooltipStyle {
142 fn default() -> Self {
143 Self {
144 background_color: strata_style::types::Color::new(18, 22, 29, 242),
145 border_color: strata_style::types::Color::new(255, 255, 255, 34),
146 text_color: strata_style::types::Color::new(241, 245, 249, 255),
147 shadow_color: strata_style::types::Color::new(0, 0, 0, 110),
148 font_size: 12.0,
149 line_height: 1.35,
150 padding_x: 9.0,
151 padding_y: 5.0,
152 corner_radius: 6.0,
153 border_width: 1.0,
154 max_width: 340.0,
155 cursor_offset_x: 12.0,
156 cursor_offset_y: 18.0,
157 }
158 }
159}
160
161impl HoverTooltipStyle {
162 pub const fn with_background_color(mut self, color: strata_style::types::Color) -> Self {
164 self.background_color = color;
165 self
166 }
167
168 pub const fn with_border_color(mut self, color: strata_style::types::Color) -> Self {
170 self.border_color = color;
171 self
172 }
173
174 pub const fn with_text_color(mut self, color: strata_style::types::Color) -> Self {
176 self.text_color = color;
177 self
178 }
179
180 pub const fn with_shadow_color(mut self, color: strata_style::types::Color) -> Self {
182 self.shadow_color = color;
183 self
184 }
185
186 pub const fn with_font_size(mut self, font_size: f32) -> Self {
188 self.font_size = font_size;
189 self
190 }
191
192 pub const fn with_padding(mut self, padding_x: f32, padding_y: f32) -> Self {
194 self.padding_x = padding_x;
195 self.padding_y = padding_y;
196 self
197 }
198
199 pub const fn with_corner_radius(mut self, corner_radius: f32) -> Self {
201 self.corner_radius = corner_radius;
202 self
203 }
204
205 pub const fn with_border_width(mut self, border_width: f32) -> Self {
207 self.border_width = border_width;
208 self
209 }
210
211 pub const fn with_max_width(mut self, max_width: f32) -> Self {
213 self.max_width = max_width;
214 self
215 }
216
217 pub const fn with_cursor_offset(mut self, x: f32, y: f32) -> Self {
219 self.cursor_offset_x = x;
220 self.cursor_offset_y = y;
221 self
222 }
223}
224
225#[derive(Debug, Clone)]
227pub struct WindowConfig {
228 pub title: String,
229 pub width: u32,
230 pub height: u32,
231 pub min_width: Option<u32>,
232 pub min_height: Option<u32>,
233 pub max_width: Option<u32>,
234 pub max_height: Option<u32>,
235 pub resizable: bool,
236 pub mode: WindowMode,
237 pub decoration: WindowDecoration,
238 pub theme: ThemeMode,
239 pub transparent: bool,
240 pub always_on_top: bool,
241 pub anchor: Option<WindowAnchor>,
242 pub dock_bottom_right: bool,
243 pub single_instance_id: Option<String>,
244 pub vibrancy: crate::vibrancy::VibrancyEffect,
245 pub present_mode: crate::surface::PresentMode,
247 pub tooltip_delay: std::time::Duration,
249 pub tooltip_style: HoverTooltipStyle,
251}
252
253impl WindowConfig {
254 pub fn with_present_mode(mut self, present_mode: crate::surface::PresentMode) -> Self {
256 self.present_mode = present_mode;
257 self
258 }
259
260 pub fn with_tooltip_delay(mut self, delay: std::time::Duration) -> Self {
262 self.tooltip_delay = delay;
263 self
264 }
265
266 pub fn with_tooltip_delay_ms(mut self, delay_ms: u64) -> Self {
268 self.tooltip_delay = std::time::Duration::from_millis(delay_ms);
269 self
270 }
271
272 pub fn with_tooltip_style(mut self, style: HoverTooltipStyle) -> Self {
274 self.tooltip_style = style;
275 self
276 }
277
278 pub fn with_single_instance(mut self, app_id: impl Into<String>) -> Self {
280 self.single_instance_id = Some(app_id.into());
281 self
282 }
283
284 pub fn with_vibrancy(mut self, vibrancy: crate::vibrancy::VibrancyEffect) -> Self {
286 self.vibrancy = vibrancy;
287 self
288 }
289
290 pub fn with_anchor(mut self, anchor: WindowAnchor) -> Self {
292 self.anchor = Some(anchor);
293 if anchor == WindowAnchor::BottomRight {
294 self.dock_bottom_right = true;
295 }
296 self
297 }
298
299 pub fn with_dock_bottom_right(mut self, dock_bottom_right: bool) -> Self {
301 self.dock_bottom_right = dock_bottom_right;
302 if dock_bottom_right && self.anchor.is_none() {
303 self.anchor = Some(WindowAnchor::BottomRight);
304 } else if !dock_bottom_right && self.anchor == Some(WindowAnchor::BottomRight) {
305 self.anchor = None;
306 }
307 self
308 }
309
310 pub const fn effective_anchor(&self) -> Option<WindowAnchor> {
312 if let Some(anchor) = self.anchor {
313 Some(anchor)
314 } else if self.dock_bottom_right {
315 Some(WindowAnchor::BottomRight)
316 } else {
317 None
318 }
319 }
320
321 pub const fn is_docked(&self) -> bool {
323 match self.effective_anchor() {
324 Some(anchor) => anchor.is_sticky_dock(),
325 None => false,
326 }
327 }
328
329 pub const fn compute_anchored_position(
331 &self,
332 workarea_x: i32,
333 workarea_y: i32,
334 workarea_w: i32,
335 workarea_h: i32,
336 ) -> Option<(i32, i32)> {
337 match self.effective_anchor() {
338 Some(anchor) => Some(anchor.compute_position(
339 workarea_x,
340 workarea_y,
341 workarea_w,
342 workarea_h,
343 self.width as i32,
344 self.height as i32,
345 )),
346 None => None,
347 }
348 }
349}
350
351impl Default for WindowConfig {
352 fn default() -> Self {
353 Self {
354 title: "Strata Application".to_string(),
355 width: 1280,
356 height: 800,
357 min_width: Some(320),
358 min_height: Some(240),
359 max_width: None,
360 max_height: None,
361 resizable: true,
362 mode: WindowMode::Windowed,
363 decoration: WindowDecoration::NativeOs,
364 theme: ThemeMode::System,
365 transparent: false,
366 always_on_top: false,
367 anchor: None,
368 dock_bottom_right: false,
369 single_instance_id: None,
370 vibrancy: crate::vibrancy::VibrancyEffect::None,
371 present_mode: crate::surface::PresentMode::Fifo,
372 tooltip_delay: std::time::Duration::from_millis(450),
373 tooltip_style: HoverTooltipStyle::default(),
374 }
375 }
376}
377
378#[derive(Debug, Clone, PartialEq)]
380pub struct MonitorInfo {
381 pub id: u64,
382 pub name: String,
383 pub x: i32,
384 pub y: i32,
385 pub width: u32,
386 pub height: u32,
387 pub scale_factor: f32,
388 pub refresh_rate: u32,
389 pub is_primary: bool,
390}
391
392impl MonitorInfo {
393 pub fn new(
395 id: u64,
396 name: impl Into<String>,
397 x: i32,
398 y: i32,
399 width: u32,
400 height: u32,
401 scale_factor: f32,
402 refresh_rate: u32,
403 is_primary: bool,
404 ) -> Self {
405 Self {
406 id,
407 name: name.into(),
408 x,
409 y,
410 width,
411 height,
412 scale_factor,
413 refresh_rate,
414 is_primary,
415 }
416 }
417
418 pub fn standard_1080p() -> Self {
420 Self::new(1, "Primary Display", 0, 0, 1920, 1080, 1.0, 60, true)
421 }
422
423 pub fn hidpi_4k() -> Self {
425 Self::new(2, "4K UHD Display", 1920, 0, 3840, 2160, 2.0, 120, false)
426 }
427}
428
429pub trait PlatformWindow {
431 fn title(&self) -> &str;
432 fn set_title(&mut self, title: &str);
433 fn size(&self) -> (u32, u32);
434 fn set_size(&mut self, width: u32, height: u32);
435 fn scale_factor(&self) -> f32;
436 fn set_scale_factor(&mut self, scale_factor: f32);
437 fn mode(&self) -> WindowMode;
438 fn set_mode(&mut self, mode: WindowMode);
439 fn is_focused(&self) -> bool;
440 fn set_focused(&mut self, focused: bool);
441 fn cursor(&self) -> CursorIcon;
442 fn set_cursor(&mut self, cursor: CursorIcon);
443 fn request_redraw(&mut self);
444 fn is_redraw_requested(&self) -> bool;
445 fn poll_events(&mut self) -> Vec<PlatformEvent>;
446 fn push_event(&mut self, event: PlatformEvent);
447 fn current_monitor(&self) -> Option<MonitorInfo> {
449 None
450 }
451 fn available_monitors(&self) -> Vec<MonitorInfo> {
453 Vec::new()
454 }
455 fn primary_monitor(&self) -> Option<MonitorInfo> {
457 None
458 }
459 fn on_accessibility_update(&mut self, _json_payload: &str) {}
461 fn set_ime_cursor_location(&mut self, _x: i32, _y: i32, _width: i32, _height: i32) {}
463 fn set_ime_surrounding_text(&mut self, _text: &str, _cursor_index: i32) {}
465 fn reset_ime(&mut self) {}
467 fn begin_window_drag(&mut self, _x: f32, _y: f32) {}
469 fn begin_window_resize(&mut self, _edge: crate::csd::CsdHitZone, _x: f32, _y: f32) {}
471 fn toggle_maximize(&mut self) -> bool {
473 let current = self.mode();
474 let new_mode = if current == WindowMode::Maximized {
475 WindowMode::Windowed
476 } else {
477 WindowMode::Maximized
478 };
479 self.set_mode(new_mode);
480 new_mode == WindowMode::Maximized
481 }
482}
483
484pub trait NativeSurfaceWindow: PlatformWindow {
486 fn present_surface(
488 &mut self,
489 pixels: &[u8],
490 width: u32,
491 height: u32,
492 ) -> Result<(), crate::surface::PresentError>;
493}
494
495#[derive(Debug, Clone)]
497pub struct MockPlatformWindow {
498 pub config: WindowConfig,
499 pub scale_factor: f32,
500 pub is_focused: bool,
501 pub cursor: CursorIcon,
502 pub redraw_requested: bool,
503 pub event_queue: VecDeque<PlatformEvent>,
504 pub last_accessibility_update: Option<String>,
505 pub ime_cursor_location: Option<(i32, i32, i32, i32)>,
506 pub ime_surrounding: Option<(String, i32)>,
507 pub monitors: Vec<MonitorInfo>,
508 pub current_monitor_id: Option<u64>,
509}
510
511impl MockPlatformWindow {
512 pub fn new(config: WindowConfig) -> Self {
514 Self {
515 config,
516 scale_factor: 1.0,
517 is_focused: true,
518 cursor: CursorIcon::Default,
519 redraw_requested: false,
520 event_queue: VecDeque::new(),
521 last_accessibility_update: None,
522 ime_cursor_location: None,
523 ime_surrounding: None,
524 monitors: vec![MonitorInfo::standard_1080p()],
525 current_monitor_id: Some(1),
526 }
527 }
528
529 pub fn new_mock() -> Self {
531 Self::new(WindowConfig::default())
532 }
533
534 pub fn inject_event(&mut self, event: PlatformEvent) {
536 self.push_event(event);
537 }
538
539 pub fn take_redraw_request(&mut self) -> bool {
541 let requested = self.redraw_requested;
542 self.redraw_requested = false;
543 requested
544 }
545
546 pub fn add_monitor(&mut self, monitor: MonitorInfo) {
548 self.monitors.push(monitor);
549 }
550
551 pub fn move_to_monitor(&mut self, monitor_id: u64) {
553 if let Some(monitor) = self.monitors.iter().find(|m| m.id == monitor_id).cloned() {
554 self.current_monitor_id = Some(monitor_id);
555 if (self.scale_factor - monitor.scale_factor).abs() > 0.001 {
556 self.set_scale_factor(monitor.scale_factor);
557 }
558 }
559 }
560}
561
562impl PlatformWindow for MockPlatformWindow {
563 fn title(&self) -> &str {
564 &self.config.title
565 }
566
567 fn set_title(&mut self, title: &str) {
568 self.config.title = title.to_string();
569 }
570
571 fn size(&self) -> (u32, u32) {
572 (self.config.width, self.config.height)
573 }
574
575 fn set_size(&mut self, width: u32, height: u32) {
576 self.config.width = width;
577 self.config.height = height;
578 self.push_event(PlatformEvent::WindowResized { width, height });
579 }
580
581 fn scale_factor(&self) -> f32 {
582 self.scale_factor
583 }
584
585 fn set_scale_factor(&mut self, scale_factor: f32) {
586 self.scale_factor = scale_factor;
587 self.push_event(PlatformEvent::ScaleFactorChanged { scale_factor });
588 }
589
590 fn mode(&self) -> WindowMode {
591 self.config.mode
592 }
593
594 fn set_mode(&mut self, mode: WindowMode) {
595 let old = self.config.mode;
596 self.config.mode = mode;
597 if old != mode {
598 self.push_event(PlatformEvent::WindowMaximized(
599 mode == WindowMode::Maximized,
600 ));
601 }
602 }
603
604 fn is_focused(&self) -> bool {
605 self.is_focused
606 }
607
608 fn set_focused(&mut self, focused: bool) {
609 self.is_focused = focused;
610 self.push_event(PlatformEvent::WindowFocused(focused));
611 }
612
613 fn cursor(&self) -> CursorIcon {
614 self.cursor
615 }
616
617 fn set_cursor(&mut self, cursor: CursorIcon) {
618 self.cursor = cursor;
619 }
620
621 fn request_redraw(&mut self) {
622 self.redraw_requested = true;
623 }
624
625 fn is_redraw_requested(&self) -> bool {
626 self.redraw_requested
627 }
628
629 fn poll_events(&mut self) -> Vec<PlatformEvent> {
630 self.event_queue.drain(..).collect()
631 }
632
633 fn push_event(&mut self, event: PlatformEvent) {
634 self.event_queue.push_back(event);
635 }
636
637 fn current_monitor(&self) -> Option<MonitorInfo> {
638 self.current_monitor_id
639 .and_then(|id| self.monitors.iter().find(|m| m.id == id).cloned())
640 }
641
642 fn available_monitors(&self) -> Vec<MonitorInfo> {
643 self.monitors.clone()
644 }
645
646 fn primary_monitor(&self) -> Option<MonitorInfo> {
647 self.monitors.iter().find(|m| m.is_primary).cloned()
648 }
649
650 fn on_accessibility_update(&mut self, json_payload: &str) {
651 self.last_accessibility_update = Some(json_payload.to_string());
652 }
653
654 fn set_ime_cursor_location(&mut self, x: i32, y: i32, width: i32, height: i32) {
655 self.ime_cursor_location = Some((x, y, width, height));
656 }
657
658 fn set_ime_surrounding_text(&mut self, text: &str, cursor_index: i32) {
659 self.ime_surrounding = Some((text.to_string(), cursor_index));
660 }
661
662 fn reset_ime(&mut self) {
663 self.ime_cursor_location = None;
664 self.ime_surrounding = None;
665 }
666}
667
668impl NativeSurfaceWindow for MockPlatformWindow {
669 fn present_surface(
670 &mut self,
671 _pixels: &[u8],
672 _width: u32,
673 _height: u32,
674 ) -> Result<(), crate::surface::PresentError> {
675 self.redraw_requested = false;
676 Ok(())
677 }
678}
679
680#[derive(Debug, Clone)]
682pub struct LivePlatformWindow {
683 pub config: WindowConfig,
684 pub surface_config: SurfaceConfig,
685 pub surface_handle: SurfaceHandle,
686 pub frame_pacer: FramePacer,
687 pub scale_factor: f32,
688 pub is_focused: bool,
689 pub cursor: CursorIcon,
690 pub redraw_requested: bool,
691 pub event_queue: VecDeque<PlatformEvent>,
692 pub monitors: Vec<MonitorInfo>,
693 pub current_monitor_id: Option<u64>,
694 pub display_buffer: Vec<u8>,
695 pub is_native_presented: bool,
696}
697
698impl LivePlatformWindow {
699 pub fn new(config: WindowConfig) -> Self {
701 let scale_factor = 1.0;
702 let surface_config = SurfaceConfig {
703 width: config.width,
704 height: config.height,
705 scale_factor,
706 ..Default::default()
707 };
708 let surface_handle = {
709 #[cfg(target_os = "linux")]
710 {
711 if std::env::var_os("WAYLAND_DISPLAY").is_some() {
712 SurfaceHandle {
713 window_id: 1001,
714 raw_window_ptr: 0x574C4E44, raw_display_ptr: 0x574C4453, width: config.width,
717 height: config.height,
718 scale_factor_snapped: (scale_factor * 64.0) as u32,
719 }
720 } else if std::env::var_os("DISPLAY").is_some() {
721 SurfaceHandle {
722 window_id: 1002,
723 raw_window_ptr: 0x58313157, raw_display_ptr: 0x58313144, width: config.width,
726 height: config.height,
727 scale_factor_snapped: (scale_factor * 64.0) as u32,
728 }
729 } else {
730 SurfaceHandle::new_headless(config.width, config.height, scale_factor)
731 }
732 }
733 #[cfg(target_os = "windows")]
734 {
735 SurfaceHandle {
736 window_id: 2001,
737 raw_window_ptr: 0x57494E32, raw_display_ptr: 0x47444931, width: config.width,
740 height: config.height,
741 scale_factor_snapped: (scale_factor * 64.0) as u32,
742 }
743 }
744 #[cfg(target_os = "macos")]
745 {
746 SurfaceHandle {
747 window_id: 3001,
748 raw_window_ptr: 0x434F4341, raw_display_ptr: 0x4150504C, width: config.width,
751 height: config.height,
752 scale_factor_snapped: (scale_factor * 64.0) as u32,
753 }
754 }
755 #[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
756 {
757 SurfaceHandle::new_headless(config.width, config.height, scale_factor)
758 }
759 };
760 let frame_pacer = FramePacer::new(RefreshRate::Hz60);
761 Self {
762 config,
763 surface_config,
764 surface_handle,
765 frame_pacer,
766 scale_factor,
767 is_focused: true,
768 cursor: CursorIcon::Default,
769 redraw_requested: true,
770 event_queue: VecDeque::new(),
771 monitors: vec![MonitorInfo::standard_1080p()],
772 current_monitor_id: Some(1),
773 display_buffer: Vec::new(),
774 is_native_presented: false,
775 }
776 }
777
778 pub fn surface_config(&self) -> &SurfaceConfig {
780 &self.surface_config
781 }
782
783 pub fn frame_pacer_mut(&mut self) -> &mut FramePacer {
785 &mut self.frame_pacer
786 }
787
788 pub fn surface_handle(&self) -> SurfaceHandle {
790 self.surface_handle
791 }
792
793 pub fn take_redraw_request(&mut self) -> bool {
795 let requested = self.redraw_requested;
796 self.redraw_requested = false;
797 requested
798 }
799}
800
801impl PlatformWindow for LivePlatformWindow {
802 fn title(&self) -> &str {
803 &self.config.title
804 }
805
806 fn set_title(&mut self, title: &str) {
807 self.config.title = title.to_string();
808 }
809
810 fn size(&self) -> (u32, u32) {
811 (self.config.width, self.config.height)
812 }
813
814 fn set_size(&mut self, width: u32, height: u32) {
815 self.config.width = width;
816 self.config.height = height;
817 self.surface_config.width = width;
818 self.surface_config.height = height;
819 self.surface_handle.width = width;
820 self.surface_handle.height = height;
821 self.request_redraw();
822 self.push_event(PlatformEvent::WindowResized { width, height });
823 }
824
825 fn scale_factor(&self) -> f32 {
826 self.scale_factor
827 }
828
829 fn set_scale_factor(&mut self, scale_factor: f32) {
830 self.scale_factor = scale_factor;
831 self.surface_config.scale_factor = scale_factor;
832 self.surface_handle.scale_factor_snapped = (scale_factor * 64.0) as u32;
833 self.request_redraw();
834 self.push_event(PlatformEvent::ScaleFactorChanged { scale_factor });
835 }
836
837 fn mode(&self) -> WindowMode {
838 self.config.mode
839 }
840
841 fn set_mode(&mut self, mode: WindowMode) {
842 self.config.mode = mode;
843 }
844
845 fn is_focused(&self) -> bool {
846 self.is_focused
847 }
848
849 fn set_focused(&mut self, focused: bool) {
850 self.is_focused = focused;
851 self.push_event(PlatformEvent::WindowFocused(focused));
852 }
853
854 fn cursor(&self) -> CursorIcon {
855 self.cursor
856 }
857
858 fn set_cursor(&mut self, cursor: CursorIcon) {
859 self.cursor = cursor;
860 }
861
862 fn request_redraw(&mut self) {
863 self.redraw_requested = true;
864 }
865
866 fn is_redraw_requested(&self) -> bool {
867 self.redraw_requested
868 }
869
870 fn poll_events(&mut self) -> Vec<PlatformEvent> {
871 self.event_queue.drain(..).collect()
872 }
873
874 fn push_event(&mut self, event: PlatformEvent) {
875 self.event_queue.push_back(event);
876 }
877
878 fn current_monitor(&self) -> Option<MonitorInfo> {
879 self.current_monitor_id
880 .and_then(|id| self.monitors.iter().find(|m| m.id == id).cloned())
881 }
882
883 fn available_monitors(&self) -> Vec<MonitorInfo> {
884 self.monitors.clone()
885 }
886
887 fn primary_monitor(&self) -> Option<MonitorInfo> {
888 self.monitors.iter().find(|m| m.is_primary).cloned()
889 }
890
891 fn set_ime_cursor_location(&mut self, x: i32, y: i32, width: i32, height: i32) {
892 #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
893 crate::gtk_window::gtk_set_ime_cursor_location(x, y, width, height);
894 #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
895 let _ = (x, y, width, height);
896 }
897
898 fn set_ime_surrounding_text(&mut self, text: &str, cursor_index: i32) {
899 #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
900 crate::gtk_window::gtk_set_ime_surrounding_text(text, cursor_index);
901 #[cfg(not(all(target_os = "linux", feature = "gtk-compat")))]
902 let _ = (text, cursor_index);
903 }
904
905 fn reset_ime(&mut self) {
906 #[cfg(all(target_os = "linux", feature = "gtk-compat"))]
907 crate::gtk_window::gtk_reset_ime();
908 }
909}
910
911impl NativeSurfaceWindow for LivePlatformWindow {
912 fn present_surface(
913 &mut self,
914 pixels: &[u8],
915 width: u32,
916 height: u32,
917 ) -> Result<(), crate::surface::PresentError> {
918 self.redraw_requested = false;
919 let expected_bytes = (width as usize) * (height as usize) * 4;
920 if pixels.len() < expected_bytes {
921 return Err(crate::surface::PresentError::BufferTooSmall);
922 }
923 self.display_buffer.resize(expected_bytes, 0);
924 self.display_buffer
925 .copy_from_slice(&pixels[..expected_bytes]);
926 self.is_native_presented = true;
927 Ok(())
928 }
929}
930
931#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
933pub enum EventLoopControl {
934 #[default]
935 Poll,
936 Wait,
937 WaitUntil(Instant),
938 Exit,
939}
940
941pub trait EventLoop {
943 fn run<F>(&mut self, handler: F)
944 where
945 F: FnMut(PlatformEvent, &mut EventLoopControl);
946}
947
948#[derive(Debug, Default)]
950pub struct MockEventLoop {
951 pending_events: VecDeque<PlatformEvent>,
952 control: EventLoopControl,
953}
954
955impl MockEventLoop {
956 pub fn new() -> Self {
957 Self {
958 pending_events: VecDeque::new(),
959 control: EventLoopControl::Poll,
960 }
961 }
962
963 pub fn queue_event(&mut self, event: PlatformEvent) {
964 self.pending_events.push_back(event);
965 }
966
967 pub fn step<F>(&mut self, mut handler: F) -> EventLoopControl
968 where
969 F: FnMut(PlatformEvent, &mut EventLoopControl),
970 {
971 while let Some(event) = self.pending_events.pop_front() {
972 handler(event, &mut self.control);
973 if self.control == EventLoopControl::Exit {
974 return EventLoopControl::Exit;
975 }
976 }
977 self.control
978 }
979}
980
981impl EventLoop for MockEventLoop {
982 fn run<F>(&mut self, mut handler: F)
983 where
984 F: FnMut(PlatformEvent, &mut EventLoopControl),
985 {
986 while self.control != EventLoopControl::Exit {
987 if self.pending_events.is_empty() {
988 break;
989 }
990 if self.step(&mut handler) == EventLoopControl::Exit {
991 break;
992 }
993 }
994 }
995}