gpui-component 0.6.0

GPUI Component: the styled component library of GPUI Kit, with 60+ desktop UI components for GPUI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! Windows native menu implementation (Win32 popup menus).

use std::{
    ffi::c_void,
    sync::{Arc, OnceLock},
};

use gpui::{Action, App, AssetSource, ImageFormat, Pixels, Point, SharedString, Window};
use raw_window_handle::{HasWindowHandle, RawWindowHandle};
use windows::Win32::Foundation::{BOOL, GlobalFree, HANDLE, HWND, POINT};
use windows::Win32::Graphics::Gdi::{
    BI_RGB, BITMAPINFO, BITMAPINFOHEADER, ClientToScreen, CreateDIBSection, DIB_RGB_COLORS,
    DeleteObject, HBITMAP, HDC, HGDIOBJ,
};
use windows::Win32::Graphics::GdiPlus::{
    GdipCreateBitmapFromStream, GdipCreateHBITMAPFromBitmap, GdipDisposeImage,
    GdipGetImageThumbnail, GdiplusShutdown, GdiplusStartup, GdiplusStartupInput, GpBitmap, GpImage,
};
use windows::Win32::System::Com::StructuredStorage::CreateStreamOnHGlobal;
use windows::Win32::System::LibraryLoader::{GetProcAddress, LoadLibraryW};
use windows::Win32::System::Memory::{GMEM_MOVEABLE, GlobalAlloc, GlobalLock, GlobalUnlock};
use windows::Win32::UI::WindowsAndMessaging::{
    AppendMenuW, CreatePopupMenu, DestroyMenu, HMENU, MENUITEMINFOW, MF_CHECKED, MF_GRAYED,
    MF_POPUP, MF_SEPARATOR, MF_STRING, MIIM_BITMAP, SetForegroundWindow, SetMenuItemInfoW,
    TPM_LEFTALIGN, TPM_NONOTIFY, TPM_RETURNCMD, TPM_TOPALIGN, TrackPopupMenuEx,
};
use windows::core::{PCSTR, PCWSTR};

use super::{NativeMenuItem, resolve_icon_image};

/// Side length (in **logical pixels**) menu item images are scaled to. The
/// physical bitmap size is this multiplied by the window's scale factor (see
/// [`show`]), so images stay sharp on the HiDPI displays.
const MENU_IMAGE_SIZE: u32 = 16;

/// Show a native popup menu and dispatch the selected item's action.
///
/// The Win32 tracking loop (`TrackPopupMenuEx`) blocks, so — like macOS — it is
/// run from a foreground task to avoid re-entering GPUI while it is borrowed.
pub(super) fn show(
    items: Vec<NativeMenuItem>,
    asset_source: Arc<dyn AssetSource>,
    position: Point<Pixels>,
    dark_mode: bool,
    window: &mut Window,
    cx: &mut App,
) {
    let Some(hwnd) = hwnd_ptr(window) else {
        return;
    };
    // `position` is logical pixels; Win32 wants physical pixels.
    let scale = window.scale_factor();
    let client_x = (f32::from(position.x) * scale).round() as i32;
    let client_y = (f32::from(position.y) * scale).round() as i32;
    // The menu draws item bitmaps at their native pixel size, so rasterize them
    // at the device pixel size to keep them sharp on HiDPI displays.
    let image_px = (MENU_IMAGE_SIZE as f32 * scale).round().max(1.0) as u32;
    // Inherent `Window::window_handle` (GPUI's `AnyWindowHandle`), not the
    // `raw_window_handle::HasWindowHandle` trait method in scope below.
    let handle = Window::window_handle(window);

    cx.spawn(async move |cx| {
        let Some(action) = run_menu(
            hwnd,
            &items,
            asset_source.as_ref(),
            client_x,
            client_y,
            image_px,
            dark_mode,
        ) else {
            return;
        };
        cx.update(move |app| {
            let _ = handle.update(app, move |_, window, app| {
                window.dispatch_action(action, app);
            });
        });
    })
    .detach();
}

/// Build the menu (recursively, including submenus), show it, and return the
/// selected item's action.
fn run_menu(
    hwnd: isize,
    items: &[NativeMenuItem],
    asset_source: &dyn AssetSource,
    client_x: i32,
    client_y: i32,
    image_px: u32,
    dark_mode: bool,
) -> Option<Box<dyn Action>> {
    let hwnd = HWND(hwnd as *mut c_void);

    // SAFETY: Win32 menu calls on a live window owned by the calling (main)
    // thread. The menu (and its submenus) is destroyed before returning.
    unsafe {
        // Start GDI+ so item images can be loaded into bitmaps. If it fails, the menu is still
        // built (images are skipped).
        let gdiplus = GdiplusSession::start();
        let mut actions: Vec<&Box<dyn Action>> = Vec::new();

        // Bitmaps attached to menu items must outlive the menu; freed below.
        let mut bitmaps: Vec<HBITMAP> = Vec::new();
        let menu = build_menu(items, asset_source, &mut actions, &mut bitmaps, image_px)?;

        let mut point = POINT {
            x: client_x,
            y: client_y,
        };
        let _ = ClientToScreen(hwnd, &mut point);
        // Required so the menu dismisses correctly when clicking elsewhere.
        let _ = SetForegroundWindow(hwnd);
        apply_menu_theme(hwnd, dark_mode);

        let flags = TPM_LEFTALIGN | TPM_TOPALIGN | TPM_RETURNCMD | TPM_NONOTIFY;
        let selected = TrackPopupMenuEx(menu, flags.0, point.x, point.y, hwnd, None);
        // Destroying the top menu also destroys its attached submenus.
        let _ = DestroyMenu(menu);

        // The menu no longer references the bitmaps, so they can be freed.
        for bitmap in &bitmaps {
            let _ = DeleteObject(HGDIOBJ(bitmap.0));
        }
        drop(gdiplus);

        // Ids are 1-based (0 means "no selection"); map back to `actions`.
        match selected.0 {
            id if id > 0 => actions
                .get((id - 1) as usize)
                .map(|action| action.boxed_clone()),
            _ => None,
        }
    }
}

/// Make Win32 popup menus follow the application's theme.
///
/// Windows does not expose a documented dark-mode API for `HMENU`. These
/// dynamically resolved uxtheme entry points are used by Windows itself and
/// degrade to the normal system menu when unavailable.
unsafe fn apply_menu_theme(hwnd: HWND, dark_mode: bool) {
    type AllowDarkModeForWindow = unsafe extern "system" fn(HWND, BOOL) -> BOOL;
    type SetPreferredAppMode = unsafe extern "system" fn(i32) -> i32;
    type FlushMenuThemes = unsafe extern "system" fn();

    static UXTHEME_MODULE: OnceLock<isize> = OnceLock::new();
    let module = *UXTHEME_MODULE.get_or_init(|| {
        unsafe { LoadLibraryW(windows::core::w!("uxtheme.dll")) }
            .map(|module| module.0 as isize)
            .unwrap_or_default()
    });
    if module == 0 {
        return;
    }
    let module = windows::Win32::Foundation::HMODULE(module as *mut c_void);

    let allow_dark_mode = unsafe { GetProcAddress(module, PCSTR(133usize as *const u8)) };
    let set_preferred_mode = unsafe { GetProcAddress(module, PCSTR(135usize as *const u8)) };
    let flush_menu_themes = unsafe { GetProcAddress(module, PCSTR(136usize as *const u8)) };

    if let Some(function) = allow_dark_mode {
        let function: AllowDarkModeForWindow = unsafe { std::mem::transmute(function) };
        let _ = unsafe { function(hwnd, BOOL::from(dark_mode)) };
    }
    if let Some(function) = set_preferred_mode {
        let function: SetPreferredAppMode = unsafe { std::mem::transmute(function) };
        const FORCE_DARK: i32 = 2;
        const FORCE_LIGHT: i32 = 3;
        let _ = unsafe { function(if dark_mode { FORCE_DARK } else { FORCE_LIGHT }) };
    }
    if let Some(function) = flush_menu_themes {
        let function: FlushMenuThemes = unsafe { std::mem::transmute(function) };
        unsafe { function() };
    }
}

/// Recursively create an `HMENU`. Each actionable leaf gets a 1-based id equal
/// to its index in `actions` plus one, so the returned id maps back to its action.
///
/// Any bitmaps created for item images are pushed onto `bitmaps`; the caller
/// must free them after destroying the menu with `DeleteObject`. Item images
/// are sized to `image_px` (physical pixels).
///
/// # Safety
/// Win32 menu creation; the returned `HMENU` must be destroyed by the caller.
unsafe fn build_menu<'a>(
    items: &'a [NativeMenuItem],
    asset_source: &dyn AssetSource,
    actions: &mut Vec<&'a Box<dyn Action>>,
    bitmaps: &mut Vec<HBITMAP>,
    image_px: u32,
) -> Option<HMENU> {
    let menu = unsafe { CreatePopupMenu() }.ok()?;

    // 0-based position of the next item appended, used to attach bitmaps by position (separators
    // and submenus advance it too).
    let mut position: u32 = 0;
    for item in items {
        match item {
            NativeMenuItem::Separator => {
                let _ = unsafe { AppendMenuW(menu, MF_SEPARATOR, 0, PCWSTR::null()) };
                position += 1;
            }
            NativeMenuItem::Item {
                label,
                disabled,
                checked,
                icon,
                action,
            } => {
                let mut flags = MF_STRING;
                if *disabled {
                    flags |= MF_GRAYED;
                }
                if *checked {
                    flags |= MF_CHECKED;
                }
                let wide: Vec<u16> = label.encode_utf16().chain(std::iter::once(0)).collect();
                // Actionable, enabled items get an id; others use 0.
                let id = match action {
                    Some(action) if !*disabled => {
                        actions.push(action);
                        actions.len()
                    }
                    _ => 0,
                };
                let _ = unsafe { AppendMenuW(menu, flags, id, PCWSTR(wide.as_ptr())) };
                if let Some(icon) = icon {
                    if let Some(bitmap) =
                        unsafe { load_hbitmap(icon.path_ref(), asset_source, image_px) }
                    {
                        let info = MENUITEMINFOW {
                            cbSize: std::mem::size_of::<MENUITEMINFOW>() as u32,
                            fMask: MIIM_BITMAP,
                            hbmpItem: bitmap,
                            ..Default::default()
                        };
                        let _ = unsafe { SetMenuItemInfoW(menu, position, true, &info) };
                        bitmaps.push(bitmap);
                    }
                }
                position += 1;
            }
            NativeMenuItem::Submenu {
                label,
                disabled,
                items,
            } => {
                let Some(submenu) =
                    (unsafe { build_menu(items, asset_source, actions, bitmaps, image_px) })
                else {
                    continue;
                };
                let mut flags = MF_POPUP;
                if *disabled {
                    flags |= MF_GRAYED;
                }
                let wide: Vec<u16> = label.encode_utf16().chain(std::iter::once(0)).collect();
                // For MF_POPUP, the id parameter is the submenu handle.
                let _ =
                    unsafe { AppendMenuW(menu, flags, submenu.0 as usize, PCWSTR(wide.as_ptr())) };
                position += 1;
            }
        }
    }

    Some(menu)
}

/// RAII guard for a GDI+ session (`GdiplusStartup` / `GdiplusShutdown`).
///
/// Loading image files into bitmaps requires GDI+ to be initialized. A `None` token means startup
/// failed; image loading is then skipped gracefully.
struct GdiplusSession {
    token: usize,
}

impl GdiplusSession {
    /// Start GDI+. Returns a guard whose `Drop` calls `GdiplusShutdown`.
    unsafe fn start() -> Option<Self> {
        let input = GdiplusStartupInput {
            GdiplusVersion: 1,
            DebugEventCallback: 0,
            SuppressBackgroundThread: BOOL(0),
            SuppressExternalCodecs: BOOL(0),
        };

        let mut token: usize = 0;
        let status = unsafe { GdiplusStartup(&mut token, &input, std::ptr::null_mut()) };
        if status.0 == 0 {
            Some(Self { token })
        } else {
            None
        }
    }
}

impl Drop for GdiplusSession {
    fn drop(&mut self) {
        unsafe { GdiplusShutdown(self.token) };
    }
}

/// Load an image into an `HBITMAP`, scaled to `image_px` square so it
/// doesn't overflow the menu row.
///
/// SVG files are rasterized with `resvg` (see [`rasterize_svg`]); GDI+ has no
/// SVG codec. Every other format (PNG, JPEG, BMP, ...) is decoded by GDI+, which
/// must already be initialized (see [`GdiplusSession`]).
///
/// Returns `None` if the file can't be read or decoded. The returned bitmap
/// must be freed with `DeleteObject`.
///
/// # Safety
/// Calls GDI+ /GDI flat APIs; the returned handle is owned by the caller.
unsafe fn load_hbitmap(
    path: &SharedString,
    asset_source: &dyn AssetSource,
    image_px: u32,
) -> Option<HBITMAP> {
    let image = resolve_icon_image(path, asset_source)?;
    if image.bytes.is_empty() {
        return None;
    }

    if image.format == ImageFormat::Svg {
        return unsafe { rasterize_svg(&image.bytes, image_px) };
    }

    let stream = unsafe { stream_from_bytes(&image.bytes) }?;
    let mut gp_bitmap: *mut GpBitmap = std::ptr::null_mut();
    let status = unsafe { GdipCreateBitmapFromStream(&stream, &mut gp_bitmap) };
    if status.0 != 0 || gp_bitmap.is_null() {
        return None;
    }

    unsafe { thumbnail_hbitmap(gp_bitmap, image_px) }
}

unsafe fn thumbnail_hbitmap(gp_bitmap: *mut GpBitmap, image_px: u32) -> Option<HBITMAP> {
    // Scale to a menu icon sized thumbnail; GDI+ does not resize on display.
    let mut thumb: *mut GpImage = std::ptr::null_mut();
    let status = unsafe {
        GdipGetImageThumbnail(
            gp_bitmap.cast(),
            image_px,
            image_px,
            &mut thumb,
            0,
            std::ptr::null_mut(),
        )
    };

    unsafe { GdipDisposeImage(gp_bitmap.cast()) };
    if status.0 != 0 || thumb.is_null() {
        return None;
    }

    let mut hbitmap = HBITMAP::default();
    // ARGB background 0 (fully transparent) for the alpha conversion.
    let status = unsafe { GdipCreateHBITMAPFromBitmap(thumb.cast(), &mut hbitmap, 0) };

    unsafe { GdipDisposeImage(thumb) };

    if status.0 != 0 || hbitmap.is_invalid() {
        None
    } else {
        Some(hbitmap)
    }
}

unsafe fn stream_from_bytes(bytes: &[u8]) -> Option<windows::Win32::System::Com::IStream> {
    let hglobal = unsafe { GlobalAlloc(GMEM_MOVEABLE, bytes.len()) }.ok()?;
    let data = unsafe { GlobalLock(hglobal) };
    if data.is_null() {
        let _ = unsafe { GlobalFree(hglobal) };
        return None;
    }

    unsafe { std::ptr::copy_nonoverlapping(bytes.as_ptr(), data.cast::<u8>(), bytes.len()) };
    let _ = unsafe { GlobalUnlock(hglobal) };

    match unsafe { CreateStreamOnHGlobal(hglobal, BOOL(1)) } {
        Ok(stream) => Some(stream),
        Err(_) => {
            let _ = unsafe { GlobalFree(hglobal) };
            None
        }
    }
}

/// Extract the Win32 `HWND` (as an `isize`) from the window's raw handle.
fn hwnd_ptr(window: &Window) -> Option<isize> {
    let handle = HasWindowHandle::window_handle(window).ok()?;
    let RawWindowHandle::Win32(handle) = handle.as_raw() else {
        return None;
    };
    Some(handle.hwnd.get())
}

/// Rasterize SVG bytes into an `HBITMAP`, scaled to `image_px` square.
///
/// GDI+ has no SVG codec, so SVG data is rendered with `resvg` and wrapped in a
/// 32-bit DIB section. The SVG is scaled uniformly to fit the square and centered.
/// Returns `None` if the SVG can't be parsed. The returned bitmap must be freed
/// with `DeleteObject`.
///
/// # Safety
/// Creates a GDI DIB section; the returned handle is owned by the caller.
unsafe fn rasterize_svg(data: &[u8], image_px: u32) -> Option<HBITMAP> {
    use resvg::{tiny_skia, usvg};

    let tree = usvg::Tree::from_data(data, &usvg::Options::default()).ok()?;

    let size = image_px;
    let mut pixmap = tiny_skia::Pixmap::new(size, size)?;

    // Fit the SVG into square without distortion, then center it.
    let svg = tree.size();
    let scale = (size as f32 / svg.width()).min(size as f32 / svg.height());
    let tx = (size as f32 - svg.width() * scale) / 2.0;
    let ty = (size as f32 - svg.height() * scale) / 2.0;
    let transform = tiny_skia::Transform::from_scale(scale, scale).post_translate(tx, ty);

    resvg::render(&tree, transform, &mut pixmap.as_mut());

    // tiny-skia produces premultiplied RGBA; a 32 bit DIB is laid out RGBA, so
    // swap the red and blue channels in place. The alpha is already
    // premultiplied, which is what the menu's alpha blending expects.
    let mut pixels = pixmap.take();
    for px in pixels.chunks_exact_mut(4) {
        px.swap(0, 2)
    }

    unsafe { create_dib(&pixels, size, size) }
}

/// Wrap premultiplied-RGBA `pixels` (top-down, `width` x `height`) in a 32-bit
/// DIB section `HBITMAP`. Returns `None` if creation fails; the returned bitmap
/// must be freed with `DeleteObject`.
///
/// # Safety
/// Calls GDI flat APIs and copies `pixels` into the section's backing store,
/// which must be `width * height * 4` bytes.
unsafe fn create_dib(pixels: &[u8], width: u32, height: u32) -> Option<HBITMAP> {
    let info = BITMAPINFO {
        bmiHeader: BITMAPINFOHEADER {
            biSize: std::mem::size_of::<BITMAPINFOHEADER>() as u32,
            biWidth: width as i32,
            // Negative height selects a top-down DIB (origin at top-left), matching
            // tiny-skia's row order.
            biHeight: -(height as i32),
            biPlanes: 1,
            biBitCount: 32,
            biCompression: BI_RGB.0,
            ..Default::default()
        },
        ..Default::default()
    };

    let mut bits: *mut c_void = std::ptr::null_mut();
    // A null `HDC` is fine with `DIB_RGB_COLORS` (no palette to resolve).
    let hbitmap = unsafe {
        CreateDIBSection(
            HDC::default(),
            &info,
            DIB_RGB_COLORS,
            &mut bits,
            HANDLE::default(),
            0,
        )
    }
    .ok()?;

    if bits.is_null() {
        let _ = unsafe { DeleteObject(HGDIOBJ(hbitmap.0)) };
        return None;
    }

    unsafe { std::ptr::copy_nonoverlapping(pixels.as_ptr(), bits as *mut u8, pixels.len()) };
    Some(hbitmap)
}