winit/window.rs
1//! The [`Window`] struct and associated types.
2use std::fmt;
3
4use crate::dpi::{PhysicalPosition, PhysicalSize, Position, Size};
5use crate::error::{ExternalError, NotSupportedError};
6use crate::monitor::{MonitorHandle, VideoModeHandle};
7use crate::platform_impl::{self, PlatformSpecificWindowAttributes};
8
9pub use crate::cursor::{BadImage, Cursor, CustomCursor, CustomCursorSource, MAX_CURSOR_SIZE};
10pub use crate::icon::{BadIcon, Icon};
11
12#[doc(inline)]
13pub use cursor_icon::{CursorIcon, ParseError as CursorIconParseError};
14#[cfg(feature = "serde")]
15use serde::{Deserialize, Serialize};
16
17/// Represents a window.
18///
19/// The window is closed when dropped.
20///
21/// ## Threading
22///
23/// This is `Send + Sync`, meaning that it can be freely used from other
24/// threads.
25///
26/// However, some platforms (macOS, Web and iOS) only allow user interface
27/// interactions on the main thread, so on those platforms, if you use the
28/// window from a thread other than the main, the code is scheduled to run on
29/// the main thread, and your thread may be blocked until that completes.
30///
31/// ## Platform-specific
32///
33/// **Web:** The [`Window`], which is represented by a `HTMLElementCanvas`, can
34/// not be closed by dropping the [`Window`].
35pub struct Window {
36 pub(crate) window: platform_impl::Window,
37}
38
39impl fmt::Debug for Window {
40 fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
41 fmtr.pad("Window { .. }")
42 }
43}
44
45impl Drop for Window {
46 /// This will close the [`Window`].
47 ///
48 /// See [`Window`] for more details.
49 fn drop(&mut self) {
50 self.window.maybe_wait_on_main(|w| {
51 // If the window is in exclusive fullscreen, we must restore the desktop
52 // video mode (generally this would be done on application exit, but
53 // closing the window doesn't necessarily always mean application exit,
54 // such as when there are multiple windows)
55 if let Some(Fullscreen::Exclusive(_)) = w.fullscreen().map(|f| f.into()) {
56 w.set_fullscreen(None);
57 }
58 })
59 }
60}
61
62/// Identifier of a window. Unique for each window.
63///
64/// Can be obtained with [`window.id()`][`Window::id`].
65///
66/// Whenever you receive an event specific to a window, this event contains a `WindowId` which you
67/// can then compare to the ids of your windows.
68#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
69pub struct WindowId(pub(crate) platform_impl::WindowId);
70
71impl WindowId {
72 /// Returns a dummy id, useful for unit testing.
73 ///
74 /// # Notes
75 ///
76 /// The only guarantee made about the return value of this function is that
77 /// it will always be equal to itself and to future values returned by this function.
78 /// No other guarantees are made. This may be equal to a real [`WindowId`].
79 pub const fn dummy() -> Self {
80 WindowId(platform_impl::WindowId::dummy())
81 }
82}
83
84impl fmt::Debug for WindowId {
85 fn fmt(&self, fmtr: &mut fmt::Formatter<'_>) -> fmt::Result {
86 self.0.fmt(fmtr)
87 }
88}
89
90impl From<WindowId> for u64 {
91 fn from(window_id: WindowId) -> Self {
92 window_id.0.into()
93 }
94}
95
96impl From<u64> for WindowId {
97 fn from(raw_id: u64) -> Self {
98 Self(raw_id.into())
99 }
100}
101
102/// Attributes used when creating a window.
103#[derive(Debug, Clone)]
104pub struct WindowAttributes {
105 pub inner_size: Option<Size>,
106 pub min_inner_size: Option<Size>,
107 pub max_inner_size: Option<Size>,
108 pub position: Option<Position>,
109 pub resizable: bool,
110 pub enabled_buttons: WindowButtons,
111 pub title: String,
112 pub maximized: bool,
113 pub visible: bool,
114 pub transparent: bool,
115 pub blur: bool,
116 pub decorations: bool,
117 pub window_icon: Option<Icon>,
118 pub preferred_theme: Option<Theme>,
119 pub resize_increments: Option<Size>,
120 pub content_protected: bool,
121 pub window_level: WindowLevel,
122 pub active: bool,
123 pub cursor: Cursor,
124 #[cfg(feature = "rwh_06")]
125 pub(crate) parent_window: Option<SendSyncRawWindowHandle>,
126 pub fullscreen: Option<Fullscreen>,
127 // Platform-specific configuration.
128 #[allow(dead_code)]
129 pub(crate) platform_specific: PlatformSpecificWindowAttributes,
130}
131
132impl Default for WindowAttributes {
133 #[inline]
134 fn default() -> WindowAttributes {
135 WindowAttributes {
136 inner_size: None,
137 min_inner_size: None,
138 max_inner_size: None,
139 position: None,
140 resizable: true,
141 enabled_buttons: WindowButtons::all(),
142 title: "winit window".to_owned(),
143 maximized: false,
144 fullscreen: None,
145 visible: true,
146 transparent: false,
147 blur: false,
148 decorations: true,
149 window_level: Default::default(),
150 window_icon: None,
151 preferred_theme: None,
152 resize_increments: None,
153 content_protected: false,
154 cursor: Cursor::default(),
155 #[cfg(feature = "rwh_06")]
156 parent_window: None,
157 active: true,
158 platform_specific: Default::default(),
159 }
160 }
161}
162
163/// Wrapper for [`rwh_06::RawWindowHandle`] for [`WindowAttributes::parent_window`].
164///
165/// # Safety
166///
167/// The user has to account for that when using [`WindowAttributes::with_parent_window()`],
168/// which is `unsafe`.
169#[derive(Debug, Clone)]
170#[cfg(feature = "rwh_06")]
171pub(crate) struct SendSyncRawWindowHandle(pub(crate) rwh_06::RawWindowHandle);
172
173#[cfg(feature = "rwh_06")]
174unsafe impl Send for SendSyncRawWindowHandle {}
175#[cfg(feature = "rwh_06")]
176unsafe impl Sync for SendSyncRawWindowHandle {}
177
178impl WindowAttributes {
179 /// Initializes new attributes with default values.
180 #[inline]
181 #[deprecated = "use `Window::default_attributes` instead"]
182 pub fn new() -> Self {
183 Default::default()
184 }
185}
186
187impl WindowAttributes {
188 /// Get the parent window stored on the attributes.
189 #[cfg(feature = "rwh_06")]
190 pub fn parent_window(&self) -> Option<&rwh_06::RawWindowHandle> {
191 self.parent_window.as_ref().map(|handle| &handle.0)
192 }
193
194 /// Requests the window to be of specific dimensions.
195 ///
196 /// If this is not set, some platform-specific dimensions will be used.
197 ///
198 /// See [`Window::request_inner_size`] for details.
199 #[inline]
200 pub fn with_inner_size<S: Into<Size>>(mut self, size: S) -> Self {
201 self.inner_size = Some(size.into());
202 self
203 }
204
205 /// Sets the minimum dimensions a window can have.
206 ///
207 /// If this is not set, the window will have no minimum dimensions (aside
208 /// from reserved).
209 ///
210 /// See [`Window::set_min_inner_size`] for details.
211 #[inline]
212 pub fn with_min_inner_size<S: Into<Size>>(mut self, min_size: S) -> Self {
213 self.min_inner_size = Some(min_size.into());
214 self
215 }
216
217 /// Sets the maximum dimensions a window can have.
218 ///
219 /// If this is not set, the window will have no maximum or will be set to
220 /// the primary monitor's dimensions by the platform.
221 ///
222 /// See [`Window::set_max_inner_size`] for details.
223 #[inline]
224 pub fn with_max_inner_size<S: Into<Size>>(mut self, max_size: S) -> Self {
225 self.max_inner_size = Some(max_size.into());
226 self
227 }
228
229 /// Sets a desired initial position for the window.
230 ///
231 /// If this is not set, some platform-specific position will be chosen.
232 ///
233 /// See [`Window::set_outer_position`] for details.
234 ///
235 /// ## Platform-specific
236 ///
237 /// - **macOS:** The top left corner position of the window content, the window's "inner"
238 /// position. The window title bar will be placed above it. The window will be positioned such
239 /// that it fits on screen, maintaining set `inner_size` if any. If you need to precisely
240 /// position the top left corner of the whole window you have to use
241 /// [`Window::set_outer_position`] after creating the window.
242 /// - **Windows:** The top left corner position of the window title bar, the window's "outer"
243 /// position. There may be a small gap between this position and the window due to the
244 /// specifics of the Window Manager.
245 /// - **X11:** The top left corner of the window, the window's "outer" position.
246 /// - **Others:** Ignored.
247 #[inline]
248 pub fn with_position<P: Into<Position>>(mut self, position: P) -> Self {
249 self.position = Some(position.into());
250 self
251 }
252
253 /// Sets whether the window is resizable or not.
254 ///
255 /// The default is `true`.
256 ///
257 /// See [`Window::set_resizable`] for details.
258 #[inline]
259 pub fn with_resizable(mut self, resizable: bool) -> Self {
260 self.resizable = resizable;
261 self
262 }
263
264 /// Sets the enabled window buttons.
265 ///
266 /// The default is [`WindowButtons::all`]
267 ///
268 /// See [`Window::set_enabled_buttons`] for details.
269 #[inline]
270 pub fn with_enabled_buttons(mut self, buttons: WindowButtons) -> Self {
271 self.enabled_buttons = buttons;
272 self
273 }
274
275 /// Sets the initial title of the window in the title bar.
276 ///
277 /// The default is `"winit window"`.
278 ///
279 /// See [`Window::set_title`] for details.
280 #[inline]
281 pub fn with_title<T: Into<String>>(mut self, title: T) -> Self {
282 self.title = title.into();
283 self
284 }
285
286 /// Sets whether the window should be put into fullscreen upon creation.
287 ///
288 /// The default is `None`.
289 ///
290 /// See [`Window::set_fullscreen`] for details.
291 #[inline]
292 pub fn with_fullscreen(mut self, fullscreen: Option<Fullscreen>) -> Self {
293 self.fullscreen = fullscreen;
294 self
295 }
296
297 /// Request that the window is maximized upon creation.
298 ///
299 /// The default is `false`.
300 ///
301 /// See [`Window::set_maximized`] for details.
302 #[inline]
303 pub fn with_maximized(mut self, maximized: bool) -> Self {
304 self.maximized = maximized;
305 self
306 }
307
308 /// Sets whether the window will be initially visible or hidden.
309 ///
310 /// The default is to show the window.
311 ///
312 /// See [`Window::set_visible`] for details.
313 #[inline]
314 pub fn with_visible(mut self, visible: bool) -> Self {
315 self.visible = visible;
316 self
317 }
318
319 /// Sets whether the background of the window should be transparent.
320 ///
321 /// If this is `true`, writing colors with alpha values different than
322 /// `1.0` will produce a transparent window. On some platforms this
323 /// is more of a hint for the system and you'd still have the alpha
324 /// buffer. To control it see [`Window::set_transparent`].
325 ///
326 /// The default is `false`.
327 #[inline]
328 pub fn with_transparent(mut self, transparent: bool) -> Self {
329 self.transparent = transparent;
330 self
331 }
332
333 /// Sets whether the background of the window should be blurred by the system.
334 ///
335 /// The default is `false`.
336 ///
337 /// See [`Window::set_blur`] for details.
338 #[inline]
339 pub fn with_blur(mut self, blur: bool) -> Self {
340 self.blur = blur;
341 self
342 }
343
344 /// Get whether the window will support transparency.
345 #[inline]
346 pub fn transparent(&self) -> bool {
347 self.transparent
348 }
349
350 /// Sets whether the window should have a border, a title bar, etc.
351 ///
352 /// The default is `true`.
353 ///
354 /// See [`Window::set_decorations`] for details.
355 #[inline]
356 pub fn with_decorations(mut self, decorations: bool) -> Self {
357 self.decorations = decorations;
358 self
359 }
360
361 /// Sets the window level.
362 ///
363 /// This is just a hint to the OS, and the system could ignore it.
364 ///
365 /// The default is [`WindowLevel::Normal`].
366 ///
367 /// See [`WindowLevel`] for details.
368 #[inline]
369 pub fn with_window_level(mut self, level: WindowLevel) -> Self {
370 self.window_level = level;
371 self
372 }
373
374 /// Sets the window icon.
375 ///
376 /// The default is `None`.
377 ///
378 /// See [`Window::set_window_icon`] for details.
379 #[inline]
380 pub fn with_window_icon(mut self, window_icon: Option<Icon>) -> Self {
381 self.window_icon = window_icon;
382 self
383 }
384
385 /// Sets a specific theme for the window.
386 ///
387 /// If `None` is provided, the window will use the system theme.
388 ///
389 /// The default is `None`.
390 ///
391 /// ## Platform-specific
392 ///
393 /// - **Wayland:** This controls only CSD. When using `None` it'll try to use dbus to get the
394 /// system preference. When explicit theme is used, this will avoid dbus all together.
395 /// - **x11:** Build window with `_GTK_THEME_VARIANT` hint set to `dark` or `light`.
396 /// - **iOS / Android / Web / x11 / Orbital:** Ignored.
397 #[inline]
398 pub fn with_theme(mut self, theme: Option<Theme>) -> Self {
399 self.preferred_theme = theme;
400 self
401 }
402
403 /// Build window with resize increments hint.
404 ///
405 /// The default is `None`.
406 ///
407 /// See [`Window::set_resize_increments`] for details.
408 #[inline]
409 pub fn with_resize_increments<S: Into<Size>>(mut self, resize_increments: S) -> Self {
410 self.resize_increments = Some(resize_increments.into());
411 self
412 }
413
414 /// Prevents the window contents from being captured by other apps.
415 ///
416 /// The default is `false`.
417 ///
418 /// ## Platform-specific
419 ///
420 /// - **macOS**: if `false`, [`NSWindowSharingNone`] is used but doesn't completely prevent all
421 /// apps from reading the window content, for instance, QuickTime.
422 /// - **iOS / Android / Web / x11 / Orbital:** Ignored.
423 ///
424 /// [`NSWindowSharingNone`]: https://developer.apple.com/documentation/appkit/nswindowsharingtype/nswindowsharingnone
425 #[inline]
426 pub fn with_content_protected(mut self, protected: bool) -> Self {
427 self.content_protected = protected;
428 self
429 }
430
431 /// Whether the window will be initially focused or not.
432 ///
433 /// The window should be assumed as not focused by default
434 /// following by the [`WindowEvent::Focused`].
435 ///
436 /// ## Platform-specific:
437 ///
438 /// **Android / iOS / X11 / Wayland / Orbital:** Unsupported.
439 ///
440 /// [`WindowEvent::Focused`]: crate::event::WindowEvent::Focused.
441 #[inline]
442 pub fn with_active(mut self, active: bool) -> Self {
443 self.active = active;
444 self
445 }
446
447 /// Modifies the cursor icon of the window.
448 ///
449 /// The default is [`CursorIcon::Default`].
450 ///
451 /// See [`Window::set_cursor()`] for more details.
452 #[inline]
453 pub fn with_cursor(mut self, cursor: impl Into<Cursor>) -> Self {
454 self.cursor = cursor.into();
455 self
456 }
457
458 /// Build window with parent window.
459 ///
460 /// The default is `None`.
461 ///
462 /// ## Safety
463 ///
464 /// `parent_window` must be a valid window handle.
465 ///
466 /// ## Platform-specific
467 ///
468 /// - **Windows** : A child window has the WS_CHILD style and is confined
469 /// to the client area of its parent window. For more information, see
470 /// <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#child-windows>
471 /// - **X11**: A child window is confined to the client area of its parent window.
472 /// - **Android / iOS / Wayland / Web:** Unsupported.
473 #[cfg(feature = "rwh_06")]
474 #[inline]
475 pub unsafe fn with_parent_window(
476 mut self,
477 parent_window: Option<rwh_06::RawWindowHandle>,
478 ) -> Self {
479 self.parent_window = parent_window.map(SendSyncRawWindowHandle);
480 self
481 }
482}
483
484/// Base Window functions.
485impl Window {
486 /// Create a new [`WindowAttributes`] which allows modifying the window's attributes before
487 /// creation.
488 #[inline]
489 pub fn default_attributes() -> WindowAttributes {
490 WindowAttributes::default()
491 }
492
493 /// Returns an identifier unique to the window.
494 #[inline]
495 pub fn id(&self) -> WindowId {
496 let _span = tracing::debug_span!("winit::Window::id",).entered();
497
498 self.window.maybe_wait_on_main(|w| WindowId(w.id()))
499 }
500
501 /// Returns the scale factor that can be used to map logical pixels to physical pixels, and
502 /// vice versa.
503 ///
504 /// Note that this value can change depending on user action (for example if the window is
505 /// moved to another screen); as such, tracking [`WindowEvent::ScaleFactorChanged`] events is
506 /// the most robust way to track the DPI you need to use to draw.
507 ///
508 /// This value may differ from [`MonitorHandle::scale_factor`].
509 ///
510 /// See the [`dpi`] crate for more information.
511 ///
512 /// ## Platform-specific
513 ///
514 /// The scale factor is calculated differently on different platforms:
515 ///
516 /// - **Windows:** On Windows 8 and 10, per-monitor scaling is readily configured by users from
517 /// the display settings. While users are free to select any option they want, they're only
518 /// given a selection of "nice" scale factors, i.e. 1.0, 1.25, 1.5... on Windows 7. The scale
519 /// factor is global and changing it requires logging out. See [this article][windows_1] for
520 /// technical details.
521 /// - **macOS:** Recent macOS versions allow the user to change the scaling factor for specific
522 /// displays. When available, the user may pick a per-monitor scaling factor from a set of
523 /// pre-defined settings. All "retina displays" have a scaling factor above 1.0 by default,
524 /// but the specific value varies across devices.
525 /// - **X11:** Many man-hours have been spent trying to figure out how to handle DPI in X11.
526 /// Winit currently uses a three-pronged approach:
527 /// + Use the value in the `WINIT_X11_SCALE_FACTOR` environment variable if present.
528 /// + If not present, use the value set in `Xft.dpi` in Xresources.
529 /// + Otherwise, calculate the scale factor based on the millimeter monitor dimensions
530 /// provided by XRandR.
531 ///
532 /// If `WINIT_X11_SCALE_FACTOR` is set to `randr`, it'll ignore the `Xft.dpi` field and use
533 /// the XRandR scaling method. Generally speaking, you should try to configure the
534 /// standard system variables to do what you want before resorting to
535 /// `WINIT_X11_SCALE_FACTOR`.
536 /// - **Wayland:** The scale factor is suggested by the compositor for each window individually
537 /// by using the wp-fractional-scale protocol if available. Falls back to integer-scale
538 /// factors otherwise.
539 ///
540 /// The monitor scale factor may differ from the window scale factor.
541 /// - **iOS:** Scale factors are set by Apple to the value that best suits the device, and range
542 /// from `1.0` to `3.0`. See [this article][apple_1] and [this article][apple_2] for more
543 /// information.
544 ///
545 /// This uses the underlying `UIView`'s [`contentScaleFactor`].
546 /// - **Android:** Scale factors are set by the manufacturer to the value that best suits the
547 /// device, and range from `1.0` to `4.0`. See [this article][android_1] for more information.
548 ///
549 /// This is currently unimplemented, and this function always returns 1.0.
550 /// - **Web:** The scale factor is the ratio between CSS pixels and the physical device pixels.
551 /// In other words, it is the value of [`window.devicePixelRatio`][web_1]. It is affected by
552 /// both the screen scaling and the browser zoom level and can go below `1.0`.
553 /// - **Orbital:** This is currently unimplemented, and this function always returns 1.0.
554 ///
555 /// [`WindowEvent::ScaleFactorChanged`]: crate::event::WindowEvent::ScaleFactorChanged
556 /// [windows_1]: https://docs.microsoft.com/en-us/windows/win32/hidpi/high-dpi-desktop-application-development-on-windows
557 /// [apple_1]: https://developer.apple.com/library/archive/documentation/DeviceInformation/Reference/iOSDeviceCompatibility/Displays/Displays.html
558 /// [apple_2]: https://developer.apple.com/design/human-interface-guidelines/macos/icons-and-images/image-size-and-resolution/
559 /// [android_1]: https://developer.android.com/training/multiscreen/screendensities
560 /// [web_1]: https://developer.mozilla.org/en-US/docs/Web/API/Window/devicePixelRatio
561 /// [`contentScaleFactor`]: https://developer.apple.com/documentation/uikit/uiview/1622657-contentscalefactor?language=objc
562 #[inline]
563 pub fn scale_factor(&self) -> f64 {
564 let _span = tracing::debug_span!("winit::Window::scale_factor",).entered();
565
566 self.window.maybe_wait_on_main(|w| w.scale_factor())
567 }
568
569 /// Queues a [`WindowEvent::RedrawRequested`] event to be emitted that aligns with the windowing
570 /// system drawing loop.
571 ///
572 /// This is the **strongly encouraged** method of redrawing windows, as it can integrate with
573 /// OS-requested redraws (e.g. when a window gets resized). To improve the event delivery
574 /// consider using [`Window::pre_present_notify`] as described in docs.
575 ///
576 /// Applications should always aim to redraw whenever they receive a `RedrawRequested` event.
577 ///
578 /// There are no strong guarantees about when exactly a `RedrawRequest` event will be emitted
579 /// with respect to other events, since the requirements can vary significantly between
580 /// windowing systems.
581 ///
582 /// However as the event aligns with the windowing system drawing loop, it may not arrive in
583 /// same or even next event loop iteration.
584 ///
585 /// ## Platform-specific
586 ///
587 /// - **Windows** This API uses `RedrawWindow` to request a `WM_PAINT` message and
588 /// `RedrawRequested` is emitted in sync with any `WM_PAINT` messages.
589 /// - **iOS:** Can only be called on the main thread.
590 /// - **Wayland:** The events are aligned with the frame callbacks when
591 /// [`Window::pre_present_notify`] is used.
592 /// - **Web:** [`WindowEvent::RedrawRequested`] will be aligned with the
593 /// `requestAnimationFrame`.
594 ///
595 /// [`WindowEvent::RedrawRequested`]: crate::event::WindowEvent::RedrawRequested
596 #[inline]
597 pub fn request_redraw(&self) {
598 let _span = tracing::debug_span!("winit::Window::request_redraw",).entered();
599
600 self.window.maybe_queue_on_main(|w| w.request_redraw())
601 }
602
603 /// Notify the windowing system before presenting to the window.
604 ///
605 /// You should call this event after your drawing operations, but before you submit
606 /// the buffer to the display or commit your drawings. Doing so will help winit to properly
607 /// schedule and make assumptions about its internal state. For example, it could properly
608 /// throttle [`WindowEvent::RedrawRequested`].
609 ///
610 /// ## Example
611 ///
612 /// This example illustrates how it looks with OpenGL, but it applies to other graphics
613 /// APIs and software rendering.
614 ///
615 /// ```no_run
616 /// # use winit::window::Window;
617 /// # fn swap_buffers() {}
618 /// # fn scope(window: &Window) {
619 /// // Do the actual drawing with OpenGL.
620 ///
621 /// // Notify winit that we're about to submit buffer to the windowing system.
622 /// window.pre_present_notify();
623 ///
624 /// // Submit buffer to the windowing system.
625 /// swap_buffers();
626 /// # }
627 /// ```
628 ///
629 /// ## Platform-specific
630 ///
631 /// - **Android / iOS / X11 / Web / Windows / macOS / Orbital:** Unsupported.
632 /// - **Wayland:** Schedules a frame callback to throttle [`WindowEvent::RedrawRequested`].
633 ///
634 /// [`WindowEvent::RedrawRequested`]: crate::event::WindowEvent::RedrawRequested
635 #[inline]
636 pub fn pre_present_notify(&self) {
637 let _span = tracing::debug_span!("winit::Window::pre_present_notify",).entered();
638
639 self.window.maybe_queue_on_main(|w| w.pre_present_notify());
640 }
641
642 /// Reset the dead key state of the keyboard.
643 ///
644 /// This is useful when a dead key is bound to trigger an action. Then
645 /// this function can be called to reset the dead key state so that
646 /// follow-up text input won't be affected by the dead key.
647 ///
648 /// ## Platform-specific
649 /// - **Web, macOS:** Does nothing
650 // ---------------------------
651 // Developers' Note: If this cannot be implemented on every desktop platform
652 // at least, then this function should be provided through a platform specific
653 // extension trait
654 pub fn reset_dead_keys(&self) {
655 let _span = tracing::debug_span!("winit::Window::reset_dead_keys",).entered();
656
657 self.window.maybe_queue_on_main(|w| w.reset_dead_keys())
658 }
659}
660
661/// Position and size functions.
662impl Window {
663 /// Returns the position of the top-left hand corner of the window's client area relative to the
664 /// top-left hand corner of the desktop.
665 ///
666 /// The same conditions that apply to [`Window::outer_position`] apply to this method.
667 ///
668 /// ## Platform-specific
669 ///
670 /// - **iOS:** Can only be called on the main thread. Returns the top left coordinates of the
671 /// window's [safe area] in the screen space coordinate system.
672 /// - **Web:** Returns the top-left coordinates relative to the viewport. _Note: this returns
673 /// the same value as [`Window::outer_position`]._
674 /// - **Android / Wayland:** Always returns [`NotSupportedError`].
675 ///
676 /// [safe area]: https://developer.apple.com/documentation/uikit/uiview/2891103-safeareainsets?language=objc
677 #[inline]
678 pub fn inner_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
679 let _span = tracing::debug_span!("winit::Window::inner_position",).entered();
680
681 self.window.maybe_wait_on_main(|w| w.inner_position())
682 }
683
684 /// Returns the position of the top-left hand corner of the window relative to the
685 /// top-left hand corner of the desktop.
686 ///
687 /// Note that the top-left hand corner of the desktop is not necessarily the same as
688 /// the screen. If the user uses a desktop with multiple monitors, the top-left hand corner
689 /// of the desktop is the top-left hand corner of the monitor at the top-left of the desktop.
690 ///
691 /// The coordinates can be negative if the top-left hand corner of the window is outside
692 /// of the visible screen region.
693 ///
694 /// ## Platform-specific
695 ///
696 /// - **iOS:** Can only be called on the main thread. Returns the top left coordinates of the
697 /// window in the screen space coordinate system.
698 /// - **Web:** Returns the top-left coordinates relative to the viewport.
699 /// - **Android / Wayland:** Always returns [`NotSupportedError`].
700 #[inline]
701 pub fn outer_position(&self) -> Result<PhysicalPosition<i32>, NotSupportedError> {
702 let _span = tracing::debug_span!("winit::Window::outer_position",).entered();
703
704 self.window.maybe_wait_on_main(|w| w.outer_position())
705 }
706
707 /// Modifies the position of the window.
708 ///
709 /// See [`Window::outer_position`] for more information about the coordinates.
710 /// This automatically un-maximizes the window if it's maximized.
711 ///
712 /// ```no_run
713 /// # use winit::dpi::{LogicalPosition, PhysicalPosition};
714 /// # use winit::window::Window;
715 /// # fn scope(window: &Window) {
716 /// // Specify the position in logical dimensions like this:
717 /// window.set_outer_position(LogicalPosition::new(400.0, 200.0));
718 ///
719 /// // Or specify the position in physical dimensions like this:
720 /// window.set_outer_position(PhysicalPosition::new(400, 200));
721 /// # }
722 /// ```
723 ///
724 /// ## Platform-specific
725 ///
726 /// - **iOS:** Can only be called on the main thread. Sets the top left coordinates of the
727 /// window in the screen space coordinate system.
728 /// - **Web:** Sets the top-left coordinates relative to the viewport. Doesn't account for CSS
729 /// [`transform`].
730 /// - **Android / Wayland:** Unsupported.
731 ///
732 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
733 #[inline]
734 pub fn set_outer_position<P: Into<Position>>(&self, position: P) {
735 let position = position.into();
736 let _span = tracing::debug_span!(
737 "winit::Window::set_outer_position",
738 position = ?position
739 )
740 .entered();
741
742 self.window.maybe_queue_on_main(move |w| w.set_outer_position(position))
743 }
744
745 /// Returns the physical size of the window's client area.
746 ///
747 /// The client area is the content of the window, excluding the title bar and borders.
748 ///
749 /// ## Platform-specific
750 ///
751 /// - **iOS:** Can only be called on the main thread. Returns the `PhysicalSize` of the window's
752 /// [safe area] in screen space coordinates.
753 /// - **Web:** Returns the size of the canvas element. Doesn't account for CSS [`transform`].
754 ///
755 /// [safe area]: https://developer.apple.com/documentation/uikit/uiview/2891103-safeareainsets?language=objc
756 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
757 #[inline]
758 pub fn inner_size(&self) -> PhysicalSize<u32> {
759 let _span = tracing::debug_span!("winit::Window::inner_size",).entered();
760
761 self.window.maybe_wait_on_main(|w| w.inner_size())
762 }
763
764 /// Request the new size for the window.
765 ///
766 /// On platforms where the size is entirely controlled by the user the
767 /// applied size will be returned immediately, resize event in such case
768 /// may not be generated.
769 ///
770 /// On platforms where resizing is disallowed by the windowing system, the current
771 /// inner size is returned immediately, and the user one is ignored.
772 ///
773 /// When `None` is returned, it means that the request went to the display system,
774 /// and the actual size will be delivered later with the [`WindowEvent::Resized`].
775 ///
776 /// See [`Window::inner_size`] for more information about the values.
777 ///
778 /// The request could automatically un-maximize the window if it's maximized.
779 ///
780 /// ```no_run
781 /// # use winit::dpi::{LogicalSize, PhysicalSize};
782 /// # use winit::window::Window;
783 /// # fn scope(window: &Window) {
784 /// // Specify the size in logical dimensions like this:
785 /// let _ = window.request_inner_size(LogicalSize::new(400.0, 200.0));
786 ///
787 /// // Or specify the size in physical dimensions like this:
788 /// let _ = window.request_inner_size(PhysicalSize::new(400, 200));
789 /// # }
790 /// ```
791 ///
792 /// ## Platform-specific
793 ///
794 /// - **Web:** Sets the size of the canvas element. Doesn't account for CSS [`transform`].
795 ///
796 /// [`WindowEvent::Resized`]: crate::event::WindowEvent::Resized
797 /// [`transform`]: https://developer.mozilla.org/en-US/docs/Web/CSS/transform
798 #[inline]
799 #[must_use]
800 pub fn request_inner_size<S: Into<Size>>(&self, size: S) -> Option<PhysicalSize<u32>> {
801 let size = size.into();
802 let _span = tracing::debug_span!(
803 "winit::Window::request_inner_size",
804 size = ?size
805 )
806 .entered();
807 self.window.maybe_wait_on_main(|w| w.request_inner_size(size))
808 }
809
810 /// Returns the physical size of the entire window.
811 ///
812 /// These dimensions include the title bar and borders. If you don't want that (and you usually
813 /// don't), use [`Window::inner_size`] instead.
814 ///
815 /// ## Platform-specific
816 ///
817 /// - **iOS:** Can only be called on the main thread. Returns the [`PhysicalSize`] of the window
818 /// in screen space coordinates.
819 /// - **Web:** Returns the size of the canvas element. _Note: this returns the same value as
820 /// [`Window::inner_size`]._
821 #[inline]
822 pub fn outer_size(&self) -> PhysicalSize<u32> {
823 let _span = tracing::debug_span!("winit::Window::outer_size",).entered();
824 self.window.maybe_wait_on_main(|w| w.outer_size())
825 }
826
827 /// Sets a minimum dimension size for the window.
828 ///
829 /// ```no_run
830 /// # use winit::dpi::{LogicalSize, PhysicalSize};
831 /// # use winit::window::Window;
832 /// # fn scope(window: &Window) {
833 /// // Specify the size in logical dimensions like this:
834 /// window.set_min_inner_size(Some(LogicalSize::new(400.0, 200.0)));
835 ///
836 /// // Or specify the size in physical dimensions like this:
837 /// window.set_min_inner_size(Some(PhysicalSize::new(400, 200)));
838 /// # }
839 /// ```
840 ///
841 /// ## Platform-specific
842 ///
843 /// - **iOS / Android / Orbital:** Unsupported.
844 #[inline]
845 pub fn set_min_inner_size<S: Into<Size>>(&self, min_size: Option<S>) {
846 let min_size = min_size.map(|s| s.into());
847 let _span = tracing::debug_span!(
848 "winit::Window::set_min_inner_size",
849 min_size = ?min_size
850 )
851 .entered();
852 self.window.maybe_queue_on_main(move |w| w.set_min_inner_size(min_size))
853 }
854
855 /// Sets a maximum dimension size for the window.
856 ///
857 /// ```no_run
858 /// # use winit::dpi::{LogicalSize, PhysicalSize};
859 /// # use winit::window::Window;
860 /// # fn scope(window: &Window) {
861 /// // Specify the size in logical dimensions like this:
862 /// window.set_max_inner_size(Some(LogicalSize::new(400.0, 200.0)));
863 ///
864 /// // Or specify the size in physical dimensions like this:
865 /// window.set_max_inner_size(Some(PhysicalSize::new(400, 200)));
866 /// # }
867 /// ```
868 ///
869 /// ## Platform-specific
870 ///
871 /// - **iOS / Android / Orbital:** Unsupported.
872 #[inline]
873 pub fn set_max_inner_size<S: Into<Size>>(&self, max_size: Option<S>) {
874 let max_size = max_size.map(|s| s.into());
875 let _span = tracing::debug_span!(
876 "winit::Window::max_size",
877 max_size = ?max_size
878 )
879 .entered();
880 self.window.maybe_queue_on_main(move |w| w.set_max_inner_size(max_size))
881 }
882
883 /// Returns window resize increments if any were set.
884 ///
885 /// ## Platform-specific
886 ///
887 /// - **iOS / Android / Web / Orbital:** Always returns [`None`].
888 #[inline]
889 pub fn resize_increments(&self) -> Option<PhysicalSize<u32>> {
890 let _span = tracing::debug_span!("winit::Window::resize_increments",).entered();
891 self.window.maybe_wait_on_main(|w| w.resize_increments())
892 }
893
894 /// Sets window resize increments.
895 ///
896 /// This is a niche constraint hint usually employed by terminal emulators
897 /// and other apps that need "blocky" resizes.
898 ///
899 /// ## Platform-specific
900 ///
901 /// - **macOS:** Increments are converted to logical size and then macOS rounds them to whole
902 /// numbers.
903 /// - **iOS / Android / Web / Orbital:** Unsupported.
904 #[inline]
905 pub fn set_resize_increments<S: Into<Size>>(&self, increments: Option<S>) {
906 let increments = increments.map(Into::into);
907 let _span = tracing::debug_span!(
908 "winit::Window::set_resize_increments",
909 increments = ?increments
910 )
911 .entered();
912 self.window.maybe_queue_on_main(move |w| w.set_resize_increments(increments))
913 }
914}
915
916/// Misc. attribute functions.
917impl Window {
918 /// Modifies the title of the window.
919 ///
920 /// ## Platform-specific
921 ///
922 /// - **iOS / Android:** Unsupported.
923 #[inline]
924 pub fn set_title(&self, title: &str) {
925 let _span = tracing::debug_span!("winit::Window::set_title", title).entered();
926 self.window.maybe_wait_on_main(|w| w.set_title(title))
927 }
928
929 /// Change the window transparency state.
930 ///
931 /// This is just a hint that may not change anything about
932 /// the window transparency, however doing a mismatch between
933 /// the content of your window and this hint may result in
934 /// visual artifacts.
935 ///
936 /// The default value follows the [`WindowAttributes::with_transparent`].
937 ///
938 /// ## Platform-specific
939 ///
940 /// - **macOS:** This will reset the window's background color.
941 /// - **Web / iOS / Android:** Unsupported.
942 /// - **X11:** Can only be set while building the window, with
943 /// [`WindowAttributes::with_transparent`].
944 #[inline]
945 pub fn set_transparent(&self, transparent: bool) {
946 let _span = tracing::debug_span!("winit::Window::set_transparent", transparent).entered();
947 self.window.maybe_queue_on_main(move |w| w.set_transparent(transparent))
948 }
949
950 /// Change the window blur state.
951 ///
952 /// If `true`, this will make the transparent window background blurry.
953 ///
954 /// ## Platform-specific
955 ///
956 /// - **Android / iOS / X11 / Web / Windows:** Unsupported.
957 /// - **Wayland:** Only works with org_kde_kwin_blur_manager protocol.
958 #[inline]
959 pub fn set_blur(&self, blur: bool) {
960 let _span = tracing::debug_span!("winit::Window::set_blur", blur).entered();
961 self.window.maybe_queue_on_main(move |w| w.set_blur(blur))
962 }
963
964 /// Modifies the window's visibility.
965 ///
966 /// If `false`, this will hide the window. If `true`, this will show the window.
967 ///
968 /// ## Platform-specific
969 ///
970 /// - **Android / Wayland / Web:** Unsupported.
971 /// - **iOS:** Can only be called on the main thread.
972 #[inline]
973 pub fn set_visible(&self, visible: bool) {
974 let _span = tracing::debug_span!("winit::Window::set_visible", visible).entered();
975 self.window.maybe_queue_on_main(move |w| w.set_visible(visible))
976 }
977
978 /// Gets the window's current visibility state.
979 ///
980 /// `None` means it couldn't be determined, so it is not recommended to use this to drive your
981 /// rendering backend.
982 ///
983 /// ## Platform-specific
984 ///
985 /// - **X11:** Not implemented.
986 /// - **Wayland / iOS / Android / Web:** Unsupported.
987 #[inline]
988 pub fn is_visible(&self) -> Option<bool> {
989 let _span = tracing::debug_span!("winit::Window::is_visible",).entered();
990 self.window.maybe_wait_on_main(|w| w.is_visible())
991 }
992
993 /// Sets whether the window is resizable or not.
994 ///
995 /// Note that making the window unresizable doesn't exempt you from handling
996 /// [`WindowEvent::Resized`], as that event can still be triggered by DPI scaling, entering
997 /// fullscreen mode, etc. Also, the window could still be resized by calling
998 /// [`Window::request_inner_size`].
999 ///
1000 /// ## Platform-specific
1001 ///
1002 /// This only has an effect on desktop platforms.
1003 ///
1004 /// - **X11:** Due to a bug in XFCE, this has no effect on Xfwm.
1005 /// - **iOS / Android / Web:** Unsupported.
1006 ///
1007 /// [`WindowEvent::Resized`]: crate::event::WindowEvent::Resized
1008 #[inline]
1009 pub fn set_resizable(&self, resizable: bool) {
1010 let _span = tracing::debug_span!("winit::Window::set_resizable", resizable).entered();
1011 self.window.maybe_queue_on_main(move |w| w.set_resizable(resizable))
1012 }
1013
1014 /// Gets the window's current resizable state.
1015 ///
1016 /// ## Platform-specific
1017 ///
1018 /// - **X11:** Not implemented.
1019 /// - **iOS / Android / Web:** Unsupported.
1020 #[inline]
1021 pub fn is_resizable(&self) -> bool {
1022 let _span = tracing::debug_span!("winit::Window::is_resizable",).entered();
1023 self.window.maybe_wait_on_main(|w| w.is_resizable())
1024 }
1025
1026 /// Sets the enabled window buttons.
1027 ///
1028 /// ## Platform-specific
1029 ///
1030 /// - **Wayland / X11 / Orbital:** Not implemented.
1031 /// - **Web / iOS / Android:** Unsupported.
1032 pub fn set_enabled_buttons(&self, buttons: WindowButtons) {
1033 let _span = tracing::debug_span!(
1034 "winit::Window::set_enabled_buttons",
1035 buttons = ?buttons
1036 )
1037 .entered();
1038 self.window.maybe_queue_on_main(move |w| w.set_enabled_buttons(buttons))
1039 }
1040
1041 /// Gets the enabled window buttons.
1042 ///
1043 /// ## Platform-specific
1044 ///
1045 /// - **Wayland / X11 / Orbital:** Not implemented. Always returns [`WindowButtons::all`].
1046 /// - **Web / iOS / Android:** Unsupported. Always returns [`WindowButtons::all`].
1047 pub fn enabled_buttons(&self) -> WindowButtons {
1048 let _span = tracing::debug_span!("winit::Window::enabled_buttons",).entered();
1049 self.window.maybe_wait_on_main(|w| w.enabled_buttons())
1050 }
1051
1052 /// Sets the window to minimized or back
1053 ///
1054 /// ## Platform-specific
1055 ///
1056 /// - **iOS / Android / Web / Orbital:** Unsupported.
1057 /// - **Wayland:** Un-minimize is unsupported.
1058 #[inline]
1059 pub fn set_minimized(&self, minimized: bool) {
1060 let _span = tracing::debug_span!("winit::Window::set_minimized", minimized).entered();
1061 self.window.maybe_queue_on_main(move |w| w.set_minimized(minimized))
1062 }
1063
1064 /// Gets the window's current minimized state.
1065 ///
1066 /// `None` will be returned, if the minimized state couldn't be determined.
1067 ///
1068 /// ## Note
1069 ///
1070 /// - You shouldn't stop rendering for minimized windows, however you could lower the fps.
1071 ///
1072 /// ## Platform-specific
1073 ///
1074 /// - **Wayland**: always `None`.
1075 /// - **iOS / Android / Web / Orbital:** Unsupported.
1076 #[inline]
1077 pub fn is_minimized(&self) -> Option<bool> {
1078 let _span = tracing::debug_span!("winit::Window::is_minimized",).entered();
1079 self.window.maybe_wait_on_main(|w| w.is_minimized())
1080 }
1081
1082 /// Sets the window to maximized or back.
1083 ///
1084 /// ## Platform-specific
1085 ///
1086 /// - **iOS / Android / Web:** Unsupported.
1087 #[inline]
1088 pub fn set_maximized(&self, maximized: bool) {
1089 let _span = tracing::debug_span!("winit::Window::set_maximized", maximized).entered();
1090 self.window.maybe_queue_on_main(move |w| w.set_maximized(maximized))
1091 }
1092
1093 /// Gets the window's current maximized state.
1094 ///
1095 /// ## Platform-specific
1096 ///
1097 /// - **iOS / Android / Web:** Unsupported.
1098 #[inline]
1099 pub fn is_maximized(&self) -> bool {
1100 let _span = tracing::debug_span!("winit::Window::is_maximized",).entered();
1101 self.window.maybe_wait_on_main(|w| w.is_maximized())
1102 }
1103
1104 /// Sets the window to fullscreen or back.
1105 ///
1106 /// ## Platform-specific
1107 ///
1108 /// - **macOS:** [`Fullscreen::Exclusive`] provides true exclusive mode with a video mode
1109 /// change. *Caveat!* macOS doesn't provide task switching (or spaces!) while in exclusive
1110 /// fullscreen mode. This mode should be used when a video mode change is desired, but for a
1111 /// better user experience, borderless fullscreen might be preferred.
1112 ///
1113 /// [`Fullscreen::Borderless`] provides a borderless fullscreen window on a
1114 /// separate space. This is the idiomatic way for fullscreen games to work
1115 /// on macOS. See `WindowExtMacOs::set_simple_fullscreen` if
1116 /// separate spaces are not preferred.
1117 ///
1118 /// The dock and the menu bar are disabled in exclusive fullscreen mode.
1119 /// - **iOS:** Can only be called on the main thread.
1120 /// - **Wayland:** Does not support exclusive fullscreen mode and will no-op a request.
1121 /// - **Windows:** Screen saver is disabled in fullscreen mode.
1122 /// - **Android / Orbital:** Unsupported.
1123 /// - **Web:** Does nothing without a [transient activation].
1124 ///
1125 /// [transient activation]: https://developer.mozilla.org/en-US/docs/Glossary/Transient_activation
1126 #[inline]
1127 pub fn set_fullscreen(&self, fullscreen: Option<Fullscreen>) {
1128 let _span = tracing::debug_span!(
1129 "winit::Window::set_fullscreen",
1130 fullscreen = ?fullscreen
1131 )
1132 .entered();
1133 self.window.maybe_queue_on_main(move |w| w.set_fullscreen(fullscreen.map(|f| f.into())))
1134 }
1135
1136 /// Gets the window's current fullscreen state.
1137 ///
1138 /// ## Platform-specific
1139 ///
1140 /// - **iOS:** Can only be called on the main thread.
1141 /// - **Android / Orbital:** Will always return `None`.
1142 /// - **Wayland:** Can return `Borderless(None)` when there are no monitors.
1143 /// - **Web:** Can only return `None` or `Borderless(None)`.
1144 #[inline]
1145 pub fn fullscreen(&self) -> Option<Fullscreen> {
1146 let _span = tracing::debug_span!("winit::Window::fullscreen",).entered();
1147 self.window.maybe_wait_on_main(|w| w.fullscreen().map(|f| f.into()))
1148 }
1149
1150 /// Turn window decorations on or off.
1151 ///
1152 /// Enable/disable window decorations provided by the server or Winit.
1153 /// By default this is enabled. Note that fullscreen windows and windows on
1154 /// mobile and web platforms naturally do not have decorations.
1155 ///
1156 /// ## Platform-specific
1157 ///
1158 /// - **iOS / Android / Web:** No effect.
1159 #[inline]
1160 pub fn set_decorations(&self, decorations: bool) {
1161 let _span = tracing::debug_span!("winit::Window::set_decorations", decorations).entered();
1162 self.window.maybe_queue_on_main(move |w| w.set_decorations(decorations))
1163 }
1164
1165 /// Gets the window's current decorations state.
1166 ///
1167 /// Returns `true` when windows are decorated (server-side or by Winit).
1168 /// Also returns `true` when no decorations are required (mobile, web).
1169 ///
1170 /// ## Platform-specific
1171 ///
1172 /// - **iOS / Android / Web:** Always returns `true`.
1173 #[inline]
1174 pub fn is_decorated(&self) -> bool {
1175 let _span = tracing::debug_span!("winit::Window::is_decorated",).entered();
1176 self.window.maybe_wait_on_main(|w| w.is_decorated())
1177 }
1178
1179 /// Change the window level.
1180 ///
1181 /// This is just a hint to the OS, and the system could ignore it.
1182 ///
1183 /// See [`WindowLevel`] for details.
1184 pub fn set_window_level(&self, level: WindowLevel) {
1185 let _span = tracing::debug_span!(
1186 "winit::Window::set_window_level",
1187 level = ?level
1188 )
1189 .entered();
1190 self.window.maybe_queue_on_main(move |w| w.set_window_level(level))
1191 }
1192
1193 /// Sets the window icon.
1194 ///
1195 /// On Windows and X11, this is typically the small icon in the top-left
1196 /// corner of the titlebar.
1197 ///
1198 /// ## Platform-specific
1199 ///
1200 /// - **iOS / Android / Web / Wayland / macOS / Orbital:** Unsupported.
1201 ///
1202 /// - **Windows:** Sets `ICON_SMALL`. The base size for a window icon is 16x16, but it's
1203 /// recommended to account for screen scaling and pick a multiple of that, i.e. 32x32.
1204 ///
1205 /// - **X11:** Has no universal guidelines for icon sizes, so you're at the whims of the WM.
1206 /// That said, it's usually in the same ballpark as on Windows.
1207 #[inline]
1208 pub fn set_window_icon(&self, window_icon: Option<Icon>) {
1209 let _span = tracing::debug_span!("winit::Window::set_window_icon",).entered();
1210 self.window.maybe_queue_on_main(move |w| w.set_window_icon(window_icon))
1211 }
1212
1213 /// Set the IME cursor editing area, where the `position` is the top left corner of that area
1214 /// and `size` is the size of this area starting from the position. An example of such area
1215 /// could be a input field in the UI or line in the editor.
1216 ///
1217 /// The windowing system could place a candidate box close to that area, but try to not obscure
1218 /// the specified area, so the user input to it stays visible.
1219 ///
1220 /// The candidate box is the window / popup / overlay that allows you to select the desired
1221 /// characters. The look of this box may differ between input devices, even on the same
1222 /// platform.
1223 ///
1224 /// (Apple's official term is "candidate window", see their [chinese] and [japanese] guides).
1225 ///
1226 /// ## Example
1227 ///
1228 /// ```no_run
1229 /// # use winit::dpi::{LogicalPosition, PhysicalPosition, LogicalSize, PhysicalSize};
1230 /// # use winit::window::Window;
1231 /// # fn scope(window: &Window) {
1232 /// // Specify the position in logical dimensions like this:
1233 /// window.set_ime_cursor_area(LogicalPosition::new(400.0, 200.0), LogicalSize::new(100, 100));
1234 ///
1235 /// // Or specify the position in physical dimensions like this:
1236 /// window.set_ime_cursor_area(PhysicalPosition::new(400, 200), PhysicalSize::new(100, 100));
1237 /// # }
1238 /// ```
1239 ///
1240 /// ## Platform-specific
1241 ///
1242 /// - **X11:** - area is not supported, only position.
1243 /// - **iOS / Android / Web / Orbital:** Unsupported.
1244 ///
1245 /// [chinese]: https://support.apple.com/guide/chinese-input-method/use-the-candidate-window-cim12992/104/mac/12.0
1246 /// [japanese]: https://support.apple.com/guide/japanese-input-method/use-the-candidate-window-jpim10262/6.3/mac/12.0
1247 #[inline]
1248 pub fn set_ime_cursor_area<P: Into<Position>, S: Into<Size>>(&self, position: P, size: S) {
1249 let position = position.into();
1250 let size = size.into();
1251 let _span = tracing::debug_span!(
1252 "winit::Window::set_ime_cursor_area",
1253 position = ?position,
1254 size = ?size,
1255 )
1256 .entered();
1257 self.window.maybe_queue_on_main(move |w| w.set_ime_cursor_area(position, size))
1258 }
1259
1260 /// Sets whether the window should get IME events
1261 ///
1262 /// When IME is allowed, the window will receive [`Ime`] events, and during the
1263 /// preedit phase the window will NOT get [`KeyboardInput`] events. The window
1264 /// should allow IME while it is expecting text input.
1265 ///
1266 /// When IME is not allowed, the window won't receive [`Ime`] events, and will
1267 /// receive [`KeyboardInput`] events for every keypress instead. Not allowing
1268 /// IME is useful for games for example.
1269 ///
1270 /// IME is **not** allowed by default.
1271 ///
1272 /// ## Platform-specific
1273 ///
1274 /// - **macOS:** IME must be enabled to receive text-input where dead-key sequences are
1275 /// combined.
1276 /// - **iOS / Android:** This will show / hide the soft keyboard.
1277 /// - **Web / Orbital:** Unsupported.
1278 /// - **X11**: Enabling IME will disable dead keys reporting during compose.
1279 ///
1280 /// [`Ime`]: crate::event::WindowEvent::Ime
1281 /// [`KeyboardInput`]: crate::event::WindowEvent::KeyboardInput
1282 #[inline]
1283 pub fn set_ime_allowed(&self, allowed: bool) {
1284 let _span = tracing::debug_span!("winit::Window::set_ime_allowed", allowed).entered();
1285 self.window.maybe_queue_on_main(move |w| w.set_ime_allowed(allowed))
1286 }
1287
1288 /// Sets the IME purpose for the window using [`ImePurpose`].
1289 ///
1290 /// ## Platform-specific
1291 ///
1292 /// - **iOS / Android / Web / Windows / X11 / macOS / Orbital:** Unsupported.
1293 #[inline]
1294 pub fn set_ime_purpose(&self, purpose: ImePurpose) {
1295 let _span = tracing::debug_span!(
1296 "winit::Window::set_ime_purpose",
1297 purpose = ?purpose
1298 )
1299 .entered();
1300 self.window.maybe_queue_on_main(move |w| w.set_ime_purpose(purpose))
1301 }
1302
1303 /// Brings the window to the front and sets input focus. Has no effect if the window is
1304 /// already in focus, minimized, or not visible.
1305 ///
1306 /// This method steals input focus from other applications. Do not use this method unless
1307 /// you are certain that's what the user wants. Focus stealing can cause an extremely disruptive
1308 /// user experience.
1309 ///
1310 /// ## Platform-specific
1311 ///
1312 /// - **iOS / Android / Wayland / Orbital:** Unsupported.
1313 #[inline]
1314 pub fn focus_window(&self) {
1315 let _span = tracing::debug_span!("winit::Window::focus_window",).entered();
1316 self.window.maybe_queue_on_main(|w| w.focus_window())
1317 }
1318
1319 /// Gets whether the window has keyboard focus.
1320 ///
1321 /// This queries the same state information as [`WindowEvent::Focused`].
1322 ///
1323 /// [`WindowEvent::Focused`]: crate::event::WindowEvent::Focused
1324 #[inline]
1325 pub fn has_focus(&self) -> bool {
1326 let _span = tracing::debug_span!("winit::Window::has_focus",).entered();
1327 self.window.maybe_wait_on_main(|w| w.has_focus())
1328 }
1329
1330 /// Requests user attention to the window, this has no effect if the application
1331 /// is already focused. How requesting for user attention manifests is platform dependent,
1332 /// see [`UserAttentionType`] for details.
1333 ///
1334 /// Providing `None` will unset the request for user attention. Unsetting the request for
1335 /// user attention might not be done automatically by the WM when the window receives input.
1336 ///
1337 /// ## Platform-specific
1338 ///
1339 /// - **iOS / Android / Web / Orbital:** Unsupported.
1340 /// - **macOS:** `None` has no effect.
1341 /// - **X11:** Requests for user attention must be manually cleared.
1342 /// - **Wayland:** Requires `xdg_activation_v1` protocol, `None` has no effect.
1343 #[inline]
1344 pub fn request_user_attention(&self, request_type: Option<UserAttentionType>) {
1345 let _span = tracing::debug_span!(
1346 "winit::Window::request_user_attention",
1347 request_type = ?request_type
1348 )
1349 .entered();
1350 self.window.maybe_queue_on_main(move |w| w.request_user_attention(request_type))
1351 }
1352
1353 /// Set or override the window theme.
1354 ///
1355 /// Specify `None` to reset the theme to the system default.
1356 ///
1357 /// ## Platform-specific
1358 ///
1359 /// - **Wayland:** Sets the theme for the client side decorations. Using `None` will use dbus to
1360 /// get the system preference.
1361 /// - **X11:** Sets `_GTK_THEME_VARIANT` hint to `dark` or `light` and if `None` is used, it
1362 /// will default to [`Theme::Dark`].
1363 /// - **iOS / Android / Web / Orbital:** Unsupported.
1364 #[inline]
1365 pub fn set_theme(&self, theme: Option<Theme>) {
1366 let _span = tracing::debug_span!(
1367 "winit::Window::set_theme",
1368 theme = ?theme
1369 )
1370 .entered();
1371 self.window.maybe_queue_on_main(move |w| w.set_theme(theme))
1372 }
1373
1374 /// Returns the current window theme.
1375 ///
1376 /// Returns `None` if it cannot be determined on the current platform.
1377 ///
1378 /// ## Platform-specific
1379 ///
1380 /// - **iOS / Android / x11 / Orbital:** Unsupported.
1381 /// - **Wayland:** Only returns theme overrides.
1382 #[inline]
1383 pub fn theme(&self) -> Option<Theme> {
1384 let _span = tracing::debug_span!("winit::Window::theme",).entered();
1385 self.window.maybe_wait_on_main(|w| w.theme())
1386 }
1387
1388 /// Prevents the window contents from being captured by other apps.
1389 ///
1390 /// ## Platform-specific
1391 ///
1392 /// - **macOS**: if `false`, [`NSWindowSharingNone`] is used but doesn't completely prevent all
1393 /// apps from reading the window content, for instance, QuickTime.
1394 /// - **iOS / Android / x11 / Wayland / Web / Orbital:** Unsupported.
1395 ///
1396 /// [`NSWindowSharingNone`]: https://developer.apple.com/documentation/appkit/nswindowsharingtype/nswindowsharingnone
1397 pub fn set_content_protected(&self, protected: bool) {
1398 let _span =
1399 tracing::debug_span!("winit::Window::set_content_protected", protected).entered();
1400 self.window.maybe_queue_on_main(move |w| w.set_content_protected(protected))
1401 }
1402
1403 /// Gets the current title of the window.
1404 ///
1405 /// ## Platform-specific
1406 ///
1407 /// - **iOS / Android / x11 / Wayland / Web:** Unsupported. Always returns an empty string.
1408 #[inline]
1409 pub fn title(&self) -> String {
1410 let _span = tracing::debug_span!("winit::Window::title",).entered();
1411 self.window.maybe_wait_on_main(|w| w.title())
1412 }
1413}
1414
1415/// Cursor functions.
1416impl Window {
1417 /// Modifies the cursor icon of the window.
1418 ///
1419 /// ## Platform-specific
1420 ///
1421 /// - **iOS / Android / Orbital:** Unsupported.
1422 /// - **Web:** Custom cursors have to be loaded and decoded first, until then the previous
1423 /// cursor is shown.
1424 #[inline]
1425 pub fn set_cursor(&self, cursor: impl Into<Cursor>) {
1426 let cursor = cursor.into();
1427 let _span = tracing::debug_span!("winit::Window::set_cursor",).entered();
1428 self.window.maybe_queue_on_main(move |w| w.set_cursor(cursor))
1429 }
1430
1431 /// Deprecated! Use [`Window::set_cursor()`] instead.
1432 #[deprecated = "Renamed to `set_cursor`"]
1433 #[inline]
1434 pub fn set_cursor_icon(&self, icon: CursorIcon) {
1435 self.set_cursor(icon)
1436 }
1437
1438 /// Changes the position of the cursor in window coordinates.
1439 ///
1440 /// ```no_run
1441 /// # use winit::dpi::{LogicalPosition, PhysicalPosition};
1442 /// # use winit::window::Window;
1443 /// # fn scope(window: &Window) {
1444 /// // Specify the position in logical dimensions like this:
1445 /// window.set_cursor_position(LogicalPosition::new(400.0, 200.0));
1446 ///
1447 /// // Or specify the position in physical dimensions like this:
1448 /// window.set_cursor_position(PhysicalPosition::new(400, 200));
1449 /// # }
1450 /// ```
1451 ///
1452 /// ## Platform-specific
1453 ///
1454 /// - **Wayland**: Cursor must be in [`CursorGrabMode::Locked`].
1455 /// - **iOS / Android / Web / Orbital:** Always returns an [`ExternalError::NotSupported`].
1456 #[inline]
1457 pub fn set_cursor_position<P: Into<Position>>(&self, position: P) -> Result<(), ExternalError> {
1458 let position = position.into();
1459 let _span = tracing::debug_span!(
1460 "winit::Window::set_cursor_position",
1461 position = ?position
1462 )
1463 .entered();
1464 self.window.maybe_wait_on_main(|w| w.set_cursor_position(position))
1465 }
1466
1467 /// Set grabbing [mode][CursorGrabMode] on the cursor preventing it from leaving the window.
1468 ///
1469 /// # Example
1470 ///
1471 /// First try confining the cursor, and if that fails, try locking it instead.
1472 ///
1473 /// ```no_run
1474 /// # use winit::window::{CursorGrabMode, Window};
1475 /// # fn scope(window: &Window) {
1476 /// window
1477 /// .set_cursor_grab(CursorGrabMode::Confined)
1478 /// .or_else(|_e| window.set_cursor_grab(CursorGrabMode::Locked))
1479 /// .unwrap();
1480 /// # }
1481 /// ```
1482 #[inline]
1483 pub fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ExternalError> {
1484 let _span = tracing::debug_span!(
1485 "winit::Window::set_cursor_grab",
1486 mode = ?mode
1487 )
1488 .entered();
1489 self.window.maybe_wait_on_main(|w| w.set_cursor_grab(mode))
1490 }
1491
1492 /// Modifies the cursor's visibility.
1493 ///
1494 /// If `false`, this will hide the cursor. If `true`, this will show the cursor.
1495 ///
1496 /// ## Platform-specific
1497 ///
1498 /// - **Windows:** The cursor is only hidden within the confines of the window.
1499 /// - **X11:** The cursor is only hidden within the confines of the window.
1500 /// - **Wayland:** The cursor is only hidden within the confines of the window.
1501 /// - **macOS:** The cursor is hidden as long as the window has input focus, even if the cursor
1502 /// is outside of the window.
1503 /// - **iOS / Android:** Unsupported.
1504 #[inline]
1505 pub fn set_cursor_visible(&self, visible: bool) {
1506 let _span = tracing::debug_span!("winit::Window::set_cursor_visible", visible).entered();
1507 self.window.maybe_queue_on_main(move |w| w.set_cursor_visible(visible))
1508 }
1509
1510 /// Moves the window with the left mouse button until the button is released.
1511 ///
1512 /// There's no guarantee that this will work unless the left mouse button was pressed
1513 /// immediately before this function is called.
1514 ///
1515 /// ## Platform-specific
1516 ///
1517 /// - **X11:** Un-grabs the cursor.
1518 /// - **Wayland:** Requires the cursor to be inside the window to be dragged.
1519 /// - **macOS:** May prevent the button release event to be triggered.
1520 /// - **iOS / Android / Web:** Always returns an [`ExternalError::NotSupported`].
1521 #[inline]
1522 pub fn drag_window(&self) -> Result<(), ExternalError> {
1523 let _span = tracing::debug_span!("winit::Window::drag_window",).entered();
1524 self.window.maybe_wait_on_main(|w| w.drag_window())
1525 }
1526
1527 /// Resizes the window with the left mouse button until the button is released.
1528 ///
1529 /// There's no guarantee that this will work unless the left mouse button was pressed
1530 /// immediately before this function is called.
1531 ///
1532 /// ## Platform-specific
1533 ///
1534 /// - **macOS:** Always returns an [`ExternalError::NotSupported`]
1535 /// - **iOS / Android / Web:** Always returns an [`ExternalError::NotSupported`].
1536 #[inline]
1537 pub fn drag_resize_window(&self, direction: ResizeDirection) -> Result<(), ExternalError> {
1538 let _span = tracing::debug_span!(
1539 "winit::Window::drag_resize_window",
1540 direction = ?direction
1541 )
1542 .entered();
1543 self.window.maybe_wait_on_main(|w| w.drag_resize_window(direction))
1544 }
1545
1546 /// Show [window menu] at a specified position .
1547 ///
1548 /// This is the context menu that is normally shown when interacting with
1549 /// the title bar. This is useful when implementing custom decorations.
1550 ///
1551 /// ## Platform-specific
1552 /// **Android / iOS / macOS / Orbital / Wayland / Web / X11:** Unsupported.
1553 ///
1554 /// [window menu]: https://en.wikipedia.org/wiki/Common_menus_in_Microsoft_Windows#System_menu
1555 pub fn show_window_menu(&self, position: impl Into<Position>) {
1556 let position = position.into();
1557 let _span = tracing::debug_span!(
1558 "winit::Window::show_window_menu",
1559 position = ?position
1560 )
1561 .entered();
1562 self.window.maybe_queue_on_main(move |w| w.show_window_menu(position))
1563 }
1564
1565 /// Modifies whether the window catches cursor events.
1566 ///
1567 /// If `true`, the window will catch the cursor events. If `false`, events are passed through
1568 /// the window such that any other window behind it receives them. By default hittest is
1569 /// enabled.
1570 ///
1571 /// ## Platform-specific
1572 ///
1573 /// - **iOS / Android / Web / Orbital:** Always returns an [`ExternalError::NotSupported`].
1574 #[inline]
1575 pub fn set_cursor_hittest(&self, hittest: bool) -> Result<(), ExternalError> {
1576 let _span = tracing::debug_span!("winit::Window::set_cursor_hittest", hittest).entered();
1577 self.window.maybe_wait_on_main(|w| w.set_cursor_hittest(hittest))
1578 }
1579}
1580
1581/// Monitor info functions.
1582impl Window {
1583 /// Returns the monitor on which the window currently resides.
1584 ///
1585 /// Returns `None` if current monitor can't be detected.
1586 #[inline]
1587 pub fn current_monitor(&self) -> Option<MonitorHandle> {
1588 let _span = tracing::debug_span!("winit::Window::current_monitor",).entered();
1589 self.window.maybe_wait_on_main(|w| w.current_monitor().map(|inner| MonitorHandle { inner }))
1590 }
1591
1592 /// Returns the list of all the monitors available on the system.
1593 ///
1594 /// This is the same as [`ActiveEventLoop::available_monitors`], and is provided for
1595 /// convenience.
1596 ///
1597 /// [`ActiveEventLoop::available_monitors`]: crate::event_loop::ActiveEventLoop::available_monitors
1598 #[inline]
1599 pub fn available_monitors(&self) -> impl Iterator<Item = MonitorHandle> {
1600 let _span = tracing::debug_span!("winit::Window::available_monitors",).entered();
1601 self.window.maybe_wait_on_main(|w| {
1602 w.available_monitors().into_iter().map(|inner| MonitorHandle { inner })
1603 })
1604 }
1605
1606 /// Returns the primary monitor of the system.
1607 ///
1608 /// Returns `None` if it can't identify any monitor as a primary one.
1609 ///
1610 /// This is the same as [`ActiveEventLoop::primary_monitor`], and is provided for convenience.
1611 ///
1612 /// ## Platform-specific
1613 ///
1614 /// **Wayland / Web:** Always returns `None`.
1615 ///
1616 /// [`ActiveEventLoop::primary_monitor`]: crate::event_loop::ActiveEventLoop::primary_monitor
1617 #[inline]
1618 pub fn primary_monitor(&self) -> Option<MonitorHandle> {
1619 let _span = tracing::debug_span!("winit::Window::primary_monitor",).entered();
1620 self.window.maybe_wait_on_main(|w| w.primary_monitor().map(|inner| MonitorHandle { inner }))
1621 }
1622}
1623
1624#[cfg(feature = "rwh_06")]
1625impl rwh_06::HasWindowHandle for Window {
1626 fn window_handle(&self) -> Result<rwh_06::WindowHandle<'_>, rwh_06::HandleError> {
1627 let raw = self.window.raw_window_handle_rwh_06()?;
1628
1629 // SAFETY: The window handle will never be deallocated while the window is alive,
1630 // and the main thread safety requirements are upheld internally by each platform.
1631 Ok(unsafe { rwh_06::WindowHandle::borrow_raw(raw) })
1632 }
1633}
1634
1635#[cfg(feature = "rwh_06")]
1636impl rwh_06::HasDisplayHandle for Window {
1637 fn display_handle(&self) -> Result<rwh_06::DisplayHandle<'_>, rwh_06::HandleError> {
1638 let raw = self.window.raw_display_handle_rwh_06()?;
1639
1640 // SAFETY: The window handle will never be deallocated while the window is alive,
1641 // and the main thread safety requirements are upheld internally by each platform.
1642 Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) })
1643 }
1644}
1645
1646/// Wrapper to make objects `Send`.
1647///
1648/// # Safety
1649///
1650/// This is not safe! This is only used for `RawWindowHandle`, which only has unsafe getters.
1651#[cfg(any(feature = "rwh_05", feature = "rwh_04"))]
1652struct UnsafeSendWrapper<T>(T);
1653
1654#[cfg(any(feature = "rwh_05", feature = "rwh_04"))]
1655unsafe impl<T> Send for UnsafeSendWrapper<T> {}
1656
1657#[cfg(feature = "rwh_05")]
1658unsafe impl rwh_05::HasRawWindowHandle for Window {
1659 fn raw_window_handle(&self) -> rwh_05::RawWindowHandle {
1660 self.window.maybe_wait_on_main(|w| UnsafeSendWrapper(w.raw_window_handle_rwh_05())).0
1661 }
1662}
1663
1664#[cfg(feature = "rwh_05")]
1665unsafe impl rwh_05::HasRawDisplayHandle for Window {
1666 /// Returns a [`rwh_05::RawDisplayHandle`] used by the [`EventLoop`] that
1667 /// created a window.
1668 ///
1669 /// [`EventLoop`]: crate::event_loop::EventLoop
1670 fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle {
1671 self.window.maybe_wait_on_main(|w| UnsafeSendWrapper(w.raw_display_handle_rwh_05())).0
1672 }
1673}
1674
1675#[cfg(feature = "rwh_04")]
1676unsafe impl rwh_04::HasRawWindowHandle for Window {
1677 fn raw_window_handle(&self) -> rwh_04::RawWindowHandle {
1678 self.window.maybe_wait_on_main(|w| UnsafeSendWrapper(w.raw_window_handle_rwh_04())).0
1679 }
1680}
1681
1682/// The behavior of cursor grabbing.
1683///
1684/// Use this enum with [`Window::set_cursor_grab`] to grab the cursor.
1685#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1686#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1687pub enum CursorGrabMode {
1688 /// No grabbing of the cursor is performed.
1689 None,
1690
1691 /// The cursor is confined to the window area.
1692 ///
1693 /// There's no guarantee that the cursor will be hidden. You should hide it by yourself if you
1694 /// want to do so.
1695 ///
1696 /// ## Platform-specific
1697 ///
1698 /// - **macOS:** Not implemented. Always returns [`ExternalError::NotSupported`] for now.
1699 /// - **iOS / Android / Web:** Always returns an [`ExternalError::NotSupported`].
1700 Confined,
1701
1702 /// The cursor is locked inside the window area to the certain position.
1703 ///
1704 /// There's no guarantee that the cursor will be hidden. You should hide it by yourself if you
1705 /// want to do so.
1706 ///
1707 /// ## Platform-specific
1708 ///
1709 /// - **X11:** Not implemented. Always returns [`ExternalError::NotSupported`] for now.
1710 /// - **iOS / Android:** Always returns an [`ExternalError::NotSupported`].
1711 Locked,
1712}
1713
1714/// Defines the orientation that a window resize will be performed.
1715#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1716pub enum ResizeDirection {
1717 East,
1718 North,
1719 NorthEast,
1720 NorthWest,
1721 South,
1722 SouthEast,
1723 SouthWest,
1724 West,
1725}
1726
1727impl From<ResizeDirection> for CursorIcon {
1728 fn from(direction: ResizeDirection) -> Self {
1729 use ResizeDirection::*;
1730 match direction {
1731 East => CursorIcon::EResize,
1732 North => CursorIcon::NResize,
1733 NorthEast => CursorIcon::NeResize,
1734 NorthWest => CursorIcon::NwResize,
1735 South => CursorIcon::SResize,
1736 SouthEast => CursorIcon::SeResize,
1737 SouthWest => CursorIcon::SwResize,
1738 West => CursorIcon::WResize,
1739 }
1740 }
1741}
1742
1743/// Fullscreen modes.
1744#[derive(Clone, Debug, PartialEq, Eq)]
1745pub enum Fullscreen {
1746 Exclusive(VideoModeHandle),
1747
1748 /// Providing `None` to `Borderless` will fullscreen on the current monitor.
1749 Borderless(Option<MonitorHandle>),
1750}
1751
1752/// The theme variant to use.
1753#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1754#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1755pub enum Theme {
1756 /// Use the light variant.
1757 Light,
1758
1759 /// Use the dark variant.
1760 Dark,
1761}
1762
1763/// ## Platform-specific
1764///
1765/// - **X11:** Sets the WM's `XUrgencyHint`. No distinction between [`Critical`] and
1766/// [`Informational`].
1767///
1768/// [`Critical`]: Self::Critical
1769/// [`Informational`]: Self::Informational
1770#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
1771pub enum UserAttentionType {
1772 /// ## Platform-specific
1773 ///
1774 /// - **macOS:** Bounces the dock icon until the application is in focus.
1775 /// - **Windows:** Flashes both the window and the taskbar button until the application is in
1776 /// focus.
1777 Critical,
1778
1779 /// ## Platform-specific
1780 ///
1781 /// - **macOS:** Bounces the dock icon once.
1782 /// - **Windows:** Flashes the taskbar button until the application is in focus.
1783 #[default]
1784 Informational,
1785}
1786
1787bitflags::bitflags! {
1788 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
1789 pub struct WindowButtons: u32 {
1790 const CLOSE = 1 << 0;
1791 const MINIMIZE = 1 << 1;
1792 const MAXIMIZE = 1 << 2;
1793 }
1794}
1795
1796/// A window level groups windows with respect to their z-position.
1797///
1798/// The relative ordering between windows in different window levels is fixed.
1799/// The z-order of a window within the same window level may change dynamically on user interaction.
1800///
1801/// ## Platform-specific
1802///
1803/// - **iOS / Android / Web / Wayland:** Unsupported.
1804#[derive(Debug, Default, PartialEq, Eq, Clone, Copy)]
1805pub enum WindowLevel {
1806 /// The window will always be below normal windows.
1807 ///
1808 /// This is useful for a widget-based app.
1809 AlwaysOnBottom,
1810
1811 /// The default.
1812 #[default]
1813 Normal,
1814
1815 /// The window will always be on top of normal windows.
1816 AlwaysOnTop,
1817}
1818
1819/// Generic IME purposes for use in [`Window::set_ime_purpose`].
1820///
1821/// The purpose may improve UX by optimizing the IME for the specific use case,
1822/// if winit can express the purpose to the platform and the platform reacts accordingly.
1823///
1824/// ## Platform-specific
1825///
1826/// - **iOS / Android / Web / Windows / X11 / macOS / Orbital:** Unsupported.
1827#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
1828#[non_exhaustive]
1829pub enum ImePurpose {
1830 /// No special hints for the IME (default).
1831 #[default]
1832 Normal,
1833 /// The IME is used for password input.
1834 Password,
1835 /// The IME is used to input into a terminal.
1836 ///
1837 /// For example, that could alter OSK on Wayland to show extra buttons.
1838 Terminal,
1839}
1840
1841/// An opaque token used to activate the [`Window`].
1842///
1843/// [`Window`]: crate::window::Window
1844#[derive(Debug, PartialEq, Eq, Clone)]
1845pub struct ActivationToken {
1846 pub(crate) token: String,
1847}
1848
1849impl ActivationToken {
1850 /// Make an [`ActivationToken`] from a string.
1851 ///
1852 /// This method should be used to wrap tokens passed by side channels to your application, like
1853 /// dbus.
1854 ///
1855 /// The validity of the token is ensured by the windowing system. Using the invalid token will
1856 /// only result in the side effect of the operation involving it being ignored (e.g. window
1857 /// won't get focused automatically), but won't yield any errors.
1858 ///
1859 /// To obtain a valid token, use
1860 #[cfg_attr(
1861 any(x11_platform, wayland_platform, docsrs),
1862 doc = " [`request_activation_token`](crate::platform::startup_notify::WindowExtStartupNotify::request_activation_token)."
1863 )]
1864 #[cfg_attr(
1865 not(any(x11_platform, wayland_platform, docsrs)),
1866 doc = " `request_activation_token`."
1867 )]
1868 pub fn from_raw(token: String) -> Self {
1869 Self { token }
1870 }
1871
1872 /// Convert the token to its string representation to later pass via IPC.
1873 pub fn into_raw(self) -> String {
1874 self.token
1875 }
1876}