gabdube / native-windows-gui

A light windows GUI toolkit for rust
https://gabdube.github.io/native-windows-gui/
MIT License
1.96k stars 127 forks source link

Providing shortcut for menu items #259

Closed Warwolt closed 2 years ago

Warwolt commented 2 years ago

In a lot of win32 programs there's context menus that provide a displayed shortcut for a given item (e.g. Ctrl+X)

image

Is there anyway to provide such a shortcut text for native_windows_gui::MenuItem?

Edit: I think what I'm looking for is whether nwg has support for Keyboard Accelerators

gabdube commented 2 years ago

Sadly nwg doesn't support accelerators because the way they are implemented in win32 doesn't work well with how the library manages window handles. See https://docs.microsoft.com/en-us/windows/win32/learnwin32/accelerator-tables .

Basically I would either need to keep a reference to all the top level window in the windows message loop ("dispatch_thread_events" in nwg) and the only way to do that would be using a global static variable and somehow and plug it in the window creation procedure.

or I could re-implement the TranslateAccelerator function in pure rust, which sounds just as annoying as the last option.

Warwolt commented 2 years ago

Aha, actually I managed to find out what I actually needed here from this stack overflow answer:

image

The shortcut label is added by prefixing it with a tab \t character:

#[nwg_control(parent: file_menu, text: "E&xit\tAlt+F4")]
pub menu_item_exit: nwg::MenuItem,

It would be excellent if this would be added to the documentation for the nwg::MenuItem. I could open up a PR for this. Actually handling the keyboard press and triggering the correct command can be done through other means than keyboard acceleration.

Warwolt commented 2 years ago

Ok, I created a PR for this with #260

gabdube commented 2 years ago

Oh, well that's some great news! Thanks. And I will push a new release today to fix the freeze in the latest version.