Skip to content
Open
Show file tree
Hide file tree
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
94 changes: 70 additions & 24 deletions plugins/topbar/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ thread_local! {
static BAR_T: Cell<u64> = const { Cell::new(0) };
static DESK_T: Cell<u64> = const { Cell::new(0) }; // last moment the bar had a reason to stay on the desktop
static SCALE: Cell<f32> = const { Cell::new(1.0) };
static FS_WND: Cell<isize> = const { Cell::new(0) }; // the window last seen fullscreen (fullscreen_now)
static FS_OFF: Cell<u64> = const { Cell::new(0) }; // when a fullscreen window was last missing, ms (0 = it is there)
static OLD_STATE: Cell<usize> = const { Cell::new(ABS_ALWAYSONTOP as usize) };
static BAT: Cell<(i32, bool)> = const { Cell::new((-1, false)) }; // (percent or -1 = no battery, on AC)
static VOL: Cell<(i32, bool)> = const { Cell::new((-1, false)) }; // (percent or -1 = unknown, muted)
Expand Down Expand Up @@ -621,10 +623,7 @@ unsafe fn alt_done(cancel: bool) {
});
let (v, i) = SW.take();
if let Some(&w) = v.get(i).filter(|_| !cancel) {
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
force_fg(w);
bring(w);
}
}

Expand Down Expand Up @@ -774,9 +773,11 @@ unsafe fn place(hwnd: HWND) {
// the dock is a pill; the other styles a plain strip
SetWindowRgn(hwnd, if dock() { CreateRoundRectRgn(0, 0, w + 1, bar_h() + 1, bar_h(), bar_h()) } else { null_mut() }, 1);
if vert() {
SetWindowPos(hwnd, HWND_TOPMOST, bar_x(), 0, bar_h(), cy, SWP_NOACTIVATE);
SetWindowPos(hwnd, HWND_TOPMOST, bar_x(), 0, bar_h(), cy, SWP_NOACTIVATE | SWP_NOZORDER);
} else {
SetWindowPos(hwnd, HWND_TOPMOST, x, bar_y(), w, bar_h(), SWP_NOACTIVATE);
// SWP_NOZORDER: the bar is topmost from its creation on. Raising it again here (this runs on every display change, which
// a game switching into fullscreen causes) put it over the game and knocked that out of fullscreen, in a loop.
SetWindowPos(hwnd, HWND_TOPMOST, x, bar_y(), w, bar_h(), SWP_NOACTIVATE | SWP_NOZORDER);
}
InvalidateRect(hwnd, null(), 0);
}
Expand Down Expand Up @@ -1550,11 +1551,13 @@ unsafe fn bar_step(hwnd: HWND) {
let p = BAR_P.get();
let p = if p < target { (p + dt / 380.0).min(target) } else { (p - dt / 240.0).max(target) };
BAR_P.set(p);
if p > 0.0 && IsWindowVisible(hwnd) == 0 {
let shown = p > 0.0 && IsWindowVisible(hwnd) == 0;
if shown {
ShowWindow(hwnd, SW_SHOWNOACTIVATE);
}
let (px, py) = bar_pos();
SetWindowPos(hwnd, HWND_TOPMOST, px, py, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
// on top of the other topmost windows only when it comes back; sliding away must not raise it over a fullscreen game
SetWindowPos(hwnd, HWND_TOPMOST, px, py, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | if shown { 0 } else { SWP_NOZORDER });
if p == target {
KillTimer(hwnd, 5);
if p == 0.0 {
Expand All @@ -1567,9 +1570,19 @@ unsafe fn bar_step(hwnd: HWND) {
/// A maximized window does not: the bar's reserved strip keeps it below the bar.
unsafe fn fullscreen_now(bar: HWND) -> bool {
let fg = GetForegroundWindow();
if fg.is_null() || fg == bar || IsWindowVisible(fg) == 0 || IsIconic(fg) != 0 {
if fg.is_null() || fg == bar || IsWindowVisible(fg) == 0 {
return false;
}
// A fullscreen game brought back with Alt+Tab gets the foreground while still minimized and only then switches the screen
// over: if the bar stayed on top then, the game lost fullscreen again. The window that was fullscreen counts as such
// while it comes back, and so does any Direct3D game Windows itself reports as fullscreen.
let mut quns = 0;
if windows_sys::Win32::UI::Shell::SHQueryUserNotificationState(&mut quns) >= 0 && quns == 3 {
return true; // QUNS_RUNNING_D3D_FULL_SCREEN
}
if IsIconic(fg) != 0 {
return fg as isize == FS_WND.get();
}
let mut cls = [0u16; 64];
let n = GetClassNameW(fg, cls.as_mut_ptr(), 64).max(0) as usize;
if matches!(String::from_utf16_lossy(&cls[..n]).as_str(), "Progman" | "WorkerW" | "Shell_TrayWnd" | "Shell_SecondaryTrayWnd" | "RustExplorer") {
Expand All @@ -1582,7 +1595,33 @@ unsafe fn fullscreen_now(bar: HWND) -> bool {
GetMonitorInfoW(MonitorFromWindow(fg, MONITOR_DEFAULTTONEAREST), &mut mi);
let m = mi.rcMonitor;
let caption = GetWindowLongW(fg, GWL_STYLE) as u32 & WS_CAPTION == WS_CAPTION;
(m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(fg) != 0)
let fs = (m.left, m.top) == (0, 0) && covers_monitor(r, m, caption, IsZoomed(fg) != 0);
if fs {
FS_WND.set(fg as isize);
}
fs
}

/// A game switching into fullscreen leaves the screen without any foreground window for a moment, again and again until it
/// settles: the bar must not come back in that moment (it would land on top and knock the game out of fullscreen, which then
/// tries again, in a loop). So the bar returns only once fullscreen has been gone for 1.5 s without a break.
unsafe fn fs_settled(now_fs: bool) -> bool {
if now_fs {
FS_OFF.set(0);
return true;
}
if !FULLSCREEN.load(Relaxed) {
return false;
}
let now = now_ms();
if FS_OFF.get() == 0 {
FS_OFF.set(now);
}
if now.saturating_sub(FS_OFF.get()) < 1500 {
return true;
}
FS_OFF.set(0);
false
}

/// A framed maximized window overhangs the monitor by its border and is not fullscreen (#88); but a game in "borderless
Expand All @@ -1600,7 +1639,7 @@ unsafe fn desktop_now() -> bool {
}

unsafe fn fs_update(hwnd: HWND) {
let fs = fullscreen_now(hwnd);
let fs = fs_settled(fullscreen_now(hwnd));
// a fullscreen game keeps the screen: the pointer at the bottom edge does not bring the bar (games park it at the
// edges all the time); Win opens Start, which brings the bar with it
let mut pt = POINT { x: 0, y: 0 };
Expand Down Expand Up @@ -1629,7 +1668,7 @@ unsafe fn fs_update(hwnd: HWND) {
}
// the 250 ms watch is only needed while the pointer decides (auto-hide, dock, desktop, bar away); otherwise
// the foreground event, ABN_FULLSCREENAPP and the 1 s tick call this
let poll = i18n::style::autohide() || dock() || desktop_now() || DESK_HIDE.load(Relaxed);
let poll = i18n::style::autohide() || dock() || desktop_now() || DESK_HIDE.load(Relaxed) || FS_OFF.get() != 0;
if FS_POLL.replace(poll) != poll {
if poll {
SetTimer(hwnd, 3, 250, None);
Expand Down Expand Up @@ -1666,6 +1705,22 @@ 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).
pub(crate) unsafe fn bring(w: HWND) {
if IsIconic(w) != 0 {
SwitchToThisWindow(w, 1);
if GetForegroundWindow() == w {
return;
}
}
force_fg(w);
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
}

/// Fills `r` (right / bottom exclusive) in colour `c`; `rad` > 0 rounds the corners (rad = corner diameter), anti-aliased
/// through GDI+. Plain GDI for square boxes and when GDI+ is not available.
unsafe fn fill(dc: HDC, r: RECT, c: u32, rad: i32) {
Expand Down Expand Up @@ -2341,10 +2396,7 @@ unsafe fn app_menu(hwnd: HWND, h: i32) {
fly::menu(tip_cx(&bar_layout(hwnd), h), Some((icon, "", name, sub)), items, move |i| {
match acts.get(i).copied() {
Some(A::Win(w)) => {
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
force_fg(w);
bring(w);
}
Some(A::Open) => open(&wide(&launch)),
Some(A::Pin) => {
Expand Down Expand Up @@ -2752,10 +2804,7 @@ unsafe fn win_num(n: usize) {
if wins.len() == 1 && i.is_some() {
ShowWindow(w, SW_MINIMIZE);
} else {
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
force_fg(w);
bring(w);
}
}

Expand Down Expand Up @@ -2854,10 +2903,7 @@ unsafe fn click(h: i32, left: bool, mid: bool) {
if wins.contains(&fg) && IsIconic(fg) == 0 {
ShowWindow(fg, SW_MINIMIZE);
} else {
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
SetForegroundWindow(w);
bring(w);
}
}
_ => {}
Expand Down
5 changes: 1 addition & 4 deletions plugins/topbar/src/thumbs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,7 @@ pub(crate) unsafe extern "system" fn pv_proc(hwnd: HWND, msg: u32, wp: WPARAM, l
}
} else {
hide_preview();
if IsIconic(w) != 0 {
ShowWindow(w, SW_RESTORE);
}
force_fg(w);
bring(w);
}
}
WM_TIMER if wp == 1 => {
Expand Down