winapi-easy 0.3.0

A safe interface to various winapi functionality
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
//! Menus and menu items.

use std::cell::RefCell;
use std::io::ErrorKind;
use std::marker::PhantomData;
use std::rc::Rc;
use std::{
    io,
    mem,
};

pub(crate) use private::MenuHandle;
use windows::Win32::UI::WindowsAndMessaging::{
    CreateMenu,
    CreatePopupMenu,
    DestroyMenu,
    GetMenuItemCount,
    GetMenuItemID,
    HMENU,
    InsertMenuItemW,
    IsMenu,
    MENUINFO,
    MENUITEMINFOW,
    MF_BYPOSITION,
    MFS_CHECKED,
    MFS_DISABLED,
    MFT_RADIOCHECK,
    MFT_SEPARATOR,
    MFT_STRING,
    MIIM_FTYPE,
    MIIM_ID,
    MIIM_STATE,
    MIIM_STRING,
    MIIM_SUBMENU,
    MIM_STYLE,
    MNS_NOTIFYBYPOS,
    RemoveMenu,
    SetMenuInfo,
    SetMenuItemInfoW,
    TrackPopupMenu,
};

#[expect(clippy::wildcard_imports)]
use self::private::*;
use crate::internal::{
    ResultExt,
    ReturnValue,
};
use crate::string::ZeroTerminatedWideString;
use crate::ui::{
    Point,
    WindowHandle,
};

mod private {
    #[expect(clippy::wildcard_imports)]
    use super::*;

    #[cfg(test)]
    static_assertions::assert_not_impl_any!(MenuHandle: Send, Sync);

    #[derive(Eq, PartialEq, Debug)]
    pub struct MenuHandle {
        pub(super) raw_handle: HMENU,
        pub(super) marker: PhantomData<*mut ()>,
    }

    pub trait MenuKindPrivate {
        type MenuItem: MenuItemKind;
        fn new_handle() -> io::Result<MenuHandle>;
    }

    pub trait MenuItemKind: Clone {
        fn call_with_raw_menu_info<O>(&self, call: impl FnOnce(MENUITEMINFOW) -> O) -> O;
    }
}

impl MenuHandle {
    fn new_menu() -> io::Result<Self> {
        let handle = unsafe { CreateMenu()?.if_null_get_last_error()? };
        let result = Self {
            raw_handle: handle,
            marker: PhantomData,
        };
        result.set_notify_by_pos()?;
        Ok(result)
    }

    fn new_submenu() -> io::Result<Self> {
        let handle = unsafe { CreatePopupMenu()?.if_null_get_last_error()? };
        let result = Self {
            raw_handle: handle,
            marker: PhantomData,
        };
        result.set_notify_by_pos()?;
        Ok(result)
    }

    #[expect(dead_code)]
    pub(crate) fn from_non_null(raw_handle: HMENU) -> Self {
        Self {
            raw_handle,
            marker: PhantomData,
        }
    }

    pub(crate) fn from_maybe_null(handle: HMENU) -> Option<Self> {
        if handle.is_null() {
            None
        } else {
            Some(Self {
                raw_handle: handle,
                marker: PhantomData,
            })
        }
    }

    pub(crate) fn as_raw_handle(&self) -> HMENU {
        self.raw_handle
    }

    /// Sets the menu to send `WM_MENUCOMMAND` instead of `WM_COMMAND` messages.
    ///
    /// According to docs: This is a menu header style and has no effect when applied to individual sub menus.
    fn set_notify_by_pos(&self) -> io::Result<()> {
        let raw_menu_info = MENUINFO {
            cbSize: mem::size_of::<MENUINFO>()
                .try_into()
                .unwrap_or_else(|_| unreachable!()),
            fMask: MIM_STYLE,
            dwStyle: MNS_NOTIFYBYPOS,
            cyMax: 0,
            hbrBack: Default::default(),
            dwContextHelpID: 0,
            dwMenuData: 0,
        };
        unsafe {
            SetMenuInfo(self.raw_handle, &raw const raw_menu_info)?;
        }
        Ok(())
    }

    fn insert_menu_item<MI: MenuItemKind>(&self, item: &MI, idx: u32) -> io::Result<()> {
        let insert_call = |raw_item_info| {
            unsafe {
                InsertMenuItemW(self.raw_handle, idx, true, &raw const raw_item_info)?;
            }
            Ok(())
        };
        item.call_with_raw_menu_info(insert_call)
    }

    fn modify_menu_item<MI: MenuItemKind>(&self, item: &MI, idx: u32) -> io::Result<()> {
        let insert_call = |raw_item_info| {
            unsafe {
                SetMenuItemInfoW(self.raw_handle, idx, true, &raw const raw_item_info)?;
            }
            Ok(())
        };
        item.call_with_raw_menu_info(insert_call)
    }

    /// Removes an item.
    ///
    /// If the item contains a submenu, the submenu itself is preserved.
    fn remove_item(&self, idx: u32) -> io::Result<()> {
        unsafe {
            RemoveMenu(self.raw_handle, idx, MF_BYPOSITION)?;
        }
        Ok(())
    }

    pub(crate) fn get_item_id(&self, item_idx: u32) -> io::Result<u32> {
        let id = unsafe { GetMenuItemID(self.raw_handle, item_idx.cast_signed()) };
        id.if_eq_to_error((-1i32).cast_unsigned(), || ErrorKind::Other.into())?;
        Ok(id)
    }

    fn get_item_count(&self) -> io::Result<i32> {
        let count = unsafe { GetMenuItemCount(Some(self.raw_handle)) };
        count.if_eq_to_error(-1, io::Error::last_os_error)?;
        Ok(count)
    }

    #[expect(dead_code)]
    fn is_menu(&self) -> bool {
        unsafe { IsMenu(self.raw_handle).as_bool() }
    }

    fn destroy(&self) -> io::Result<()> {
        unsafe {
            DestroyMenu(self.raw_handle)?;
        }
        Ok(())
    }
}

impl From<MenuHandle> for HMENU {
    fn from(value: MenuHandle) -> Self {
        value.raw_handle
    }
}

impl From<&MenuHandle> for HMENU {
    fn from(value: &MenuHandle) -> Self {
        value.raw_handle
    }
}

pub trait MenuKind: MenuKindPrivate {}

#[derive(Debug)]
pub enum MenuBarKind {}

impl MenuKindPrivate for MenuBarKind {
    type MenuItem = TextMenuItem;

    fn new_handle() -> io::Result<MenuHandle> {
        MenuHandle::new_menu()
    }
}

impl MenuKind for MenuBarKind {}

#[derive(Debug)]
pub enum SubMenuKind {}

impl MenuKindPrivate for SubMenuKind {
    type MenuItem = SubMenuItem;

    fn new_handle() -> io::Result<MenuHandle> {
        MenuHandle::new_submenu()
    }
}

impl MenuKind for SubMenuKind {}

#[cfg(test)]
static_assertions::assert_not_impl_any!(Menu<MenuBarKind>: Send, Sync);
#[cfg(test)]
static_assertions::assert_not_impl_any!(Menu<SubMenuKind>: Send, Sync);

/// Generic menu (top-level or submenu).
#[derive(Debug)]
pub struct Menu<MK: MenuKind> {
    handle: MenuHandle,
    items: Vec<MK::MenuItem>,
}

impl<MK: MenuKind> Menu<MK> {
    pub fn new() -> io::Result<Self> {
        Ok(Self {
            handle: MK::new_handle()?,
            items: Vec::new(),
        })
    }

    pub fn new_from_items<I>(items: I) -> io::Result<Self>
    where
        I: IntoIterator<Item = MK::MenuItem>,
    {
        let mut result = Self::new()?;
        result.insert_menu_items(items)?;
        Ok(result)
    }

    pub fn as_handle(&self) -> &MenuHandle {
        &self.handle
    }

    /// Inserts a menu item before the item with the given index.
    ///
    /// If no index is given, it will be inserted after the last item.
    ///
    /// # Panics
    ///
    /// Will panic if the given index is greater than the current amount of items.
    pub fn insert_menu_item(&mut self, item: MK::MenuItem, index: Option<u32>) -> io::Result<()> {
        let handle_item_count: u32 = self
            .handle
            .get_item_count()?
            .try_into()
            .unwrap_or_else(|_| unreachable!());
        assert_eq!(handle_item_count, self.items.len().try_into().unwrap());
        let idx = match index {
            Some(idx) => idx,
            None => handle_item_count,
        };
        self.handle.insert_menu_item(&item, idx)?;
        self.items.insert(idx.try_into().unwrap(), item);
        Ok(())
    }

    pub fn insert_menu_items<I>(&mut self, items: I) -> io::Result<()>
    where
        I: IntoIterator<Item = MK::MenuItem>,
    {
        for item in items {
            self.insert_menu_item(item, None)?;
        }
        Ok(())
    }

    /// Modifies a menu item at the given index using the given closure.
    ///
    /// # Panics
    ///
    /// Will panic if the given index is out of bounds.
    pub fn modify_menu_item_by_index(
        &mut self,
        index: u32,
        modify_fn: impl FnOnce(&mut MK::MenuItem) -> io::Result<()>,
    ) -> io::Result<()> {
        let item = &mut self.items[usize::try_from(index).unwrap()];
        let mut modified_item = item.clone();
        modify_fn(&mut modified_item)?;
        self.handle.modify_menu_item(&modified_item, index)?;
        *item = modified_item;
        Ok(())
    }

    /// Removes a menu item.
    ///
    /// # Panics
    ///
    /// Will panic if the given index is out of bounds.
    pub fn remove_menu_item(&mut self, index: u32) -> io::Result<()> {
        let index_usize = usize::try_from(index).unwrap();
        assert!(index_usize < self.items.len());
        self.handle.remove_item(index)?;
        let _ = self.items.remove(index_usize);
        Ok(())
    }
}

impl Menu<SubMenuKind> {
    /// Modifies all text menu items with the given ID using the given closure.
    ///
    /// Will do nothing if no item with a matching ID is found.
    pub fn modify_text_menu_items_by_id(
        &mut self,
        id: u32,
        mut modify_fn: impl FnMut(&mut TextMenuItem) -> io::Result<()>,
    ) -> io::Result<()> {
        let indexes: Vec<_> = (0..)
            .zip(&self.items)
            .filter_map(|(index, item)| match item {
                SubMenuItem::Text(text_menu_item) => {
                    if text_menu_item.id == id {
                        Some(index)
                    } else {
                        None
                    }
                }
                SubMenuItem::Separator => None,
            })
            .collect();
        let mut internal_modify_fn = |item: &mut SubMenuItem| {
            if let SubMenuItem::Text(item) = item {
                modify_fn(item)?;
            } else {
                unreachable!()
            }
            Ok(())
        };
        for index in indexes {
            self.modify_menu_item_by_index(index, &mut internal_modify_fn)?;
        }
        Ok(())
    }

    /// Shows the popup menu at the given coordinates.
    ///
    /// The coordinates can for example be retrieved from the window message handler, see
    /// [`crate::ui::messaging::ListenerMessageVariant::NotificationIconContextSelect`]
    ///
    /// The given window needs to be the foreground window for the menu to show
    /// (use [`WindowHandle::set_as_foreground`]).
    pub fn show_menu(&self, window: WindowHandle, coords: Point) -> io::Result<()> {
        unsafe {
            TrackPopupMenu(
                self.handle.raw_handle,
                Default::default(),
                coords.x,
                coords.y,
                None,
                window.into(),
                None,
            )
            .if_null_get_last_error_else_drop()?;
        }
        Ok(())
    }
}

impl<MK: MenuKind> Drop for Menu<MK> {
    fn drop(&mut self) {
        let size_u32 = u32::try_from(self.items.len()).unwrap();
        // Remove all items first to avoid submenus getting destroyed by `DestroyMenu`
        for index in (0..size_u32).rev() {
            self.remove_menu_item(index)
                .unwrap_or_default_and_print_error();
        }
        self.handle.destroy().unwrap_or_default_and_print_error();
    }
}

/// A top-level menu.
///
/// Can be added to a window with [`crate::ui::window::Window::set_menu`].
pub type MenuBar = Menu<MenuBarKind>;

/// A submenu or popup menu.
///
/// Can for example be used with [`crate::ui::window::NotificationIcon`].
pub type SubMenu = Menu<SubMenuKind>;

/// A submenu item.
///
/// Can be added with [`SubMenu::insert_menu_item`].
#[derive(Clone, Debug)]
pub enum SubMenuItem {
    Text(TextMenuItem),
    Separator,
}

impl MenuItemKind for SubMenuItem {
    fn call_with_raw_menu_info<O>(&self, call: impl FnOnce(MENUITEMINFOW) -> O) -> O {
        match self {
            SubMenuItem::Text(text_item) => text_item.call_with_raw_menu_info(call),
            SubMenuItem::Separator => {
                let mut item_info = default_raw_item_info();
                item_info.fMask |= MIIM_FTYPE;
                item_info.fType |= MFT_SEPARATOR;
                call(item_info)
            }
        }
    }
}

#[derive(Clone, Default, Debug)]
pub struct TextMenuItem {
    pub id: u32,
    pub text: String,
    pub disabled: bool,
    pub item_symbol: Option<ItemSymbol>,
    pub sub_menu: Option<Rc<RefCell<SubMenu>>>,
}

impl TextMenuItem {
    pub fn default_with_text(id: u32, text: impl Into<String>) -> Self {
        Self {
            id,
            text: text.into(),
            disabled: false,
            item_symbol: None,
            sub_menu: None,
        }
    }
}

impl MenuItemKind for TextMenuItem {
    fn call_with_raw_menu_info<O>(&self, call: impl FnOnce(MENUITEMINFOW) -> O) -> O {
        // Must outlive the `MENUITEMINFOW` struct
        let mut text_wide_string = ZeroTerminatedWideString::from_os_str(&self.text);
        let mut item_info = default_raw_item_info();
        item_info.fMask |= MIIM_FTYPE | MIIM_STATE | MIIM_ID | MIIM_SUBMENU | MIIM_STRING;
        item_info.fType |= MFT_STRING;
        item_info.cch = text_wide_string.as_ref().len().try_into().unwrap();
        item_info.dwTypeData = text_wide_string.as_raw_pwstr();
        if self.disabled {
            item_info.fState |= MFS_DISABLED;
        }
        if let Some(checkmark) = self.item_symbol {
            item_info.fState |= MFS_CHECKED;
            match checkmark {
                ItemSymbol::CheckMark => (),
                ItemSymbol::RadioButton => item_info.fType |= MFT_RADIOCHECK,
            }
        }
        // `MFS_HILITE` highlights an item as if selected, but only once, and has no further effects, so we skip it.

        item_info.wID = self.id;
        if let Some(submenu) = &self.sub_menu {
            item_info.hSubMenu = submenu.borrow().handle.raw_handle;
        }
        call(item_info)
    }
}

#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub enum ItemSymbol {
    #[default]
    CheckMark,
    RadioButton,
}

fn default_raw_item_info() -> MENUITEMINFOW {
    MENUITEMINFOW {
        cbSize: mem::size_of::<MENUITEMINFOW>()
            .try_into()
            .unwrap_or_else(|_| unreachable!()),
        ..Default::default()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn create_test_menu() -> io::Result<()> {
        let mut menu = SubMenu::new()?;
        const TEST_ID: u32 = 42;
        const TEST_ID2: u32 = 43;
        menu.insert_menu_items([
            SubMenuItem::Text(TextMenuItem::default_with_text(TEST_ID, "text")),
            SubMenuItem::Separator,
        ])?;
        menu.modify_menu_item_by_index(0, |item| {
            if let SubMenuItem::Text(item) = item {
                item.disabled = true;
                Ok(())
            } else {
                panic!()
            }
        })?;
        menu.modify_menu_item_by_index(1, |item| {
            *item = SubMenuItem::Text(TextMenuItem::default_with_text(TEST_ID2, "text2"));
            Ok(())
        })?;
        let submenu2: Rc<RefCell<_>> = {
            let submenu2 = SubMenu::new_from_items([SubMenuItem::Separator])?;
            Rc::new(RefCell::new(submenu2))
        };
        {
            let mut menu2 = SubMenu::new()?;
            menu2.insert_menu_item(
                SubMenuItem::Text(TextMenuItem {
                    sub_menu: Some(submenu2.clone()),
                    ..TextMenuItem::default_with_text(0, "")
                }),
                None,
            )?;
        }
        menu.insert_menu_item(
            SubMenuItem::Text(TextMenuItem {
                sub_menu: Some(submenu2),
                ..TextMenuItem::default_with_text(0, "Submenu")
            }),
            None,
        )?;
        assert_eq!(menu.handle.get_item_count()?, 3);
        assert_eq!(menu.handle.get_item_id(0)?, TEST_ID);
        assert_eq!(menu.handle.get_item_id(1)?, TEST_ID2);

        let menu = Rc::new(RefCell::new(menu));
        let menu_bar = MenuBar::new_from_items([TextMenuItem {
            sub_menu: Some(menu),
            ..TextMenuItem::default_with_text(0, "File")
        }])?;
        assert_eq!(menu_bar.handle.get_item_count()?, 1);

        Ok(())
    }
}