witer 0.11.4

An iterator-based Win32 windowing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use std::{
  ops::BitAnd,
  sync::{
    atomic::{AtomicBool, Ordering},
    OnceLock,
  },
};

use cursor_icon::CursorIcon;
use windows::{
  core::{PCSTR, PCWSTR},
  Win32::{
    Devices::HumanInterfaceDevice,
    Foundation::{HWND, NTSTATUS, RECT},
    Graphics::Gdi::{GetDC, GetMonitorInfoW, HMONITOR, MONITORINFO, MONITORINFOEXW},
    System::{
      LibraryLoader::{GetProcAddress, LoadLibraryA},
      SystemInformation::OSVERSIONINFOW,
    },
    UI::{
      HiDpi::{self, GetDpiForMonitor, GetDpiForWindow},
      Input::{
        self,
        GetRawInputData,
        RegisterRawInputDevices,
        HRAWINPUT,
        RAWINPUT,
        RAWINPUTDEVICE,
        RAWINPUTHEADER,
      },
      WindowsAndMessaging::{
        self,
        ClipCursor,
        ShowCursor,
        WINDOW_EX_STYLE,
        WINDOW_STYLE,
      },
    },
  },
  UI::ViewManagement::{UIColorType, UISettings},
};

use crate::{
  prelude::{PhysicalPosition, PhysicalSize},
  window::{
    data::{Fullscreen, Visibility},
    frame::Style,
  },
};

pub fn signed_lo_word(dword: i32) -> i16 {
  dword as i16
}

pub fn lo_word(dword: u32) -> u16 {
  dword as u16
}

pub fn signed_hi_word(dword: i32) -> i16 {
  (dword >> 16) as i16
}

pub fn hi_word(dword: u32) -> u16 {
  (dword >> 16) as u16
}

pub fn signed_lo_byte(word: i16) -> i8 {
  word as i8
}

pub fn lo_byte(word: u16) -> u8 {
  word as u8
}

pub fn signed_hi_byte(word: i16) -> i8 {
  (word >> 8) as i8
}

pub fn hi_byte(word: u16) -> u8 {
  (word >> 8) as u8
}

/*
  Some of the following code was taken directly from `winit` and is currently under the Apache-2.0 copyright.
  > https://github.com/rust-windowing/winit/blob/master/src/platform_impl/windows/
  Some functions are simplified to be more specific to the goals of ezwin or reduce dependencies.
  Changes were also made to adapt from the crate `windows-sys` to `windows`

  The dark mode algorithm was NOT taken from `winit`, but instead from here:
  > https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes
*/

pub(crate) fn get_function_impl(
  library: &str,
  function: &str,
) -> Option<*const std::ffi::c_void> {
  assert_eq!(library.chars().last(), Some('\0'));
  assert_eq!(function.chars().last(), Some('\0'));

  // Library names we will use are ASCII so we can use the A version to avoid
  // string conversion.
  let module = match unsafe { LoadLibraryA(PCSTR::from_raw(library.as_ptr())) } {
    Ok(module) => module,
    Err(_) => return None,
  };

  unsafe { GetProcAddress(module, PCSTR::from_raw(function.as_ptr())) }
    .map(|function_ptr| function_ptr as _)
}

macro_rules! get_function {
  ($lib:expr, $func:ident) => {
    crate::utilities::get_function_impl(
      concat!($lib, '\0'),
      concat!(stringify!($func), '\0'),
    )
    .map(|f| unsafe { std::mem::transmute::<*const _, $func>(f) })
  };
}

pub fn windows_10_build_version() -> Option<u32> {
  static WIN10_BUILD_VERSION: OnceLock<Option<u32>> = OnceLock::new();
  *WIN10_BUILD_VERSION.get_or_init(|| {
    type RtlGetVersion = unsafe extern "system" fn(*mut OSVERSIONINFOW) -> NTSTATUS;
    let handle = get_function!("ntdll.dll", RtlGetVersion);

    if let Some(rtl_get_version) = handle {
      unsafe {
        let mut vi = OSVERSIONINFOW {
          dwOSVersionInfoSize: 0,
          dwMajorVersion: 0,
          dwMinorVersion: 0,
          dwBuildNumber: 0,
          dwPlatformId: 0,
          szCSDVersion: [0; 128],
        };

        let status = (rtl_get_version)(&mut vi);

        if status.0 >= 0 && vi.dwMajorVersion == 10 && vi.dwMinorVersion == 0 {
          Some(vi.dwBuildNumber)
        } else {
          None
        }
      }
    } else {
      None
    }
  })
}

pub fn is_dark_mode_supported() -> bool {
  static DARK_MODE_SUPPORTED: OnceLock<bool> = OnceLock::new();
  *DARK_MODE_SUPPORTED.get_or_init(|| {
    // We won't try to do anything for windows versions < 17763
    // (Windows 10 October 2018 update)
    match windows_10_build_version() {
      Some(v) => v >= 17763,
      None => false,
    }
  })
}

pub fn is_system_dark_mode_enabled() -> bool {
  static IS_SYSTEM_DARK_MODE: OnceLock<bool> = OnceLock::new();
  *IS_SYSTEM_DARK_MODE.get_or_init(|| {
    let settings = UISettings::new().unwrap();
    let foreground = settings
      .GetColorValue(UIColorType::Foreground)
      .unwrap_or_default();
    is_color_light(&foreground)
  })
}

#[inline]
fn is_color_light(clr: &windows::UI::Color) -> bool {
  ((5 * clr.G as u32) + (2 * clr.R as u32) + clr.B as u32) > (8 * 128)
}

pub(crate) fn get_window_style(info: &Style) -> WINDOW_STYLE {
  let mut style = WindowsAndMessaging::WS_CAPTION
    | WindowsAndMessaging::WS_BORDER
    | WindowsAndMessaging::WS_CLIPSIBLINGS
    | WindowsAndMessaging::WS_SYSMENU;

  if info.resizeable {
    style |= WindowsAndMessaging::WS_SIZEBOX;
    style |= WindowsAndMessaging::WS_MAXIMIZEBOX;
    style |= WindowsAndMessaging::WS_MINIMIZEBOX;
  }

  if let Visibility::Shown = info.visibility {
    style |= WindowsAndMessaging::WS_VISIBLE;
  }

  if let Some(Fullscreen::Borderless) = info.fullscreen {
    style &= !WindowsAndMessaging::WS_OVERLAPPEDWINDOW;
    style |= WindowsAndMessaging::WS_POPUP;
  }

  if let Visibility::Hidden = info.decorations {
    style &= !(WindowsAndMessaging::WS_CAPTION | WindowsAndMessaging::WS_BORDER);
  }

  style
}

pub(crate) fn get_window_ex_style(info: &Style) -> WINDOW_EX_STYLE {
  let mut style =
    WindowsAndMessaging::WS_EX_WINDOWEDGE | WindowsAndMessaging::WS_EX_APPWINDOW;

  if let Some(Fullscreen::Borderless) = info.fullscreen {
    style &= !WindowsAndMessaging::WS_EX_OVERLAPPEDWINDOW;
  }

  if let Visibility::Hidden = info.decorations {
    style &= !WindowsAndMessaging::WS_EX_WINDOWEDGE;
  }

  style
}

pub(crate) fn set_cursor_clip(rect: Option<&RECT>) {
  if let Err(_e) = unsafe { ClipCursor(rect.map(|r| r as _)) } {
    tracing::error!("{_e}");
  }
}

pub(crate) fn set_cursor_visibility(visible: Visibility) {
  let hidden = visible == Visibility::Hidden;
  static HIDDEN: AtomicBool = AtomicBool::new(false);
  let changed = HIDDEN.swap(hidden, Ordering::SeqCst) ^ hidden;
  if changed {
    unsafe { ShowCursor(!hidden) };
  }
}

pub const BASE_DPI: u32 = 96;

pub fn dpi_to_scale_factor(dpi: u32) -> f64 {
  dpi as f64 / BASE_DPI as f64
}

pub fn hwnd_dpi(hwnd: HWND) -> u32 {
  let hdc = unsafe { GetDC(hwnd) };
  if hdc.is_invalid() {
    panic!("device context was invalid");
  }

  match unsafe { GetDpiForWindow(hwnd) } {
    0 => BASE_DPI, // 0 is returned if hwnd is invalid
    dpi => dpi,
  }
}

pub fn register_all_mice_and_keyboards_for_raw_input(hwnd: HWND) -> bool {
  // RIDEV_DEVNOTIFY: receive hotplug events
  // RIDEV_INPUTSINK: receive events even if we're not in the foreground
  // RIDEV_REMOVE: don't receive device events (requires NULL hwndTarget)
  let flags = Input::RIDEV_DEVNOTIFY;

  let devices: [RAWINPUTDEVICE; 2] = [
    RAWINPUTDEVICE {
      usUsagePage: HumanInterfaceDevice::HID_USAGE_PAGE_GENERIC,
      usUsage: HumanInterfaceDevice::HID_USAGE_GENERIC_MOUSE,
      dwFlags: flags,
      hwndTarget: hwnd,
    },
    RAWINPUTDEVICE {
      usUsagePage: HumanInterfaceDevice::HID_USAGE_PAGE_GENERIC,
      usUsage: HumanInterfaceDevice::HID_USAGE_GENERIC_KEYBOARD,
      dwFlags: flags,
      hwndTarget: hwnd,
    },
  ];

  register_raw_input_devices(&devices)
}

pub fn register_raw_input_devices(devices: &[RAWINPUTDEVICE]) -> bool {
  let device_size = std::mem::size_of::<RAWINPUTDEVICE>() as u32;

  unsafe { RegisterRawInputDevices(devices, device_size) }.is_err()
}

pub fn read_raw_input(handle: HRAWINPUT) -> Option<RAWINPUT> {
  let mut data = RAWINPUT::default();
  let mut data_size = std::mem::size_of::<RAWINPUT>() as u32;
  let header_size = std::mem::size_of::<RAWINPUTHEADER>() as u32;

  let status = unsafe {
    GetRawInputData(
      handle,
      Input::RID_INPUT,
      Some(&mut data as *mut _ as _),
      &mut data_size,
      header_size,
    )
  };

  if status == u32::MAX || status == 0 {
    return None;
  }

  Some(data)
}

pub fn is_flag_set<T: Copy + BitAnd<T, Output = T> + PartialEq<T>>(
  var: T,
  flag: T,
) -> bool {
  (var & flag) == flag
}

pub struct Monitor {
  hmonitor: HMONITOR,
}

impl Monitor {
  pub fn new(hmonitor: HMONITOR) -> Self {
    Self { hmonitor }
  }

  fn monitor_info(&self) -> Option<MONITORINFOEXW> {
    let mut monitor_info: MONITORINFOEXW = unsafe { std::mem::zeroed() };
    monitor_info.monitorInfo.cbSize = std::mem::size_of::<MONITORINFOEXW>() as u32;
    let status = unsafe {
      GetMonitorInfoW(
        self.hmonitor,
        &mut monitor_info as *mut MONITORINFOEXW as *mut MONITORINFO,
      )
    };

    if status.as_bool() {
      Some(monitor_info)
    } else {
      None
    }
  }

  pub fn position(&self) -> PhysicalPosition {
    let info = self.monitor_info();
    info
      .map(|info| {
        let rect = info.monitorInfo.rcMonitor;
        PhysicalPosition {
          x: rect.left,
          y: rect.top,
        }
      })
      .unwrap_or_default()
  }

  pub fn size(&self) -> PhysicalSize {
    let info = self.monitor_info();
    info
      .map(|info| {
        let rect = info.monitorInfo.rcMonitor;
        PhysicalSize {
          width: (rect.right - rect.left) as u32,
          height: (rect.bottom - rect.top) as u32,
        }
      })
      .unwrap_or_default()
  }

  pub fn scale_factor(&self) -> f64 {
    let mut dpi_x = 0;
    let mut _dpi_y = 0;
    unsafe {
      GetDpiForMonitor(self.hmonitor, HiDpi::MDT_EFFECTIVE_DPI, &mut dpi_x, &mut _dpi_y)
    }
    .unwrap();

    dpi_to_scale_factor(dpi_x)
  }
}

pub(crate) fn to_windows_cursor(cursor: CursorIcon) -> PCWSTR {
  match cursor {
    CursorIcon::Default => WindowsAndMessaging::IDC_ARROW,
    CursorIcon::Pointer => WindowsAndMessaging::IDC_HAND,
    CursorIcon::Crosshair => WindowsAndMessaging::IDC_CROSS,
    CursorIcon::Text | CursorIcon::VerticalText => WindowsAndMessaging::IDC_IBEAM,
    CursorIcon::NotAllowed | CursorIcon::NoDrop => WindowsAndMessaging::IDC_NO,
    CursorIcon::Grab
    | CursorIcon::Grabbing
    | CursorIcon::Move
    | CursorIcon::AllScroll => WindowsAndMessaging::IDC_SIZEALL,
    CursorIcon::EResize
    | CursorIcon::WResize
    | CursorIcon::EwResize
    | CursorIcon::ColResize => WindowsAndMessaging::IDC_SIZEWE,
    CursorIcon::NResize
    | CursorIcon::SResize
    | CursorIcon::NsResize
    | CursorIcon::RowResize => WindowsAndMessaging::IDC_SIZENS,
    CursorIcon::NeResize | CursorIcon::SwResize | CursorIcon::NeswResize => {
      WindowsAndMessaging::IDC_SIZENESW
    }
    CursorIcon::NwResize | CursorIcon::SeResize | CursorIcon::NwseResize => {
      WindowsAndMessaging::IDC_SIZENWSE
    }
    CursorIcon::Wait => WindowsAndMessaging::IDC_WAIT,
    CursorIcon::Progress => WindowsAndMessaging::IDC_APPSTARTING,
    CursorIcon::Help => WindowsAndMessaging::IDC_HELP,
    _ => WindowsAndMessaging::IDC_ARROW, // use arrow for the missing cases.
  }
}