Skip to content

Commit

Permalink
Extracted SetMenuShortcuts() into an NSMenu category.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekazakov committed Dec 27, 2024
1 parent 8e41455 commit cbcadc1
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,7 @@ - (void)applicationWillFinishLaunching:(NSNotification *) [[maybe_unused]] _noti
[self updateMainMenuFeaturesByVersionAndState];

// update menu with current shortcuts layout
nc::core::ActionsShortcutsManager::Instance().SetMenuShortcuts([NSApp mainMenu]);

[NSApp.mainMenu nc_setMenuItemShortcutsWithActionsShortcutsManager:nc::core::ActionsShortcutsManager::Instance()];
[self wireMenuDelegates];

if( nc::base::AmISandboxed() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,6 @@ class ActionsShortcutsManager : nc::base::ObservableBase
// If the _action doesn't exist or already has the same value, returns false.
bool SetShortcutsOverride(std::string_view _action, std::span<const Shortcut> _shortcuts);

#ifdef __OBJC__
void SetMenuShortcuts(NSMenu *_menu) const;
#endif

static std::span<const std::pair<const char *, int>> AllShortcuts();

using ObservationTicket = ObservableBase::ObservationTicket;
Expand Down Expand Up @@ -137,3 +133,13 @@ class ActionsShortcutsManager : nc::base::ObservableBase
};

} // namespace nc::core

#ifdef __OBJC__

@interface NSMenu (ActionsShortcutsManagerSupport)

- (void)nc_setMenuItemShortcutsWithActionsShortcutsManager:(const nc::core::ActionsShortcutsManager &)_asm;

@end

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -493,31 +493,6 @@ static constexpr auto make_array_n(T &&value)
return std::nullopt;
}

void ActionsShortcutsManager::SetMenuShortcuts(NSMenu *_menu) const
{
NSArray *const array = _menu.itemArray;
for( NSMenuItem *i : array ) {
if( i.submenu != nil ) {
SetMenuShortcuts(i.submenu);
continue;
}

const int tag = static_cast<int>(i.tag);
if( auto shortcut_override = m_ShortcutsOverrides.find(tag); shortcut_override != m_ShortcutsOverrides.end() ) {
const auto &shortcuts = shortcut_override->second;
[i nc_setKeyEquivalentWithShortcut:shortcuts.empty() ? Shortcut{} : shortcuts.front()];
}
else if( auto shortcut_defaults = m_ShortcutsDefaults.find(tag);
shortcut_defaults != m_ShortcutsDefaults.end() ) {
const auto &shortcuts = shortcut_defaults->second;
[i nc_setKeyEquivalentWithShortcut:shortcuts.empty() ? Shortcut{} : shortcuts.front()];
}
else if( g_TagToAction.contains(tag) ) {
[i nc_setKeyEquivalentWithShortcut:Shortcut{}];
}
}
}

void ActionsShortcutsManager::ReadOverrideFromConfig()
{
using namespace rapidjson;
Expand Down Expand Up @@ -801,3 +776,23 @@ static constexpr auto make_array_n(T &&value)
}

} // namespace nc::core

@implementation NSMenu (ActionsShortcutsManagerSupport)

- (void)nc_setMenuItemShortcutsWithActionsShortcutsManager:(const nc::core::ActionsShortcutsManager &)_asm
{
NSArray *const array = self.itemArray;
for( NSMenuItem *i : array ) {
if( i.submenu != nil ) {
[i.submenu nc_setMenuItemShortcutsWithActionsShortcutsManager:_asm];
continue;
}

const int tag = static_cast<int>(i.tag);
if( const auto shortcuts = _asm.ShortcutsFromTag(tag) ) {
[i nc_setKeyEquivalentWithShortcut:shortcuts->empty() ? nc::utility::ActionShortcut{} : shortcuts->front()];
}
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ - (IBAction)onHKChanged:(id)sender
}

if( am.SetShortcutsOverride(*action, updated_shortcuts) ) {
am.SetMenuShortcuts(NSApp.mainMenu);
[NSApp.mainMenu nc_setMenuItemShortcutsWithActionsShortcutsManager:am];
}

// Rebuild everything just in case the shortcut was a duplicate that ASM filtered out
Expand All @@ -463,7 +463,7 @@ - (IBAction)OnDefaults:(id) [[maybe_unused]] sender
[[alert.buttons objectAtIndex:0] setKeyEquivalent:@""];
if( [alert runModal] == NSAlertFirstButtonReturn ) {
ActionsShortcutsManager::Instance().RevertToDefaults();
ActionsShortcutsManager::Instance().SetMenuShortcuts(NSApp.mainMenu);
[NSApp.mainMenu nc_setMenuItemShortcutsWithActionsShortcutsManager:ActionsShortcutsManager::Instance()];
[self rebuildAll];
}
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Utility/include/Utility/ActionShortcut.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct std::hash<nc::utility::ActionShortcut> {
};

#ifdef __OBJC__
@interface NSMenuItem (NCAdditions)
@interface NSMenuItem (ActionShortcutSupport)

- (void)nc_setKeyEquivalentWithShortcut:(nc::utility::ActionShortcut)_shortcut;

Expand Down
2 changes: 1 addition & 1 deletion Source/Utility/source/ActionShortcut.mm
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@
return static_cast<size_t>(_ac.unicode) | (static_cast<size_t>(_ac.modifiers.flags) << 16);
}

@implementation NSMenuItem (NCAdditions)
@implementation NSMenuItem (ActionShortcutSupport)

- (void)nc_setKeyEquivalentWithShortcut:(nc::utility::ActionShortcut)_shortcut
{
Expand Down

0 comments on commit cbcadc1

Please sign in to comment.