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