From 47543c42ec7031564e74287fd2190dbcbf3b1ba5 Mon Sep 17 00:00:00 2001 From: sigoden Date: Mon, 21 Oct 2024 13:56:01 +0800 Subject: [PATCH] refactor: update deps, especially windows-rs v0.58.0 (#139) --- Cargo.lock | 101 +++++++++++++++++------------- Cargo.toml | 2 +- src/app.rs | 31 ++++----- src/keyboard.rs | 2 +- src/utils/check_error.rs | 38 +---------- src/utils/window.rs | 6 +- tools/inspect-windows/Cargo.toml | 2 +- tools/inspect-windows/src/main.rs | 10 +-- 8 files changed, 88 insertions(+), 104 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e967a9..0cc9703 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,15 +4,15 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.86" +version = "1.0.90" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" +checksum = "37bf3594c4c988a53154954629820791dde498571819ae4ca50ca811e060cc95" [[package]] name = "cc" -version = "1.1.15" +version = "1.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" dependencies = [ "shlex", ] @@ -60,9 +60,9 @@ dependencies = [ [[package]] name = "embed-resource" -version = "2.4.3" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4edcacde9351c33139a41e3c97eb2334351a81a2791bebb0b243df837128f602" +checksum = "f4e24052d7be71f0efb50c201557f6fe7d237cfd5a64fd5bcd7fd8fe32dbbffa" dependencies = [ "cc", "memchr", @@ -95,14 +95,20 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "hashbrown" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" + [[package]] name = "indexmap" -version = "2.5.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.15.0", ] [[package]] @@ -123,9 +129,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.158" +version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" [[package]] name = "log" @@ -141,9 +147,9 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775" [[package]] name = "ordered-multimap" @@ -152,14 +158,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" dependencies = [ "dlv-list", - "hashbrown", + "hashbrown 0.14.5", ] [[package]] name = "proc-macro2" -version = "1.0.86" +version = "1.0.88" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +checksum = "7c3a7fc5db1e57d5a779a352c8cdb57b29aa4c40cc69c3a68a7fedc815fbf2f9" dependencies = [ "unicode-ident", ] @@ -207,18 +213,18 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" [[package]] name = "serde" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09" +checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.209" +version = "1.0.210" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170" +checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", @@ -227,9 +233,9 @@ dependencies = [ [[package]] name = "serde_spanned" -version = "0.6.7" +version = "0.6.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d" +checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1" dependencies = [ "serde", ] @@ -253,9 +259,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.77" +version = "2.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" dependencies = [ "proc-macro2", "quote", @@ -305,9 +311,9 @@ dependencies = [ [[package]] name = "toml_edit" -version = "0.22.20" +version = "0.22.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d" +checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5" dependencies = [ "indexmap", "serde", @@ -324,9 +330,9 @@ checksum = "343e926fc669bc8cde4fa3129ab681c63671bae288b1f1081ceee6d9d37904fc" [[package]] name = "unicode-ident" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" [[package]] name = "vswhom" @@ -393,9 +399,9 @@ dependencies = [ [[package]] name = "windows" -version = "0.56.0" +version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de69df01bdf1ead2f4ac895dc77c9351aefff65b2f3db429a343f9cbf05e132" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" dependencies = [ "windows-core", "windows-targets 0.52.6", @@ -403,21 +409,22 @@ dependencies = [ [[package]] name = "windows-core" -version = "0.56.0" +version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4698e52ed2d08f8658ab0c39512a7c00ee5fe2688c65f8c0a4f06750d729f2a6" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" dependencies = [ "windows-implement", "windows-interface", "windows-result", + "windows-strings", "windows-targets 0.52.6", ] [[package]] name = "windows-implement" -version = "0.56.0" +version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" dependencies = [ "proc-macro2", "quote", @@ -426,9 +433,9 @@ dependencies = [ [[package]] name = "windows-interface" -version = "0.56.0" +version = "0.58.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" dependencies = [ "proc-macro2", "quote", @@ -437,13 +444,23 @@ dependencies = [ [[package]] name = "windows-result" -version = "0.1.2" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -576,9 +593,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.6.18" +version = "0.6.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f" +checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b" dependencies = [ "memchr", ] @@ -604,6 +621,6 @@ dependencies = [ [[package]] name = "xml-rs" -version = "0.8.21" +version = "0.8.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "539a77ee7c0de333dcc6da69b177380a0b81e0dacfa4f7344c465a36871ee601" +checksum = "af4e2e2f7cba5a093896c1e150fbfe177d1883e7448200efb81d40b9d339ef26" diff --git a/Cargo.toml b/Cargo.toml index 9f9d40c..1b1a0dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", diff --git a/src/app.rs b/src/app.rs index 63f3629..c128445 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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}; @@ -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()?; @@ -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, @@ -158,7 +157,6 @@ impl App { None, ) } - .check_error() .map_err(|err| anyhow!("Failed to create windows, {err}"))?; // hide caption @@ -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), + ) }; }); } @@ -342,7 +345,7 @@ impl App { } else { state_id = *cache_id; let mut windows_set: IndexSet = - 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); @@ -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, @@ -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, @@ -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) } } diff --git a/src/keyboard.rs b/src/keyboard.rs index 32af55f..ce464c2 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -19,7 +19,7 @@ use windows::Win32::UI::{ }; static mut KEYBOARD_STATE: Vec = 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; diff --git a/src/utils/check_error.rs b/src/utils/check_error.rs index 5ac0fcc..6db1f5b 100644 --- a/src/utils/check_error.rs +++ b/src/utils/check_error.rs @@ -1,5 +1,5 @@ use windows::core::Error; -use windows::Win32::Foundation::{SetLastError, ERROR_SUCCESS, HANDLE, HWND}; +use windows::Win32::Foundation::{SetLastError, ERROR_SUCCESS}; #[allow(unused)] #[inline] @@ -22,39 +22,3 @@ where } } } - -pub trait CheckError: Sized { - fn check_error(self) -> windows::core::Result; -} - -impl CheckError for HANDLE { - fn check_error(self) -> windows::core::Result { - if self.is_invalid() { - Err(Error::from_win32()) - } else { - Ok(self) - } - } -} - -impl CheckError for HWND { - fn check_error(self) -> windows::core::Result { - // If the function fails, the return value is NULL. - if self.0 == 0 { - Err(Error::from_win32()) - } else { - Ok(self) - } - } -} - -impl CheckError for u16 { - fn check_error(self) -> windows::core::Result { - // If the function fails, the return value is zero - if self == 0 { - Err(Error::from_win32()) - } else { - Ok(self) - } - } -} diff --git a/src/utils/window.rs b/src/utils/window.rs index 44a985a..ab26e73 100644 --- a/src/utils/window.rs +++ b/src/utils/window.rs @@ -151,13 +151,13 @@ pub fn get_window_title(hwnd: HWND) -> String { } pub fn get_owner_window(hwnd: HWND) -> HWND { - unsafe { GetWindow(hwnd, GW_OWNER) } + unsafe { GetWindow(hwnd, GW_OWNER) }.unwrap_or_default() } pub fn get_module_icon(hwnd: HWND) -> Option { let ret = unsafe { SendMessageW(hwnd, WM_GETICON, WPARAM(ICON_BIG as _), None) }.0; if ret != 0 { - return Some(HICON(ret)); + return Some(HICON(ret as _)); } let ret = get_class_icon(hwnd); @@ -255,7 +255,7 @@ pub fn list_windows(ignore_minimal: bool) -> Result BOOL { - let windows: &mut Vec = unsafe { &mut *(lparam.0 as *mut _) }; + let windows: &mut Vec = unsafe { &mut *(lparam.0 as *mut Vec) }; windows.push(hwnd); BOOL(1) } diff --git a/tools/inspect-windows/Cargo.toml b/tools/inspect-windows/Cargo.toml index 1b94cb0..79284c5 100644 --- a/tools/inspect-windows/Cargo.toml +++ b/tools/inspect-windows/Cargo.toml @@ -11,7 +11,7 @@ indexmap = "2.2.3" window-switcher = { path = "../.."} [dependencies.windows] -version = "0.56.0" +version = "0.58.0" features = [ "Win32_Foundation", "Win32_UI_WindowsAndMessaging", diff --git a/tools/inspect-windows/src/main.rs b/tools/inspect-windows/src/main.rs index 51fd0ec..9868357 100644 --- a/tools/inspect-windows/src/main.rs +++ b/tools/inspect-windows/src/main.rs @@ -37,9 +37,9 @@ impl WindowInfo { pretty_bool(self.is_iconic), pretty_bool(self.is_topmost), size, - self.hwnd.0, + self.hwnd.0 as isize, self.title, - self.owner_hwnd.0, + self.owner_hwnd.0 as isize, self.owner_title ) } @@ -57,8 +57,8 @@ fn collect_windows_info() -> anyhow::Result> { let is_topmost = is_topmost_window(hwnd); let is_visible = is_visible_window(hwnd); let (width, height) = get_window_size(hwnd); - let owner_hwnd: HWND = unsafe { GetWindow(hwnd, GW_OWNER) }; - let owner_title = if owner_hwnd.0 > 0 { + let owner_hwnd: HWND = unsafe { GetWindow(hwnd, GW_OWNER) }.unwrap_or_default(); + let owner_title = if !owner_hwnd.is_invalid() { get_window_title(owner_hwnd) } else { "".into() @@ -88,7 +88,7 @@ fn pretty_bool(value: bool) -> String { } extern "system" fn enum_window(hwnd: HWND, lparam: LPARAM) -> BOOL { - let windows: &mut Vec = unsafe { &mut *(lparam.0 as *mut _) }; + let windows: &mut Vec = unsafe { &mut *(lparam.0 as *mut Vec) }; windows.push(hwnd); BOOL(1) }