Skip to content

Commit 17d463d

Browse files
committed
Add ability to assign a menu when creating a window on Windows
The primary reason for doing this here instead of just assigning a menu later on with `SetMenu` is to avoid that `with_inner_size` breaks, since the menu takes up some space that is not taken into account, which means the user would have to use `set_inner_size` again after adding the menu. Notes: - Child windows cannot have a menu, so perhaps this should be represented typewise? - Dark mode doesn't look good with win32 menus, unfortunately not fixable, see ysc3839/win32-darkmode#1
1 parent 0512502 commit 17d463d

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

FEATURES.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ If your PR makes notable changes to Winit's features, please update this section
116116
### Windows
117117
* Setting the taskbar icon
118118
* Setting the parent window
119+
* Setting a menu bar
119120
* `WS_EX_NOREDIRECTIONBITMAP` support
120121
* Theme the title bar according to Windows 10 Dark Mode setting or set a preferred theme
121122

src/platform/windows.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::Path;
55

66
use libc;
77
use winapi::shared::minwindef::WORD;
8-
use winapi::shared::windef::HWND;
8+
use winapi::shared::windef::{HMENU, HWND};
99

1010
use crate::{
1111
dpi::PhysicalSize,
@@ -112,6 +112,14 @@ pub trait WindowBuilderExtWindows {
112112
/// Sets a parent to the window to be created.
113113
fn with_parent_window(self, parent: HWND) -> WindowBuilder;
114114

115+
/// Sets a menu on the window to be created.
116+
///
117+
/// The menu must have been manually created beforehand with [`winapi::um::winuser::CreateMenu`] or similar.
118+
///
119+
/// Note: Dark mode cannot be supported for win32 menus, it's simply not possible to change how the menus look.
120+
/// If you use this, it is recommended that you combine it with `with_theme(Some(Theme::Light))` to avoid a jarring effect.
121+
fn with_menu(self, menu: HMENU) -> WindowBuilder;
122+
115123
/// This sets `ICON_BIG`. A good ceiling here is 256x256.
116124
fn with_taskbar_icon(self, taskbar_icon: Option<Icon>) -> WindowBuilder;
117125

@@ -137,6 +145,12 @@ impl WindowBuilderExtWindows for WindowBuilder {
137145
self
138146
}
139147

148+
#[inline]
149+
fn with_menu(mut self, menu: HMENU) -> WindowBuilder {
150+
self.platform_specific.menu = Some(menu);
151+
self
152+
}
153+
140154
#[inline]
141155
fn with_taskbar_icon(mut self, taskbar_icon: Option<Icon>) -> WindowBuilder {
142156
self.platform_specific.taskbar_icon = taskbar_icon;

src/platform_impl/windows/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg(target_os = "windows")]
22

3-
use winapi::{self, shared::windef::HWND};
3+
use winapi::{self, shared::windef::HMENU, shared::windef::HWND};
44

55
pub use self::{
66
event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget},
@@ -17,7 +17,9 @@ use crate::window::Theme;
1717

1818
#[derive(Clone)]
1919
pub struct PlatformSpecificWindowBuilderAttributes {
20+
// TODO: parent and menu are mutually exclusive; a child window cannot have a menu!
2021
pub parent: Option<HWND>,
22+
pub menu: Option<HMENU>,
2123
pub taskbar_icon: Option<Icon>,
2224
pub no_redirection_bitmap: bool,
2325
pub drag_and_drop: bool,
@@ -28,6 +30,7 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
2830
fn default() -> Self {
2931
Self {
3032
parent: None,
33+
menu: None,
3134
taskbar_icon: None,
3235
no_redirection_bitmap: false,
3336
drag_and_drop: true,

src/platform_impl/windows/window.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ unsafe fn init<T: 'static>(
718718
winuser::CW_USEDEFAULT,
719719
winuser::CW_USEDEFAULT,
720720
pl_attribs.parent.unwrap_or(ptr::null_mut()),
721-
ptr::null_mut(),
721+
pl_attribs.menu.unwrap_or(ptr::null_mut()),
722722
libloaderapi::GetModuleHandleW(ptr::null()),
723723
ptr::null_mut(),
724724
);

0 commit comments

Comments
 (0)