waft_embedder/window.rs
1//! Asking for views, and the windows around them. Not a Flutter file.
2//!
3//! An app asks for a view in a window configured as a [`WindowConfig`]
4//! (`Platform::request_view`), and the view arrives later, as every view does, carrying the
5//! request's [`RequestId`], as iOS hands back the activity of `requestSceneSessionActivation` with
6//! the connecting scene. On a desktop the host makes the window; where the platform gives the app
7//! views and makes none, on a phone and when embedded, a request is refused. A view in a window
8//! the app may manage, one it asked for or one the platform gave, has a [`HostWindow`]
9//! (`View::window`): the configuration every window wants up front, whether the window is
10//! minimized or occluded, which the host reports with the window's moves
11//! (`EmbedderClient::window_changed`), the limits the user resizes it within where users resize
12//! windows ([`WindowSizeLimits`]), the native handle for anything beyond it, and the window's
13//! closing, which is how a view closes.
14//!
15//! Flutter's windowing API (`widgets/_window.dart`, experimental) builds window archetypes
16//! over the engine's views, each controller making its window within its constructor.
17
18use std::fmt;
19use std::rc::Rc;
20use std::time::Duration;
21
22pub use raw_window_handle::RawWindowHandle;
23
24use crate::{Rect, Size, ViewRef};
25
26/// How a window is made. Sizes and positions are logical, in the host's screen space with
27/// the origin at the top left of the primary screen.
28///
29/// Every option names what the window is for, not how a system does it: the host maps each
30/// onto what its system has, and one its system lacks it leaves at the system's default.
31/// What no option expresses is set through the window's
32/// [`native_handle`](HostWindow::native_handle).
33#[derive(Clone, Debug, PartialEq)]
34pub struct WindowConfig {
35 pub title: String,
36 pub size: [f64; 2],
37 /// Where the window opens; the host chooses when `None`.
38 pub position: Option<[f64; 2]>,
39 /// The system's title bar and border.
40 pub decorations: bool,
41 pub resizable: bool,
42 pub level: WindowLevel,
43 /// Whether showing or clicking the window makes it the focused one. A host may not be
44 /// able to refuse activation.
45 pub activating: bool,
46 /// Whether the window is on every virtual desktop: macOS Spaces, X11 workspaces.
47 pub all_desktops: bool,
48 pub background: WindowBackground,
49 /// Whether the window casts the system's shadow.
50 pub shadow: bool,
51 pub visible: bool,
52}
53
54impl Default for WindowConfig {
55 fn default() -> WindowConfig {
56 WindowConfig {
57 title: String::new(),
58 size: [900.0, 600.0],
59 position: None,
60 decorations: true,
61 resizable: true,
62 level: WindowLevel::Normal,
63 activating: true,
64 all_desktops: false,
65 background: WindowBackground::Solid,
66 shadow: true,
67 visible: true,
68 }
69 }
70}
71
72#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
73pub enum WindowLevel {
74 #[default]
75 Normal,
76 /// Above ordinary windows, as a palette or a panel sits.
77 AlwaysOnTop,
78 /// Below ordinary windows, as a desktop widget sits.
79 AlwaysOnBottom,
80}
81
82/// What the window shows behind the app's painting. Anything but `Solid` lets what is
83/// behind the window show where the app paints nothing.
84#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
85pub enum WindowBackground {
86 #[default]
87 Solid,
88 Transparent,
89 /// The system's blur of what lies behind the window (macOS vibrancy).
90 Blurred,
91 /// The system's glass material (macOS 26's Liquid Glass); the blur where the system
92 /// has none.
93 Glass,
94}
95
96impl WindowBackground {
97 /// Whether what is behind the window can show through it.
98 pub fn sees_through(self) -> bool {
99 self != WindowBackground::Solid
100 }
101}
102
103/// The number of a request for a view, which the view carries when it arrives
104/// ([`View::request_id`](crate::View::request_id)).
105#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
106pub struct RequestId(pub u64);
107
108/// Why a request for a view got no view.
109#[derive(Clone, Debug, PartialEq, Eq)]
110pub enum ViewError {
111 /// This platform gives the app its views and makes none on request.
112 Unsupported,
113 Failed(String),
114}
115
116impl fmt::Display for ViewError {
117 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
118 match self {
119 ViewError::Unsupported => f.write_str("the platform makes no views on request"),
120 ViewError::Failed(reason) => {
121 write!(f, "the platform could not make the view: {reason}")
122 }
123 }
124 }
125}
126
127impl std::error::Error for ViewError {}
128
129pub type WindowRef = Rc<dyn HostWindow>;
130
131/// The window around a view the host puts on screen in a window the app may manage.
132pub trait HostWindow: 'static {
133 fn view(&self) -> ViewRef;
134
135 /// The window's logical frame in screen space.
136 fn frame(&self) -> Rect;
137
138 /// Moves and sizes the window, over `animate` when the host can animate a frame.
139 fn set_frame(&self, frame: Rect, animate: Option<Duration>);
140
141 fn set_title(&self, title: &str);
142
143 fn show(&self);
144
145 fn hide(&self);
146
147 /// Flutter `WindowController.isMinimized`; macOS `isMiniaturized`, Windows `IsIconic`.
148 /// `false` where the system does not tell a client, as Wayland does not.
149 fn is_minimized(&self) -> bool;
150
151 /// Covered all over, or otherwise not shown: macOS's occlusion state without its visible bit,
152 /// winit's `Occluded`. Not in Flutter's controller; Waft's, after the platforms. `false`
153 /// where the system does not tell a window, as Windows and Wayland do not.
154 fn is_occluded(&self) -> bool;
155
156 /// Closes the window; its view goes away with it. The user's asking to close it only reports
157 /// it ([`EmbedderClient::view_close_requested`](crate::EmbedderClient::view_close_requested));
158 /// this closes it.
159 fn close(&self);
160
161 /// The smallest and largest size the user may resize this window to; none where users don't
162 /// resize windows (phones). Flutter `WindowController.setConstraints`.
163 fn size_limits(&self) -> Option<&dyn WindowSizeLimits>;
164
165 /// The native window, for configuration this interface does not carry.
166 fn native_handle(&self) -> Option<RawWindowHandle>;
167}
168
169/// The limits a window the user resizes keeps its view within, its content without the system's
170/// title bar and border, in logical points: Flutter's `BoxConstraints` on a window, its minimum
171/// and maximum apart. A limit holds from when it is set until it is set again, and `None` removes
172/// it.
173pub trait WindowSizeLimits {
174 fn set_min_size(&self, size: Option<Size>);
175
176 fn set_max_size(&self, size: Option<Size>);
177}
178
179#[cfg(test)]
180mod tests {
181 use super::*;
182
183 #[test]
184 fn a_default_window_is_an_ordinary_visible_one() {
185 let config = WindowConfig::default();
186 assert!(config.decorations && config.visible && config.activating);
187 assert_eq!(config.level, WindowLevel::Normal);
188 assert_eq!(config.background, WindowBackground::Solid);
189 }
190
191 #[test]
192 fn every_background_but_solid_sees_through() {
193 assert!(!WindowBackground::Solid.sees_through());
194 assert!(WindowBackground::Transparent.sees_through());
195 assert!(WindowBackground::Blurred.sees_through());
196 assert!(WindowBackground::Glass.sees_through());
197 }
198}