Skip to content

Commit e7fb167

Browse files
committed
fix: Windows build errors in wm
1 parent dc75b2b commit e7fb167

File tree

6 files changed

+39
-55
lines changed

6 files changed

+39
-55
lines changed

packages/wm-platform/src/dispatcher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ impl Dispatcher {
509509
}
510510
}
511511

512-
/// Gets the window at the specified point.
512+
/// Gets the top-level window at the specified point.
513513
pub fn window_from_point(
514514
&self,
515515
point: &Point,

packages/wm-platform/src/platform_impl/windows/display.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,21 @@ pub trait DisplayExtWindows {
3737

3838
/// Windows-specific extensions for `DisplayDevice`.
3939
pub trait DisplayDeviceExtWindows {
40-
/// Gets the device interface path.
40+
/// Gets the device path.
4141
///
4242
/// This can be an empty string for virtual display devices.
4343
///
4444
/// # Platform-specific
4545
///
4646
/// This method is only available on Windows.
47-
fn device_interface_path(&self) -> &str;
47+
fn device_path(&self) -> Option<String>;
4848

49-
/// Gets the hardware ID from the device interface path.
49+
/// Gets the hardware ID from the device path.
5050
///
5151
/// # Example usage
5252
///
5353
/// ```rust,no_run
54-
/// device.device_interface_path(); // "\\?\DISPLAY#DEL40A3#5&1234abcd&0&UID256#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}"
54+
/// device.device_path(); // "\\?\DISPLAY#DEL40A3#5&1234abcd&0&UID256#{e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}"
5555
/// device.hardware_id(); // Some("DEL40A3")
5656
/// ```
5757
///
@@ -75,8 +75,8 @@ impl DisplayExtWindows for crate::Display {
7575
}
7676

7777
impl DisplayDeviceExtWindows for crate::DisplayDevice {
78-
fn device_interface_path(&self) -> &str {
79-
&self.inner.device_interface_path
78+
fn device_path(&self) -> Option<String> {
79+
self.inner.device_path.clone()
8080
}
8181

8282
fn hardware_id(&self) -> Option<String> {
@@ -184,8 +184,8 @@ impl Display {
184184
};
185185

186186
// When passing the `EDD_GET_DEVICE_INTERFACE_NAME` flag, the
187-
// returned `DISPLAY_DEVICEW` will contain the device interface
188-
// path in the `DeviceID` field.
187+
// returned `DISPLAY_DEVICEW` will contain the device path in the
188+
// `DeviceID` field.
189189
unsafe {
190190
EnumDisplayDevicesW(
191191
PCWSTR(monitor_info.szDevice.as_ptr()),
@@ -200,14 +200,7 @@ impl Display {
200200
// Filter out any devices that are not active.
201201
.filter(|device| device.StateFlags & DISPLAY_DEVICE_ACTIVE != 0)
202202
.map(|device| {
203-
// NOTE: This may be an empty string for virtual display devices.
204-
let device_interface_path =
205-
String::from_utf16_lossy(&device.DeviceID)
206-
.trim_end_matches('\0')
207-
.to_string();
208-
209-
DisplayDevice::new(adapter_name.clone(), device_interface_path)
210-
.into()
203+
DisplayDevice::new(adapter_name.clone(), &device.DeviceID).into()
211204
})
212205
.collect();
213206

@@ -274,19 +267,25 @@ pub(crate) struct DisplayDevice {
274267
/// Device interface path (e.g.
275268
/// `\\?\DISPLAY#DEL40A3#5&1234abcd&0&UID256#
276269
/// {e6f07b5f-ee97-4a90-b076-33f57bf4eaa7}`).
277-
device_interface_path: String,
270+
device_path: Option<String>,
278271
}
279272

280273
impl DisplayDevice {
281274
/// Windows-specific implementation of [`DisplayDevice::new`].
282275
#[must_use]
283-
pub(crate) fn new(
284-
adapter_name: String,
285-
device_interface_path: String,
286-
) -> Self {
276+
pub(crate) fn new(adapter_name: String, device_path: &[u16]) -> Self {
277+
// NOTE: This may be an empty string for virtual display devices.
278+
let device_path = String::from_utf16_lossy(device_path)
279+
.trim_end_matches('\0')
280+
.to_string();
281+
282+
// Check that the device path is valid. If not, set it to `None`.
283+
let device_path =
284+
(device_path.split('#').count() >= 4).then_some(device_path);
285+
287286
Self {
288287
adapter_name,
289-
device_interface_path,
288+
device_path,
290289
}
291290
}
292291

@@ -304,7 +303,8 @@ impl DisplayDevice {
304303
/// [`DisplayDeviceExtWindows::hardware_id`].
305304
fn hardware_id(&self) -> Option<String> {
306305
self
307-
.device_interface_path
306+
.device_path
307+
.as_deref()?
308308
.split('#')
309309
.nth(1)
310310
.map(ToString::to_string)

packages/wm-platform/src/platform_impl/windows/native_window.rs

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,6 @@ pub trait NativeWindowWindowsExt {
227227
&self,
228228
opacity_delta: &Delta<OpacityValue>,
229229
) -> crate::Result<()>;
230-
231-
/// Gets the root ancestor (top-level) window.
232-
///
233-
/// # Platform-specific
234-
///
235-
/// This method is only available on Windows.
236-
fn root_window(&self) -> crate::Result<crate::NativeWindow>;
237230
}
238231

239232
impl NativeWindowWindowsExt for crate::NativeWindow {
@@ -334,18 +327,6 @@ impl NativeWindowWindowsExt for crate::NativeWindow {
334327
) -> crate::Result<()> {
335328
self.inner.adjust_transparency(opacity_delta)
336329
}
337-
338-
fn root_window(&self) -> crate::Result<crate::NativeWindow> {
339-
let handle = unsafe { GetAncestor(self.inner.hwnd(), GA_ROOT) };
340-
341-
if handle.0 == 0 {
342-
return Err(crate::Error::Platform(
343-
"Failed to get root ancestor window.".to_string(),
344-
));
345-
}
346-
347-
Ok(NativeWindow::new(handle.0).into())
348-
}
349330
}
350331

351332
#[derive(Clone, Debug)]
@@ -1096,12 +1077,16 @@ pub(crate) fn window_from_point(
10961077
};
10971078

10981079
let handle = unsafe { WindowFromPoint(point) };
1099-
11001080
if handle.0 == 0 {
11011081
return Ok(None);
11021082
}
11031083

1104-
Ok(Some(NativeWindow::new(handle.0).into()))
1084+
let root = unsafe { GetAncestor(handle, GA_ROOT) };
1085+
if root.0 == 0 {
1086+
return Ok(None);
1087+
}
1088+
1089+
Ok(Some(NativeWindow::new(root.0).into()))
11051090
}
11061091

11071092
/// Windows-specific implementation of [`Dispatcher::reset_focus`].

packages/wm/src/events/handle_display_settings_changed.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use anyhow::Context;
2-
#[cfg(target_os = "windows")]
3-
use wm_platform::{DisplayDeviceExtWindows, DisplayExtWindows};
42

53
use crate::{
64
commands::monitor::{
@@ -115,20 +113,21 @@ fn find_matching_monitor<'a>(
115113
#[cfg(target_os = "windows")]
116114
{
117115
existing.handle == properties.handle
118-
|| existing.device_path.is_some_and(|device_path| {
119-
device_path == properties.device_path()
116+
|| existing.device_path.as_deref().is_some_and(|device_path| {
117+
properties.device_path.as_deref() == Some(device_path)
120118
})
121-
|| existing.hardware_id.is_some_and(|hardware_id| {
119+
|| existing.hardware_id.as_deref().is_some_and(|hardware_id| {
122120
let is_unique = monitors
123121
.iter()
124122
.filter(|other_monitor| {
125-
other_monitor.native_properties().hardware_id
126-
== hardware_id
123+
other_monitor.native_properties().hardware_id.as_deref()
124+
== Some(hardware_id)
127125
})
128126
.count()
129127
== 1;
130128

131-
is_unique && hardware_id == properties.hardware_id
129+
is_unique
130+
&& properties.hardware_id.as_deref() == Some(hardware_id)
132131
})
133132
}
134133
};

packages/wm/src/events/handle_mouse_move.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ pub fn handle_mouse_move(
8686
state
8787
.dispatcher
8888
.window_from_point(position)?
89-
.map(|native| native.root_window())
90-
.ok()
9189
.and_then(|native| state.window_from_native(&native))
9290
}
9391
};

packages/wm/src/models/native_monitor_properties.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use wm_platform::{Display, Rect};
2+
#[cfg(target_os = "windows")]
3+
use wm_platform::{DisplayDeviceExtWindows, DisplayExtWindows};
24

35
#[derive(Debug, Clone)]
46
pub struct NativeMonitorProperties {

0 commit comments

Comments
 (0)