From bf0a1348c36d86464da1fbd1aea770b0b49211e5 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 22:03:58 +0100 Subject: [PATCH 1/8] Don't tie hotkeys to only the main window by default An optional boolean `primary_only` has been added to `link_action_shortcut` when it is necessary to seperate primary and secondary window hotkeys. Currently this is only used for the fullscreen binding, as a different prodedure is used for each window. --- src/lime_qt/main.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index 5d54c00e9..1ff4d244e 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Citra Emulator Project +// Copyright Citra Emulator Project / Lime3DS Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. @@ -618,12 +618,14 @@ void GMainWindow::InitializeHotkeys() { // TODO: This code kind of sucks const QString swap_screens = QStringLiteral("Swap Screens"); const QString rotate_screens = QStringLiteral("Rotate Screens Upright"); - const auto link_action_shortcut = [&](QAction* action, const QString& action_name) { + const auto link_action_shortcut = [&](QAction* action, const QString& action_name, + const bool primary_only = false) { static const QString main_window = QStringLiteral("Main Window"); action->setShortcut(hotkey_registry.GetKeySequence(main_window, action_name)); - action->setShortcutContext(hotkey_registry.GetShortcutContext(main_window, action_name)); action->setAutoRepeat(false); this->addAction(action); + if (!primary_only) + secondary_window->addAction(action); }; link_action_shortcut(ui->action_Load_File, QStringLiteral("Load File")); @@ -635,7 +637,7 @@ void GMainWindow::InitializeHotkeys() { // TODO: This code kind of sucks link_action_shortcut(ui->action_Stop, QStringLiteral("Stop Emulation")); link_action_shortcut(ui->action_Show_Filter_Bar, QStringLiteral("Toggle Filter Bar")); link_action_shortcut(ui->action_Show_Status_Bar, QStringLiteral("Toggle Status Bar")); - link_action_shortcut(ui->action_Fullscreen, fullscreen); + link_action_shortcut(ui->action_Fullscreen, fullscreen, true); link_action_shortcut(ui->action_Capture_Screenshot, QStringLiteral("Capture Screenshot")); link_action_shortcut(ui->action_Screen_Layout_Swap_Screens, swap_screens); link_action_shortcut(ui->action_Screen_Layout_Upright_Screens, rotate_screens); @@ -666,19 +668,6 @@ void GMainWindow::InitializeHotkeys() { // TODO: This code kind of sucks add_secondary_window_hotkey(action_secondary_fullscreen, fullscreen_hotkey, SLOT(ToggleSecondaryFullscreen())); - const auto toggle_screen_hotkey = - hotkey_registry.GetKeySequence(main_window, toggle_screen_layout); - add_secondary_window_hotkey(action_secondary_toggle_screen, toggle_screen_hotkey, - SLOT(ToggleScreenLayout())); - - const auto swap_screen_hotkey = hotkey_registry.GetKeySequence(main_window, swap_screens); - add_secondary_window_hotkey(action_secondary_swap_screen, swap_screen_hotkey, - SLOT(TriggerSwapScreens())); - - const auto rotate_screen_hotkey = hotkey_registry.GetKeySequence(main_window, rotate_screens); - add_secondary_window_hotkey(action_secondary_rotate_screen, rotate_screen_hotkey, - SLOT(TriggerRotateScreens())); - const auto connect_shortcut = [&](const QString& action_name, const auto& function) { const auto* hotkey = hotkey_registry.GetHotkey(main_window, action_name, this); connect(hotkey, &QShortcut::activated, this, function); From 303c67d3c82b2b69ad4db4a5699fd6475ec31c0d Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 23:43:32 +0100 Subject: [PATCH 2/8] Fixed `HotkeyRegistry::GetHotkey` locking shortcuts to the first widget that calls it --- src/lime_qt/hotkeys.cpp | 15 +++++++++------ src/lime_qt/hotkeys.h | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lime_qt/hotkeys.cpp b/src/lime_qt/hotkeys.cpp index 32a3ae95a..d81d52e54 100644 --- a/src/lime_qt/hotkeys.cpp +++ b/src/lime_qt/hotkeys.cpp @@ -33,21 +33,24 @@ void HotkeyRegistry::LoadHotkeys() { QKeySequence::fromString(shortcut.shortcut.keyseq, QKeySequence::NativeText); hk.context = static_cast(shortcut.shortcut.context); } - if (hk.shortcut) { - hk.shortcut->disconnect(); - hk.shortcut->setKey(hk.keyseq); + for(auto const& [_, hotkey_shortcut] : hk.shortcuts) { + if (hotkey_shortcut) { + hotkey_shortcut->disconnect(); + hotkey_shortcut->setKey(hk.keyseq); + } } } } QShortcut* HotkeyRegistry::GetHotkey(const QString& group, const QString& action, QObject* widget) { Hotkey& hk = hotkey_groups[group][action]; + QShortcut* shortcut = hk.shortcuts[widget->objectName()]; - if (!hk.shortcut) { - hk.shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context); + if (!shortcut) { + shortcut = new QShortcut(hk.keyseq, widget, nullptr, nullptr, hk.context); } - return hk.shortcut; + return shortcut; } QKeySequence HotkeyRegistry::GetKeySequence(const QString& group, const QString& action) { diff --git a/src/lime_qt/hotkeys.h b/src/lime_qt/hotkeys.h index 1c10b04a7..639d5db51 100644 --- a/src/lime_qt/hotkeys.h +++ b/src/lime_qt/hotkeys.h @@ -71,7 +71,7 @@ private: struct Hotkey { QKeySequence keyseq; QString controller_keyseq; - QShortcut* shortcut = nullptr; + std::map shortcuts; Qt::ShortcutContext context = Qt::WindowShortcut; }; From fc8ef10853b44451f030fd23c530994975adeed4 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 23:44:08 +0100 Subject: [PATCH 3/8] main.cpp: `connect_shortcut` now also connects to the secondary window --- src/lime_qt/main.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index 1ff4d244e..3bd9f93e9 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -609,7 +609,7 @@ void GMainWindow::InitializeSaveStateMenuActions() { UpdateSaveStates(); } -void GMainWindow::InitializeHotkeys() { // TODO: This code kind of sucks +void GMainWindow::InitializeHotkeys() { hotkey_registry.LoadHotkeys(); const QString main_window = QStringLiteral("Main Window"); @@ -670,7 +670,9 @@ void GMainWindow::InitializeHotkeys() { // TODO: This code kind of sucks const auto connect_shortcut = [&](const QString& action_name, const auto& function) { const auto* hotkey = hotkey_registry.GetHotkey(main_window, action_name, this); + const auto* secondary_hotkey = hotkey_registry.GetHotkey(main_window, action_name, secondary_window); connect(hotkey, &QShortcut::activated, this, function); + connect(secondary_hotkey, &QShortcut::activated, this, function); }; connect(hotkey_registry.GetHotkey(main_window, toggle_screen_layout, render_window), From e5cb0d559bf926e99a18044754877d6b43b8f7b4 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 23:47:23 +0100 Subject: [PATCH 4/8] main.cpp: Reorganized `GMainWindow::InitializeHotkeys` --- src/lime_qt/main.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index 3bd9f93e9..1e978cc0a 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -618,6 +618,7 @@ void GMainWindow::InitializeHotkeys() { const QString swap_screens = QStringLiteral("Swap Screens"); const QString rotate_screens = QStringLiteral("Rotate Screens Upright"); + // QAction Hotkeys const auto link_action_shortcut = [&](QAction* action, const QString& action_name, const bool primary_only = false) { static const QString main_window = QStringLiteral("Main Window"); @@ -654,20 +655,7 @@ void GMainWindow::InitializeHotkeys() { link_action_shortcut(ui->action_Show_Room, QStringLiteral("Multiplayer Show Current Room")); link_action_shortcut(ui->action_Leave_Room, QStringLiteral("Multiplayer Leave Room")); - const auto add_secondary_window_hotkey = [this](QAction* action, QKeySequence hotkey, - const char* slot) { - // This action will fire specifically when secondary_window is in focus - action->setShortcut(hotkey); - disconnect(action, SIGNAL(triggered()), this, slot); - connect(action, SIGNAL(triggered()), this, slot); - secondary_window->addAction(action); - }; - - // Use the same fullscreen hotkey as the main window - const auto fullscreen_hotkey = hotkey_registry.GetKeySequence(main_window, fullscreen); - add_secondary_window_hotkey(action_secondary_fullscreen, fullscreen_hotkey, - SLOT(ToggleSecondaryFullscreen())); - + // QShortcut Hotkeys const auto connect_shortcut = [&](const QString& action_name, const auto& function) { const auto* hotkey = hotkey_registry.GetHotkey(main_window, action_name, this); const auto* secondary_hotkey = hotkey_registry.GetHotkey(main_window, action_name, secondary_window); @@ -748,6 +736,21 @@ void GMainWindow::InitializeHotkeys() { UpdateStatusBar(); } }); + + // Secondary Window QAction Hotkeys + const auto add_secondary_window_hotkey = [this](QAction* action, QKeySequence hotkey, + const char* slot) { + // This action will fire specifically when secondary_window is in focus + action->setShortcut(hotkey); + disconnect(action, SIGNAL(triggered()), this, slot); + connect(action, SIGNAL(triggered()), this, slot); + secondary_window->addAction(action); + }; + + // Use the same fullscreen hotkey as the main window + const auto fullscreen_hotkey = hotkey_registry.GetKeySequence(main_window, fullscreen); + add_secondary_window_hotkey(action_secondary_fullscreen, fullscreen_hotkey, + SLOT(ToggleSecondaryFullscreen())); } void GMainWindow::SetDefaultUIGeometry() { From 10b9fd2ecaf38a5095768802160802d7748df16e Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 23:52:56 +0100 Subject: [PATCH 5/8] main.cpp: `Toggle Screen Layout` hotkey now uses `connect_shortcut` --- src/lime_qt/main.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index 1e978cc0a..3c6110887 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -614,7 +614,6 @@ void GMainWindow::InitializeHotkeys() { const QString main_window = QStringLiteral("Main Window"); const QString fullscreen = QStringLiteral("Fullscreen"); - const QString toggle_screen_layout = QStringLiteral("Toggle Screen Layout"); const QString swap_screens = QStringLiteral("Swap Screens"); const QString rotate_screens = QStringLiteral("Rotate Screens Upright"); @@ -663,9 +662,7 @@ void GMainWindow::InitializeHotkeys() { connect(secondary_hotkey, &QShortcut::activated, this, function); }; - connect(hotkey_registry.GetHotkey(main_window, toggle_screen_layout, render_window), - &QShortcut::activated, this, &GMainWindow::ToggleScreenLayout); - + connect_shortcut(QStringLiteral("Toggle Screen Layout"), &GMainWindow::ToggleScreenLayout); connect_shortcut(QStringLiteral("Exit Fullscreen"), [&] { if (emulation_running) { ui->action_Fullscreen->setChecked(false); From a05dbcdfb02a2401f9b74e0eeeaa675b3d4aa06a Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Fri, 2 Aug 2024 23:55:53 +0100 Subject: [PATCH 6/8] main.cpp: Removed unnecessary `QString`s --- src/lime_qt/main.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index 3c6110887..ef014ac41 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -614,8 +614,6 @@ void GMainWindow::InitializeHotkeys() { const QString main_window = QStringLiteral("Main Window"); const QString fullscreen = QStringLiteral("Fullscreen"); - const QString swap_screens = QStringLiteral("Swap Screens"); - const QString rotate_screens = QStringLiteral("Rotate Screens Upright"); // QAction Hotkeys const auto link_action_shortcut = [&](QAction* action, const QString& action_name, @@ -639,8 +637,9 @@ void GMainWindow::InitializeHotkeys() { link_action_shortcut(ui->action_Show_Status_Bar, QStringLiteral("Toggle Status Bar")); link_action_shortcut(ui->action_Fullscreen, fullscreen, true); link_action_shortcut(ui->action_Capture_Screenshot, QStringLiteral("Capture Screenshot")); - link_action_shortcut(ui->action_Screen_Layout_Swap_Screens, swap_screens); - link_action_shortcut(ui->action_Screen_Layout_Upright_Screens, rotate_screens); + link_action_shortcut(ui->action_Screen_Layout_Swap_Screens, QStringLiteral("Swap Screens")); + link_action_shortcut(ui->action_Screen_Layout_Upright_Screens, + QStringLiteral("Rotate Screens Upright")); link_action_shortcut(ui->action_Enable_Frame_Advancing, QStringLiteral("Toggle Frame Advancing")); link_action_shortcut(ui->action_Advance_Frame, QStringLiteral("Advance Frame")); From aae84524eb09d39d54854ad3a0370099adc4d524 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Sat, 3 Aug 2024 00:00:33 +0100 Subject: [PATCH 7/8] Updated license headers --- src/lime_qt/hotkeys.cpp | 2 +- src/lime_qt/hotkeys.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lime_qt/hotkeys.cpp b/src/lime_qt/hotkeys.cpp index d81d52e54..4750de493 100644 --- a/src/lime_qt/hotkeys.cpp +++ b/src/lime_qt/hotkeys.cpp @@ -1,4 +1,4 @@ -// Copyright 2014 Citra Emulator Project +// Copyright Citra Emulator Project / Lime3DS Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. diff --git a/src/lime_qt/hotkeys.h b/src/lime_qt/hotkeys.h index 639d5db51..1c26b10aa 100644 --- a/src/lime_qt/hotkeys.h +++ b/src/lime_qt/hotkeys.h @@ -1,4 +1,4 @@ -// Copyright 2014 Citra Emulator Project +// Copyright Citra Emulator Project / Lime3DS Emulator Project // Licensed under GPLv2 or any later version // Refer to the license.txt file included. From ddd832104cbe2085d3ab78750df8cf2fc19214d8 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Sat, 3 Aug 2024 00:04:36 +0100 Subject: [PATCH 8/8] Corrected code formatting --- src/lime_qt/hotkeys.cpp | 2 +- src/lime_qt/main.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/lime_qt/hotkeys.cpp b/src/lime_qt/hotkeys.cpp index 4750de493..465541a9f 100644 --- a/src/lime_qt/hotkeys.cpp +++ b/src/lime_qt/hotkeys.cpp @@ -33,7 +33,7 @@ void HotkeyRegistry::LoadHotkeys() { QKeySequence::fromString(shortcut.shortcut.keyseq, QKeySequence::NativeText); hk.context = static_cast(shortcut.shortcut.context); } - for(auto const& [_, hotkey_shortcut] : hk.shortcuts) { + for (auto const& [_, hotkey_shortcut] : hk.shortcuts) { if (hotkey_shortcut) { hotkey_shortcut->disconnect(); hotkey_shortcut->setKey(hk.keyseq); diff --git a/src/lime_qt/main.cpp b/src/lime_qt/main.cpp index ef014ac41..4bd97e1dc 100644 --- a/src/lime_qt/main.cpp +++ b/src/lime_qt/main.cpp @@ -656,7 +656,8 @@ void GMainWindow::InitializeHotkeys() { // QShortcut Hotkeys const auto connect_shortcut = [&](const QString& action_name, const auto& function) { const auto* hotkey = hotkey_registry.GetHotkey(main_window, action_name, this); - const auto* secondary_hotkey = hotkey_registry.GetHotkey(main_window, action_name, secondary_window); + const auto* secondary_hotkey = + hotkey_registry.GetHotkey(main_window, action_name, secondary_window); connect(hotkey, &QShortcut::activated, this, function); connect(secondary_hotkey, &QShortcut::activated, this, function); };