Skip to content

Commit

Permalink
chore(deps): update window-sys to 0.59 (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Aug 15, 2024
1 parent 11d8b7a commit 32bff56
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 108 deletions.
5 changes: 5 additions & 0 deletions .changes/windows-0.59.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"muda": "patch"
---

Update `window-sys` crate to `0.59`
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ serde = { version = "1", optional = true }
dpi = "0.1"

[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
version = "0.52"
version = "0.59"
features = [
"Win32_UI_WindowsAndMessaging",
"Win32_Foundation",
Expand All @@ -45,7 +45,7 @@ gtk = "0.18"
libxdo = { version = "0.6.0", optional = true }

[target."cfg(target_os = \"macos\")".dependencies]
cocoa = "0.25"
cocoa = "0.26"
objc = "0.2"
png = "0.17"

Expand Down
2 changes: 1 addition & 1 deletion examples/tao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
unsafe {
let msg = msg as *const MSG;
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel(), msg);
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel() as _, msg);
translated == 1
}
});
Expand Down
6 changes: 3 additions & 3 deletions examples/windows-common-controls-v6/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ edition = "2021"

[dependencies]
muda = { path = "../../", features = ["common-controls-v6"] }
tao = "0.26"
image = "0.24"
tao = "0.28"
image = "0.25"

[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
version = "0.52"
version = "0.59"
features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"]

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/windows-common-controls-v6/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
unsafe {
let msg = msg as *const MSG;
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel(), msg);
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel() as _, msg);
translated == 1
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
unsafe {
let msg = msg as *const MSG;
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel(), msg);
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel() as _, msg);
translated == 1
}
});
Expand Down
2 changes: 1 addition & 1 deletion examples/wry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn main() -> wry::Result<()> {
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
unsafe {
let msg = msg as *const MSG;
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel(), msg);
let translated = TranslateAcceleratorW((*msg).hwnd, menu_bar.haccel() as _, msg);
translated == 1
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/items/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl Submenu {

impl ContextMenu for Submenu {
#[cfg(target_os = "windows")]
fn hpopupmenu(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HMENU {
fn hpopupmenu(&self) -> isize {
self.inner.borrow().hpopupmenu()
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub trait ContextMenu {
///
/// [`HMENU`]: windows_sys::Win32::UI::WindowsAndMessaging::HMENU
#[cfg(target_os = "windows")]
fn hpopupmenu(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HMENU;
fn hpopupmenu(&self) -> isize;

/// Shows this menu as a context menu inside a win32 window.
///
Expand Down
8 changes: 4 additions & 4 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ impl Menu {
/// let menu = Menu::new();
/// unsafe {
/// let mut msg: MSG = std::mem::zeroed();
/// while GetMessageW(&mut msg, 0, 0, 0) == 1 {
/// let translated = TranslateAcceleratorW(msg.hwnd, menu.haccel(), &msg as *const _);
/// while GetMessageW(&mut msg, std::ptr::null_mut(), 0, 0) == 1 {
/// let translated = TranslateAcceleratorW(msg.hwnd, menu.haccel() as _, &msg as *const _);
/// if translated != 1{
/// TranslateMessage(&msg);
/// DispatchMessageW(&msg);
Expand Down Expand Up @@ -247,7 +247,7 @@ impl Menu {
/// It can be used with [`TranslateAcceleratorW`](windows_sys::Win32::UI::WindowsAndMessaging::TranslateAcceleratorW)
/// in the event loop to enable accelerators
#[cfg(target_os = "windows")]
pub fn haccel(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HACCEL {
pub fn haccel(&self) -> isize {
self.inner.borrow_mut().haccel()
}

Expand Down Expand Up @@ -336,7 +336,7 @@ impl Menu {

impl ContextMenu for Menu {
#[cfg(target_os = "windows")]
fn hpopupmenu(&self) -> windows_sys::Win32::UI::WindowsAndMessaging::HMENU {
fn hpopupmenu(&self) -> isize {
self.inner.borrow().hpopupmenu()
}

Expand Down
35 changes: 20 additions & 15 deletions src/platform_impl/windows/dark_menu_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use once_cell::sync::Lazy;
use windows_sys::{
s,
Win32::{
Foundation::{HMODULE, HWND, LPARAM, RECT, WPARAM},
Foundation::{HWND, LPARAM, RECT, WPARAM},
Graphics::Gdi::*,
System::LibraryLoader::{GetProcAddress, LoadLibraryA},
UI::{
Expand Down Expand Up @@ -97,7 +97,7 @@ fn selected_background_brush() -> HBRUSH {
}

/// Draws a dark menu bar if needed and returns whether it draws it or not
pub fn draw(hwnd: HWND, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
pub fn draw(hwnd: super::Hwnd, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
match msg {
// draw over the annoying white line blow menubar
// ref: https://github.com/notepad-plus-plus/notepad-plus-plus/pull/9985
Expand All @@ -106,16 +106,21 @@ pub fn draw(hwnd: HWND, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
cbSize: std::mem::size_of::<MENUBARINFO>() as _,
..unsafe { std::mem::zeroed() }
};
unsafe { GetMenuBarInfo(hwnd, OBJID_MENU, 0, &mut mbi) };
unsafe { GetMenuBarInfo(hwnd as _, OBJID_MENU, 0, &mut mbi) };

let mut client_rc: RECT = unsafe { std::mem::zeroed() };
unsafe {
GetClientRect(hwnd, &mut client_rc);
MapWindowPoints(hwnd, 0, &mut client_rc as *mut _ as *mut _, 2);
GetClientRect(hwnd as _, &mut client_rc);
MapWindowPoints(
hwnd as _,
std::ptr::null_mut(),
&mut client_rc as *mut _ as *mut _,
2,
);
};

let mut window_rc: RECT = unsafe { std::mem::zeroed() };
unsafe { GetWindowRect(hwnd, &mut window_rc) };
unsafe { GetWindowRect(hwnd as _, &mut window_rc) };

unsafe { OffsetRect(&mut client_rc, -window_rc.left, -window_rc.top) };

Expand All @@ -124,9 +129,9 @@ pub fn draw(hwnd: HWND, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
annoying_rc.top -= 1;

unsafe {
let hdc = GetWindowDC(hwnd);
let hdc = GetWindowDC(hwnd as _);
FillRect(hdc, &annoying_rc, background_brush());
ReleaseDC(hwnd, hdc);
ReleaseDC(hwnd as _, hdc);
}
}

Expand All @@ -140,10 +145,10 @@ pub fn draw(hwnd: HWND, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
cbSize: std::mem::size_of::<MENUBARINFO>() as _,
..unsafe { std::mem::zeroed() }
};
unsafe { GetMenuBarInfo(hwnd, OBJID_MENU, 0, &mut mbi) };
unsafe { GetMenuBarInfo(hwnd as _, OBJID_MENU, 0, &mut mbi) };

let mut window_rc: RECT = unsafe { std::mem::zeroed() };
unsafe { GetWindowRect(hwnd, &mut window_rc) };
unsafe { GetWindowRect(hwnd as _, &mut window_rc) };

let mut rc = mbi.rcBar;
// the rcBar is offset by the window rect
Expand Down Expand Up @@ -244,11 +249,11 @@ pub fn draw(hwnd: HWND, msg: u32, _wparam: WPARAM, lparam: LPARAM) {
};
}

pub fn should_use_dark_mode(hwnd: HWND) -> bool {
should_apps_use_dark_mode() && !is_high_contrast() && is_dark_mode_allowed_for_window(hwnd)
pub fn should_use_dark_mode(hwnd: super::Hwnd) -> bool {
should_apps_use_dark_mode() && !is_high_contrast() && is_dark_mode_allowed_for_window(hwnd as _)
}

static HUXTHEME: Lazy<HMODULE> = Lazy::new(|| unsafe { LoadLibraryA(s!("uxtheme.dll")) });
static HUXTHEME: Lazy<isize> = Lazy::new(|| unsafe { LoadLibraryA(s!("uxtheme.dll")) as _ });

fn should_apps_use_dark_mode() -> bool {
const UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL: u16 = 132;
Expand All @@ -259,7 +264,7 @@ fn should_apps_use_dark_mode() -> bool {
}

GetProcAddress(
*HUXTHEME,
(*HUXTHEME) as *mut _,
UXTHEME_SHOULDAPPSUSEDARKMODE_ORDINAL as usize as *mut _,
)
.map(|handle| std::mem::transmute(handle))
Expand All @@ -280,7 +285,7 @@ fn is_dark_mode_allowed_for_window(hwnd: HWND) -> bool {
}

GetProcAddress(
*HUXTHEME,
(*HUXTHEME) as *mut _,
UXTHEME_ISDARKMODEALLOWEDFORWINDOW_ORDINAL as usize as *mut _,
)
.map(|handle| std::mem::transmute(handle))
Expand Down
28 changes: 18 additions & 10 deletions src/platform_impl/windows/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl RgbaIcon {
assert_eq!(and_mask.len(), pixel_count);
let handle = unsafe {
CreateIcon(
0,
std::ptr::null_mut(),
self.width as i32,
self.height as i32,
1,
Expand All @@ -54,7 +54,7 @@ impl RgbaIcon {
rgba.as_ptr(),
)
};
if handle != 0 {
if !handle.is_null() {
Ok(WinIcon::from_handle(handle))
} else {
Err(BadIcon::OsError(io::Error::last_os_error()))
Expand All @@ -76,7 +76,7 @@ unsafe impl Send for WinIcon {}

impl WinIcon {
pub unsafe fn to_hbitmap(&self) -> HBITMAP {
let hdc = CreateCompatibleDC(0);
let hdc = CreateCompatibleDC(std::ptr::null_mut());

let rc = RECT {
left: 0,
Expand All @@ -93,11 +93,18 @@ impl WinIcon {
bitmap_info.bmiHeader.biBitCount = 32;
bitmap_info.bmiHeader.biCompression = BI_RGB as _;

let h_dc_bitmap = GetDC(0);
let h_dc_bitmap = GetDC(std::ptr::null_mut());

let hbitmap = CreateDIBSection(h_dc_bitmap, &bitmap_info, DIB_RGB_COLORS, 0 as _, 0, 0);
let hbitmap = CreateDIBSection(
h_dc_bitmap,
&bitmap_info,
DIB_RGB_COLORS,
0 as _,
std::ptr::null_mut(),
0,
);

ReleaseDC(0, h_dc_bitmap);
ReleaseDC(std::ptr::null_mut(), h_dc_bitmap);

let h_bitmap_old = SelectObject(hdc, hbitmap);

Expand All @@ -109,7 +116,7 @@ impl WinIcon {
rc.right,
rc.bottom,
0,
0,
std::ptr::null_mut(),
DI_NORMAL,
);

Expand All @@ -126,6 +133,7 @@ impl WinIcon {

fn from_handle(handle: HICON) -> Self {
Self {
#[allow(clippy::arc_with_non_send_sync)]
inner: Arc::new(RaiiIcon { handle }),
}
}
Expand All @@ -141,15 +149,15 @@ impl WinIcon {

let handle = unsafe {
LoadImageW(
0,
std::ptr::null_mut(),
wide_path.as_ptr(),
IMAGE_ICON,
width as i32,
height as i32,
LR_DEFAULTSIZE | LR_LOADFROMFILE,
)
};
if handle != 0 {
if !handle.is_null() {
Ok(WinIcon::from_handle(handle as HICON))
} else {
Err(BadIcon::OsError(io::Error::last_os_error()))
Expand All @@ -172,7 +180,7 @@ impl WinIcon {
LR_DEFAULTSIZE,
)
};
if handle != 0 {
if !handle.is_null() {
Ok(WinIcon::from_handle(handle as HICON))
} else {
Err(BadIcon::OsError(io::Error::last_os_error()))
Expand Down
Loading

0 comments on commit 32bff56

Please sign in to comment.