From da3a2d60e5f982341fcb986f2b2716862bece2cc Mon Sep 17 00:00:00 2001 From: sigoden Date: Fri, 15 Nov 2024 07:57:54 +0800 Subject: [PATCH] chore: improve code quality - Group windows::Win32 import expression --- src/app.rs | 22 ++++++++++-------- src/foreground.rs | 12 ++++++---- src/keyboard.rs | 16 +++++++------ src/macros.rs | 3 +-- src/painter.rs | 45 +++++++++++++++++++++--------------- src/trayicon.rs | 25 +++++++++++--------- src/utils/regedit.rs | 12 ++++++---- src/utils/scheduled_task.rs | 16 ++++++++----- src/utils/single_instance.rs | 6 +++-- src/utils/window.rs | 40 +++++++++++++++++--------------- 10 files changed, 112 insertions(+), 85 deletions(-) diff --git a/src/app.rs b/src/app.rs index 03a4d18..bf6cb0c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -12,16 +12,18 @@ use crate::utils::{ use anyhow::{anyhow, Result}; use indexmap::IndexSet; use std::collections::HashMap; -use windows::core::w; -use windows::core::PCWSTR; -use windows::Win32::Foundation::{GetLastError, HINSTANCE, HWND, LPARAM, LRESULT, WPARAM}; -use windows::Win32::System::LibraryLoader::GetModuleHandleW; -use windows::Win32::UI::WindowsAndMessaging::{ - CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, GetWindowLongPtrW, LoadCursorW, - PostMessageW, PostQuitMessage, RegisterClassW, RegisterWindowMessageW, SetWindowLongPtrW, - TranslateMessage, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWL_STYLE, HICON, HTCLIENT, IDC_ARROW, - MSG, WINDOW_STYLE, WM_COMMAND, WM_ERASEBKGND, WM_LBUTTONUP, WM_NCHITTEST, WM_RBUTTONUP, - WNDCLASSW, WS_CAPTION, WS_EX_LAYERED, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, +use windows::core::{w, PCWSTR}; +use windows::Win32::{ + Foundation::{GetLastError, HINSTANCE, HWND, LPARAM, LRESULT, WPARAM}, + System::LibraryLoader::GetModuleHandleW, + UI::WindowsAndMessaging::{ + CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, GetWindowLongPtrW, + LoadCursorW, PostMessageW, PostQuitMessage, RegisterClassW, RegisterWindowMessageW, + SetWindowLongPtrW, TranslateMessage, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWL_STYLE, + HICON, HTCLIENT, IDC_ARROW, MSG, WINDOW_STYLE, WM_COMMAND, WM_ERASEBKGND, WM_LBUTTONUP, + WM_NCHITTEST, WM_RBUTTONUP, WNDCLASSW, WS_CAPTION, WS_EX_LAYERED, WS_EX_TOOLWINDOW, + WS_EX_TOPMOST, + }, }; pub const NAME: PCWSTR = w!("Window Switcher"); diff --git a/src/foreground.rs b/src/foreground.rs index d036ff6..10fe020 100644 --- a/src/foreground.rs +++ b/src/foreground.rs @@ -2,10 +2,14 @@ use crate::utils::get_window_exe; use anyhow::{bail, Result}; use once_cell::sync::OnceCell; use std::collections::HashSet; -use windows::Win32::Foundation::HWND; -use windows::Win32::UI::Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK}; -use windows::Win32::UI::WindowsAndMessaging::{ - EVENT_SYSTEM_FOREGROUND, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS, +use windows::Win32::{ + Foundation::HWND, + UI::{ + Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK}, + WindowsAndMessaging::{ + EVENT_SYSTEM_FOREGROUND, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS, + }, + }, }; pub static mut IS_FOREGROUND_IN_BLACKLIST: bool = false; diff --git a/src/keyboard.rs b/src/keyboard.rs index ce464c2..9d21580 100644 --- a/src/keyboard.rs +++ b/src/keyboard.rs @@ -8,13 +8,15 @@ use crate::{ }; use anyhow::{anyhow, Result}; -use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM}; -use windows::Win32::System::LibraryLoader::GetModuleHandleW; -use windows::Win32::UI::{ - Input::KeyboardAndMouse::{VIRTUAL_KEY, VK_ESCAPE, VK_LSHIFT, VK_RSHIFT}, - WindowsAndMessaging::{ - CallNextHookEx, SendMessageW, SetWindowsHookExW, UnhookWindowsHookEx, HHOOK, - KBDLLHOOKSTRUCT, WH_KEYBOARD_LL, +use windows::Win32::{ + Foundation::{HWND, LPARAM, LRESULT, WPARAM}, + System::LibraryLoader::GetModuleHandleW, + UI::{ + Input::KeyboardAndMouse::{VIRTUAL_KEY, VK_ESCAPE, VK_LSHIFT, VK_RSHIFT}, + WindowsAndMessaging::{ + CallNextHookEx, SendMessageW, SetWindowsHookExW, UnhookWindowsHookEx, HHOOK, + KBDLLHOOKSTRUCT, WH_KEYBOARD_LL, + }, }, }; diff --git a/src/macros.rs b/src/macros.rs index 9a82fc5..4b24b2e 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,5 +1,4 @@ -use windows::core::w; -use windows::core::PCWSTR; +use windows::core::{w, PCWSTR}; use windows::Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_ICONERROR, MB_OK}; use crate::utils::to_wstring; diff --git a/src/painter.rs b/src/painter.rs index 26433ae..386428b 100644 --- a/src/painter.rs +++ b/src/painter.rs @@ -2,26 +2,33 @@ use crate::app::SwitchAppsState; use crate::utils::{check_error, get_moinitor_rect, is_light_theme, is_win11}; use anyhow::{Context, Result}; -use windows::Win32::Foundation::{COLORREF, POINT, RECT, SIZE}; -use windows::Win32::Graphics::Gdi::{ - CreateCompatibleBitmap, CreateCompatibleDC, CreateRoundRectRgn, CreateSolidBrush, DeleteDC, - DeleteObject, FillRect, FillRgn, ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt, - AC_SRC_ALPHA, AC_SRC_OVER, BLENDFUNCTION, HALFTONE, HBITMAP, HBRUSH, HDC, HPALETTE, SRCCOPY, +use windows::Win32::{ + Foundation::{COLORREF, HWND, POINT, RECT, SIZE}, + Graphics::{ + Gdi::{ + CreateCompatibleBitmap, CreateCompatibleDC, CreateRoundRectRgn, CreateSolidBrush, + DeleteDC, DeleteObject, FillRect, FillRgn, GetDC, ReleaseDC, SelectObject, + SetStretchBltMode, StretchBlt, AC_SRC_ALPHA, AC_SRC_OVER, BLENDFUNCTION, HALFTONE, + HBITMAP, HBRUSH, HDC, HPALETTE, SRCCOPY, + }, + GdiPlus::{ + FillModeAlternate, GdipAddPathArc, GdipClosePathFigure, GdipCreateBitmapFromHBITMAP, + GdipCreateFromHDC, GdipCreatePath, GdipCreatePen1, GdipDeleteBrush, GdipDeleteGraphics, + GdipDeletePath, GdipDeletePen, GdipDisposeImage, GdipDrawImageRect, GdipFillPath, + GdipFillRectangle, GdipGetPenBrushFill, GdipSetInterpolationMode, GdipSetSmoothingMode, + GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, GpBitmap, GpBrush, GpGraphics, + GpImage, GpPath, GpPen, InterpolationModeHighQualityBicubic, SmoothingModeAntiAlias, + Unit, + }, + }, + UI::{ + Input::KeyboardAndMouse::SetFocus, + WindowsAndMessaging::{ + DrawIconEx, GetCursorPos, ShowWindow, UpdateLayeredWindow, DI_NORMAL, SW_HIDE, SW_SHOW, + ULW_ALPHA, + }, + }, }; -use windows::Win32::Graphics::GdiPlus::{ - FillModeAlternate, GdipAddPathArc, GdipClosePathFigure, GdipCreateBitmapFromHBITMAP, - GdipCreateFromHDC, GdipCreatePath, GdipCreatePen1, GdipDeleteBrush, GdipDeleteGraphics, - GdipDeletePath, GdipDeletePen, GdipDisposeImage, GdipDrawImageRect, GdipFillPath, - GdipFillRectangle, GdipGetPenBrushFill, GdipSetInterpolationMode, GdipSetSmoothingMode, - GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, GpBitmap, GpBrush, GpGraphics, GpImage, - GpPath, GpPen, InterpolationModeHighQualityBicubic, SmoothingModeAntiAlias, Unit, -}; -use windows::Win32::UI::Input::KeyboardAndMouse::SetFocus; -use windows::Win32::UI::WindowsAndMessaging::{ - DrawIconEx, GetCursorPos, ShowWindow, UpdateLayeredWindow, DI_NORMAL, SW_HIDE, SW_SHOW, - ULW_ALPHA, -}; -use windows::Win32::{Foundation::HWND, Graphics::Gdi::GetDC}; pub const BG_DARK_COLOR: u32 = 0x4c4c4c; pub const FG_DARK_COLOR: u32 = 0x3b3b3b; diff --git a/src/trayicon.rs b/src/trayicon.rs index d79ac27..0fae9ae 100644 --- a/src/trayicon.rs +++ b/src/trayicon.rs @@ -1,17 +1,20 @@ use crate::app::{IDM_CONFIGURE, IDM_EXIT, IDM_STARTUP, NAME, WM_USER_TRAYICON}; use anyhow::{anyhow, Result}; -use windows::core::w; -use windows::core::PCWSTR; -use windows::Win32::Foundation::{HWND, POINT}; -use windows::Win32::UI::Shell::{ - Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY, - NOTIFYICONDATAW, -}; -use windows::Win32::UI::WindowsAndMessaging::{ - AppendMenuW, CreateIconFromResourceEx, CreatePopupMenu, GetCursorPos, - LookupIconIdFromDirectoryEx, SetForegroundWindow, TrackPopupMenu, HMENU, LR_DEFAULTCOLOR, - MF_CHECKED, MF_STRING, MF_UNCHECKED, TPM_BOTTOMALIGN, TPM_LEFTALIGN, +use windows::core::{w, PCWSTR}; +use windows::Win32::{ + Foundation::{HWND, POINT}, + UI::{ + Shell::{ + Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY, + NOTIFYICONDATAW, + }, + WindowsAndMessaging::{ + AppendMenuW, CreateIconFromResourceEx, CreatePopupMenu, GetCursorPos, + LookupIconIdFromDirectoryEx, SetForegroundWindow, TrackPopupMenu, HMENU, + LR_DEFAULTCOLOR, MF_CHECKED, MF_STRING, MF_UNCHECKED, TPM_BOTTOMALIGN, TPM_LEFTALIGN, + }, + }, }; const ICON_BYTES: &[u8] = include_bytes!("../assets/icon.ico"); diff --git a/src/utils/regedit.rs b/src/utils/regedit.rs index c1d571c..7f86610 100644 --- a/src/utils/regedit.rs +++ b/src/utils/regedit.rs @@ -1,10 +1,12 @@ use anyhow::{anyhow, bail, Result}; use windows::core::PCWSTR; -use windows::Win32::Foundation::ERROR_FILE_NOT_FOUND; -use windows::Win32::System::Registry::{ - RegCloseKey, RegDeleteValueW, RegGetValueW, RegOpenKeyExW, RegSetValueExW, HKEY, - HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_DWORD_BIG_ENDIAN, REG_SZ, REG_VALUE_TYPE, - RRF_RT_REG_DWORD, RRF_RT_REG_SZ, +use windows::Win32::{ + Foundation::ERROR_FILE_NOT_FOUND, + System::Registry::{ + RegCloseKey, RegDeleteValueW, RegGetValueW, RegOpenKeyExW, RegSetValueExW, HKEY, + HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_DWORD_BIG_ENDIAN, REG_SZ, REG_VALUE_TYPE, + RRF_RT_REG_DWORD, RRF_RT_REG_SZ, + }, }; #[derive(Debug)] diff --git a/src/utils/scheduled_task.rs b/src/utils/scheduled_task.rs index e820728..51fe7ed 100644 --- a/src/utils/scheduled_task.rs +++ b/src/utils/scheduled_task.rs @@ -9,13 +9,17 @@ use std::{ process::Command, }; use windows::core::{Result as WindowsResult, PWSTR}; -use windows::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER; -use windows::Win32::Security::Authorization::ConvertSidToStringSidW; -use windows::Win32::Security::{ - GetTokenInformation, LookupAccountSidW, TokenUser, SID_NAME_USE, TOKEN_QUERY, TOKEN_USER, +use windows::Win32::{ + Foundation::ERROR_INSUFFICIENT_BUFFER, + Security::{ + Authorization::ConvertSidToStringSidW, GetTokenInformation, LookupAccountSidW, TokenUser, + SID_NAME_USE, TOKEN_QUERY, TOKEN_USER, + }, + System::{ + SystemInformation::GetLocalTime, + Threading::{GetCurrentProcess, OpenProcessToken, CREATE_NO_WINDOW}, + }, }; -use windows::Win32::System::SystemInformation::GetLocalTime; -use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken, CREATE_NO_WINDOW}; pub fn create_scheduled_task(name: &str, exe_path: &str) -> Result<()> { let task_xml_path = create_task_file(name, exe_path) diff --git a/src/utils/single_instance.rs b/src/utils/single_instance.rs index 154f07b..975f747 100644 --- a/src/utils/single_instance.rs +++ b/src/utils/single_instance.rs @@ -2,8 +2,10 @@ use super::to_wstring; use anyhow::{anyhow, Result}; use windows::core::PCWSTR; -use windows::Win32::Foundation::{CloseHandle, BOOL, ERROR_ALREADY_EXISTS, HANDLE}; -use windows::Win32::System::Threading::{CreateMutexW, ReleaseMutex}; +use windows::Win32::{ + Foundation::{CloseHandle, BOOL, ERROR_ALREADY_EXISTS, HANDLE}, + System::Threading::{CreateMutexW, ReleaseMutex}, +}; /// A struct representing one running instance. pub struct SingleInstance { diff --git a/src/utils/window.rs b/src/utils/window.rs index 1d24930..cdd3088 100644 --- a/src/utils/window.rs +++ b/src/utils/window.rs @@ -1,26 +1,28 @@ use anyhow::{anyhow, Result}; use indexmap::IndexMap; +use std::{ffi::c_void, mem::size_of, path::PathBuf}; use windows::core::PWSTR; -use windows::Win32::Foundation::{BOOL, HWND, LPARAM, MAX_PATH, POINT, RECT}; -use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED}; -use windows::Win32::Graphics::Gdi::{ - GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST, +use windows::Win32::{ + Foundation::{BOOL, HWND, LPARAM, MAX_PATH, POINT, RECT}, + Graphics::{ + Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED}, + Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST}, + }, + System::{ + Console::{AllocConsole, FreeConsole, GetConsoleWindow}, + LibraryLoader::GetModuleFileNameW, + Threading::{ + OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_INFORMATION, + PROCESS_VM_READ, + }, + }, + UI::WindowsAndMessaging::{ + EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW, + GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible, + SetForegroundWindow, SetWindowPos, ShowWindow, GWL_EXSTYLE, GWL_USERDATA, GW_OWNER, + SWP_NOZORDER, SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOPMOST, + }, }; -use windows::Win32::System::Console::{AllocConsole, FreeConsole, GetConsoleWindow}; -use windows::Win32::System::LibraryLoader::GetModuleFileNameW; -use windows::Win32::System::Threading::{ - OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_INFORMATION, - PROCESS_VM_READ, -}; -use windows::Win32::UI::WindowsAndMessaging::{ - EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW, - GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible, - SetForegroundWindow, SetWindowPos, ShowWindow, GWL_EXSTYLE, GWL_USERDATA, GW_OWNER, - SWP_NOZORDER, SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOPMOST, -}; - -use std::path::PathBuf; -use std::{ffi::c_void, mem::size_of}; pub fn is_iconic_window(hwnd: HWND) -> bool { unsafe { IsIconic(hwnd) }.as_bool()