2024-02-23 15:35:18 +01:00
//! # Windows
//!
//! The supported OS version is Windows 7 or higher, though Windows 10 is
//! tested regularly.
2024-04-26 09:28:10 -07:00
use std ::borrow ::Borrow ;
2022-03-30 10:30:45 +02:00
use std ::ffi ::c_void ;
2024-08-23 23:40:27 +03:00
use std ::ops ::Deref ;
2022-03-30 10:30:45 +02:00
use std ::path ::Path ;
2018-05-07 17:36:21 -04:00
2024-08-08 00:46:28 +02:00
#[ cfg(feature = " serde " ) ]
use serde ::{ Deserialize , Serialize } ;
2024-10-11 11:15:54 +03:00
#[ cfg(windows_platform) ]
use windows_sys ::Win32 ::Foundation ::HANDLE ;
2024-08-08 00:46:28 +02:00
2020-03-07 19:42:21 +00:00
use crate ::dpi ::PhysicalSize ;
2024-10-11 20:08:51 +03:00
use crate ::event ::DeviceId ;
2022-02-16 22:09:03 +01:00
use crate ::event_loop ::EventLoopBuilder ;
2019-06-21 11:33:15 -04:00
use crate ::monitor ::MonitorHandle ;
2024-01-31 17:29:59 +04:00
use crate ::window ::{ BadIcon , Icon , Window , WindowAttributes } ;
2019-02-05 10:30:33 -05:00
2022-03-07 22:58:12 +01:00
/// Window Handle type used by Win32 API
pub type HWND = isize ;
/// Menu Handle type used by Win32 API
pub type HMENU = isize ;
/// Monitor Handle type used by Win32 API
pub type HMONITOR = isize ;
2024-01-25 20:59:10 +03:00
/// Describes a system-drawn backdrop material of a window.
///
/// For a detailed explanation, see [`DWM_SYSTEMBACKDROP_TYPE docs`].
///
/// [`DWM_SYSTEMBACKDROP_TYPE docs`]: https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwm_systembackdrop_type
#[ derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash) ]
2024-08-08 00:46:28 +02:00
#[ cfg_attr(feature = " serde " , derive(Serialize, Deserialize)) ]
2024-01-25 20:59:10 +03:00
pub enum BackdropType {
/// Corresponds to `DWMSBT_AUTO`.
///
/// Usually draws a default backdrop effect on the title bar.
#[ default ]
Auto = 0 ,
/// Corresponds to `DWMSBT_NONE`.
None = 1 ,
/// Corresponds to `DWMSBT_MAINWINDOW`.
///
/// Draws the Mica backdrop material.
MainWindow = 2 ,
/// Corresponds to `DWMSBT_TRANSIENTWINDOW`.
///
/// Draws the Background Acrylic backdrop material.
TransientWindow = 3 ,
/// Corresponds to `DWMSBT_TABBEDWINDOW`.
///
/// Draws the Alt Mica backdrop material.
TabbedWindow = 4 ,
}
2024-01-19 12:43:39 +01:00
/// Describes a color used by Windows
#[ repr(transparent) ]
#[ derive(Debug, Copy, Clone, Eq, PartialEq, Hash) ]
2024-08-08 00:46:28 +02:00
#[ cfg_attr(feature = " serde " , derive(Serialize, Deserialize)) ]
2024-01-19 12:43:39 +01:00
pub struct Color ( u32 ) ;
impl Color {
// Special constant only valid for the window border and therefore modeled using Option<Color>
// for user facing code
const NONE : Color = Color ( 0xfffffffe ) ;
/// Use the system's default color
pub const SYSTEM_DEFAULT : Color = Color ( 0xffffffff ) ;
/// Create a new color from the given RGB values
pub const fn from_rgb ( r : u8 , g : u8 , b : u8 ) -> Self {
Self ( ( r as u32 ) | ( ( g as u32 ) < < 8 ) | ( ( b as u32 ) < < 16 ) )
}
}
impl Default for Color {
fn default ( ) -> Self {
Self ::SYSTEM_DEFAULT
}
}
/// Describes how the corners of a window should look like.
///
/// For a detailed explanation, see [`DWM_WINDOW_CORNER_PREFERENCE docs`].
///
/// [`DWM_WINDOW_CORNER_PREFERENCE docs`]: https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/ne-dwmapi-dwm_window_corner_preference
#[ repr(i32) ]
#[ derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash) ]
2024-08-08 00:46:28 +02:00
#[ cfg_attr(feature = " serde " , derive(Serialize, Deserialize)) ]
2024-01-19 12:43:39 +01:00
pub enum CornerPreference {
/// Corresponds to `DWMWCP_DEFAULT`.
///
/// Let the system decide when to round window corners.
#[ default ]
Default = 0 ,
/// Corresponds to `DWMWCP_DONOTROUND`.
///
/// Never round window corners.
DoNotRound = 1 ,
/// Corresponds to `DWMWCP_ROUND`.
///
/// Round the corners, if appropriate.
Round = 2 ,
/// Corresponds to `DWMWCP_ROUNDSMALL`.
///
/// Round the corners if appropriate, with a small radius.
RoundSmall = 3 ,
}
2024-04-26 09:28:10 -07:00
/// A wrapper around a [`Window`] that ignores thread-specific window handle limitations.
///
/// See [`WindowBorrowExtWindows::any_thread`] for more information.
2024-08-08 00:46:28 +02:00
#[ derive(Clone, Debug) ]
2024-08-23 23:40:27 +03:00
pub struct AnyThread < W : Window > ( W ) ;
2024-04-26 09:28:10 -07:00
2024-08-23 23:40:27 +03:00
impl < W : Window > AnyThread < W > {
2024-04-26 09:28:10 -07:00
/// Get a reference to the inner window.
#[ inline ]
2024-08-23 23:40:27 +03:00
pub fn get_ref ( & self ) -> & dyn Window {
2024-04-26 09:28:10 -07:00
& self . 0
}
}
2024-08-23 23:40:27 +03:00
impl < W : Window > Deref for AnyThread < W > {
type Target = W ;
2024-04-26 09:28:10 -07:00
fn deref ( & self ) -> & Self ::Target {
2024-08-23 23:40:27 +03:00
& self . 0
2024-04-26 09:28:10 -07:00
}
}
#[ cfg(feature = " rwh_06 " ) ]
2024-08-23 23:40:27 +03:00
impl < W : Window > rwh_06 ::HasWindowHandle for AnyThread < W > {
2024-04-26 09:28:10 -07:00
fn window_handle ( & self ) -> Result < rwh_06 ::WindowHandle < '_ > , rwh_06 ::HandleError > {
// SAFETY: The top level user has asserted this is only used safely.
unsafe { self . get_ref ( ) . window_handle_any_thread ( ) }
}
}
2019-02-05 10:30:33 -05:00
/// Additional methods on `EventLoop` that are specific to Windows.
2022-02-16 22:09:03 +01:00
pub trait EventLoopBuilderExtWindows {
/// Whether to allow the event loop to be created off of the main thread.
///
/// By default, the window is only allowed to be created on the main
/// thread, to make platform compatibility easier.
2019-10-18 11:51:06 -04:00
///
/// # `Window` caveats
///
/// Note that any `Window` created on the new thread will be destroyed when the thread
/// terminates. Attempting to use a `Window` after its parent thread terminates has
/// unspecified, although explicitly not undefined, behavior.
2022-02-16 22:09:03 +01:00
fn with_any_thread ( & mut self , any_thread : bool ) -> & mut Self ;
2019-10-18 11:51:06 -04:00
2022-02-16 22:09:03 +01:00
/// Whether to enable process-wide DPI awareness.
///
/// By default, `winit` will attempt to enable process-wide DPI awareness. If
/// that's undesirable, you can disable it with this function.
///
/// # Example
///
/// Disable process-wide DPI awareness.
2019-10-18 11:51:06 -04:00
///
2022-02-16 22:09:03 +01:00
/// ```
2024-07-26 16:44:47 +03:00
/// use winit::event_loop::EventLoop;
2022-02-16 22:09:03 +01:00
/// #[cfg(target_os = "windows")]
/// use winit::platform::windows::EventLoopBuilderExtWindows;
///
2024-07-26 16:44:47 +03:00
/// let mut builder = EventLoop::builder();
2022-02-16 22:09:03 +01:00
/// #[cfg(target_os = "windows")]
/// builder.with_dpi_aware(false);
/// # if false { // We can't test this part
/// let event_loop = builder.build();
/// # }
/// ```
fn with_dpi_aware ( & mut self , dpi_aware : bool ) -> & mut Self ;
2022-03-30 10:30:45 +02:00
/// A callback to be executed before dispatching a win32 message to the window procedure.
/// Return true to disable winit's internal message dispatching.
///
/// # Example
///
/// ```
/// # use windows_sys::Win32::UI::WindowsAndMessaging::{ACCEL, CreateAcceleratorTableW, TranslateAcceleratorW, DispatchMessageW, TranslateMessage, MSG};
2024-07-26 16:44:47 +03:00
/// use winit::event_loop::EventLoop;
2022-03-30 10:30:45 +02:00
/// #[cfg(target_os = "windows")]
/// use winit::platform::windows::EventLoopBuilderExtWindows;
///
2024-07-26 16:44:47 +03:00
/// let mut builder = EventLoop::builder();
2022-03-30 10:30:45 +02:00
/// #[cfg(target_os = "windows")]
/// builder.with_msg_hook(|msg|{
/// let msg = msg as *const MSG;
/// # let accels: Vec<ACCEL> = Vec::new();
/// let translated = unsafe {
/// TranslateAcceleratorW(
/// (*msg).hwnd,
/// CreateAcceleratorTableW(accels.as_ptr() as _, 1),
/// msg,
/// ) == 1
/// };
/// translated
/// });
/// ```
fn with_msg_hook < F > ( & mut self , callback : F ) -> & mut Self
where
F : FnMut ( * const c_void ) -> bool + 'static ;
2018-06-14 19:42:18 -04:00
}
2024-06-24 13:04:55 +03:00
impl EventLoopBuilderExtWindows for EventLoopBuilder {
2018-06-14 19:42:18 -04:00
#[ inline ]
2022-02-16 22:09:03 +01:00
fn with_any_thread ( & mut self , any_thread : bool ) -> & mut Self {
self . platform_specific . any_thread = any_thread ;
self
2019-10-18 11:51:06 -04:00
}
#[ inline ]
2022-02-16 22:09:03 +01:00
fn with_dpi_aware ( & mut self , dpi_aware : bool ) -> & mut Self {
self . platform_specific . dpi_aware = dpi_aware ;
self
2018-06-14 19:42:18 -04:00
}
2022-03-30 10:30:45 +02:00
#[ inline ]
fn with_msg_hook < F > ( & mut self , callback : F ) -> & mut Self
where
F : FnMut ( * const c_void ) -> bool + 'static ,
{
self . platform_specific . msg_hook = Some ( Box ::new ( callback ) ) ;
self
}
2018-06-14 19:42:18 -04:00
}
2018-05-07 17:36:21 -04:00
2016-01-07 16:01:18 +01:00
/// Additional methods on `Window` that are specific to Windows.
2019-02-05 10:30:33 -05:00
pub trait WindowExtWindows {
2021-04-10 10:47:19 -03:00
/// Enables or disables mouse and keyboard input to the specified window.
///
/// A window must be enabled before it can be activated.
/// If an application has create a modal dialog box by disabling its owner window
2024-01-31 17:29:59 +04:00
/// (as described in [`WindowAttributesExtWindows::with_owner_window`]), the application must
2021-04-10 10:47:19 -03:00
/// enable the owner window before destroying the dialog box.
/// Otherwise, another window will receive the keyboard focus and be activated.
///
/// If a child window is disabled, it is ignored when the system tries to determine which
/// window should receive mouse messages.
///
/// For more information, see <https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow#remarks>
/// and <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#disabled-windows>
fn set_enable ( & self , enabled : bool ) ;
2018-05-07 17:36:21 -04:00
/// This sets `ICON_BIG`. A good ceiling here is 256x256.
fn set_taskbar_icon ( & self , taskbar_icon : Option < Icon > ) ;
2019-12-22 19:04:09 +00:00
2022-04-01 20:21:09 +02:00
/// Whether to show or hide the window icon in the taskbar.
fn set_skip_taskbar ( & self , skip : bool ) ;
2022-08-15 02:36:37 +02:00
/// Shows or hides the background drop shadow for undecorated windows.
///
/// Enabling the shadow causes a thin 1px line to appear on the top of the window.
fn set_undecorated_shadow ( & self , shadow : bool ) ;
2024-01-19 12:43:39 +01:00
2024-01-25 20:59:10 +03:00
/// Sets system-drawn backdrop type.
///
/// Requires Windows 11 build 22523+.
fn set_system_backdrop ( & self , backdrop_type : BackdropType ) ;
2024-01-19 12:43:39 +01:00
/// Sets the color of the window border.
///
/// Supported starting with Windows 11 Build 22000.
fn set_border_color ( & self , color : Option < Color > ) ;
/// Sets the background color of the title bar.
///
/// Supported starting with Windows 11 Build 22000.
fn set_title_background_color ( & self , color : Option < Color > ) ;
/// Sets the color of the window title.
///
/// Supported starting with Windows 11 Build 22000.
fn set_title_text_color ( & self , color : Color ) ;
/// Sets the preferred style of the window corners.
///
/// Supported starting with Windows 11 Build 22000.
fn set_corner_preference ( & self , preference : CornerPreference ) ;
2024-04-26 09:28:10 -07:00
/// Get the raw window handle for this [`Window`] without checking for thread affinity.
///
/// Window handles in Win32 have a property called "thread affinity" that ties them to their
/// origin thread. Some operations can only happen on the window's origin thread, while others
/// can be called from any thread. For example, [`SetWindowSubclass`] is not thread safe while
/// [`GetDC`] is thread safe.
///
/// In Rust terms, the window handle is `Send` sometimes but `!Send` other times.
///
/// Therefore, in order to avoid confusing threading errors, [`Window`] only returns the
/// window handle when the [`window_handle`] function is called from the thread that created
/// the window. In other cases, it returns an [`Unavailable`] error.
///
/// However in some cases you may already know that you are using the window handle for
/// operations that are guaranteed to be thread-safe. In which case this function aims
/// to provide an escape hatch so these functions are still accessible from other threads.
///
/// # Safety
///
/// It is the responsibility of the user to only pass the window handle into thread-safe
/// Win32 APIs.
///
/// [`SetWindowSubclass`]: https://learn.microsoft.com/en-us/windows/win32/api/commctrl/nf-commctrl-setwindowsubclass
/// [`GetDC`]: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdc
/// [`Window`]: crate::window::Window
/// [`window_handle`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/trait.HasWindowHandle.html#tymethod.window_handle
/// [`Unavailable`]: https://docs.rs/raw-window-handle/latest/raw_window_handle/enum.HandleError.html#variant.Unavailable
///
/// ## Example
///
/// ```no_run
/// # use winit::window::Window;
2024-08-23 23:40:27 +03:00
/// # fn scope(window: Box<dyn Window>) {
2024-04-26 09:28:10 -07:00
/// use std::thread;
2024-07-07 18:38:50 +02:00
///
2024-04-26 09:28:10 -07:00
/// use winit::platform::windows::WindowExtWindows;
/// use winit::raw_window_handle::HasWindowHandle;
///
/// // We can get the window handle on the current thread.
/// let handle = window.window_handle().unwrap();
///
/// // However, on another thread, we can't!
/// thread::spawn(move || {
/// assert!(window.window_handle().is_err());
///
/// // We can use this function as an escape hatch.
/// let handle = unsafe { window.window_handle_any_thread().unwrap() };
/// });
/// # }
/// ```
#[ cfg(feature = " rwh_06 " ) ]
unsafe fn window_handle_any_thread (
& self ,
) -> Result < rwh_06 ::WindowHandle < '_ > , rwh_06 ::HandleError > ;
2015-09-25 18:04:55 +02:00
}
2024-08-23 23:40:27 +03:00
impl WindowExtWindows for dyn Window + '_ {
2021-04-10 10:47:19 -03:00
#[ inline ]
fn set_enable ( & self , enabled : bool ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_enable ( enabled )
2021-04-10 10:47:19 -03:00
}
2018-05-07 17:36:21 -04:00
#[ inline ]
fn set_taskbar_icon ( & self , taskbar_icon : Option < Icon > ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_taskbar_icon ( taskbar_icon )
2018-05-07 17:36:21 -04:00
}
2019-12-22 19:04:09 +00:00
2022-04-01 20:21:09 +02:00
#[ inline ]
fn set_skip_taskbar ( & self , skip : bool ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_skip_taskbar ( skip )
2022-04-01 20:21:09 +02:00
}
2022-08-15 02:36:37 +02:00
#[ inline ]
fn set_undecorated_shadow ( & self , shadow : bool ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_undecorated_shadow ( shadow )
2022-08-15 02:36:37 +02:00
}
2024-01-19 12:43:39 +01:00
2024-01-25 20:59:10 +03:00
#[ inline ]
fn set_system_backdrop ( & self , backdrop_type : BackdropType ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_system_backdrop ( backdrop_type )
2024-01-25 20:59:10 +03:00
}
2024-01-19 12:43:39 +01:00
#[ inline ]
fn set_border_color ( & self , color : Option < Color > ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_border_color ( color . unwrap_or ( Color ::NONE ) )
2024-01-19 12:43:39 +01:00
}
#[ inline ]
fn set_title_background_color ( & self , color : Option < Color > ) {
// The windows docs don't mention NONE as a valid options but it works in practice and is
// useful to circumvent the Windows option "Show accent color on title bars and
// window borders"
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_title_background_color ( color . unwrap_or ( Color ::NONE ) )
2024-01-19 12:43:39 +01:00
}
#[ inline ]
fn set_title_text_color ( & self , color : Color ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_title_text_color ( color )
2024-01-19 12:43:39 +01:00
}
#[ inline ]
fn set_corner_preference ( & self , preference : CornerPreference ) {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
window . set_corner_preference ( preference )
2024-01-19 12:43:39 +01:00
}
2024-04-26 09:28:10 -07:00
#[ cfg(feature = " rwh_06 " ) ]
unsafe fn window_handle_any_thread (
& self ,
) -> Result < rwh_06 ::WindowHandle < '_ > , rwh_06 ::HandleError > {
2024-08-23 23:40:27 +03:00
let window = self . as_any ( ) . downcast_ref ::< crate ::platform_impl ::Window > ( ) . unwrap ( ) ;
2024-04-26 09:28:10 -07:00
unsafe {
2024-08-23 23:40:27 +03:00
let handle = window . rwh_06_no_thread_check ( ) ? ;
2024-04-26 09:28:10 -07:00
// SAFETY: The handle is valid in this context.
Ok ( rwh_06 ::WindowHandle ::borrow_raw ( handle ) )
}
}
2015-09-25 18:04:55 +02:00
}
2016-01-07 16:01:18 +01:00
2024-04-26 09:28:10 -07:00
/// Additional methods for anything that dereference to [`Window`].
///
/// [`Window`]: crate::window::Window
2024-08-23 23:40:27 +03:00
pub trait WindowBorrowExtWindows : Borrow < dyn Window > + Sized {
2024-04-26 09:28:10 -07:00
/// Create an object that allows accessing the inner window handle in a thread-unsafe way.
///
/// It is possible to call [`window_handle_any_thread`] to get around Windows's thread
/// affinity limitations. However, it may be desired to pass the [`Window`] into something
/// that requires the [`HasWindowHandle`] trait, while ignoring thread affinity limitations.
///
/// This function wraps anything that implements `Borrow<Window>` into a structure that
/// uses the inner window handle as a mean of implementing [`HasWindowHandle`]. It wraps
/// `Window`, `&Window`, `Arc<Window>`, and other reference types.
///
/// # Safety
///
/// It is the responsibility of the user to only pass the window handle into thread-safe
/// Win32 APIs.
///
/// [`Window`]: crate::window::Window
2024-08-04 00:18:39 +02:00
#[ cfg_attr(
feature = " rwh_06 " ,
doc = " [`HasWindowHandle`]: rwh_06::HasWindowHandle " ,
doc = " [`window_handle_any_thread`]: WindowExtWindows::window_handle_any_thread "
) ]
#[ cfg_attr(
not ( feature = " rwh_06 " ) ,
doc = " [`HasWindowHandle`]: #only-available-with-rwh_06 " ,
doc = " [`window_handle_any_thread`]: #only-available-with-rwh_06 "
) ]
2024-08-23 23:40:27 +03:00
unsafe fn any_thread ( self ) -> AnyThread < Self >
where
Self : Window ,
{
2024-04-26 09:28:10 -07:00
AnyThread ( self )
}
}
2024-08-23 23:40:27 +03:00
impl < W : Borrow < dyn Window > + Sized > WindowBorrowExtWindows for W { }
2024-04-26 09:28:10 -07:00
2024-01-31 17:29:59 +04:00
/// Additional methods on `WindowAttributes` that are specific to Windows.
2023-10-14 19:07:39 -07:00
#[ allow(rustdoc::broken_intra_doc_links) ]
2024-01-31 17:29:59 +04:00
pub trait WindowAttributesExtWindows {
2021-04-10 10:47:19 -03:00
/// Set an owner to the window to be created. Can be used to create a dialog box, for example.
2024-01-31 17:29:59 +04:00
/// This only works when [`WindowAttributes::with_parent_window`] isn't called or set to `None`.
2024-03-16 10:22:29 +01:00
/// Can be used in combination with
/// [`WindowExtWindows::set_enable(false)`][WindowExtWindows::set_enable] on the owner
2021-04-10 10:47:19 -03:00
/// window to create a modal dialog box.
///
/// From MSDN:
/// - An owned window is always above its owner in the z-order.
/// - The system automatically destroys an owned window when its owner is destroyed.
/// - An owned window is hidden when its owner is minimized.
///
/// For more information, see <https://docs.microsoft.com/en-us/windows/win32/winmsg/window-features#owned-windows>
2023-10-14 19:07:39 -07:00
fn with_owner_window ( self , parent : HWND ) -> Self ;
2021-04-10 10:47:19 -03:00
2021-02-04 22:26:33 +01:00
/// Sets a menu on the window to be created.
///
/// Parent and menu are mutually exclusive; a child window cannot have a menu!
///
2022-03-07 22:58:12 +01:00
/// The menu must have been manually created beforehand with [`CreateMenu`] or similar.
2021-02-04 22:26:33 +01:00
///
/// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how
/// the menus look. If you use this, it is recommended that you combine it with
/// `with_theme(Some(Theme::Light))` to avoid a jarring effect.
2024-01-04 12:54:35 +01:00
#[ cfg_attr(
2024-05-06 07:11:57 +02:00
windows_platform ,
2024-01-04 12:54:35 +01:00
doc = " [`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu "
) ]
2024-05-06 07:11:57 +02:00
#[ cfg_attr(not(windows_platform), doc = " [`CreateMenu`]: #only-available-on-windows " ) ]
2023-10-14 19:07:39 -07:00
fn with_menu ( self , menu : HMENU ) -> Self ;
2021-02-04 22:26:33 +01:00
2018-05-07 17:36:21 -04:00
/// This sets `ICON_BIG`. A good ceiling here is 256x256.
2023-10-14 19:07:39 -07:00
fn with_taskbar_icon ( self , taskbar_icon : Option < Icon > ) -> Self ;
2018-06-22 10:33:29 +09:00
/// This sets `WS_EX_NOREDIRECTIONBITMAP`.
2023-10-14 19:07:39 -07:00
fn with_no_redirection_bitmap ( self , flag : bool ) -> Self ;
2020-06-29 01:17:27 +03:00
/// Enables or disables drag and drop support (enabled by default). Will interfere with other
/// crates that use multi-threaded COM API (`CoInitializeEx` with `COINIT_MULTITHREADED`
/// instead of `COINIT_APARTMENTTHREADED`) on the same thread. Note that winit may still
/// attempt to initialize COM API regardless of this option. Currently only fullscreen mode
/// does that, but there may be more in the future. If you need COM API with
2021-03-30 21:27:32 +02:00
/// `COINIT_MULTITHREADED` you must initialize it before calling any winit functions. See <https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize#remarks> for more information.
2023-10-14 19:07:39 -07:00
fn with_drag_and_drop ( self , flag : bool ) -> Self ;
2020-11-30 19:04:26 +01:00
2022-04-01 20:21:09 +02:00
/// Whether show or hide the window icon in the taskbar.
2023-10-14 19:07:39 -07:00
fn with_skip_taskbar ( self , skip : bool ) -> Self ;
2022-08-15 02:36:37 +02:00
2023-07-29 15:39:23 +02:00
/// Customize the window class name.
2023-10-14 19:07:39 -07:00
fn with_class_name < S : Into < String > > ( self , class_name : S ) -> Self ;
2023-07-29 15:39:23 +02:00
2022-08-15 02:36:37 +02:00
/// Shows or hides the background drop shadow for undecorated windows.
///
/// The shadow is hidden by default.
/// Enabling the shadow causes a thin 1px line to appear on the top of the window.
2023-10-14 19:07:39 -07:00
fn with_undecorated_shadow ( self , shadow : bool ) -> Self ;
2024-01-19 12:43:39 +01:00
2024-01-25 20:59:10 +03:00
/// Sets system-drawn backdrop type.
///
/// Requires Windows 11 build 22523+.
fn with_system_backdrop ( self , backdrop_type : BackdropType ) -> Self ;
2024-01-22 19:55:37 +02:00
/// This sets or removes `WS_CLIPCHILDREN` style.
fn with_clip_children ( self , flag : bool ) -> Self ;
2024-01-19 12:43:39 +01:00
/// Sets the color of the window border.
///
/// Supported starting with Windows 11 Build 22000.
fn with_border_color ( self , color : Option < Color > ) -> Self ;
/// Sets the background color of the title bar.
///
/// Supported starting with Windows 11 Build 22000.
fn with_title_background_color ( self , color : Option < Color > ) -> Self ;
/// Sets the color of the window title.
///
/// Supported starting with Windows 11 Build 22000.
fn with_title_text_color ( self , color : Color ) -> Self ;
/// Sets the preferred style of the window corners.
///
/// Supported starting with Windows 11 Build 22000.
fn with_corner_preference ( self , corners : CornerPreference ) -> Self ;
2016-01-07 16:01:18 +01:00
}
2024-01-31 17:29:59 +04:00
impl WindowAttributesExtWindows for WindowAttributes {
2021-04-10 10:47:19 -03:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_owner_window ( mut self , parent : HWND ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . owner = Some ( parent ) ;
2016-11-25 17:05:39 +01:00
self
2018-05-07 17:36:21 -04:00
}
2021-02-04 22:26:33 +01:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_menu ( mut self , menu : HMENU ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . menu = Some ( menu ) ;
2021-02-04 22:26:33 +01:00
self
}
2018-05-07 17:36:21 -04:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_taskbar_icon ( mut self , taskbar_icon : Option < Icon > ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . taskbar_icon = taskbar_icon ;
2018-05-07 17:36:21 -04:00
self
2018-06-22 10:33:29 +09:00
}
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_no_redirection_bitmap ( mut self , flag : bool ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . no_redirection_bitmap = flag ;
2018-06-22 10:33:29 +09:00
self
2016-11-25 17:05:39 +01:00
}
2020-06-29 01:17:27 +03:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_drag_and_drop ( mut self , flag : bool ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . drag_and_drop = flag ;
2020-06-29 01:17:27 +03:00
self
}
2020-11-30 19:04:26 +01:00
2022-04-01 20:21:09 +02:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_skip_taskbar ( mut self , skip : bool ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . skip_taskbar = skip ;
2022-04-01 20:21:09 +02:00
self
}
2022-08-15 02:36:37 +02:00
2023-07-29 15:39:23 +02:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_class_name < S : Into < String > > ( mut self , class_name : S ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . class_name = class_name . into ( ) ;
2023-07-29 15:39:23 +02:00
self
}
2022-08-15 02:36:37 +02:00
#[ inline ]
2023-10-14 19:07:39 -07:00
fn with_undecorated_shadow ( mut self , shadow : bool ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . decoration_shadow = shadow ;
2022-08-15 02:36:37 +02:00
self
}
2024-01-19 12:43:39 +01:00
2024-01-25 20:59:10 +03:00
#[ inline ]
fn with_system_backdrop ( mut self , backdrop_type : BackdropType ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . backdrop_type = backdrop_type ;
2024-01-25 20:59:10 +03:00
self
}
2024-01-22 19:55:37 +02:00
#[ inline ]
fn with_clip_children ( mut self , flag : bool ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . clip_children = flag ;
2024-01-22 19:55:37 +02:00
self
}
2024-01-19 12:43:39 +01:00
#[ inline ]
fn with_border_color ( mut self , color : Option < Color > ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . border_color = Some ( color . unwrap_or ( Color ::NONE ) ) ;
2024-01-19 12:43:39 +01:00
self
}
#[ inline ]
fn with_title_background_color ( mut self , color : Option < Color > ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . title_background_color = Some ( color . unwrap_or ( Color ::NONE ) ) ;
2024-01-19 12:43:39 +01:00
self
}
#[ inline ]
fn with_title_text_color ( mut self , color : Color ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . title_text_color = Some ( color ) ;
2024-01-19 12:43:39 +01:00
self
}
#[ inline ]
fn with_corner_preference ( mut self , corners : CornerPreference ) -> Self {
2024-01-31 17:29:59 +04:00
self . platform_specific . corner_preference = Some ( corners ) ;
2024-01-19 12:43:39 +01:00
self
}
2016-01-07 16:01:18 +01:00
}
2017-08-30 08:49:18 +02:00
2019-02-05 10:30:33 -05:00
/// Additional methods on `MonitorHandle` that are specific to Windows.
pub trait MonitorHandleExtWindows {
2017-10-25 18:12:39 +03:00
/// Returns the name of the monitor adapter specific to the Win32 API.
2017-08-30 08:49:18 +02:00
fn native_id ( & self ) -> String ;
2017-10-25 18:12:39 +03:00
/// Returns the handle of the monitor - `HMONITOR`.
2022-03-07 22:58:12 +01:00
fn hmonitor ( & self ) -> HMONITOR ;
2017-08-30 08:49:18 +02:00
}
2019-02-05 10:30:33 -05:00
impl MonitorHandleExtWindows for MonitorHandle {
2017-08-30 08:49:18 +02:00
#[ inline ]
fn native_id ( & self ) -> String {
2019-05-29 21:29:54 -04:00
self . inner . native_identifier ( )
2017-08-30 08:49:18 +02:00
}
2017-10-25 18:12:39 +03:00
#[ inline ]
2022-03-07 22:58:12 +01:00
fn hmonitor ( & self ) -> HMONITOR {
self . inner . hmonitor ( )
2017-10-25 18:12:39 +03:00
}
2017-08-30 08:49:18 +02:00
}
Windows: Implement DeviceEvents (#482)
Fixes #467
All variants other than Text have been implemented. While Text can
be implemented using ToUnicode, that doesn't play nice with dead
keys, IME, etc.
Most of the mouse DeviceEvents were already implemented, but due
to the flags that were used when registering for raw input events,
they only worked when the window was in the foreground.
This is also a step forward for #338, as DeviceIds are no longer
useless on Windows. On DeviceEvents, the DeviceId contains that
device's handle. While that handle could ostensibly be used by
developers to query device information, my actual reason for
choosing it is because it's simply a very easy way to handle this.
As a fun bonus, this enabled me to create this method:
DevideIdExt::get_persistent_identifier() -> Option<String>
Using this gives you a unique identifier for the device that
persists across replugs/reboots/etc., so it's ideal for something
like device-specific configuration.
There's a notable caveat to the new DeviceIds, which is that the
value will always be 0 for a WindowEvent. There doesn't seem to be
any straightforward way around this limitation.
I was concerned that multi-window applications would receive n
copies of every DeviceEvent, but Windows only sends them to one
window per application.
Lastly, there's a chance that these additions will cause
antivirus/etc. software to detect winit applications as keyloggers.
I don't know how likely that is to actually happen to people, but
if it does become an issue, the raw input code is neatly
sequestered and would be easy to make optional during compilation.
2018-04-28 12:42:33 -04:00
/// Additional methods on `DeviceId` that are specific to Windows.
2019-02-05 10:30:33 -05:00
pub trait DeviceIdExtWindows {
Windows: Implement DeviceEvents (#482)
Fixes #467
All variants other than Text have been implemented. While Text can
be implemented using ToUnicode, that doesn't play nice with dead
keys, IME, etc.
Most of the mouse DeviceEvents were already implemented, but due
to the flags that were used when registering for raw input events,
they only worked when the window was in the foreground.
This is also a step forward for #338, as DeviceIds are no longer
useless on Windows. On DeviceEvents, the DeviceId contains that
device's handle. While that handle could ostensibly be used by
developers to query device information, my actual reason for
choosing it is because it's simply a very easy way to handle this.
As a fun bonus, this enabled me to create this method:
DevideIdExt::get_persistent_identifier() -> Option<String>
Using this gives you a unique identifier for the device that
persists across replugs/reboots/etc., so it's ideal for something
like device-specific configuration.
There's a notable caveat to the new DeviceIds, which is that the
value will always be 0 for a WindowEvent. There doesn't seem to be
any straightforward way around this limitation.
I was concerned that multi-window applications would receive n
copies of every DeviceEvent, but Windows only sends them to one
window per application.
Lastly, there's a chance that these additions will cause
antivirus/etc. software to detect winit applications as keyloggers.
I don't know how likely that is to actually happen to people, but
if it does become an issue, the raw input code is neatly
sequestered and would be easy to make optional during compilation.
2018-04-28 12:42:33 -04:00
/// Returns an identifier that persistently refers to this specific device.
///
/// Will return `None` if the device is no longer available.
2019-05-29 21:29:54 -04:00
fn persistent_identifier ( & self ) -> Option < String > ;
Windows: Implement DeviceEvents (#482)
Fixes #467
All variants other than Text have been implemented. While Text can
be implemented using ToUnicode, that doesn't play nice with dead
keys, IME, etc.
Most of the mouse DeviceEvents were already implemented, but due
to the flags that were used when registering for raw input events,
they only worked when the window was in the foreground.
This is also a step forward for #338, as DeviceIds are no longer
useless on Windows. On DeviceEvents, the DeviceId contains that
device's handle. While that handle could ostensibly be used by
developers to query device information, my actual reason for
choosing it is because it's simply a very easy way to handle this.
As a fun bonus, this enabled me to create this method:
DevideIdExt::get_persistent_identifier() -> Option<String>
Using this gives you a unique identifier for the device that
persists across replugs/reboots/etc., so it's ideal for something
like device-specific configuration.
There's a notable caveat to the new DeviceIds, which is that the
value will always be 0 for a WindowEvent. There doesn't seem to be
any straightforward way around this limitation.
I was concerned that multi-window applications would receive n
copies of every DeviceEvent, but Windows only sends them to one
window per application.
Lastly, there's a chance that these additions will cause
antivirus/etc. software to detect winit applications as keyloggers.
I don't know how likely that is to actually happen to people, but
if it does become an issue, the raw input code is neatly
sequestered and would be easy to make optional during compilation.
2018-04-28 12:42:33 -04:00
}
2024-10-11 11:15:54 +03:00
#[ cfg(windows_platform) ]
2019-02-05 10:30:33 -05:00
impl DeviceIdExtWindows for DeviceId {
2019-05-29 21:29:54 -04:00
fn persistent_identifier ( & self ) -> Option < String > {
2024-10-11 11:15:54 +03:00
let raw_id = self . into_raw ( ) ;
if raw_id ! = 0 {
crate ::platform_impl ::raw_input ::get_raw_input_device_name ( raw_id as HANDLE )
} else {
None
}
Windows: Implement DeviceEvents (#482)
Fixes #467
All variants other than Text have been implemented. While Text can
be implemented using ToUnicode, that doesn't play nice with dead
keys, IME, etc.
Most of the mouse DeviceEvents were already implemented, but due
to the flags that were used when registering for raw input events,
they only worked when the window was in the foreground.
This is also a step forward for #338, as DeviceIds are no longer
useless on Windows. On DeviceEvents, the DeviceId contains that
device's handle. While that handle could ostensibly be used by
developers to query device information, my actual reason for
choosing it is because it's simply a very easy way to handle this.
As a fun bonus, this enabled me to create this method:
DevideIdExt::get_persistent_identifier() -> Option<String>
Using this gives you a unique identifier for the device that
persists across replugs/reboots/etc., so it's ideal for something
like device-specific configuration.
There's a notable caveat to the new DeviceIds, which is that the
value will always be 0 for a WindowEvent. There doesn't seem to be
any straightforward way around this limitation.
I was concerned that multi-window applications would receive n
copies of every DeviceEvent, but Windows only sends them to one
window per application.
Lastly, there's a chance that these additions will cause
antivirus/etc. software to detect winit applications as keyloggers.
I don't know how likely that is to actually happen to people, but
if it does become an issue, the raw input code is neatly
sequestered and would be easy to make optional during compilation.
2018-04-28 12:42:33 -04:00
}
}
2020-03-07 19:42:21 +00:00
/// Additional methods on `Icon` that are specific to Windows.
pub trait IconExtWindows : Sized {
/// Create an icon from a file path.
///
/// Specify `size` to load a specific icon size from the file, or `None` to load the default
/// icon size from the file.
///
/// In cases where the specified size does not exist in the file, Windows may perform scaling
/// to get an icon of the desired size.
fn from_path < P : AsRef < Path > > ( path : P , size : Option < PhysicalSize < u32 > > )
-> Result < Self , BadIcon > ;
/// Create an icon from a resource embedded in this executable or library.
///
/// Specify `size` to load a specific icon size from the file, or `None` to load the default
/// icon size from the file.
///
/// In cases where the specified size does not exist in the file, Windows may perform scaling
/// to get an icon of the desired size.
2022-03-07 22:58:12 +01:00
fn from_resource ( ordinal : u16 , size : Option < PhysicalSize < u32 > > ) -> Result < Self , BadIcon > ;
2020-03-07 19:42:21 +00:00
}
impl IconExtWindows for Icon {
fn from_path < P : AsRef < Path > > (
path : P ,
size : Option < PhysicalSize < u32 > > ,
) -> Result < Self , BadIcon > {
2024-01-04 12:54:35 +01:00
let win_icon = crate ::platform_impl ::WinIcon ::from_path ( path , size ) ? ;
2020-03-07 19:42:21 +00:00
Ok ( Icon { inner : win_icon } )
}
2022-03-07 22:58:12 +01:00
fn from_resource ( ordinal : u16 , size : Option < PhysicalSize < u32 > > ) -> Result < Self , BadIcon > {
2024-01-04 12:54:35 +01:00
let win_icon = crate ::platform_impl ::WinIcon ::from_resource ( ordinal , size ) ? ;
2020-03-07 19:42:21 +00:00
Ok ( Icon { inner : win_icon } )
}
}