Skip to main content

winit/platform_impl/windows/
mod.rs

1use smol_str::SmolStr;
2use windows_sys::Win32::Foundation::{HANDLE, HWND};
3use windows_sys::Win32::UI::WindowsAndMessaging::{HMENU, WINDOW_LONG_PTR_INDEX};
4
5pub(crate) use self::event_loop::{
6    ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
7    PlatformSpecificEventLoopAttributes,
8};
9pub(crate) use self::icon::{SelectedCursor, WinIcon};
10pub(crate) use self::keyboard::{physicalkey_to_scancode, scancode_to_physicalkey};
11pub(crate) use self::monitor::{MonitorHandle, VideoModeHandle};
12pub(crate) use self::window::Window;
13
14pub(crate) use self::icon::WinCursor as PlatformCustomCursor;
15pub use self::icon::WinIcon as PlatformIcon;
16pub(crate) use crate::cursor::OnlyCursorImageSource as PlatformCustomCursorSource;
17use crate::platform_impl::Fullscreen;
18
19use crate::event::DeviceId as RootDeviceId;
20use crate::icon::Icon;
21use crate::keyboard::Key;
22use crate::platform::windows::{BackdropType, Color, CornerPreference};
23
24#[derive(Clone, Debug)]
25pub struct PlatformSpecificWindowAttributes {
26    pub owner: Option<HWND>,
27    pub menu: Option<HMENU>,
28    pub taskbar_icon: Option<Icon>,
29    pub no_redirection_bitmap: bool,
30    pub drag_and_drop: bool,
31    pub skip_taskbar: bool,
32    pub class_name: String,
33    pub decoration_shadow: bool,
34    pub backdrop_type: BackdropType,
35    pub clip_children: bool,
36    pub border_color: Option<Color>,
37    pub title_background_color: Option<Color>,
38    pub title_text_color: Option<Color>,
39    pub corner_preference: Option<CornerPreference>,
40}
41
42impl Default for PlatformSpecificWindowAttributes {
43    fn default() -> Self {
44        Self {
45            owner: None,
46            menu: None,
47            taskbar_icon: None,
48            no_redirection_bitmap: false,
49            drag_and_drop: true,
50            skip_taskbar: false,
51            class_name: "Window Class".to_string(),
52            decoration_shadow: false,
53            backdrop_type: BackdropType::default(),
54            clip_children: true,
55            border_color: None,
56            title_background_color: None,
57            title_text_color: None,
58            corner_preference: None,
59        }
60    }
61}
62
63unsafe impl Send for PlatformSpecificWindowAttributes {}
64unsafe impl Sync for PlatformSpecificWindowAttributes {}
65
66#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
67pub struct DeviceId(u32);
68
69impl DeviceId {
70    pub const fn dummy() -> Self {
71        DeviceId(0)
72    }
73}
74
75impl DeviceId {
76    pub fn persistent_identifier(&self) -> Option<String> {
77        if self.0 != 0 {
78            raw_input::get_raw_input_device_name(self.0 as HANDLE)
79        } else {
80            None
81        }
82    }
83}
84
85// Constant device ID, to be removed when this backend is updated to report real device IDs.
86const DEVICE_ID: RootDeviceId = RootDeviceId(DeviceId(0));
87
88fn wrap_device_id(id: u32) -> RootDeviceId {
89    RootDeviceId(DeviceId(id))
90}
91
92pub type OsError = std::io::Error;
93
94#[derive(Debug, Clone, Eq, PartialEq, Hash)]
95pub struct KeyEventExtra {
96    pub text_with_all_modifiers: Option<SmolStr>,
97    pub key_without_modifiers: Key,
98}
99
100#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
101pub struct WindowId(HWND);
102unsafe impl Send for WindowId {}
103unsafe impl Sync for WindowId {}
104
105impl WindowId {
106    pub const fn dummy() -> Self {
107        WindowId(0)
108    }
109}
110
111impl From<WindowId> for u64 {
112    fn from(window_id: WindowId) -> Self {
113        window_id.0 as u64
114    }
115}
116
117impl From<WindowId> for HWND {
118    fn from(window_id: WindowId) -> Self {
119        window_id.0
120    }
121}
122
123impl From<u64> for WindowId {
124    fn from(raw_id: u64) -> Self {
125        Self(raw_id as HWND)
126    }
127}
128
129#[inline(always)]
130const fn get_xbutton_wparam(x: u32) -> u16 {
131    hiword(x)
132}
133
134#[inline(always)]
135const fn get_x_lparam(x: u32) -> i16 {
136    loword(x) as _
137}
138
139#[inline(always)]
140const fn get_y_lparam(x: u32) -> i16 {
141    hiword(x) as _
142}
143
144#[inline(always)]
145pub(crate) const fn primarylangid(lgid: u16) -> u16 {
146    lgid & 0x3ff
147}
148
149#[inline(always)]
150pub(crate) const fn loword(x: u32) -> u16 {
151    (x & 0xffff) as u16
152}
153
154#[inline(always)]
155const fn hiword(x: u32) -> u16 {
156    ((x >> 16) & 0xffff) as u16
157}
158
159#[inline(always)]
160unsafe fn get_window_long(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX) -> isize {
161    #[cfg(target_pointer_width = "64")]
162    return unsafe { windows_sys::Win32::UI::WindowsAndMessaging::GetWindowLongPtrW(hwnd, nindex) };
163    #[cfg(target_pointer_width = "32")]
164    return unsafe {
165        windows_sys::Win32::UI::WindowsAndMessaging::GetWindowLongW(hwnd, nindex) as isize
166    };
167}
168
169#[inline(always)]
170unsafe fn set_window_long(hwnd: HWND, nindex: WINDOW_LONG_PTR_INDEX, dwnewlong: isize) -> isize {
171    #[cfg(target_pointer_width = "64")]
172    return unsafe {
173        windows_sys::Win32::UI::WindowsAndMessaging::SetWindowLongPtrW(hwnd, nindex, dwnewlong)
174    };
175    #[cfg(target_pointer_width = "32")]
176    return unsafe {
177        windows_sys::Win32::UI::WindowsAndMessaging::SetWindowLongW(hwnd, nindex, dwnewlong as i32)
178            as isize
179    };
180}
181
182#[macro_use]
183mod util;
184mod dark_mode;
185mod definitions;
186mod dpi;
187mod drop_handler;
188mod event_loop;
189mod icon;
190mod ime;
191mod keyboard;
192mod keyboard_layout;
193mod monitor;
194mod raw_input;
195mod window;
196mod window_state;