Skip to content

Commit

Permalink
refactor: update deps, especially windows-rs v0.58.0 (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 21, 2024
1 parent 0c99b27 commit 47543c4
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 104 deletions.
101 changes: 59 additions & 42 deletions 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ simple-logging = "2.0.2"
xml = "0.8.10"

[dependencies.windows]
version = "0.56.0"
version = "0.58.0"
features = [
"Win32_Foundation",
"Win32_UI_WindowsAndMessaging",
Expand Down
31 changes: 17 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::trayicon::TrayIcon;
use crate::utils::{
check_error, create_hicon_from_resource, get_foreground_window, get_module_icon,
get_module_icon_ex, get_uwp_icon_data, get_window_user_data, is_iconic_window,
is_running_as_admin, list_windows, set_foreground_window, set_window_user_data, CheckError,
is_running_as_admin, list_windows, set_foreground_window, set_window_user_data,
};

use crate::painter::{GdiAAPainter, ICON_BORDER_SIZE, WINDOW_BORDER_SIZE};
Expand Down Expand Up @@ -108,7 +108,7 @@ impl App {
fn eventloop() -> Result<()> {
let mut message = MSG::default();
loop {
let ret = unsafe { GetMessageW(&mut message, HWND(0), 0, 0) };
let ret = unsafe { GetMessageW(&mut message, HWND::default(), 0, 0) };
match ret.0 {
-1 => {
unsafe { GetLastError() }.ok()?;
Expand Down Expand Up @@ -138,14 +138,13 @@ impl App {
..Default::default()
};

let atom = unsafe { RegisterClassW(&window_class) }
.check_error()
let atom = check_error(|| unsafe { RegisterClassW(&window_class) })
.map_err(|err| anyhow!("Failed to register class, {err}"))?;

let hwnd = unsafe {
CreateWindowExW(
WS_EX_TOOLWINDOW,
PCWSTR(atom as *mut u16),
PCWSTR(atom as _),
NAME,
WINDOW_STYLE(0),
CW_USEDEFAULT,
Expand All @@ -158,7 +157,6 @@ impl App {
None,
)
}
.check_error()
.map_err(|err| anyhow!("Failed to create windows, {err}"))?;

// hide caption
Expand All @@ -176,11 +174,16 @@ impl App {
Err(err) => {
if !trayicon.exist() {
error!("{err}, retrying in 3 second");
let hwnd = self.hwnd;
let hwnd = self.hwnd.0 as isize;
std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_secs(3));
let _ = unsafe {
PostMessageW(hwnd, WM_USER_REGISTER_TRAYICON, WPARAM(0), LPARAM(0))
PostMessageW(
HWND(hwnd as _),
WM_USER_REGISTER_TRAYICON,
WPARAM(0),
LPARAM(0),
)
};
});
}
Expand Down Expand Up @@ -342,7 +345,7 @@ impl App {
} else {
state_id = *cache_id;
let mut windows_set: IndexSet<isize> =
windows.iter().map(|(v, _)| v.0).collect();
windows.iter().map(|(v, _)| v.0 as _).collect();
for id in cache_windows {
if windows_set.contains(id) {
state_windows.push(*id);
Expand All @@ -366,9 +369,9 @@ impl App {
}
}
if state_windows.is_empty() {
state_windows = windows.iter().map(|(v, _)| v.0).collect();
state_windows = windows.iter().map(|(v, _)| v.0 as _).collect();
}
let hwnd = HWND(state_windows[index]);
let hwnd = HWND(state_windows[index] as _);
self.switch_windows_state = SwitchWindowsState {
cache: Some((module_path.clone(), state_id, index, state_windows)),
modifier_released: false,
Expand Down Expand Up @@ -452,10 +455,10 @@ impl App {

unsafe {
// Change busy cursor to array cursor
if let Ok(hcursor) = LoadCursorW(HMODULE(0), IDC_ARROW) {
if let Ok(hcursor) = LoadCursorW(HMODULE::default(), IDC_ARROW) {
SetCursor(hcursor);
}
SetFocus(hwnd);
let _ = SetFocus(hwnd);
let _ = SetWindowPos(
hwnd,
HWND_TOPMOST,
Expand Down Expand Up @@ -542,7 +545,7 @@ fn get_app(hwnd: HWND) -> Result<&'static mut App> {
unsafe {
let ptr = check_error(|| get_window_user_data(hwnd))
.map_err(|err| anyhow!("Failed to get window ptr, {err}"))?;
let tx: &mut App = &mut *(ptr as *mut _);
let tx: &mut App = &mut *(ptr as *mut App);
Ok(tx)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use windows::Win32::UI::{
};

static mut KEYBOARD_STATE: Vec<HotKeyState> = vec![];
static mut WINDOW: HWND = HWND(0);
static mut WINDOW: HWND = HWND(0 as _);
static mut IS_SHIFT_PRESSED: bool = false;
static mut PREVIOUS_KEYCODE: u16 = 0;

Expand Down
Loading

0 comments on commit 47543c4

Please sign in to comment.