Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 111 additions & 10 deletions plugins/topbar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,9 +937,76 @@ unsafe extern "system" fn fit_maximized(h: HWND, lp: isize) -> i32 {
if (n.left, n.top, n.right, n.bottom) != (r.left, r.top, r.right, r.bottom) && n.right > n.left && n.bottom > n.top {
SetWindowPos(h, null_mut(), n.left, n.top, n.right - n.left, n.bottom - n.top, SWP_NOZORDER | SWP_NOACTIVATE);
}
fit_normal(h);
1
}

/// Shrink `n` (a restore rectangle) into `work`, keeping it as large as still fits. Returns true if it changed.
fn clamp_rect(n: &mut RECT, work: RECT) -> bool {
let maxw = work.right - work.left;
let maxh = work.bottom - work.top;
let nw = (n.right - n.left).min(maxw);
let nh = (n.bottom - n.top).min(maxh);
let x = n.left.max(work.left).min(work.right - nw);
let y = n.top.max(work.top).min(work.bottom - nh);
if (n.left, n.top, n.right, n.bottom) == (x, y, x + nw, y + nh) {
return false;
}
*n = RECT { left: x, top: y, right: x + nw, bottom: y + nh };
true
}

unsafe fn work_area_of(w: HWND) -> Option<RECT> {
let mut mi: MONITORINFO = zeroed();
mi.cbSize = size_of::<MONITORINFO>() as u32;
(GetMonitorInfoW(MonitorFromWindow(w, MONITOR_DEFAULTTONEAREST), &mut mi) != 0).then_some(mi.rcWork)
}

/// A window maximized while the work area was the whole monitor keeps that size as its restore rectangle, so
/// restoring it later leaves it past the screen edge and under the bar (#268). Clamp a maximized window's normal
/// placement into its monitor's work area; the next restore then lands inside it. Only maximized windows: a
/// borderless fullscreen game or a window spanning monitors is not zoomed and is left alone.
unsafe fn fit_normal(w: HWND) {
if IsWindowVisible(w) == 0 || IsIconic(w) != 0 || IsZoomed(w) == 0 {
return;
}
let Some(work) = work_area_of(w) else { return };
let mut wp: WINDOWPLACEMENT = zeroed();
wp.length = size_of::<WINDOWPLACEMENT>() as u32;
if GetWindowPlacement(w, &mut wp) == 0 {
return;
}
if clamp_rect(&mut wp.rcNormalPosition, work) {
let _ = SetWindowPlacement(w, &wp);
}
}

/// A window restored from maximized keeps the maximized frame (the monitor plus its invisible resize border), which
/// leaves it past the screen edge and under the bar (#268); being restored, fit_normal no longer sees it. Shrink exactly
/// that shape to the work area: larger than the monitor on both axes but by no more than the border. A window spanning
/// monitors or one genuinely larger is much bigger and left alone.
unsafe fn fit_restored(w: HWND) {
if IsWindowVisible(w) == 0 || IsIconic(w) != 0 || IsZoomed(w) != 0 {
return;
}
let mut r: RECT = zeroed();
if GetWindowRect(w, &mut r) == 0 {
return;
}
let mut mi: MONITORINFO = zeroed();
mi.cbSize = size_of::<MONITORINFO>() as u32;
if GetMonitorInfoW(MonitorFromWindow(w, MONITOR_DEFAULTTONEAREST), &mut mi) == 0 {
return;
}
let (ww, wh) = (r.right - r.left, r.bottom - r.top);
let (mw, mh) = (mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top);
if ww <= mw || wh <= mh || ww > mw + 32 || wh > mh + 32 {
return;
}
let work = mi.rcWork;
SetWindowPos(w, null_mut(), work.left, work.top, work.right - work.left, work.bottom - work.top, SWP_NOZORDER | SWP_NOACTIVATE);
}

// ---------- data: windows, pins, tray apps, volume, wifi, battery ----------

thread_local! {
Expand Down Expand Up @@ -1487,7 +1554,7 @@ fn refresh() {
/// The buttons: on window events (win_event) and every few seconds as a safety net.
fn refresh_windows() {
refresh_buttons();
BTNS.with_borrow(|b| b.iter().flat_map(|b| &b.wins).for_each(|&w| unsafe { hide_stub(w) }));
BTNS.with_borrow(|b| b.iter().flat_map(|b| &b.wins).for_each(|&w| unsafe { hide_stub(w); fit_normal(w); fit_restored(w) }));
}

/// Everything polled once a second: battery, volume, wifi, explorer, dead tray icons.
Expand Down Expand Up @@ -1997,19 +2064,51 @@ unsafe fn force_fg(hw: HWND) {
winrust_common::fg::force(hw);
}

/// Switches to a window (Alt+Tab, its button, a preview, a menu). A minimized one goes through SwitchToThisWindow, the call
/// Windows' own Alt+Tab makes: it activates and restores in one step. Restoring first and activating after made a fullscreen
/// game get the foreground, lose it to the previous window and get it again; it then gave up on fullscreen (stayed black).
thread_local! {
// windows topbar itself minimized while maximized: they must come back maximized even if the placement flags lost it (#268)
static WAS_MAX: RefCell<Vec<HWND>> = const { RefCell::new(Vec::new()) };
}

/// Records that `w` is being minimized from a maximized state, so `bring` returns it maximized.
unsafe fn note_minimize(w: HWND) {
WAS_MAX.with_borrow_mut(|v| {
v.retain(|&x| x != w && IsWindow(x) != 0);
if IsZoomed(w) != 0 {
v.push(w);
}
});
}

/// Switches to a window (Alt+Tab, its button, a preview, a menu). A maximized one comes back maximized in a single step; any
/// other minimized one goes through SwitchToThisWindow, the call Windows' own Alt+Tab makes: it activates and restores in one
/// step. Restoring first and activating after made a fullscreen game get the foreground, lose it to the previous window and
/// get it again; it then gave up on fullscreen (stayed black).
pub(crate) unsafe fn bring(w: HWND) {
if IsIconic(w) != 0 {
// WPF_RESTORETOMAXIMIZED: the window was maximized before it was minimized, so it must come back maximized.
const RESTORE_TO_MAX: u32 = 0x0002;
let mut wp: WINDOWPLACEMENT = zeroed();
wp.length = size_of::<WINDOWPLACEMENT>() as u32;
let placed = GetWindowPlacement(w, &mut wp) != 0;
let was_iconic = IsIconic(w) != 0;
let remembered = WAS_MAX.with_borrow_mut(|v| v.iter().position(|&x| x == w).map(|i| { v.swap_remove(i); }));
let was_max = was_iconic && (remembered.is_some() || (placed && wp.flags & RESTORE_TO_MAX != 0));
if was_max && placed {
// show it maximized straight away: SwitchToThisWindow first restores it, and maximizing after visibly flickers
if let Some(work) = work_area_of(w) {
clamp_rect(&mut wp.rcNormalPosition, work);
}
wp.showCmd = 3; // SW_SHOWMAXIMIZED
if SetWindowPlacement(w, &wp) == 0 {
SwitchToThisWindow(w, 1);
}
} else if was_iconic {
SwitchToThisWindow(w, 1);
if GetForegroundWindow() == w {
return;
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
}
force_fg(w);
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
if GetForegroundWindow() != w {
force_fg(w); // the click that called us owns the foreground, so showing it may not have taken it
}
}

Expand Down Expand Up @@ -3114,6 +3213,7 @@ unsafe fn win_num(n: usize) {
let i = wins.iter().position(|&w| w == GetForegroundWindow());
let w = i.map_or(wins[0], |i| wins[(i + 1) % wins.len()]);
if wins.len() == 1 && i.is_some() {
note_minimize(w);
ShowWindow(w, SW_MINIMIZE);
} else {
bring(w);
Expand Down Expand Up @@ -3213,6 +3313,7 @@ unsafe fn click(h: i32, left: bool, mid: bool) {
// like the Windows taskbar: the active app's button minimizes it, a minimized one restores, any other raises
let fg = GetForegroundWindow();
if wins.contains(&fg) && IsIconic(fg) == 0 {
note_minimize(fg);
ShowWindow(fg, SW_MINIMIZE);
} else {
bring(w);
Expand Down
Loading