Skip to content

Commit

Permalink
chore: improve code quality
Browse files Browse the repository at this point in the history
- Group windows::Win32 import expression
  • Loading branch information
sigoden committed Nov 14, 2024
1 parent 88431aa commit da3a2d6
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 85 deletions.
22 changes: 12 additions & 10 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 8 additions & 4 deletions src/foreground.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
};

Expand Down
3 changes: 1 addition & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
45 changes: 26 additions & 19 deletions src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
25 changes: 14 additions & 11 deletions src/trayicon.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
12 changes: 7 additions & 5 deletions src/utils/regedit.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
16 changes: 10 additions & 6 deletions src/utils/scheduled_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions src/utils/single_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
40 changes: 21 additions & 19 deletions src/utils/window.rs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down

0 comments on commit da3a2d6

Please sign in to comment.