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, and on Android a task with an
7//! activity of its own; where the platform gives the app views and makes none, on an iPhone and
8//! when embedded, a request is refused. A view in a window
9//! the app may manage, one it asked for or one the platform gave, has a [`HostWindow`]
10//! (`View::window`): the configuration every window wants up front, whether the window is
11//! minimized, occluded, maximized or full screen, which the host reports with the window's moves
12//! (`EmbedderClient::window_changed`), the limits the user resizes it within where users resize
13//! windows ([`WindowSizeLimits`]), the native handle for anything beyond it, and the window's
14//! closing, which is how a view closes. On Android a window is an activity's task, the card in
15//! Recents: its title and icon are the card's, closing it removes the task, and minimizing it sends
16//! the task to the background.
17//!
18//! Flutter's windowing API (`widgets/_window.dart`, experimental) builds window archetypes
19//! over the engine's views, each controller making its window within its constructor.
20
21use std::fmt;
22use std::path::PathBuf;
23use std::rc::Rc;
24use std::time::Duration;
25
26pub use raw_window_handle::RawWindowHandle;
27
28use crate::{IconImage, Rect, ScreenId, Size, ViewRef};
29
30/// How a window is made. Sizes and positions are logical, in the host's screen space with
31/// the origin at the top left of the primary screen.
32///
33/// Every option names what the window is for, not how a system does it: the host maps each
34/// onto what its system has, and one its system lacks it leaves at the system's default.
35/// What no option expresses is set through the window's
36/// [`native_handle`](HostWindow::native_handle).
37#[derive(Clone, Debug, PartialEq)]
38pub struct WindowConfig {
39 pub title: String,
40 /// The window's own icon, where the system shows one for a window rather than for the app:
41 /// the title bar's and the taskbar's on Windows and X11, the card's in Android's Recents. The
42 /// app's icon where `None`.
43 pub icon: Option<IconImage>,
44 pub size: [f64; 2],
45 /// Where the window opens; the host chooses when `None`.
46 pub position: Option<[f64; 2]>,
47 /// The system's title bar and border.
48 pub decorations: bool,
49 pub resizable: bool,
50 pub level: WindowLevel,
51 /// Whether the window is on every virtual desktop: macOS Spaces, X11 workspaces.
52 pub all_desktops: bool,
53 pub background: WindowBackground,
54 /// Whether the window casts the system's shadow.
55 pub shadow: bool,
56 /// Whether the window shows: once its view's first frame is drawn, never empty before, and
57 /// activated, as a newly opened window is. An app that wants it seen sooner calls
58 /// [`HostWindow::show`] or [`HostWindow::activate`], which act at once.
59 pub visible: bool,
60 /// What only macOS windows offer; other systems leave it out.
61 pub macos: MacosWindowConfig,
62 /// What only Windows' windows offer; other systems leave it out.
63 pub windows: WindowsWindowConfig,
64 /// What only Linux's windows offer; other systems leave it out.
65 pub linux: LinuxWindowConfig,
66}
67
68impl Default for WindowConfig {
69 fn default() -> WindowConfig {
70 WindowConfig {
71 title: String::new(),
72 icon: None,
73 size: [900.0, 600.0],
74 position: None,
75 decorations: true,
76 resizable: true,
77 level: WindowLevel::Normal,
78 all_desktops: false,
79 background: WindowBackground::Solid,
80 shadow: true,
81 visible: true,
82 macos: MacosWindowConfig::default(),
83 windows: WindowsWindowConfig::default(),
84 linux: LinuxWindowConfig::default(),
85 }
86 }
87}
88
89/// What a window offers on macOS alone.
90#[derive(Clone, Copy, Debug, Default, PartialEq)]
91pub struct MacosWindowConfig {
92 pub title_bar: MacosTitleBar,
93}
94
95/// The title bar of a macOS window.
96#[derive(Clone, Copy, Debug, Default, PartialEq)]
97pub enum MacosTitleBar {
98 /// The system's, with the window's title in it.
99 #[default]
100 Standard,
101 /// None of its own, as Safari's and Finder's windows: the view runs to the window's top edge,
102 /// and the close, minimize and zoom buttons sit over it, centred in a band `height` points tall
103 /// at the top, as far from the left edge as from the band's top. Presses in the band reach the
104 /// app, which moves the window, and does a title bar's double press, from where it draws
105 /// nothing to press ([`HostWindow::drag`], [`HostWindow::title_bar_double_click_action`]).
106 Hidden { height: f64 },
107}
108
109/// What a window offers on Windows alone: the app it is on the taskbar, which one process may give
110/// each of its windows apart, and what a pin of it starts.
111#[derive(Clone, Debug, Default, PartialEq)]
112pub struct WindowsWindowConfig {
113 /// The window's own AppUserModelID (`System.AppUserModel.ID`): the taskbar groups the window
114 /// with that app's, apart from the process's, and pins it as that app. The process's ID where
115 /// `None`. At most 128 characters, without spaces.
116 pub app_user_model_id: Option<String>,
117 /// What a pin of the window starts, where no Start menu shortcut carries its ID: such a
118 /// shortcut is what the taskbar pins instead. Only with an `app_user_model_id`.
119 pub relaunch: Option<WindowsRelaunch>,
120}
121
122/// What a pin of a window starts, and how the pin shows (`System.AppUserModel.RelaunchCommand`,
123/// `RelaunchDisplayNameResource`, `RelaunchIconResource`).
124#[derive(Clone, Debug, PartialEq)]
125pub struct WindowsRelaunch {
126 /// The whole command line a pin runs, its program quoted where its path has spaces.
127 pub command: String,
128 /// The pin's name.
129 pub display_name: String,
130 /// The pin's icon; the program's where `None`. An icon Windows cannot load makes it drop the
131 /// whole relaunch, command and name too, without a word: the pin then starts the process's
132 /// executable alone.
133 pub icon: Option<WindowsIconResource>,
134}
135
136/// An icon in a file, as Windows names one: an `.ico` file and the index of its icon, or a module
137/// and a resource id as a negative index.
138#[derive(Clone, Debug, PartialEq, Eq)]
139pub struct WindowsIconResource {
140 pub path: PathBuf,
141 pub index: i32,
142}
143
144/// What a window offers on Linux alone.
145#[derive(Clone, Debug, Default, PartialEq, Eq)]
146pub struct LinuxWindowConfig {
147 /// The window's own app id (xdg-shell's `set_app_id`; the class, `WM_CLASS`, on X11), by which
148 /// the desktop matches it to the desktop file `<app_id>.desktop`, in place of the app's own.
149 /// The app's where `None`.
150 pub app_id: Option<String>,
151 /// The XDG activation token the window shows with, which lets it take the focus the desktop
152 /// gave the launch that brought it (`XDG_ACTIVATION_TOKEN`): a later launch's, passed on.
153 pub activation_token: Option<String>,
154}
155
156#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
157pub enum WindowLevel {
158 #[default]
159 Normal,
160 /// Above ordinary windows, as a palette or a panel sits.
161 AlwaysOnTop,
162 /// Below ordinary windows, as a desktop widget sits.
163 AlwaysOnBottom,
164}
165
166/// What the window shows behind the app's painting. Anything but `Solid` lets what is
167/// behind the window show where the app paints nothing.
168#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
169pub enum WindowBackground {
170 #[default]
171 Solid,
172 Transparent,
173 /// The system's blur of what lies behind the window (macOS vibrancy).
174 Blurred,
175 /// The system's glass material (macOS 26's Liquid Glass); the blur where the system
176 /// has none.
177 Glass,
178}
179
180impl WindowBackground {
181 /// Whether what is behind the window can show through it.
182 pub fn sees_through(self) -> bool {
183 self != WindowBackground::Solid
184 }
185}
186
187/// What a double-click on a window's title bar does
188/// ([`HostWindow::title_bar_double_click_action`]).
189#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
190pub enum TitleBarAction {
191 Maximize,
192 Minimize,
193 Nothing,
194 /// The host can't tell, or the system's setting has a value it doesn't know.
195 #[default]
196 Unknown,
197}
198
199/// The number of a request for a view, which the view carries when it arrives
200/// ([`View::request_id`](crate::View::request_id)).
201#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
202pub struct RequestId(pub u64);
203
204/// Why a request for a view got no view.
205#[derive(Clone, Debug, PartialEq, Eq)]
206pub enum ViewError {
207 /// This platform gives the app its views and makes none on request.
208 Unsupported,
209 Failed(String),
210}
211
212impl fmt::Display for ViewError {
213 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
214 match self {
215 ViewError::Unsupported => f.write_str("the platform makes no views on request"),
216 ViewError::Failed(reason) => {
217 write!(f, "the platform could not make the view: {reason}")
218 }
219 }
220 }
221}
222
223impl std::error::Error for ViewError {}
224
225pub type WindowRef = Rc<dyn HostWindow>;
226
227/// The window around a view the host puts on screen in a window the app may manage.
228pub trait HostWindow: 'static {
229 fn view(&self) -> ViewRef;
230
231 /// The window's logical frame in screen space.
232 fn frame(&self) -> Rect;
233
234 /// Moves and sizes the window, over `animate` when the host can animate a frame.
235 fn set_frame(&self, frame: Rect, animate: Option<Duration>);
236
237 fn set_title(&self, title: &str);
238
239 /// The window's own icon from now on ([`WindowConfig::icon`]); the app's icon where `None`.
240 /// A system that shows no icon for a window, as macOS and Wayland do not, drops it.
241 fn set_icon(&self, icon: Option<&IconImage>);
242
243 /// Makes the window visible, where it is in the stack, without taking the focus, at once,
244 /// before its view's first frame too. A system that cannot show a window without bringing it
245 /// forward with the focus does that instead.
246 fn show(&self);
247
248 /// Takes the window off the screen, kept, at once. A system that cannot hide a window leaves
249 /// it as it is.
250 fn hide(&self);
251
252 /// Flutter `WindowController.activate`: makes the window visible at once, a minimized one at
253 /// the size and position it had before, brings it to the front and gives it the focus, as far
254 /// as the system lets the app; where it does not, the system draws attention to the window
255 /// instead.
256 /// The window alone: where the system activates an application apart from its windows, as
257 /// macOS does, the application is not activated, which its platform's own call does.
258 fn activate(&self);
259
260 /// Flutter `WindowController.isMinimized`; macOS `isMiniaturized`, Windows `IsIconic`.
261 /// `false` where the system does not tell a client, as Wayland does not.
262 fn is_minimized(&self) -> bool;
263
264 /// Covered all over, or otherwise not shown: macOS's occlusion state without its visible bit,
265 /// winit's `Occluded`. Not in Flutter's controller; Waft's, after the platforms. `false`
266 /// where the system does not tell a window, as Windows and Wayland do not.
267 fn is_occluded(&self) -> bool;
268
269 /// Closes the window; its view goes away with it. The user's asking to close it only reports
270 /// it ([`EmbedderClient::view_close_requested`](crate::EmbedderClient::view_close_requested));
271 /// this closes it.
272 fn close(&self);
273
274 /// The smallest and largest size the user may resize this window to; none where users don't
275 /// resize windows (phones). Flutter `WindowController.setConstraints`.
276 fn size_limits(&self) -> Option<&dyn WindowSizeLimits>;
277
278 /// Starts the system's own move of the window from the press under way, as a press on its
279 /// title bar does: for an app that draws its own ([`MacosTitleBar::Hidden`]). Call it from
280 /// the press.
281 fn drag(&self);
282
283 /// Flutter `WindowController.isMaximized`; on macOS, zoomed. `false` while the window is full
284 /// screen.
285 fn is_maximized(&self) -> bool;
286
287 /// Flutter `WindowController.setMaximized`; on macOS, zooms the window or returns it to its
288 /// size from before.
289 fn set_maximized(&self, maximized: bool);
290
291 /// Flutter `WindowController.setMinimized`.
292 fn set_minimized(&self, minimized: bool);
293
294 /// Flutter `WindowController.isFullscreen`: whether the window fills a screen, without its
295 /// title bar and over the system's own bars; on macOS, in a Space of its own. `false` where a
296 /// window has no such state, as a phone's task, which fills the screen as it is.
297 fn is_fullscreen(&self) -> bool;
298
299 /// Flutter `WindowController.setFullscreen`: has the window fill `screen`, the one it is on
300 /// where `None`, or returns it to how it was before, when `screen` is not read. The system
301 /// may take a while, or refuse: the window answers [`is_fullscreen`](Self::is_fullscreen)
302 /// once it has, and the host reports the change
303 /// ([`EmbedderClient::window_changed`](crate::EmbedderClient::window_changed)).
304 fn set_fullscreen(&self, fullscreen: bool, screen: Option<ScreenId>);
305
306 /// What the system's settings say a double-click on a title bar does, for an app that draws
307 /// its own ([`MacosTitleBar::Hidden`]): macOS's "Double-click a window's title bar to".
308 fn title_bar_double_click_action(&self) -> TitleBarAction {
309 TitleBarAction::Unknown
310 }
311
312 /// The native window, for configuration this interface does not carry.
313 fn native_handle(&self) -> Option<RawWindowHandle>;
314}
315
316/// The limits a window the user resizes keeps its view within, its content without the system's
317/// title bar and border, in logical points: Flutter's `BoxConstraints` on a window, its minimum
318/// and maximum apart. A limit holds from when it is set until it is set again, and `None` removes
319/// it.
320pub trait WindowSizeLimits {
321 fn set_min_size(&self, size: Option<Size>);
322
323 fn set_max_size(&self, size: Option<Size>);
324}
325
326#[cfg(test)]
327mod tests {
328 use super::*;
329
330 #[test]
331 fn a_default_window_is_an_ordinary_visible_one() {
332 let config = WindowConfig::default();
333 assert!(config.decorations && config.visible);
334 assert_eq!(config.level, WindowLevel::Normal);
335 assert_eq!(config.background, WindowBackground::Solid);
336 assert_eq!(config.macos.title_bar, MacosTitleBar::Standard);
337 assert_eq!(config.windows, WindowsWindowConfig::default());
338 assert_eq!(config.linux, LinuxWindowConfig::default());
339 }
340
341 #[test]
342 fn every_background_but_solid_sees_through() {
343 assert!(!WindowBackground::Solid.sees_through());
344 assert!(WindowBackground::Transparent.sees_through());
345 assert!(WindowBackground::Blurred.sees_through());
346 assert!(WindowBackground::Glass.sees_through());
347 }
348}