Skip to content

Visual Studio Code, rebuilt in Rust

Rust
1.2 KB
Raw Blame
//! Win32 calls on GPUI's windows.

use gpui::Window;
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::UI::WindowsAndMessaging::{
    GWL_EXSTYLE, GetWindowLongPtrW, HWND_NOTOPMOST, HWND_TOPMOST, SWP_NOACTIVATE, SWP_NOMOVE, SWP_NOSIZE, SetWindowPos, WS_EX_TOPMOST,
};

/// The HWND of a GPUI window; none on GPUI's test platform.
fn hwnd(window: &Window) -> Option<HWND> {
    match HasWindowHandle::window_handle(window).ok()?.as_raw() {
        RawWindowHandle::Win32(handle) => Some(handle.hwnd.get() as HWND),
        _ => None,
    }
}

/// Chromium's HWNDMessageHandler `SetAlwaysOnTop` reads it back from the extended style.
pub fn is_always_on_top(window: &Window) -> bool {
    // SAFETY: a live window's HWND.
    hwnd(window).is_some_and(|hwnd| unsafe { GetWindowLongPtrW(hwnd, GWL_EXSTYLE) } as u32 & WS_EX_TOPMOST != 0)
}

pub fn set_always_on_top(window: &Window, on_top: bool) {
    let hwnd = hwnd(window).expect("Windows windows are Win32 windows");
    let after = if on_top { HWND_TOPMOST } else { HWND_NOTOPMOST };
    // SAFETY: a live window's HWND; only the z-order changes.
    unsafe { SetWindowPos(hwnd, after, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) };
}