Skip to main content

cvkg_core/
window.rs

1//! Window management types and traits.
2//!
3//! Extracted from lib.rs (P1-13).
4
5use std::sync::Arc;
6
7/// Unique identifier for a window instance.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
9pub struct WindowId(pub u64);
10
11/// Specifies the layering behavior of the window relative to other windows.
12#[derive(
13    Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize, Default,
14)]
15pub enum WindowLevel {
16    /// Standard window.
17    #[default]
18    Normal,
19    /// Window stays above all standard windows.
20    AlwaysOnTop,
21    /// Menu or pop-up level window.
22    PopUpMenu,
23}
24
25/// Configuration settings for creating a new window.
26#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
27pub struct WindowConfig {
28    /// The window title bar text.
29    pub title: String,
30    /// Default width and height of the window.
31    pub size: (f32, f32),
32    /// Minimum allowed dimensions.
33    pub min_size: Option<(f32, f32)>,
34    /// Maximum allowed dimensions.
35    pub max_size: Option<(f32, f32)>,
36    /// Whether the window can be resized by the user.
37    pub resizable: bool,
38    /// Whether the window background is transparent.
39    pub transparent: bool,
40    /// Whether the window title bar and border decorations are drawn.
41    pub decorations: bool,
42    /// The window level layer.
43    pub level: WindowLevel,
44}
45
46impl Default for WindowConfig {
47    /// Create a standard default window configuration.
48    fn default() -> Self {
49        Self {
50            title: "CVKG Window".to_string(),
51            size: (800.0, 600.0),
52            min_size: None,
53            max_size: None,
54            resizable: true,
55            transparent: false,
56            decorations: true,
57            level: WindowLevel::Normal,
58        }
59    }
60}
61
62/// Abstract trait representing a platform-native window.
63/// Implementations delegate calls back to the platform renderers and events.
64pub trait Window: Send + Sync {
65    /// Request closing of the window.
66    fn close(&self);
67    /// Change the title bar text of the window.
68    fn set_title(&self, title: &str);
69    /// Update the window's physical dimensions.
70    fn set_size(&self, width: f32, height: f32);
71    /// Check if the window currently has keyboard focus.
72    fn is_key(&self) -> bool;
73    /// Check if this is the primary main application window.
74    fn is_main(&self) -> bool;
75    /// Check if the window is currently visible/mapped.
76    fn is_visible(&self) -> bool;
77    /// Hide or show the window.
78    fn set_visible(&self, visible: bool);
79    /// Bring the window to the front and focus it.
80    fn bring_to_front(&self);
81}
82
83/// A handle to a native window that can be used by application code.
84#[derive(Clone)]
85pub struct WindowHandle {
86    /// The unique identifier of this window.
87    pub id: WindowId,
88    /// Reference to the underlying platform window.
89    pub inner: Arc<dyn Window>,
90}
91
92impl std::fmt::Debug for WindowHandle {
93    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
94        f.debug_struct("WindowHandle")
95            .field("id", &self.id)
96            .finish()
97    }
98}
99
100impl WindowHandle {
101    /// Create a new WindowHandle.
102    pub fn new(id: WindowId, inner: Arc<dyn Window>) -> Self {
103        Self { id, inner }
104    }
105    /// Request the window to close.
106    pub fn close(self) {
107        self.inner.close();
108    }
109    /// Set the title text of the window.
110    pub fn set_title(&self, title: &str) {
111        self.inner.set_title(title);
112    }
113    /// Resize the window.
114    pub fn set_size(&self, width: f32, height: f32) {
115        self.inner.set_size(width, height);
116    }
117    /// Returns true if this window has key focus.
118    pub fn is_key(&self) -> bool {
119        self.inner.is_key()
120    }
121    /// Returns true if this is the main application window.
122    pub fn is_main(&self) -> bool {
123        self.inner.is_main()
124    }
125    /// Returns true if the window is visible.
126    pub fn is_visible(&self) -> bool {
127        self.inner.is_visible()
128    }
129    /// Set visibility of the window.
130    pub fn set_visible(&self, visible: bool) {
131        self.inner.set_visible(visible);
132    }
133    /// Bring this window to the foreground.
134    pub fn bring_to_front(&self) {
135        self.inner.bring_to_front();
136    }
137}
138
139/// Action to take when a window close request event is received.
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, serde::Serialize, serde::Deserialize)]
141pub enum WindowCloseAction {
142    /// Close the window immediately.
143    Allow,
144    /// Request confirmation from the user (e.g. show dialog).
145    Confirm,
146    /// Ignore the close request.
147    Deny,
148}