Skip to content

Commit da3a2d6

Browse files
committed
chore: improve code quality
- Group windows::Win32 import expression
1 parent 88431aa commit da3a2d6

10 files changed

Lines changed: 112 additions & 85 deletions

File tree

src/app.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,18 @@ use crate::utils::{
1212
use anyhow::{anyhow, Result};
1313
use indexmap::IndexSet;
1414
use std::collections::HashMap;
15-
use windows::core::w;
16-
use windows::core::PCWSTR;
17-
use windows::Win32::Foundation::{GetLastError, HINSTANCE, HWND, LPARAM, LRESULT, WPARAM};
18-
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
19-
use windows::Win32::UI::WindowsAndMessaging::{
20-
CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, GetWindowLongPtrW, LoadCursorW,
21-
PostMessageW, PostQuitMessage, RegisterClassW, RegisterWindowMessageW, SetWindowLongPtrW,
22-
TranslateMessage, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWL_STYLE, HICON, HTCLIENT, IDC_ARROW,
23-
MSG, WINDOW_STYLE, WM_COMMAND, WM_ERASEBKGND, WM_LBUTTONUP, WM_NCHITTEST, WM_RBUTTONUP,
24-
WNDCLASSW, WS_CAPTION, WS_EX_LAYERED, WS_EX_TOOLWINDOW, WS_EX_TOPMOST,
15+
use windows::core::{w, PCWSTR};
16+
use windows::Win32::{
17+
Foundation::{GetLastError, HINSTANCE, HWND, LPARAM, LRESULT, WPARAM},
18+
System::LibraryLoader::GetModuleHandleW,
19+
UI::WindowsAndMessaging::{
20+
CreateWindowExW, DefWindowProcW, DispatchMessageW, GetMessageW, GetWindowLongPtrW,
21+
LoadCursorW, PostMessageW, PostQuitMessage, RegisterClassW, RegisterWindowMessageW,
22+
SetWindowLongPtrW, TranslateMessage, CS_HREDRAW, CS_VREDRAW, CW_USEDEFAULT, GWL_STYLE,
23+
HICON, HTCLIENT, IDC_ARROW, MSG, WINDOW_STYLE, WM_COMMAND, WM_ERASEBKGND, WM_LBUTTONUP,
24+
WM_NCHITTEST, WM_RBUTTONUP, WNDCLASSW, WS_CAPTION, WS_EX_LAYERED, WS_EX_TOOLWINDOW,
25+
WS_EX_TOPMOST,
26+
},
2527
};
2628

2729
pub const NAME: PCWSTR = w!("Window Switcher");

src/foreground.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ use crate::utils::get_window_exe;
22
use anyhow::{bail, Result};
33
use once_cell::sync::OnceCell;
44
use std::collections::HashSet;
5-
use windows::Win32::Foundation::HWND;
6-
use windows::Win32::UI::Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK};
7-
use windows::Win32::UI::WindowsAndMessaging::{
8-
EVENT_SYSTEM_FOREGROUND, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS,
5+
use windows::Win32::{
6+
Foundation::HWND,
7+
UI::{
8+
Accessibility::{SetWinEventHook, UnhookWinEvent, HWINEVENTHOOK},
9+
WindowsAndMessaging::{
10+
EVENT_SYSTEM_FOREGROUND, WINEVENT_OUTOFCONTEXT, WINEVENT_SKIPOWNPROCESS,
11+
},
12+
},
913
};
1014

1115
pub static mut IS_FOREGROUND_IN_BLACKLIST: bool = false;

src/keyboard.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ use crate::{
88
};
99

1010
use anyhow::{anyhow, Result};
11-
use windows::Win32::Foundation::{HWND, LPARAM, LRESULT, WPARAM};
12-
use windows::Win32::System::LibraryLoader::GetModuleHandleW;
13-
use windows::Win32::UI::{
14-
Input::KeyboardAndMouse::{VIRTUAL_KEY, VK_ESCAPE, VK_LSHIFT, VK_RSHIFT},
15-
WindowsAndMessaging::{
16-
CallNextHookEx, SendMessageW, SetWindowsHookExW, UnhookWindowsHookEx, HHOOK,
17-
KBDLLHOOKSTRUCT, WH_KEYBOARD_LL,
11+
use windows::Win32::{
12+
Foundation::{HWND, LPARAM, LRESULT, WPARAM},
13+
System::LibraryLoader::GetModuleHandleW,
14+
UI::{
15+
Input::KeyboardAndMouse::{VIRTUAL_KEY, VK_ESCAPE, VK_LSHIFT, VK_RSHIFT},
16+
WindowsAndMessaging::{
17+
CallNextHookEx, SendMessageW, SetWindowsHookExW, UnhookWindowsHookEx, HHOOK,
18+
KBDLLHOOKSTRUCT, WH_KEYBOARD_LL,
19+
},
1820
},
1921
};
2022

src/macros.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use windows::core::w;
2-
use windows::core::PCWSTR;
1+
use windows::core::{w, PCWSTR};
32
use windows::Win32::UI::WindowsAndMessaging::{MessageBoxW, MB_ICONERROR, MB_OK};
43

54
use crate::utils::to_wstring;

src/painter.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,33 @@ use crate::app::SwitchAppsState;
22
use crate::utils::{check_error, get_moinitor_rect, is_light_theme, is_win11};
33

44
use anyhow::{Context, Result};
5-
use windows::Win32::Foundation::{COLORREF, POINT, RECT, SIZE};
6-
use windows::Win32::Graphics::Gdi::{
7-
CreateCompatibleBitmap, CreateCompatibleDC, CreateRoundRectRgn, CreateSolidBrush, DeleteDC,
8-
DeleteObject, FillRect, FillRgn, ReleaseDC, SelectObject, SetStretchBltMode, StretchBlt,
9-
AC_SRC_ALPHA, AC_SRC_OVER, BLENDFUNCTION, HALFTONE, HBITMAP, HBRUSH, HDC, HPALETTE, SRCCOPY,
5+
use windows::Win32::{
6+
Foundation::{COLORREF, HWND, POINT, RECT, SIZE},
7+
Graphics::{
8+
Gdi::{
9+
CreateCompatibleBitmap, CreateCompatibleDC, CreateRoundRectRgn, CreateSolidBrush,
10+
DeleteDC, DeleteObject, FillRect, FillRgn, GetDC, ReleaseDC, SelectObject,
11+
SetStretchBltMode, StretchBlt, AC_SRC_ALPHA, AC_SRC_OVER, BLENDFUNCTION, HALFTONE,
12+
HBITMAP, HBRUSH, HDC, HPALETTE, SRCCOPY,
13+
},
14+
GdiPlus::{
15+
FillModeAlternate, GdipAddPathArc, GdipClosePathFigure, GdipCreateBitmapFromHBITMAP,
16+
GdipCreateFromHDC, GdipCreatePath, GdipCreatePen1, GdipDeleteBrush, GdipDeleteGraphics,
17+
GdipDeletePath, GdipDeletePen, GdipDisposeImage, GdipDrawImageRect, GdipFillPath,
18+
GdipFillRectangle, GdipGetPenBrushFill, GdipSetInterpolationMode, GdipSetSmoothingMode,
19+
GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, GpBitmap, GpBrush, GpGraphics,
20+
GpImage, GpPath, GpPen, InterpolationModeHighQualityBicubic, SmoothingModeAntiAlias,
21+
Unit,
22+
},
23+
},
24+
UI::{
25+
Input::KeyboardAndMouse::SetFocus,
26+
WindowsAndMessaging::{
27+
DrawIconEx, GetCursorPos, ShowWindow, UpdateLayeredWindow, DI_NORMAL, SW_HIDE, SW_SHOW,
28+
ULW_ALPHA,
29+
},
30+
},
1031
};
11-
use windows::Win32::Graphics::GdiPlus::{
12-
FillModeAlternate, GdipAddPathArc, GdipClosePathFigure, GdipCreateBitmapFromHBITMAP,
13-
GdipCreateFromHDC, GdipCreatePath, GdipCreatePen1, GdipDeleteBrush, GdipDeleteGraphics,
14-
GdipDeletePath, GdipDeletePen, GdipDisposeImage, GdipDrawImageRect, GdipFillPath,
15-
GdipFillRectangle, GdipGetPenBrushFill, GdipSetInterpolationMode, GdipSetSmoothingMode,
16-
GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, GpBitmap, GpBrush, GpGraphics, GpImage,
17-
GpPath, GpPen, InterpolationModeHighQualityBicubic, SmoothingModeAntiAlias, Unit,
18-
};
19-
use windows::Win32::UI::Input::KeyboardAndMouse::SetFocus;
20-
use windows::Win32::UI::WindowsAndMessaging::{
21-
DrawIconEx, GetCursorPos, ShowWindow, UpdateLayeredWindow, DI_NORMAL, SW_HIDE, SW_SHOW,
22-
ULW_ALPHA,
23-
};
24-
use windows::Win32::{Foundation::HWND, Graphics::Gdi::GetDC};
2532

2633
pub const BG_DARK_COLOR: u32 = 0x4c4c4c;
2734
pub const FG_DARK_COLOR: u32 = 0x3b3b3b;

src/trayicon.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
use crate::app::{IDM_CONFIGURE, IDM_EXIT, IDM_STARTUP, NAME, WM_USER_TRAYICON};
22

33
use anyhow::{anyhow, Result};
4-
use windows::core::w;
5-
use windows::core::PCWSTR;
6-
use windows::Win32::Foundation::{HWND, POINT};
7-
use windows::Win32::UI::Shell::{
8-
Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY,
9-
NOTIFYICONDATAW,
10-
};
11-
use windows::Win32::UI::WindowsAndMessaging::{
12-
AppendMenuW, CreateIconFromResourceEx, CreatePopupMenu, GetCursorPos,
13-
LookupIconIdFromDirectoryEx, SetForegroundWindow, TrackPopupMenu, HMENU, LR_DEFAULTCOLOR,
14-
MF_CHECKED, MF_STRING, MF_UNCHECKED, TPM_BOTTOMALIGN, TPM_LEFTALIGN,
4+
use windows::core::{w, PCWSTR};
5+
use windows::Win32::{
6+
Foundation::{HWND, POINT},
7+
UI::{
8+
Shell::{
9+
Shell_NotifyIconW, NIF_ICON, NIF_MESSAGE, NIF_TIP, NIM_ADD, NIM_DELETE, NIM_MODIFY,
10+
NOTIFYICONDATAW,
11+
},
12+
WindowsAndMessaging::{
13+
AppendMenuW, CreateIconFromResourceEx, CreatePopupMenu, GetCursorPos,
14+
LookupIconIdFromDirectoryEx, SetForegroundWindow, TrackPopupMenu, HMENU,
15+
LR_DEFAULTCOLOR, MF_CHECKED, MF_STRING, MF_UNCHECKED, TPM_BOTTOMALIGN, TPM_LEFTALIGN,
16+
},
17+
},
1518
};
1619

1720
const ICON_BYTES: &[u8] = include_bytes!("../assets/icon.ico");

src/utils/regedit.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use anyhow::{anyhow, bail, Result};
22
use windows::core::PCWSTR;
3-
use windows::Win32::Foundation::ERROR_FILE_NOT_FOUND;
4-
use windows::Win32::System::Registry::{
5-
RegCloseKey, RegDeleteValueW, RegGetValueW, RegOpenKeyExW, RegSetValueExW, HKEY,
6-
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_DWORD_BIG_ENDIAN, REG_SZ, REG_VALUE_TYPE,
7-
RRF_RT_REG_DWORD, RRF_RT_REG_SZ,
3+
use windows::Win32::{
4+
Foundation::ERROR_FILE_NOT_FOUND,
5+
System::Registry::{
6+
RegCloseKey, RegDeleteValueW, RegGetValueW, RegOpenKeyExW, RegSetValueExW, HKEY,
7+
HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_DWORD_BIG_ENDIAN, REG_SZ, REG_VALUE_TYPE,
8+
RRF_RT_REG_DWORD, RRF_RT_REG_SZ,
9+
},
810
};
911

1012
#[derive(Debug)]

src/utils/scheduled_task.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ use std::{
99
process::Command,
1010
};
1111
use windows::core::{Result as WindowsResult, PWSTR};
12-
use windows::Win32::Foundation::ERROR_INSUFFICIENT_BUFFER;
13-
use windows::Win32::Security::Authorization::ConvertSidToStringSidW;
14-
use windows::Win32::Security::{
15-
GetTokenInformation, LookupAccountSidW, TokenUser, SID_NAME_USE, TOKEN_QUERY, TOKEN_USER,
12+
use windows::Win32::{
13+
Foundation::ERROR_INSUFFICIENT_BUFFER,
14+
Security::{
15+
Authorization::ConvertSidToStringSidW, GetTokenInformation, LookupAccountSidW, TokenUser,
16+
SID_NAME_USE, TOKEN_QUERY, TOKEN_USER,
17+
},
18+
System::{
19+
SystemInformation::GetLocalTime,
20+
Threading::{GetCurrentProcess, OpenProcessToken, CREATE_NO_WINDOW},
21+
},
1622
};
17-
use windows::Win32::System::SystemInformation::GetLocalTime;
18-
use windows::Win32::System::Threading::{GetCurrentProcess, OpenProcessToken, CREATE_NO_WINDOW};
1923

2024
pub fn create_scheduled_task(name: &str, exe_path: &str) -> Result<()> {
2125
let task_xml_path = create_task_file(name, exe_path)

src/utils/single_instance.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use super::to_wstring;
22

33
use anyhow::{anyhow, Result};
44
use windows::core::PCWSTR;
5-
use windows::Win32::Foundation::{CloseHandle, BOOL, ERROR_ALREADY_EXISTS, HANDLE};
6-
use windows::Win32::System::Threading::{CreateMutexW, ReleaseMutex};
5+
use windows::Win32::{
6+
Foundation::{CloseHandle, BOOL, ERROR_ALREADY_EXISTS, HANDLE},
7+
System::Threading::{CreateMutexW, ReleaseMutex},
8+
};
79

810
/// A struct representing one running instance.
911
pub struct SingleInstance {

src/utils/window.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
11
use anyhow::{anyhow, Result};
22
use indexmap::IndexMap;
3+
use std::{ffi::c_void, mem::size_of, path::PathBuf};
34
use windows::core::PWSTR;
4-
use windows::Win32::Foundation::{BOOL, HWND, LPARAM, MAX_PATH, POINT, RECT};
5-
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED};
6-
use windows::Win32::Graphics::Gdi::{
7-
GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST,
5+
use windows::Win32::{
6+
Foundation::{BOOL, HWND, LPARAM, MAX_PATH, POINT, RECT},
7+
Graphics::{
8+
Dwm::{DwmGetWindowAttribute, DWMWA_CLOAKED},
9+
Gdi::{GetMonitorInfoW, MonitorFromPoint, MONITORINFO, MONITOR_DEFAULTTONEAREST},
10+
},
11+
System::{
12+
Console::{AllocConsole, FreeConsole, GetConsoleWindow},
13+
LibraryLoader::GetModuleFileNameW,
14+
Threading::{
15+
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_INFORMATION,
16+
PROCESS_VM_READ,
17+
},
18+
},
19+
UI::WindowsAndMessaging::{
20+
EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW,
21+
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible,
22+
SetForegroundWindow, SetWindowPos, ShowWindow, GWL_EXSTYLE, GWL_USERDATA, GW_OWNER,
23+
SWP_NOZORDER, SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOPMOST,
24+
},
825
};
9-
use windows::Win32::System::Console::{AllocConsole, FreeConsole, GetConsoleWindow};
10-
use windows::Win32::System::LibraryLoader::GetModuleFileNameW;
11-
use windows::Win32::System::Threading::{
12-
OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_INFORMATION,
13-
PROCESS_VM_READ,
14-
};
15-
use windows::Win32::UI::WindowsAndMessaging::{
16-
EnumWindows, GetCursorPos, GetForegroundWindow, GetWindow, GetWindowLongPtrW,
17-
GetWindowPlacement, GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible,
18-
SetForegroundWindow, SetWindowPos, ShowWindow, GWL_EXSTYLE, GWL_USERDATA, GW_OWNER,
19-
SWP_NOZORDER, SW_RESTORE, WINDOWPLACEMENT, WS_EX_TOPMOST,
20-
};
21-
22-
use std::path::PathBuf;
23-
use std::{ffi::c_void, mem::size_of};
2426

2527
pub fn is_iconic_window(hwnd: HWND) -> bool {
2628
unsafe { IsIconic(hwnd) }.as_bool()

0 commit comments

Comments
 (0)