Skip to main content

miracle_plugin/
window.rs

1use super::application::*;
2use super::bindings;
3use super::container::*;
4use super::core::{self, *};
5use super::host::*;
6use super::workspace::*;
7use glam::Mat4;
8
9#[doc(hidden)]
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11#[repr(u32)]
12pub enum WindowAttrib {
13    Type = 0,
14    State = 1,
15    Focus = 3,
16    Dpi = 4,
17    Visibility = 5,
18    PreferredOrientation = 6,
19}
20
21impl From<WindowAttrib> for bindings::MirWindowAttrib {
22    fn from(value: WindowAttrib) -> Self {
23        value as bindings::MirWindowAttrib
24    }
25}
26
27impl TryFrom<bindings::MirWindowAttrib> for WindowAttrib {
28    type Error = ();
29
30    fn try_from(value: bindings::MirWindowAttrib) -> Result<Self, Self::Error> {
31        match value {
32            0 => Ok(Self::Type),
33            1 => Ok(Self::State),
34            3 => Ok(Self::Focus),
35            4 => Ok(Self::Dpi),
36            5 => Ok(Self::Visibility),
37            6 => Ok(Self::PreferredOrientation),
38            _ => Err(()),
39        }
40    }
41}
42
43/// Window type.
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
45#[repr(u32)]
46pub enum WindowType {
47    /// AKA "regular"
48    #[default]
49    Normal = 0,
50    /// AKA "floating"
51    Utility = 1,
52    /// A dialog box.
53    Dialog = 2,
54    /// A splash or branding surface.
55    Gloss = 3,
56    /// A window whose layout is entirely managed by a plugin.
57    Freestyle = 4,
58    /// A popup or context menu.
59    Menu = 5,
60    /// AKA "OSK" or handwriting etc.
61    InputMethod = 6,
62    /// AKA "toolbox"/"toolbar"
63    Satellite = 7,
64    /// AKA "tooltip"
65    Tip = 8,
66    /// A server-side window decoration surface.
67    Decoration = 9,
68}
69
70impl From<WindowType> for bindings::MirWindowType {
71    fn from(value: WindowType) -> Self {
72        value as bindings::MirWindowType
73    }
74}
75
76impl TryFrom<bindings::MirWindowType> for WindowType {
77    type Error = ();
78
79    fn try_from(value: bindings::MirWindowType) -> Result<Self, Self::Error> {
80        match value {
81            0 => Ok(Self::Normal),
82            1 => Ok(Self::Utility),
83            2 => Ok(Self::Dialog),
84            3 => Ok(Self::Gloss),
85            4 => Ok(Self::Freestyle),
86            5 => Ok(Self::Menu),
87            6 => Ok(Self::InputMethod),
88            7 => Ok(Self::Satellite),
89            8 => Ok(Self::Tip),
90            9 => Ok(Self::Decoration),
91            _ => Err(()),
92        }
93    }
94}
95
96/// Window state.
97#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
98#[repr(u32)]
99pub enum WindowState {
100    #[default]
101    Unknown = 0,
102    Restored = 1,
103    Minimized = 2,
104    Maximized = 3,
105    VertMaximized = 4,
106    Fullscreen = 5,
107    HorizMaximized = 6,
108    Hidden = 7,
109    /// Used for panels, notifications and other windows attached to output edges.
110    Attached = 8,
111}
112
113impl From<WindowState> for bindings::MirWindowState {
114    fn from(value: WindowState) -> Self {
115        value as bindings::MirWindowState
116    }
117}
118
119impl TryFrom<bindings::MirWindowState> for WindowState {
120    type Error = ();
121
122    fn try_from(value: bindings::MirWindowState) -> Result<Self, Self::Error> {
123        match value {
124            0 => Ok(Self::Unknown),
125            1 => Ok(Self::Restored),
126            2 => Ok(Self::Minimized),
127            3 => Ok(Self::Maximized),
128            4 => Ok(Self::VertMaximized),
129            5 => Ok(Self::Fullscreen),
130            6 => Ok(Self::HorizMaximized),
131            7 => Ok(Self::Hidden),
132            8 => Ok(Self::Attached),
133            _ => Err(()),
134        }
135    }
136}
137
138/// Window focus state.
139#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
140#[repr(u32)]
141pub enum WindowFocusState {
142    /// Inactive and does not have focus.
143    #[default]
144    Unfocused = 0,
145    /// Active and has keyboard focus.
146    Focused = 1,
147    /// Active but does not have keyboard focus.
148    Active = 2,
149}
150
151impl From<WindowFocusState> for bindings::MirWindowFocusState {
152    fn from(value: WindowFocusState) -> Self {
153        value as bindings::MirWindowFocusState
154    }
155}
156
157impl TryFrom<bindings::MirWindowFocusState> for WindowFocusState {
158    type Error = ();
159
160    fn try_from(value: bindings::MirWindowFocusState) -> Result<Self, Self::Error> {
161        match value {
162            0 => Ok(Self::Unfocused),
163            1 => Ok(Self::Focused),
164            2 => Ok(Self::Active),
165            _ => Err(()),
166        }
167    }
168}
169
170/// Window visibility state.
171#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
172#[repr(u32)]
173pub enum WindowVisibility {
174    /// The window is fully obscured by other surfaces.
175    #[default]
176    Occluded = 0,
177    /// The window is at least partially visible.
178    Exposed = 1,
179}
180
181impl From<WindowVisibility> for bindings::MirWindowVisibility {
182    fn from(value: WindowVisibility) -> Self {
183        value as bindings::MirWindowVisibility
184    }
185}
186
187impl TryFrom<bindings::MirWindowVisibility> for WindowVisibility {
188    type Error = ();
189
190    fn try_from(value: bindings::MirWindowVisibility) -> Result<Self, Self::Error> {
191        match value {
192            0 => Ok(Self::Occluded),
193            1 => Ok(Self::Exposed),
194            _ => Err(()),
195        }
196    }
197}
198
199/// Controls the z-ordering layer of a freestyle window.
200#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
201#[repr(u32)]
202pub enum DepthLayer {
203    /// For desktop backgrounds (lowest layer).
204    Background = 0,
205    /// For panels or other controls/decorations below normal windows.
206    Below = 1,
207    /// For normal application windows.
208    #[default]
209    Application = 2,
210    /// For always-on-top application windows.
211    AlwaysOnTop = 3,
212    /// For panels or notifications that want to be above normal windows.
213    Above = 4,
214    /// For overlays such as lock screens (highest layer).
215    Overlay = 5,
216}
217
218impl From<DepthLayer> for bindings::MirDepthLayer {
219    fn from(value: DepthLayer) -> Self {
220        value as bindings::MirDepthLayer
221    }
222}
223
224impl TryFrom<bindings::MirDepthLayer> for DepthLayer {
225    type Error = ();
226
227    fn try_from(value: bindings::MirDepthLayer) -> Result<Self, Self::Error> {
228        match value {
229            0 => Ok(Self::Background),
230            1 => Ok(Self::Below),
231            2 => Ok(Self::Application),
232            3 => Ok(Self::AlwaysOnTop),
233            4 => Ok(Self::Above),
234            5 => Ok(Self::Overlay),
235            _ => Err(()),
236        }
237    }
238}
239
240/// A window's state, exposed to plugin callbacks and to [`crate::plugin::managed_windows`].
241///
242/// Read-only fields describe the window at the time of the callback. Setter
243/// methods (`set_state`, `set_rectangle`, etc.) are available for windows
244/// returned by [`crate::plugin::managed_windows`].
245#[derive(Debug)]
246pub struct Window {
247    /// The type of this window.
248    pub window_type: WindowType,
249    /// The state of the window.
250    pub state: WindowState,
251    /// The position of the window.
252    pub top_left: Point,
253    /// The size of the window.
254    pub size: Size,
255    /// The depth layer of the window.
256    pub depth_layer: DepthLayer,
257    /// The name of the window.
258    pub name: String,
259    /// The 4x4 transform matrix of the window (column-major).
260    pub transform: Mat4,
261    /// The alpha (opacity) of the window.
262    pub alpha: f32,
263    /// Internal pointer for C interop.
264    internal: u64,
265}
266
267impl Window {
268    #[doc(hidden)]
269    pub unsafe fn from_c_with_name(value: &bindings::miracle_window_info_t, name: String) -> Self {
270        Self {
271            window_type: WindowType::try_from(value.window_type).unwrap_or_default(),
272            state: WindowState::try_from(value.state).unwrap_or_default(),
273            top_left: value.top_left.into(),
274            size: value.size.into(),
275            depth_layer: DepthLayer::try_from(value.depth_layer).unwrap_or_default(),
276            name,
277            transform: core::mat4_from_f32_array(value.transform),
278            alpha: value.alpha,
279            internal: value.internal,
280        }
281    }
282
283    /// Retrieve the ID of this window.
284    ///
285    /// Plugins may elect to keep a reference to this ID so that they can
286    /// match it with [`Window`] later.
287    pub fn id(&self) -> u64 {
288        self.internal
289    }
290
291    /// Get the application that owns this window.
292    pub fn application(&self) -> Option<ApplicationInfo> {
293        const NAME_BUF_LEN: usize = 256;
294        let mut name_buf: [u8; NAME_BUF_LEN] = [0; NAME_BUF_LEN];
295
296        unsafe {
297            let internal = miracle_window_info_get_application(
298                self.internal as i64,
299                name_buf.as_mut_ptr() as i32,
300                NAME_BUF_LEN as i32,
301            );
302
303            if internal == -1 {
304                return None;
305            }
306
307            // Find the null terminator to get the actual string length
308            let name_len = name_buf
309                .iter()
310                .position(|&c| c == 0)
311                .unwrap_or(NAME_BUF_LEN);
312            let name = String::from_utf8_lossy(&name_buf[..name_len]).into_owned();
313
314            Some(ApplicationInfo {
315                name,
316                internal: internal as u64,
317            })
318        }
319    }
320
321    /// Get the container that holds this window.
322    pub fn container(&self) -> Option<Container> {
323        let mut container = std::mem::MaybeUninit::<crate::bindings::miracle_container_t>::uninit();
324
325        unsafe {
326            let result = miracle_window_info_get_container(
327                self.internal as i64,
328                container.as_mut_ptr() as i32,
329            );
330
331            if result != 0 {
332                return None;
333            }
334
335            Some(Container::from(container.assume_init()))
336        }
337    }
338
339    /// Get the workspace that this window is on.
340    pub fn workspace(&self) -> Option<Workspace> {
341        const NAME_BUF_LEN: usize = 256;
342        let mut workspace = std::mem::MaybeUninit::<crate::bindings::miracle_workspace_t>::uninit();
343        let mut name_buf: [u8; NAME_BUF_LEN] = [0; NAME_BUF_LEN];
344
345        unsafe {
346            let result = miracle_window_info_get_workspace(
347                self.internal as i64,
348                workspace.as_mut_ptr() as i32,
349                name_buf.as_mut_ptr() as i32,
350                NAME_BUF_LEN as i32,
351            );
352
353            if result != 0 {
354                return None;
355            }
356
357            let workspace = workspace.assume_init();
358            if workspace.is_set == 0 {
359                return None;
360            }
361
362            // Find the null terminator to get the actual string length
363            let name_len = name_buf
364                .iter()
365                .position(|&c| c == 0)
366                .unwrap_or(NAME_BUF_LEN);
367            let name = String::from_utf8_lossy(&name_buf[..name_len]).into_owned();
368
369            Some(Workspace::from_c_with_name(&workspace, name))
370        }
371    }
372
373    /// Set the state of this window.
374    pub fn set_state(&self, state: WindowState) -> Result<(), ()> {
375        let r = unsafe { miracle_window_set_state(self.internal as i64, state as i32) };
376        if r == 0 { Ok(()) } else { Err(()) }
377    }
378
379    /// Move this window to a different workspace.
380    pub fn set_workspace(&self, workspace: &Workspace) -> Result<(), ()> {
381        let r =
382            unsafe { miracle_window_set_workspace(self.internal as i64, workspace.id() as i64) };
383        if r == 0 { Ok(()) } else { Err(()) }
384    }
385
386    /// Set the position and size of this window.
387    pub fn set_rectangle(&self, rect: Rectangle, animate: bool) -> Result<(), ()> {
388        let r = unsafe {
389            miracle_window_set_rectangle(
390                self.internal as i64,
391                rect.x,
392                rect.y,
393                rect.width,
394                rect.height,
395                if animate { 1 } else { 0 },
396            )
397        };
398        if r == 0 { Ok(()) } else { Err(()) }
399    }
400
401    /// Set the 4x4 column-major transform matrix of this window.
402    pub fn set_transform(&self, transform: Mat4) -> Result<(), ()> {
403        let arr = transform.to_cols_array();
404        let r = unsafe { miracle_window_set_transform(self.internal as i64, arr.as_ptr() as i32) };
405        if r == 0 { Ok(()) } else { Err(()) }
406    }
407
408    /// Set the alpha (opacity) of this window.
409    pub fn set_alpha(&self, alpha: f32) -> Result<(), ()> {
410        let r = unsafe {
411            miracle_window_set_alpha(self.internal as i64, (&alpha as *const f32) as i32)
412        };
413        if r == 0 { Ok(()) } else { Err(()) }
414    }
415
416    /// Request keyboard focus on this window.
417    pub fn request_focus(&self) -> Result<(), ()> {
418        let r = unsafe { miracle_window_request_focus(self.internal as i64) };
419        if r == 0 { Ok(()) } else { Err(()) }
420    }
421
422    /// Set the custom shader applied to this window.
423    ///
424    /// Pass `Some(id)` with the ID returned by `register_window_sample_to_rgba` to activate
425    /// the shader, or `None` to clear any custom shader and revert to default rendering.
426    pub fn set_shader(&self, shader_id: Option<u8>) -> Result<(), ()> {
427        let id = shader_id.map(|id| id as i32).unwrap_or(-1);
428        let r = unsafe { miracle_window_set_shader_id(self.internal as i64, id) };
429        if r == 0 { Ok(()) } else { Err(()) }
430    }
431}
432
433impl PartialEq for Window {
434    fn eq(&self, other: &Self) -> bool {
435        self.internal == other.internal
436    }
437}