Skip to content

Commit

Permalink
migrate from windows-sys to windows crate
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed May 23, 2024
1 parent 39568ae commit 4f4be5e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion v2/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion v2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ window-vibrancy = "0.5.0"
wry = "0.40.0"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_UI_WindowsAndMessaging"] }
windows = { version = "0.56.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_UI_WindowsAndMessaging"] }

[dev-dependencies]
insta = { version = "1.34.0", features = ["json"] }
Expand Down
15 changes: 11 additions & 4 deletions v2/src/windows.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use windows_sys::Win32::System::Console::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};
use windows::Win32::System::Console::{AttachConsole, FreeConsole, ATTACH_PARENT_PROCESS};

pub struct WindowsConsole {
success: bool,
Expand All @@ -8,8 +8,13 @@ impl WindowsConsole {
pub fn attach() -> Self {
// SAFETY: Using Windows C API is always unsafe. I confirmed the usage in official document.
// https://learn.microsoft.com/en-us/windows/console/attachconsole
let success = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) != 0 };
Self { success }
match unsafe { AttachConsole(ATTACH_PARENT_PROCESS) } {
Ok(()) => Self { success: true },
Err(err) => {
log::error!("Failed to attach to console: {err}");
Self { success: false }
}
}
}
}

Expand All @@ -18,7 +23,9 @@ impl Drop for WindowsConsole {
if self.success {
// SAFETY: Using Windows C API is always unsafe. I confirmed the usage in official document.
// https://learn.microsoft.com/en-us/windows/console/freeconsole
unsafe { FreeConsole() };
if let Err(err) = unsafe { FreeConsole() } {
log::error!("Failed to free console: {err}");
}
}
}
}
6 changes: 3 additions & 3 deletions v2/src/wry/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Rendering for Wry {
#[cfg(target_os = "windows")]
fn new() -> Result<Self> {
use tao::platform::windows::EventLoopBuilderExtWindows;
use windows_sys::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, MSG};
use windows::Win32::UI::WindowsAndMessaging::{TranslateAcceleratorW, HACCEL, MSG};

let mut menu_events = MenuEvents::new();
let menu = Menu::new(&mut menu_events)?;
Expand All @@ -44,8 +44,8 @@ impl Rendering for Wry {
EventLoopBuilder::with_user_event()
.with_msg_hook(move |msg| {
let msg = msg as *const MSG;
let haccel = menu.haccel();
// SAFETY: `msg` pointer was given by `EventLoopBuilder::with_user_event` which internally receives
let haccel = HACCEL(menu.haccel());
// SAFETY: `msg` pointer was given by `EventLoopBuilder::with_msg_hook` which internally receives
// events via message loop. `haccel` is validated by muda's API.
// Ref: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-translateacceleratorw
let translated = unsafe { TranslateAcceleratorW((*msg).hwnd, haccel, msg) };
Expand Down

0 comments on commit 4f4be5e

Please sign in to comment.