Skip to main content

hypersig_desktop/
arrange.rs

1//! Where a window sits on the screen and how large it is, which
2//! [`Window`](crate::Window)'s methods change.
3
4use tao::dpi::{LogicalPosition, LogicalSize};
5use tao::window::Fullscreen;
6
7/// How a window fills the screen, for [`Window::place`](crate::Window::place).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
9#[non_exhaustive]
10pub enum Placement {
11    /// Neither maximized, minimized, nor fullscreen, as a window opens.
12    #[default]
13    Normal,
14    /// As large as the screen allows, beside the system's bars.
15    Maximized,
16    /// Hidden in the Dock, the taskbar, or the system's list of windows.
17    Minimized,
18    /// Over the whole screen, without the window's frame.
19    Fullscreen,
20}
21
22/// Whether a window stays above the others, for [`Window::layer`](crate::Window::layer).
23#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
24#[non_exhaustive]
25pub enum Layer {
26    /// Among the other windows, as a window opens.
27    #[default]
28    Normal,
29    /// Above the windows of every other application, even when it loses focus.
30    AlwaysOnTop,
31}
32
33/// One change of a window's place, layer, position, or size.
34#[derive(Debug, Clone, Copy, PartialEq, Eq)]
35pub(crate) enum Arrange {
36    Place(Placement),
37    Layer(Layer),
38    /// The top-left corner, in logical pixels from the top left of the
39    /// primary screen.
40    Move(i32, i32),
41    /// The size of the page's area, in logical pixels.
42    Resize(u32, u32),
43}
44
45/// Makes `change` to `window`.
46pub(crate) fn apply(window: &tao::window::Window, change: Arrange) {
47    match change {
48        Arrange::Place(placement) => place(window, placement),
49        Arrange::Layer(layer) => window.set_always_on_top(layer == Layer::AlwaysOnTop),
50        Arrange::Move(x, y) => window.set_outer_position(LogicalPosition::new(x, y)),
51        Arrange::Resize(width, height) => window.set_inner_size(LogicalSize::new(width, height)),
52    }
53}
54
55/// Puts `window` in `placement`, leaving the placements it is not in.
56fn place(window: &tao::window::Window, placement: Placement) {
57    if placement != Placement::Fullscreen {
58        window.set_fullscreen(None);
59    }
60    window.set_minimized(placement == Placement::Minimized);
61    window.set_maximized(placement == Placement::Maximized);
62    if placement == Placement::Fullscreen {
63        window.set_fullscreen(Some(Fullscreen::Borderless(None)));
64    }
65}