From 5ee786c31ff068d588ec9cc6d7b00d1e8f88c3fb Mon Sep 17 00:00:00 2001 From: John Scipione Date: Fri, 7 Feb 2025 00:08:25 -0500 Subject: [PATCH] Interface Kit: Fix missing Alt key in menu shortcuts The shortcut detection was working, just not the display of the modifiers in the menu. I've added back the necessary code to fix this in BMenuItem. BWindow does the heavy lifting of preparing the keys and modifiers. I have changes _FindShortcut() used by BMenuItem to send the prepared modifiers back to BMenuItem. Set the parameters raw in the constructor, they will get fixed up in Install(). I also make sure to use the prepped version of the key and mods in BWindow::AddShortcut() to remove the old one. This is a minor update that eliminates an edge failure case of malformed input. Fixes #19395 a regression from hrev58589. Change-Id: I4333f89149ff843f92dbffbd53d58ffc2def6760 Reviewed-on: https://review.haiku-os.org/c/haiku/+/8943 Tested-by: Commit checker robot Reviewed-by: John Scipione --- headers/os/interface/Window.h | 2 +- src/kits/interface/MenuItem.cpp | 16 ++++++++++++---- src/kits/interface/Window.cpp | 14 ++++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/headers/os/interface/Window.h b/headers/os/interface/Window.h index 9ee1320a5f..9599f04ee0 100644 --- a/headers/os/interface/Window.h +++ b/headers/os/interface/Window.h @@ -316,7 +316,7 @@ private: BPoint AlertPosition(const BRect& frame); virtual BMessage* ConvertToMessage(void* raw, int32 code); - void _AddShortcut(uint32 key, uint32 modifiers, + void _AddShortcut(uint32* _key, uint32* _modifiers, BMenuItem* item); BHandler* _DetermineTarget(BMessage* message, BHandler* target); diff --git a/src/kits/interface/MenuItem.cpp b/src/kits/interface/MenuItem.cpp index 5556bedce2..e575784963 100644 --- a/src/kits/interface/MenuItem.cpp +++ b/src/kits/interface/MenuItem.cpp @@ -275,11 +275,13 @@ BMenuItem::SetShortcut(char shortcut, uint32 modifiers) if (fShortcutChar != 0 && fWindow != NULL) fWindow->RemoveShortcut(fShortcutChar, fModifiers); - fShortcutChar = shortcut; - fModifiers = (fShortcutChar != 0 ? modifiers : 0); + uint32 key = (uint32)fShortcutChar; if (fShortcutChar != 0 && fWindow != NULL) - fWindow->_AddShortcut(fShortcutChar, fModifiers, this); + fWindow->_AddShortcut(&key, &modifiers, this); + + fShortcutChar = (char)key; + fModifiers = (fShortcutChar != 0 ? modifiers : 0); if (fSuper != NULL) { fSuper->InvalidateLayout(); @@ -566,8 +568,14 @@ BMenuItem::Install(BWindow* window) fWindow = window; + uint32 key = (uint32)fShortcutChar; + uint32 modifiers = fModifiers; + if (fShortcutChar != 0 && fWindow != NULL) - fWindow->_AddShortcut(fShortcutChar, fModifiers, this); + fWindow->_AddShortcut(&key, &modifiers, this); + + fShortcutChar = (char)key; + fModifiers = (fShortcutChar != 0 ? modifiers : 0); if (!Messenger().IsValid()) SetTarget(fWindow); diff --git a/src/kits/interface/Window.cpp b/src/kits/interface/Window.cpp index 673ab36014..a72becb807 100644 --- a/src/kits/interface/Window.cpp +++ b/src/kits/interface/Window.cpp @@ -99,6 +99,8 @@ public: bool Matches(uint32 key, uint32 modifiers) const; + uint32 Key() const { return fKey; }; + uint32 Modifiers() const { return fModifiers; }; BMenuItem* MenuItem() const { return fMenuItem; } BMessage* Message() const { return fMessage; } BHandler* Target() const { return fTarget; } @@ -1684,14 +1686,18 @@ BWindow::PulseRate() const //! \brief Used by BMenuItem to add its shortcut to the window. void -BWindow::_AddShortcut(uint32 key, uint32 modifiers, BMenuItem* item) +BWindow::_AddShortcut(uint32* _key, uint32* _modifiers, BMenuItem* item) { - Shortcut* shortcut = new(std::nothrow) Shortcut(key, modifiers, item); + Shortcut* shortcut = new(std::nothrow) Shortcut(*_key, *_modifiers, item); if (shortcut == NULL) return; // removes the shortcut if it already exists! - RemoveShortcut(key, modifiers); + RemoveShortcut(shortcut->Key(), shortcut->Modifiers()); + + // pass the prepared key and modifiers back to caller + *_key = shortcut->Key(); + *_modifiers = shortcut->Modifiers(); fShortcuts.AddItem(shortcut); } @@ -1715,7 +1721,7 @@ BWindow::AddShortcut(uint32 key, uint32 modifiers, BMessage* message, BHandler* return; // removes the shortcut if it already exists! - RemoveShortcut(key, modifiers); + RemoveShortcut(shortcut->Key(), shortcut->Modifiers()); fShortcuts.AddItem(shortcut); }