wcpopup 0.9.3

Context menu for Windows and Linux
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
#[cfg(feature = "accelerator")]
use super::{accelerator::create_haccel, get_menu_data};
use super::{
    calculate, hwnd,
    image::{create_check_svg, create_menu_image, create_render_target, create_submenu_svg, get_icon_space, MenuImageType},
    is_win11,
    menu_item::MenuItem,
    util::set_window_border_color,
    IconSpace, Menu, PopupInfo, Size,
};
use crate::{
    config::{Config, Corner, IconSettings, Theme},
    MenuIcon, MenuItemType, MenuType,
};
#[cfg(feature = "accelerator")]
use std::rc::Rc;
use std::{
    collections::HashMap,
    mem::size_of,
    sync::atomic::{AtomicU32, Ordering},
};
#[cfg(feature = "accelerator")]
use windows::Win32::UI::WindowsAndMessaging::HACCEL;
use windows::{
    core::Error,
    Win32::{
        Foundation::HWND,
        Graphics::{
            Direct2D::ID2D1DCRenderTarget,
            Dwm::{DwmSetWindowAttribute, DWMWA_WINDOW_CORNER_PREFERENCE, DWMWCP_ROUND, DWM_WINDOW_CORNER_PREFERENCE},
        },
        UI::WindowsAndMessaging::{SetWindowLongPtrW, GWL_USERDATA},
    },
};

static COUNTER: AtomicU32 = AtomicU32::new(400);

#[derive(Debug)]
pub(crate) struct MenuData {
    pub(crate) popup_info: Option<PopupInfo>,
    pub(crate) menu_type: MenuType,
    pub(crate) items: Vec<MenuItem>,
    pub(crate) win_subclass_id: u32,
    pub(crate) selected_index: i32,
    pub(crate) size: Size,
    pub(crate) icon_space: IconSpace,
    pub(crate) visible_submenu_index: i32,
    pub(crate) current_theme: Theme,
    pub(crate) config: Config,
    pub(crate) parent: isize,
    pub(crate) dc_render_target: ID2D1DCRenderTarget,
    pub(crate) check_icon: MenuImageType,
    pub(crate) submenu_icon: MenuImageType,
    pub(crate) icon_map: HashMap<u16, MenuImageType>,
    #[cfg(feature = "accelerator")]
    pub(crate) haccel: Option<Rc<HACCEL>>,
    #[cfg(feature = "accelerator")]
    pub(crate) accelerators: HashMap<u16, String>,
}

/// Builder to create Menu.
pub struct MenuBuilder {
    pub(crate) menu: Menu,
    items: Vec<MenuItem>,
    theme: Theme,
    config: Config,
    menu_type: MenuType,
}

impl MenuBuilder {
    /// Creates a new Menu for the specified window handle.
    pub fn new(window_handle: isize) -> Self {
        Self::new_builder(window_handle, MenuType::Main)
    }

    /// Creates a new Menu for the specified HWND.
    pub fn new_for_hwnd(hwnd: HWND) -> Self {
        let window_handle = hwnd.0 as isize;
        Self::new_builder(window_handle, MenuType::Main)
    }

    fn new_builder(window_handle: isize, menu_type: MenuType) -> Self {
        let mut menu = Menu::default();
        menu.parent_window_handle = window_handle;
        menu.window_handle = menu.create_window(window_handle);
        let config = Config::default();
        let theme = config.theme;
        Self {
            menu,
            items: Vec::new(),
            config,
            theme,
            menu_type,
        }
    }

    /// Creates a new Menu with the specified Theme for the specified window handle.
    pub fn new_with_theme(window_handle: isize, theme: Theme) -> Self {
        Self::new_builder_with_theme(window_handle, theme, MenuType::Main)
    }

    /// Creates a new Menu with the specified Theme for the specified HWND.
    pub fn new_for_hwnd_with_theme(hwnd: HWND, theme: Theme) -> Self {
        let window_handle = hwnd.0 as isize;
        Self::new_builder_with_theme(window_handle, theme, MenuType::Main)
    }

    fn new_builder_with_theme(window_handle: isize, theme: Theme, menu_type: MenuType) -> Self {
        let mut menu = Menu::default();
        menu.parent_window_handle = window_handle;
        menu.window_handle = menu.create_window(window_handle);
        let config = Config {
            theme,
            ..Default::default()
        };
        Self {
            menu,
            items: Vec::new(),
            config,
            theme,
            menu_type,
        }
    }

    /// Creates a new Menu using the specified Config for the specified window handle.
    pub fn new_from_config(window_handle: isize, config: Config) -> Self {
        Self::new_builder_from_config(window_handle, config, MenuType::Main)
    }

    /// Creates a new Menu using the specified Config for the specified HWND.
    pub fn new_for_hwnd_from_config(hwnd: HWND, config: Config) -> Self {
        let window_handle = hwnd.0 as isize;
        Self::new_builder_from_config(window_handle, config, MenuType::Main)
    }

    fn new_builder_from_config(window_handle: isize, config: Config, menu_type: MenuType) -> Self {
        let mut menu = Menu::default();
        menu.parent_window_handle = window_handle;
        menu.window_handle = menu.create_window(window_handle);
        let theme = config.theme;

        Self {
            menu,
            items: Vec::new(),
            config: Config {
                icon: if let Some(icon) = config.icon {
                    Some(icon)
                } else {
                    Some(IconSettings::default())
                },
                ..config
            },
            theme,
            menu_type,
        }
    }

    /// Adds a text MenuItem to Menu.
    pub fn text(&mut self, id: &str, label: &str, disabled: bool) -> &Self {
        let mut item = MenuItem::new_text_item(id, label, None, disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn text_with_accelerator(&mut self, id: &str, label: &str, disabled: bool, accelerator: &str) -> &Self {
        let mut item = MenuItem::new_text_item(id, label, Some(accelerator), disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn text_with_icon(&mut self, id: &str, label: &str, disabled: bool, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_text_item(id, label, None, disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn text_with_accel_icon(&mut self, id: &str, label: &str, disabled: bool, accelerator: &str, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_text_item(id, label, Some(accelerator), disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    /// Adds a check MenuItem to Menu.
    pub fn check(&mut self, id: &str, label: &str, checked: bool, disabled: bool) -> &Self {
        let mut item = MenuItem::new_check_item(id, label, None, checked, disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn check_with_accelerator(&mut self, id: &str, label: &str, checked: bool, disabled: bool, accelerator: &str) -> &Self {
        let mut item = MenuItem::new_check_item(id, label, Some(accelerator), checked, disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn check_with_icon(&mut self, id: &str, label: &str, checked: bool, disabled: bool, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_check_item(id, label, None, checked, disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn check_with_accel_icon(&mut self, id: &str, label: &str, checked: bool, disabled: bool, accelerator: &str, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_check_item(id, label, Some(accelerator), checked, disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    /// Adds a radio MenuItem to Menu.
    pub fn radio(&mut self, id: &str, label: &str, name: &str, checked: bool, disabled: bool) -> &Self {
        let mut item = MenuItem::new_radio_item(id, label, name, None, checked, disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn radio_with_accelerator(&mut self, id: &str, label: &str, name: &str, checked: bool, disabled: bool, accelerator: &str) -> &Self {
        let mut item = MenuItem::new_radio_item(id, label, name, Some(accelerator), checked, disabled, None);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn radio_with_icon(&mut self, id: &str, label: &str, name: &str, checked: bool, disabled: bool, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_radio_item(id, label, name, None, checked, disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    #[allow(clippy::too_many_arguments)]
    pub fn radio_with_accel_icon(&mut self, id: &str, label: &str, name: &str, checked: bool, disabled: bool, accelerator: &str, icon: MenuIcon) -> &Self {
        let mut item = MenuItem::new_radio_item(id, label, name, Some(accelerator), checked, disabled, Some(icon));
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    /// Adds a separator to Menu.
    pub fn separator(&mut self) -> &Self {
        let mut item = MenuItem::new_separator();
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    pub fn separator_with_id(&mut self, id: &str) -> &Self {
        let mut item = MenuItem::new_separator_with_id(id);
        item.menu_window_handle = self.menu.window_handle;
        self.items.push(item);
        self
    }

    /// Adds a submenu MenuItem to Menu.
    pub fn submenu(&mut self, id: &str, label: &str, disabled: bool) -> Self {
        let mut item = MenuItem::new(self.menu.window_handle, id, label, "", "", false, disabled, MenuItemType::Submenu, None, None);
        let builder = Self::new_builder_from_config(self.menu.window_handle, self.config.clone(), MenuType::Submenu);

        item.submenu = Some(builder.menu.clone());
        self.items.push(item);

        builder
    }

    pub fn submenu_with_icon(&mut self, id: &str, label: &str, disabled: bool, icon: MenuIcon) -> Self {
        let mut item = MenuItem::new(self.menu.window_handle, id, label, "", "", false, disabled, MenuItemType::Submenu, None, Some(icon));
        let builder = Self::new_builder_from_config(self.menu.window_handle, self.config.clone(), MenuType::Submenu);

        item.submenu = Some(builder.menu.clone());
        self.items.push(item);

        builder
    }

    pub(crate) fn new_for_submenu(parent: &Menu, config: &Config, current_theme: Theme, items: &mut [MenuItem]) -> Self {
        let mut builder = Self::new_builder_from_config(parent.window_handle, config.clone(), MenuType::Submenu);
        for item in items.iter_mut() {
            item.menu_window_handle = builder.menu.window_handle
        }

        builder.items = items.to_vec();
        builder.theme = current_theme;

        builder
    }

    /// Adds a MenuItem to MenuBuilder.
    pub fn append(&mut self, mut menu_item: MenuItem) -> &Self {
        if menu_item.menu_item_type == MenuItemType::Submenu && menu_item.menu_window_handle == 0 {
            let builder = MenuBuilder::new_for_submenu(&self.menu, &self.config, self.config.theme, menu_item.items.as_mut().unwrap());
            let submenu = builder.build().unwrap();
            menu_item.menu_window_handle = submenu.parent_window_handle;
            menu_item.submenu = Some(submenu);
        }
        menu_item.menu_window_handle = self.menu.window_handle;
        self.items.push(menu_item);
        self
    }

    /// Adds MenuItems to MenuBuilder.
    pub fn append_all(&mut self, menu_items: Vec<MenuItem>) -> &Self {
        for menu_item in menu_items {
            self.append(menu_item);
        }
        self
    }

    /// Build Menu to make it ready to become visible.
    /// Must call this function before showing Menu, otherwise nothing shows up.
    pub fn build(mut self) -> Result<Menu, Error> {
        let is_main_menu = self.menu_type == MenuType::Main;

        #[cfg(feature = "accelerator")]
        let mut accelerators = HashMap::new();
        #[cfg(feature = "accelerator")]
        let mut haccel = None;
        #[cfg(feature = "accelerator")]
        if is_main_menu {
            collect_accelerators(&self.items, &mut accelerators);
            if !accelerators.is_empty() {
                match create_haccel(&accelerators) {
                    Some(accel) => haccel = Some(Rc::new(accel)),
                    None => haccel = None,
                }
            }
        }

        let dc_render_target = create_render_target()?;

        let check_icon = if let Some(check_icon) = &self.config.icon.as_ref().unwrap().check {
            create_menu_image(&dc_render_target, check_icon)?
        } else {
            MenuImageType::Svg(create_check_svg(&dc_render_target, &self.config)?)
        };

        let submenu_icon = if let Some(submenu_icon) = &self.config.icon.as_ref().unwrap().arrow {
            create_menu_image(&dc_render_target, submenu_icon)?
        } else {
            MenuImageType::Svg(create_submenu_svg(&dc_render_target, &self.config)?)
        };

        /* Safe to unwrap icon which is Some(IconSettings::default()) by default */
        let icon_space = get_icon_space(&self.items, self.config.icon.as_ref().unwrap(), &check_icon, &submenu_icon);
        let menu_size = calculate(&mut self.items, &self.config, self.config.theme, icon_space)?;

        if is_main_menu {
            /* Calculate items in all submenus */
            self.rebuild_submenu(icon_space)?;
        }

        /* Save icon for reuse */
        let mut icon_map = HashMap::new();
        for item in &self.items {
            if let Some(icon) = &item.icon {
                let bitmap = create_menu_image(&dc_render_target, icon)?;
                icon_map.insert(item.uuid, bitmap);
            }
        }

        let win_subclass_id = COUNTER.fetch_add(1, Ordering::Relaxed);
        if is_main_menu {
            self.menu.attach_owner_subclass(win_subclass_id as usize);
        }

        let data = MenuData {
            menu_type: self.menu_type,
            items: self.items,
            win_subclass_id,
            #[cfg(feature = "accelerator")]
            haccel,
            #[cfg(feature = "accelerator")]
            accelerators,
            size: menu_size,
            icon_space,
            selected_index: -1,
            visible_submenu_index: -1,
            current_theme: self.theme,
            config: self.config,
            parent: if is_main_menu {
                0
            } else {
                self.menu.parent_window_handle
            },
            dc_render_target,
            check_icon,
            submenu_icon,
            popup_info: None,
            icon_map,
        };

        let hwnd = hwnd!(self.menu.window_handle);

        if is_win11() {
            if data.config.corner == Corner::Round {
                unsafe { DwmSetWindowAttribute(hwnd, DWMWA_WINDOW_CORNER_PREFERENCE, &DWMWCP_ROUND as *const _ as *const _, size_of::<DWM_WINDOW_CORNER_PREFERENCE>() as u32)? };
            }

            set_window_border_color(self.menu.window_handle, &data)?;
        }

        unsafe { SetWindowLongPtrW(hwnd, GWL_USERDATA, Box::into_raw(Box::new(data)) as isize) };

        Ok(self.menu)
    }

    fn rebuild_submenu(&mut self, icon_space: IconSpace) -> Result<(), Error> {
        for item in self.items.iter_mut() {
            if item.menu_item_type == MenuItemType::Submenu {
                let submenu = item.submenu.as_mut().unwrap();
                // let submenu = item.submenu.as_mut().unwrap();
                // submenu.menu_type = MenuType::Submenu;
                let _ = calculate(&mut submenu.items(), &self.config, self.config.theme, icon_space)?;
            }
        }
        Ok(())
    }
}

#[cfg(feature = "accelerator")]
fn collect_accelerators(items: &Vec<MenuItem>, accelerators: &mut HashMap<u16, String>) {
    for item in items {
        if item.menu_item_type == MenuItemType::Submenu {
            let submenu_window_handle = item.submenu.as_ref().unwrap().window_handle;
            let data = get_menu_data(submenu_window_handle);
            collect_accelerators(&data.items, accelerators);
        } else if !item.accelerator.is_empty() {
            accelerators.insert(item.uuid, item.accelerator.clone());
        }
    }
}