iced_runtime/window.rs
1//! Build window-based GUI applications.
2use crate::core::time::Instant;
3use crate::core::window::{
4 Direction, Event, Icon, Id, Level, Mode, Screenshot, Settings,
5 UserAttention,
6};
7use crate::core::{Point, Size};
8use crate::futures::Subscription;
9use crate::futures::event;
10use crate::futures::futures::channel::oneshot;
11use crate::task::{self, Task};
12
13pub use raw_window_handle;
14
15use raw_window_handle::WindowHandle;
16
17/// An operation to be performed on some window.
18pub enum Action {
19 /// Opens a new window with some [`Settings`].
20 Open(Id, Settings, oneshot::Sender<Id>),
21
22 /// Close the window and exits the application.
23 Close(Id),
24
25 /// Gets the [`Id`] of the oldest window.
26 GetOldest(oneshot::Sender<Option<Id>>),
27
28 /// Gets the [`Id`] of the latest window.
29 GetLatest(oneshot::Sender<Option<Id>>),
30
31 /// Move the window with the left mouse button until the button is
32 /// released.
33 ///
34 /// There's no guarantee that this will work unless the left mouse
35 /// button was pressed immediately before this function is called.
36 Drag(Id),
37
38 /// Resize the window with the left mouse button until the button is
39 /// released.
40 ///
41 /// There's no guarantee that this will work unless the left mouse
42 /// button was pressed immediately before this function is called.
43 DragResize(Id, Direction),
44
45 /// Resize the window to the given logical dimensions.
46 Resize(Id, Size),
47
48 /// Get the current logical dimensions of the window.
49 GetSize(Id, oneshot::Sender<Size>),
50
51 /// Get if the current window is maximized or not.
52 GetMaximized(Id, oneshot::Sender<bool>),
53
54 /// Set the window to maximized or back
55 Maximize(Id, bool),
56
57 /// Get if the current window is minimized or not.
58 ///
59 /// ## Platform-specific
60 /// - **Wayland:** Always `None`.
61 GetMinimized(Id, oneshot::Sender<Option<bool>>),
62
63 /// Set the window to minimized or back
64 Minimize(Id, bool),
65
66 /// Get the current logical coordinates of the window.
67 GetPosition(Id, oneshot::Sender<Option<Point>>),
68
69 /// Get the current scale factor (DPI) of the window.
70 GetScaleFactor(Id, oneshot::Sender<f32>),
71
72 /// Move the window to the given logical coordinates.
73 ///
74 /// Unsupported on Wayland.
75 Move(Id, Point),
76
77 /// Change the [`Mode`] of the window.
78 SetMode(Id, Mode),
79
80 /// Get the current [`Mode`] of the window.
81 GetMode(Id, oneshot::Sender<Mode>),
82
83 /// Toggle the window to maximized or back
84 ToggleMaximize(Id),
85
86 /// Toggle whether window has decorations.
87 ///
88 /// ## Platform-specific
89 /// - **X11:** Not implemented.
90 /// - **Web:** Unsupported.
91 ToggleDecorations(Id),
92
93 /// Request user attention to the window, this has no effect if the application
94 /// is already focused. How requesting for user attention manifests is platform dependent,
95 /// see [`UserAttention`] for details.
96 ///
97 /// Providing `None` will unset the request for user attention. Unsetting the request for
98 /// user attention might not be done automatically by the WM when the window receives input.
99 ///
100 /// ## Platform-specific
101 ///
102 /// - **iOS / Android / Web:** Unsupported.
103 /// - **macOS:** `None` has no effect.
104 /// - **X11:** Requests for user attention must be manually cleared.
105 /// - **Wayland:** Requires `xdg_activation_v1` protocol, `None` has no effect.
106 RequestUserAttention(Id, Option<UserAttention>),
107
108 /// Bring the window to the front and sets input focus. Has no effect if the window is
109 /// already in focus, minimized, or not visible.
110 ///
111 /// This method steals input focus from other applications. Do not use this method unless
112 /// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive
113 /// user experience.
114 ///
115 /// ## Platform-specific
116 ///
117 /// - **Web / Wayland:** Unsupported.
118 GainFocus(Id),
119
120 /// Change the window [`Level`].
121 SetLevel(Id, Level),
122
123 /// Show the system menu at cursor position.
124 ///
125 /// ## Platform-specific
126 /// Android / iOS / macOS / Orbital / Web / X11: Unsupported.
127 ShowSystemMenu(Id),
128
129 /// Get the raw identifier unique to the window.
130 GetRawId(Id, oneshot::Sender<u64>),
131
132 /// Change the window [`Icon`].
133 ///
134 /// On Windows and X11, this is typically the small icon in the top-left
135 /// corner of the titlebar.
136 ///
137 /// ## Platform-specific
138 ///
139 /// - **Web / Wayland / macOS:** Unsupported.
140 ///
141 /// - **Windows:** Sets `ICON_SMALL`. The base size for a window icon is 16x16, but it's
142 /// recommended to account for screen scaling and pick a multiple of that, i.e. 32x32.
143 ///
144 /// - **X11:** Has no universal guidelines for icon sizes, so you're at the whims of the WM. That
145 /// said, it's usually in the same ballpark as on Windows.
146 SetIcon(Id, Icon),
147
148 /// Runs the closure with the native window handle of the window with the given [`Id`].
149 RunWithHandle(Id, Box<dyn FnOnce(WindowHandle<'_>) + Send>),
150
151 /// Screenshot the viewport of the window.
152 Screenshot(Id, oneshot::Sender<Screenshot>),
153
154 /// Enables mouse passthrough for the given window.
155 ///
156 /// This disables mouse events for the window and passes mouse events
157 /// through to whatever window is underneath.
158 EnableMousePassthrough(Id),
159
160 /// Disable mouse passthrough for the given window.
161 ///
162 /// This enables mouse events for the window and stops mouse events
163 /// from being passed to whatever is underneath.
164 DisableMousePassthrough(Id),
165
166 /// Set the minimum inner window size.
167 SetMinSize(Id, Option<Size>),
168
169 /// Set the maximum inner window size.
170 SetMaxSize(Id, Option<Size>),
171
172 /// Set the window to be resizable or not.
173 SetResizable(Id, bool),
174
175 /// Set the window size increment.
176 SetResizeIncrements(Id, Option<Size>),
177}
178
179/// Subscribes to the frames of the window of the running application.
180///
181/// The resulting [`Subscription`] will produce items at a rate equal to the
182/// refresh rate of the first application window. Note that this rate may be variable, as it is
183/// normally managed by the graphics driver and/or the OS.
184///
185/// In any case, this [`Subscription`] is useful to smoothly draw application-driven
186/// animations without missing any frames.
187pub fn frames() -> Subscription<Instant> {
188 event::listen_raw(|event, _status, _window| match event {
189 crate::core::Event::Window(Event::RedrawRequested(at)) => Some(at),
190 _ => None,
191 })
192}
193
194/// Subscribes to all window events of the running application.
195pub fn events() -> Subscription<(Id, Event)> {
196 event::listen_with(|event, _status, id| {
197 if let crate::core::Event::Window(event) = event {
198 Some((id, event))
199 } else {
200 None
201 }
202 })
203}
204
205/// Subscribes to all [`Event::Opened`] occurrences in the running application.
206pub fn open_events() -> Subscription<Id> {
207 event::listen_with(|event, _status, id| {
208 if let crate::core::Event::Window(Event::Opened { .. }) = event {
209 Some(id)
210 } else {
211 None
212 }
213 })
214}
215
216/// Subscribes to all [`Event::Closed`] occurrences in the running application.
217pub fn close_events() -> Subscription<Id> {
218 event::listen_with(|event, _status, id| {
219 if let crate::core::Event::Window(Event::Closed) = event {
220 Some(id)
221 } else {
222 None
223 }
224 })
225}
226
227/// Subscribes to all [`Event::Resized`] occurrences in the running application.
228pub fn resize_events() -> Subscription<(Id, Size)> {
229 event::listen_with(|event, _status, id| {
230 if let crate::core::Event::Window(Event::Resized(size)) = event {
231 Some((id, size))
232 } else {
233 None
234 }
235 })
236}
237
238/// Subscribes to all [`Event::CloseRequested`] occurrences in the running application.
239pub fn close_requests() -> Subscription<Id> {
240 event::listen_with(|event, _status, id| {
241 if let crate::core::Event::Window(Event::CloseRequested) = event {
242 Some(id)
243 } else {
244 None
245 }
246 })
247}
248
249/// Opens a new window with the given [`Settings`]; producing the [`Id`]
250/// of the new window on completion.
251pub fn open(settings: Settings) -> (Id, Task<Id>) {
252 let id = Id::unique();
253
254 (
255 id,
256 task::oneshot(|channel| {
257 crate::Action::Window(Action::Open(id, settings, channel))
258 }),
259 )
260}
261
262/// Closes the window with `id`.
263pub fn close<T>(id: Id) -> Task<T> {
264 task::effect(crate::Action::Window(Action::Close(id)))
265}
266
267/// Gets the window [`Id`] of the oldest window.
268pub fn oldest() -> Task<Option<Id>> {
269 task::oneshot(|channel| crate::Action::Window(Action::GetOldest(channel)))
270}
271
272/// Gets the window [`Id`] of the latest window.
273pub fn latest() -> Task<Option<Id>> {
274 task::oneshot(|channel| crate::Action::Window(Action::GetLatest(channel)))
275}
276
277/// Begins dragging the window while the left mouse button is held.
278pub fn drag<T>(id: Id) -> Task<T> {
279 task::effect(crate::Action::Window(Action::Drag(id)))
280}
281
282/// Begins resizing the window while the left mouse button is held.
283pub fn drag_resize<T>(id: Id, direction: Direction) -> Task<T> {
284 task::effect(crate::Action::Window(Action::DragResize(id, direction)))
285}
286
287/// Resizes the window to the given logical dimensions.
288pub fn resize<T>(id: Id, new_size: Size) -> Task<T> {
289 task::effect(crate::Action::Window(Action::Resize(id, new_size)))
290}
291
292/// Set the window to be resizable or not.
293pub fn set_resizable<T>(id: Id, resizable: bool) -> Task<T> {
294 task::effect(crate::Action::Window(Action::SetResizable(id, resizable)))
295}
296
297/// Set the inner maximum size of the window.
298pub fn set_max_size<T>(id: Id, size: Option<Size>) -> Task<T> {
299 task::effect(crate::Action::Window(Action::SetMaxSize(id, size)))
300}
301
302/// Set the inner minimum size of the window.
303pub fn set_min_size<T>(id: Id, size: Option<Size>) -> Task<T> {
304 task::effect(crate::Action::Window(Action::SetMinSize(id, size)))
305}
306
307/// Set the window size increment.
308///
309/// This is usually used by apps such as terminal emulators that need "blocky" resizing.
310pub fn set_resize_increments<T>(id: Id, increments: Option<Size>) -> Task<T> {
311 task::effect(crate::Action::Window(Action::SetResizeIncrements(
312 id, increments,
313 )))
314}
315
316/// Get the window's size in logical dimensions.
317pub fn size(id: Id) -> Task<Size> {
318 task::oneshot(move |channel| {
319 crate::Action::Window(Action::GetSize(id, channel))
320 })
321}
322
323/// Gets the maximized state of the window with the given [`Id`].
324pub fn is_maximized(id: Id) -> Task<bool> {
325 task::oneshot(move |channel| {
326 crate::Action::Window(Action::GetMaximized(id, channel))
327 })
328}
329
330/// Maximizes the window.
331pub fn maximize<T>(id: Id, maximized: bool) -> Task<T> {
332 task::effect(crate::Action::Window(Action::Maximize(id, maximized)))
333}
334
335/// Gets the minimized state of the window with the given [`Id`].
336pub fn is_minimized(id: Id) -> Task<Option<bool>> {
337 task::oneshot(move |channel| {
338 crate::Action::Window(Action::GetMinimized(id, channel))
339 })
340}
341
342/// Minimizes the window.
343pub fn minimize<T>(id: Id, minimized: bool) -> Task<T> {
344 task::effect(crate::Action::Window(Action::Minimize(id, minimized)))
345}
346
347/// Gets the position in logical coordinates of the window with the given [`Id`].
348pub fn position(id: Id) -> Task<Option<Point>> {
349 task::oneshot(move |channel| {
350 crate::Action::Window(Action::GetPosition(id, channel))
351 })
352}
353
354/// Gets the scale factor of the window with the given [`Id`].
355pub fn scale_factor(id: Id) -> Task<f32> {
356 task::oneshot(move |channel| {
357 crate::Action::Window(Action::GetScaleFactor(id, channel))
358 })
359}
360
361/// Moves the window to the given logical coordinates.
362pub fn move_to<T>(id: Id, position: Point) -> Task<T> {
363 task::effect(crate::Action::Window(Action::Move(id, position)))
364}
365
366/// Gets the current [`Mode`] of the window.
367pub fn mode(id: Id) -> Task<Mode> {
368 task::oneshot(move |channel| {
369 crate::Action::Window(Action::GetMode(id, channel))
370 })
371}
372
373/// Changes the [`Mode`] of the window.
374pub fn set_mode<T>(id: Id, mode: Mode) -> Task<T> {
375 task::effect(crate::Action::Window(Action::SetMode(id, mode)))
376}
377
378/// Toggles the window to maximized or back.
379pub fn toggle_maximize<T>(id: Id) -> Task<T> {
380 task::effect(crate::Action::Window(Action::ToggleMaximize(id)))
381}
382
383/// Toggles the window decorations.
384pub fn toggle_decorations<T>(id: Id) -> Task<T> {
385 task::effect(crate::Action::Window(Action::ToggleDecorations(id)))
386}
387
388/// Request user attention to the window. This has no effect if the application
389/// is already focused. How requesting for user attention manifests is platform dependent,
390/// see [`UserAttention`] for details.
391///
392/// Providing `None` will unset the request for user attention. Unsetting the request for
393/// user attention might not be done automatically by the WM when the window receives input.
394pub fn request_user_attention<T>(
395 id: Id,
396 user_attention: Option<UserAttention>,
397) -> Task<T> {
398 task::effect(crate::Action::Window(Action::RequestUserAttention(
399 id,
400 user_attention,
401 )))
402}
403
404/// Brings the window to the front and sets input focus. Has no effect if the window is
405/// already in focus, minimized, or not visible.
406///
407/// This [`Task`] steals input focus from other applications. Do not use this method unless
408/// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive
409/// user experience.
410pub fn gain_focus<T>(id: Id) -> Task<T> {
411 task::effect(crate::Action::Window(Action::GainFocus(id)))
412}
413
414/// Changes the window [`Level`].
415pub fn set_level<T>(id: Id, level: Level) -> Task<T> {
416 task::effect(crate::Action::Window(Action::SetLevel(id, level)))
417}
418
419/// Show the [system menu] at cursor position.
420///
421/// [system menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu
422pub fn show_system_menu<T>(id: Id) -> Task<T> {
423 task::effect(crate::Action::Window(Action::ShowSystemMenu(id)))
424}
425
426/// Gets an identifier unique to the window, provided by the underlying windowing system. This is
427/// not to be confused with [`Id`].
428pub fn raw_id<Message>(id: Id) -> Task<u64> {
429 task::oneshot(|channel| {
430 crate::Action::Window(Action::GetRawId(id, channel))
431 })
432}
433
434/// Changes the [`Icon`] of the window.
435pub fn set_icon<T>(id: Id, icon: Icon) -> Task<T> {
436 task::effect(crate::Action::Window(Action::SetIcon(id, icon)))
437}
438
439/// Runs the given callback with the native window handle for the window with the given id.
440///
441/// Note that if the window closes before this call is processed the callback will not be run.
442pub fn run_with_handle<T>(
443 id: Id,
444 f: impl FnOnce(WindowHandle<'_>) -> T + Send + 'static,
445) -> Task<T>
446where
447 T: Send + 'static,
448{
449 task::oneshot(move |channel| {
450 crate::Action::Window(Action::RunWithHandle(
451 id,
452 Box::new(move |handle| {
453 let _ = channel.send(f(handle));
454 }),
455 ))
456 })
457}
458
459/// Captures a [`Screenshot`] from the window.
460pub fn screenshot(id: Id) -> Task<Screenshot> {
461 task::oneshot(move |channel| {
462 crate::Action::Window(Action::Screenshot(id, channel))
463 })
464}
465
466/// Enables mouse passthrough for the given window.
467///
468/// This disables mouse events for the window and passes mouse events
469/// through to whatever window is underneath.
470pub fn enable_mouse_passthrough<Message>(id: Id) -> Task<Message> {
471 task::effect(crate::Action::Window(Action::EnableMousePassthrough(id)))
472}
473
474/// Disable mouse passthrough for the given window.
475///
476/// This enables mouse events for the window and stops mouse events
477/// from being passed to whatever is underneath.
478pub fn disable_mouse_passthrough<Message>(id: Id) -> Task<Message> {
479 task::effect(crate::Action::Window(Action::DisableMousePassthrough(id)))
480}