Skip to content

Commit

Permalink
Use async windows pos functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Askestad authored and MartinAskestad committed May 9, 2024
1 parent 13e4fc9 commit 7dc791f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
58 changes: 33 additions & 25 deletions src/win32/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
path::PathBuf,
};

use log::debug;
use windows::{
core::PCWSTR,
w,
Expand All @@ -27,14 +26,14 @@ use windows::{
Accessibility::{SetWinEventHook, HWINEVENTHOOK, WINEVENTPROC},
Shell::{FOLDERID_LocalAppData, SHGetKnownFolderPath, KF_FLAG_DEFAULT},
WindowsAndMessaging::{
DefWindowProcW, EnumWindows, FindWindowW, GetClassNameW, GetCursorPos,
GetSystemMetrics, GetWindow, GetWindowLongPtrW, GetWindowTextLengthW,
GetWindowTextW, GetWindowThreadProcessId, IsIconic, IsWindowVisible, LoadIconW,
PostMessageW, PostQuitMessage, RegisterClassW, RegisterShellHookWindow,
RegisterWindowMessageW, SetWindowLongPtrW, SetWindowPos, ShowWindow,
SystemParametersInfoW, GET_WINDOW_CMD, GWL_EXSTYLE, GWL_STYLE, HICON, HWND_TOP,
SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN,
SPI_GETWORKAREA, SWP_NOACTIVATE, SW_SHOWMINNOACTIVE,
BeginDeferWindowPos, DefWindowProcW, DeferWindowPos, EndDeferWindowPos,
EnumWindows, FindWindowW, GetClassNameW, GetCursorPos, GetSystemMetrics, GetWindow,
GetWindowLongPtrW, GetWindowTextLengthW, GetWindowTextW, GetWindowThreadProcessId,
IsIconic, IsWindowVisible, LoadIconW, PostMessageW, PostQuitMessage,
RegisterClassW, RegisterShellHookWindow, RegisterWindowMessageW, SetWindowLongPtrW,
ShowWindow, SystemParametersInfoW, GET_WINDOW_CMD, GWL_EXSTYLE, GWL_STYLE, HDWP,
HICON, HWND_TOP, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN,
SM_YVIRTUALSCREEN, SPI_GETWORKAREA, SWP_NOACTIVATE, SW_SHOWMINNOACTIVE,
SYSTEM_PARAMETERS_INFO_UPDATE_FLAGS, WINDOW_LONG_PTR_INDEX, WINEVENT_OUTOFCONTEXT,
WNDCLASSW, WNDENUMPROC,
},
Expand All @@ -50,6 +49,31 @@ pub(crate) mod taskbar;
pub(crate) mod theme;
pub(crate) mod virtualdesktop;

pub fn begin_defer_window_pos(num_windows: usize) -> Result<HDWP> {
Ok(unsafe { BeginDeferWindowPos(num_windows as i32)? })
}

pub fn defer_window_pos(hdwp: HDWP, hwnd: HWND, rect: RECT) -> Result<HDWP> {
let margin = dwm::get_window_extended_frame_bounds(hwnd); // Should be: (left: 7, top: 0, right: -7, bottom: -7)
let res = unsafe {
DeferWindowPos(
hdwp,
hwnd,
HWND_TOP,
rect.left - margin.left,
rect.top - margin.top,
(rect.right - rect.left) + margin.left * 2,
(rect.bottom - rect.top) - margin.bottom,
SWP_NOACTIVATE,
)?
};
Ok(res)
}

pub fn end_defer_window_pos(hdwp: HDWP) {
unsafe { EndDeferWindowPos(hdwp) };
}

pub fn is_iconic(hwnd: HWND) -> bool {
unsafe { IsIconic(hwnd).into() }
}
Expand Down Expand Up @@ -101,22 +125,6 @@ pub fn get_working_area() -> Result<RECT> {
}
}

pub fn set_window_pos(hwnd: HWND, r: RECT) {
let margin = dwm::get_window_extended_frame_bounds(hwnd); // should be: { left: 7, top: 0, right:-7, bottom -7 }
debug!("{margin:?}");
unsafe {
SetWindowPos(
hwnd,
HWND_TOP,
r.left - margin.left,
r.top - margin.top,
(r.right - r.left) - margin.right * 2,
(r.bottom - r.top) - margin.bottom,
SWP_NOACTIVATE,
);
}
}

pub fn get_module_handle() -> windows::core::Result<HMODULE> {
unsafe { GetModuleHandleA(None) }
}
Expand Down
9 changes: 7 additions & 2 deletions src/windowmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ impl WindowManager {
.collect();
let number_of_windows = windows_on_screen.len();
let ds = self.layout.arrange(self.working_area, number_of_windows);
for (w, d) in windows_on_screen.iter().zip(ds.iter()) {
win32::set_window_pos(w.0, *d);
if let Ok(mut hdwp) = win32::begin_defer_window_pos(number_of_windows) {
for (w, d) in windows_on_screen.iter().zip(ds.iter()) {
if let Ok(res) = win32::defer_window_pos(hdwp, w.0, *d) {
hdwp = res;
}
}
win32::end_defer_window_pos(hdwp);
}
let _ = win32::dwm::invalidate_iconic_bitmaps(self.hwnd);
}
Expand Down

0 comments on commit 7dc791f

Please sign in to comment.