2022-03-30 10:30:45 +02:00
|
|
|
use std::{ffi::c_void, path::Path};
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2019-06-21 11:33:15 -04:00
|
|
|
use crate::{
|
2020-03-07 19:42:21 +00:00
|
|
|
dpi::PhysicalSize,
|
2019-06-21 11:33:15 -04:00
|
|
|
event::DeviceId,
|
2022-02-16 22:09:03 +01:00
|
|
|
event_loop::EventLoopBuilder,
|
2019-06-21 11:33:15 -04:00
|
|
|
monitor::MonitorHandle,
|
2022-12-22 08:07:13 +08:00
|
|
|
platform_impl::WinIcon,
|
2022-10-19 03:34:36 +09:00
|
|
|
window::{BadIcon, Icon, Window, WindowBuilder},
|
2019-06-21 11:33:15 -04:00
|
|
|
};
|
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;
|
|
|
|
|
/// Instance Handle type used by Win32 API
|
|
|
|
|
pub type HINSTANCE = isize;
|
|
|
|
|
|
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
|
|
|
/// ```
|
|
|
|
|
/// use winit::event_loop::EventLoopBuilder;
|
|
|
|
|
/// #[cfg(target_os = "windows")]
|
|
|
|
|
/// use winit::platform::windows::EventLoopBuilderExtWindows;
|
|
|
|
|
///
|
|
|
|
|
/// let mut builder = EventLoopBuilder::new();
|
|
|
|
|
/// #[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};
|
|
|
|
|
/// use winit::event_loop::EventLoopBuilder;
|
|
|
|
|
/// #[cfg(target_os = "windows")]
|
|
|
|
|
/// use winit::platform::windows::EventLoopBuilderExtWindows;
|
|
|
|
|
///
|
|
|
|
|
/// let mut builder = EventLoopBuilder::new();
|
|
|
|
|
/// #[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
|
|
|
}
|
|
|
|
|
|
2022-02-16 22:09:03 +01:00
|
|
|
impl<T> EventLoopBuilderExtWindows for EventLoopBuilder<T> {
|
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 {
|
2019-10-05 20:52:40 +02:00
|
|
|
/// Returns the HINSTANCE of the window
|
2022-03-07 22:58:12 +01:00
|
|
|
fn hinstance(&self) -> HINSTANCE;
|
2017-09-13 10:19:54 +03:00
|
|
|
/// Returns the native handle that is used by this window.
|
2015-09-25 18:04:55 +02:00
|
|
|
///
|
2017-09-13 10:19:54 +03:00
|
|
|
/// The pointer will become invalid when the native window was destroyed.
|
2022-03-07 22:58:12 +01:00
|
|
|
fn hwnd(&self) -> HWND;
|
2018-05-07 17:36:21 -04:00
|
|
|
|
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
|
|
|
|
|
/// (as described in [`WindowBuilderExtWindows::with_owner_window`]), the application must 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);
|
2015-09-25 18:04:55 +02:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl WindowExtWindows for Window {
|
2019-10-05 20:52:40 +02:00
|
|
|
#[inline]
|
2022-03-07 22:58:12 +01:00
|
|
|
fn hinstance(&self) -> HINSTANCE {
|
|
|
|
|
self.window.hinstance()
|
2019-10-05 20:52:40 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-25 18:04:55 +02:00
|
|
|
#[inline]
|
2022-03-07 22:58:12 +01:00
|
|
|
fn hwnd(&self) -> HWND {
|
|
|
|
|
self.window.hwnd()
|
2015-09-25 18:04:55 +02:00
|
|
|
}
|
2018-05-07 17:36:21 -04:00
|
|
|
|
2021-04-10 10:47:19 -03:00
|
|
|
#[inline]
|
|
|
|
|
fn set_enable(&self, enabled: bool) {
|
2021-12-01 12:20:56 +01:00
|
|
|
self.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>) {
|
|
|
|
|
self.window.set_taskbar_icon(taskbar_icon)
|
|
|
|
|
}
|
2019-12-22 19:04:09 +00:00
|
|
|
|
2022-04-01 20:21:09 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn set_skip_taskbar(&self, skip: bool) {
|
|
|
|
|
self.window.set_skip_taskbar(skip)
|
|
|
|
|
}
|
2022-08-15 02:36:37 +02:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn set_undecorated_shadow(&self, shadow: bool) {
|
|
|
|
|
self.window.set_undecorated_shadow(shadow)
|
|
|
|
|
}
|
2015-09-25 18:04:55 +02:00
|
|
|
}
|
2016-01-07 16:01:18 +01:00
|
|
|
|
|
|
|
|
/// Additional methods on `WindowBuilder` that are specific to Windows.
|
2019-02-05 10:30:33 -05:00
|
|
|
pub trait WindowBuilderExtWindows {
|
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.
|
2022-12-22 08:07:13 +08:00
|
|
|
/// This only works when [`WindowBuilder::with_parent_window`] isn't called or set to `None`.
|
2021-04-10 10:47:19 -03:00
|
|
|
/// Can be used in combination with [`WindowExtWindows::set_enable(false)`](WindowExtWindows::set_enable)
|
|
|
|
|
/// on the owner 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>
|
|
|
|
|
fn with_owner_window(self, parent: HWND) -> WindowBuilder;
|
|
|
|
|
|
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.
|
2022-03-07 22:58:12 +01:00
|
|
|
///
|
|
|
|
|
/// [`CreateMenu`]: windows_sys::Win32::UI::WindowsAndMessaging::CreateMenu
|
2021-02-04 22:26:33 +01:00
|
|
|
fn with_menu(self, menu: HMENU) -> WindowBuilder;
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
/// This sets `ICON_BIG`. A good ceiling here is 256x256.
|
|
|
|
|
fn with_taskbar_icon(self, taskbar_icon: Option<Icon>) -> WindowBuilder;
|
2018-06-22 10:33:29 +09:00
|
|
|
|
|
|
|
|
/// This sets `WS_EX_NOREDIRECTIONBITMAP`.
|
|
|
|
|
fn with_no_redirection_bitmap(self, flag: bool) -> WindowBuilder;
|
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 `COINIT_MULTITHREADED` you must initialize it before calling any winit functions.
|
2021-03-30 21:27:32 +02:00
|
|
|
/// See <https://docs.microsoft.com/en-us/windows/win32/api/objbase/nf-objbase-coinitialize#remarks> for more information.
|
2020-06-29 01:17:27 +03:00
|
|
|
fn with_drag_and_drop(self, flag: bool) -> WindowBuilder;
|
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.
|
|
|
|
|
fn with_skip_taskbar(self, skip: bool) -> WindowBuilder;
|
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.
|
|
|
|
|
fn with_undecorated_shadow(self, shadow: bool) -> WindowBuilder;
|
2016-01-07 16:01:18 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl WindowBuilderExtWindows for WindowBuilder {
|
2021-04-10 10:47:19 -03:00
|
|
|
#[inline]
|
|
|
|
|
fn with_owner_window(mut self, parent: HWND) -> WindowBuilder {
|
2022-12-22 08:07:13 +08: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]
|
|
|
|
|
fn with_menu(mut self, menu: HMENU) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.menu = Some(menu);
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:36:21 -04:00
|
|
|
#[inline]
|
|
|
|
|
fn with_taskbar_icon(mut self, taskbar_icon: Option<Icon>) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.taskbar_icon = taskbar_icon;
|
|
|
|
|
self
|
2018-06-22 10:33:29 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn with_no_redirection_bitmap(mut self, flag: bool) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.no_redirection_bitmap = flag;
|
|
|
|
|
self
|
2016-11-25 17:05:39 +01:00
|
|
|
}
|
2020-06-29 01:17:27 +03:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn with_drag_and_drop(mut self, flag: bool) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.drag_and_drop = flag;
|
|
|
|
|
self
|
|
|
|
|
}
|
2020-11-30 19:04:26 +01:00
|
|
|
|
2022-04-01 20:21:09 +02:00
|
|
|
#[inline]
|
|
|
|
|
fn with_skip_taskbar(mut self, skip: bool) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.skip_taskbar = skip;
|
|
|
|
|
self
|
|
|
|
|
}
|
2022-08-15 02:36:37 +02:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
fn with_undecorated_shadow(mut self, shadow: bool) -> WindowBuilder {
|
|
|
|
|
self.platform_specific.decoration_shadow = shadow;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl DeviceIdExtWindows for DeviceId {
|
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
|
|
|
#[inline]
|
2019-05-29 21:29:54 -04:00
|
|
|
fn persistent_identifier(&self) -> Option<String> {
|
|
|
|
|
self.0.persistent_identifier()
|
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> {
|
|
|
|
|
let win_icon = WinIcon::from_path(path, size)?;
|
|
|
|
|
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> {
|
2020-03-07 19:42:21 +00:00
|
|
|
let win_icon = WinIcon::from_resource(ordinal, size)?;
|
|
|
|
|
Ok(Icon { inner: win_icon })
|
|
|
|
|
}
|
|
|
|
|
}
|