Skip to content

refactor: unify per-platform dispatch in window helpers via cfg_select! #102

@StudentWeis

Description

@StudentWeis

Current State

src/gui/utils.rs::hide_window and src/gui/utils.rs::active_window both organise their per-platform code as two adjacent blocks:

// 1. Windows path written as a standalone #[cfg] block
#[cfg(target_os = "windows")]
if let Ok(window_handle) = HasWindowHandle::window_handle(window)
    && let RawWindowHandle::Win32(win32_handle) = window_handle.as_raw()
{
    let hwnd = win32_handle.hwnd.get() as *mut std::ffi::c_void;
    unsafe { /* ShowWindow / SetForegroundWindow */ }
}

// 2. macOS / Linux paths written through cfg_select!
cfg_select! {
    target_os = "macos" => { /* … */ }
    target_os = "linux" => { /* … */ }
    _ => {}
}

The split is purely historical: the Windows block was written with a multi-line unsafe body and a local hwnd binding, while the other branches were one-liners that fit naturally into cfg_select!. There is no language reason for the asymmetry — cfg_select! arms accept arbitrary statement blocks, including unsafe { … } and local let bindings.

Target State

Each helper has a single per-platform dispatch site:

cfg_select! {
    target_os = "macos" => { /* … */ }
    target_os = "windows" => {
        if let Ok(window_handle) = HasWindowHandle::window_handle(window)
            && let RawWindowHandle::Win32(win32_handle) = window_handle.as_raw()
        {
            let hwnd = win32_handle.hwnd.get() as *mut std::ffi::c_void;
            unsafe { /* … */ }
        }
    }
    target_os = "linux" => { /* … */ }
    _ => {}
}

No platform-specific logic is added or removed. Both the X11 map_windowunmap_window pairing and the Win32 SW_RESTORESW_HIDE pairing (see issue #101) are preserved verbatim — they are the project's bridges between its own hide path and GPUI's activation path, and must stay.

Rationale

  • Consistency — the same dispatch shape across all three platforms makes the function easier to read at a glance.
  • DRY — one cfg dispatch site per function instead of two.
  • No behaviour changecfg_select! expands to the same compile-time branch selection as #[cfg], so the binary is byte-identical on each target.
  • Risk is essentially zero; clippy backstops any unused-import fallout.

Scope

gui (style cleanup inside two helpers in src/gui/utils.rs).

Safety Net

  • Existing tests cover the affected behavior — pure refactor, runtime semantics unchanged.
  • New tests will be added as part of this refactor — N/A.
  • This refactor is purely mechanical (no behavior change).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions