From fd6de349046258d5b7e3e495e4979b09399bf567 Mon Sep 17 00:00:00 2001 From: David Erceg Date: Sat, 31 Aug 2024 18:50:10 +1000 Subject: [PATCH] Define accelerator table in code This is part of some broader work to make accelerators updatable. To do that, the accelerator text on menu items will need to be dynamically generated (rather than fixed in the .rc file). That then requires that items that have multiple accelerators have the "default" accelerator (the one shown on the menu entry) chosen in a deterministic way. That isn't going to work with the .rc file, since entries there can be rearranged incidentally, when adding or updating entries. Additionally, I'm not sure that LoadAccelerators() makes any guarantees about the order in which items appear in the loaded table anyway. Fixed ordering is important, since without it, items that have multiple accelerators could have different menu accelerator text generated in different builds. For example, a search can be initiated using either Ctrl+F or F3. Only one of those shortcuts should appear on the menu and that choice should be explicit and deterministic. By defining the accelerators in code, the "default" accelerator can be set by the order in which accelerators are defined. This is part of the work for issue #476. --- Explorer++/Explorer++/AcceleratorHelper.cpp | 2 +- Explorer++/Explorer++/AcceleratorHelper.h | 4 +- Explorer++/Explorer++/AcceleratorManager.cpp | 9 +- Explorer++/Explorer++/AcceleratorManager.h | 5 +- Explorer++/Explorer++/DefaultAccelerators.cpp | 96 +++++++++++++++++++ Explorer++/Explorer++/DefaultAccelerators.h | 10 ++ Explorer++/Explorer++/Explorer++.vcxproj | 2 + .../Explorer++/Explorer++.vcxproj.filters | 57 ++++++----- .../Explorer++/Explorer++_NoTranslation.rc | 80 ---------------- Explorer++/Explorer++/NoTranslationResource.h | 1 - Explorer++/Explorer++/WinMain.cpp | 6 +- .../TestExplorer++/AcceleratorHelperTest.cpp | 16 +++- .../TestExplorer++/AcceleratorManagerTest.cpp | 43 +++------ .../TestExplorer++/AcceleratorTestHelper.cpp | 11 +++ .../TestExplorer++/AcceleratorTestHelper.h | 9 ++ .../TestExplorer++/TestExplorer++.vcxproj | 2 + .../TestExplorer++.vcxproj.filters | 21 ++-- 17 files changed, 220 insertions(+), 154 deletions(-) create mode 100644 Explorer++/Explorer++/DefaultAccelerators.cpp create mode 100644 Explorer++/Explorer++/DefaultAccelerators.h create mode 100644 Explorer++/TestExplorer++/AcceleratorTestHelper.cpp create mode 100644 Explorer++/TestExplorer++/AcceleratorTestHelper.h diff --git a/Explorer++/Explorer++/AcceleratorHelper.cpp b/Explorer++/Explorer++/AcceleratorHelper.cpp index c129770d2..da279de27 100644 --- a/Explorer++/Explorer++/AcceleratorHelper.cpp +++ b/Explorer++/Explorer++/AcceleratorHelper.cpp @@ -160,7 +160,7 @@ std::vector TableToAcceleratorItems(HACCEL acceleratorTable) return accelerators; } -wil::unique_haccel AcceleratorItemsToTable(const std::vector &accelerators) +wil::unique_haccel AcceleratorItemsToTable(std::span accelerators) { wil::unique_haccel acceleratorTable(CreateAcceleratorTable( const_cast(accelerators.data()), static_cast(accelerators.size()))); diff --git a/Explorer++/Explorer++/AcceleratorHelper.h b/Explorer++/Explorer++/AcceleratorHelper.h index c9db91d35..68300f4d0 100644 --- a/Explorer++/Explorer++/AcceleratorHelper.h +++ b/Explorer++/Explorer++/AcceleratorHelper.h @@ -4,10 +4,12 @@ #pragma once +#include + class AcceleratorManager; void UpdateMenuAcceleratorStrings(HMENU menu, const AcceleratorManager *acceleratorManager); std::wstring BuildAcceleratorString(const ACCEL &accelerator); std::vector TableToAcceleratorItems(HACCEL acceleratorTable); -wil::unique_haccel AcceleratorItemsToTable(const std::vector &accelerators); +wil::unique_haccel AcceleratorItemsToTable(std::span accelerators); diff --git a/Explorer++/Explorer++/AcceleratorManager.cpp b/Explorer++/Explorer++/AcceleratorManager.cpp index f868236da..4e03cc509 100644 --- a/Explorer++/Explorer++/AcceleratorManager.cpp +++ b/Explorer++/Explorer++/AcceleratorManager.cpp @@ -6,10 +6,9 @@ #include "AcceleratorManager.h" #include "AcceleratorHelper.h" -AcceleratorManager::AcceleratorManager(wil::unique_haccel acceleratorTable) +AcceleratorManager::AcceleratorManager(std::span accelerators) { - m_acceleratorTable = std::move(acceleratorTable); - m_accelerators = TableToAcceleratorItems(m_acceleratorTable.get()); + SetAccelerators(accelerators); } HACCEL AcceleratorManager::GetAcceleratorTable() const @@ -22,10 +21,10 @@ const std::vector &AcceleratorManager::GetAccelerators() const return m_accelerators; } -void AcceleratorManager::SetAccelerators(const std::vector &updatedAccelerators) +void AcceleratorManager::SetAccelerators(std::span updatedAccelerators) { m_acceleratorTable = AcceleratorItemsToTable(updatedAccelerators); - m_accelerators = updatedAccelerators; + m_accelerators = { updatedAccelerators.begin(), updatedAccelerators.end() }; } std::optional AcceleratorManager::GetAcceleratorForCommand(WORD command) const diff --git a/Explorer++/Explorer++/AcceleratorManager.h b/Explorer++/Explorer++/AcceleratorManager.h index 8a67be9a3..a5c3d6f28 100644 --- a/Explorer++/Explorer++/AcceleratorManager.h +++ b/Explorer++/Explorer++/AcceleratorManager.h @@ -11,12 +11,11 @@ class AcceleratorManager { public: - // Initializes the accelerators from the provided accelerator table. - AcceleratorManager(wil::unique_haccel acceleratorTable); + AcceleratorManager(std::span accelerators); HACCEL GetAcceleratorTable() const; const std::vector &GetAccelerators() const; - void SetAccelerators(const std::vector &updatedAccelerators); + void SetAccelerators(std::span updatedAccelerators); std::optional GetAcceleratorForCommand(WORD command) const; private: diff --git a/Explorer++/Explorer++/DefaultAccelerators.cpp b/Explorer++/Explorer++/DefaultAccelerators.cpp new file mode 100644 index 000000000..f42828499 --- /dev/null +++ b/Explorer++/Explorer++/DefaultAccelerators.cpp @@ -0,0 +1,96 @@ +// Copyright (C) Explorer++ Project +// SPDX-License-Identifier: GPL-3.0-only +// See LICENSE in the top level directory + +#include "stdafx.h" +#include "DefaultAccelerators.h" +#include "AcceleratorManager.h" +#include "MainResource.h" + +namespace +{ + +// Note that the ordering of the items below has some importance. For items that have multiple +// accelerator items defined, the first accelerator that appears will be used as the "default" +// accelerator (i.e. the accelerator used in any associated menu items). +// clang-format off +constexpr ACCEL g_defaultAccelerators[] = { + { FVIRTKEY | FALT, 'D', IDA_ADDRESSBAR }, + { FVIRTKEY | FCONTROL, 'L', IDA_ADDRESSBAR }, + { FVIRTKEY, VK_F4, IDA_COMBODROPDOWN }, + { FVIRTKEY | FALT, VK_HOME, IDA_HOME }, + { FVIRTKEY | FCONTROL, '9', IDA_LASTTAB }, + { FVIRTKEY | FCONTROL, VK_NEXT, IDA_NEXTTAB }, + { FVIRTKEY | FCONTROL, VK_OEM_6, IDA_NEXTTAB }, + { FVIRTKEY | FCONTROL, VK_TAB, IDA_NEXTTAB }, + { FVIRTKEY, VK_F6, IDA_NEXTWINDOW }, + { FVIRTKEY, VK_TAB, IDA_NEXTWINDOW }, + { FVIRTKEY | FCONTROL, VK_OEM_4, IDA_PREVIOUSTAB }, + { FVIRTKEY | FCONTROL, VK_PRIOR, IDA_PREVIOUSTAB }, + { FVIRTKEY | FCONTROL | FSHIFT, VK_TAB, IDA_PREVIOUSTAB }, + { FVIRTKEY | FSHIFT, VK_F6, IDA_PREVIOUSWINDOW }, + { FVIRTKEY | FSHIFT, VK_TAB, IDA_PREVIOUSWINDOW }, + { FVIRTKEY | FCONTROL, '0', IDA_RESET_TEXT_SIZE }, + { FVIRTKEY | FCONTROL, VK_NUMPAD0, IDA_RESET_TEXT_SIZE }, + { FVIRTKEY | FCONTROL | FSHIFT, 'T', IDA_RESTORE_LAST_TAB }, + { FVIRTKEY | FCONTROL, '1', IDA_TAB1 }, + { FVIRTKEY | FCONTROL, '2', IDA_TAB2 }, + { FVIRTKEY | FCONTROL, '3', IDA_TAB3 }, + { FVIRTKEY | FCONTROL, '4', IDA_TAB4 }, + { FVIRTKEY | FCONTROL, '5', IDA_TAB5 }, + { FVIRTKEY | FCONTROL, '6', IDA_TAB6 }, + { FVIRTKEY | FCONTROL, '7', IDA_TAB7 }, + { FVIRTKEY | FCONTROL, '8', IDA_TAB8 }, + { FVIRTKEY | FCONTROL, 'E', IDA_TAB_DUPLICATETAB }, + { FVIRTKEY | FCONTROL, 'N', IDM_ACTIONS_NEWFOLDER }, + { FVIRTKEY | FCONTROL | FSHIFT, 'D', IDM_BOOKMARKS_BOOKMARK_ALL_TABS }, + { FVIRTKEY | FCONTROL, 'D', IDM_BOOKMARKS_BOOKMARKTHISTAB }, + { FVIRTKEY | FCONTROL, 'B', IDM_BOOKMARKS_MANAGEBOOKMARKS }, + { FVIRTKEY | FCONTROL | FSHIFT, 'C', IDM_EDIT_COPYTOFOLDER }, + { FVIRTKEY | FCONTROL | FSHIFT, 'M', IDM_EDIT_MOVETOFOLDER }, + { FVIRTKEY | FCONTROL | FSHIFT, 'V', IDM_EDIT_PASTESHORTCUT }, + { FVIRTKEY | FCONTROL | FSHIFT, 'N', IDM_EDIT_SELECTNONE }, + { FVIRTKEY | FCONTROL | FALT, 'D', IDM_EDIT_WILDCARDDESELECT }, + { FVIRTKEY | FCONTROL | FALT, 'S', IDM_EDIT_WILDCARDSELECTION }, + { FVIRTKEY | FCONTROL, 'W', IDM_FILE_CLOSETAB }, + { FVIRTKEY | FCONTROL, VK_F4, IDM_FILE_CLOSETAB }, + { FVIRTKEY | FCONTROL | FSHIFT, 'P', IDM_FILE_COPYFOLDERPATH }, + { FVIRTKEY | FSHIFT, VK_DELETE, IDM_FILE_DELETEPERMANENTLY }, + { FVIRTKEY | FCONTROL, 'T', IDM_FILE_NEWTAB }, + { FVIRTKEY | FALT, VK_RETURN, IDM_FILE_PROPERTIES }, + { FVIRTKEY, VK_F2, IDM_FILE_RENAME }, + { FVIRTKEY | FCONTROL, 'G', IDM_FILTER_APPLYFILTER }, + { FVIRTKEY | FCONTROL | FSHIFT, 'F', IDM_FILTER_FILTERRESULTS }, + { FVIRTKEY | FALT, VK_LEFT, IDM_GO_BACK }, + { FVIRTKEY | FALT, VK_RIGHT, IDM_GO_FORWARD }, + { FVIRTKEY | FALT, VK_UP, IDM_GO_UP }, + { FVIRTKEY, VK_F1, IDM_HELP_ONLINE_DOCUMENTATION }, + { FVIRTKEY | FCONTROL, 'F', IDM_TOOLS_SEARCH }, + { FVIRTKEY, VK_F3, IDM_TOOLS_SEARCH }, + { FVIRTKEY | FCONTROL, VK_SUBTRACT, IDM_VIEW_DECREASE_TEXT_SIZE }, + { FVIRTKEY | FCONTROL, VK_OEM_MINUS, IDM_VIEW_DECREASE_TEXT_SIZE }, + { FVIRTKEY | FCONTROL | FSHIFT, '5', IDM_VIEW_DETAILS }, + { FVIRTKEY | FCONTROL | FSHIFT, '0', IDM_VIEW_EXTRALARGEICONS }, + { FVIRTKEY | FCONTROL | FSHIFT, '2', IDM_VIEW_ICONS }, + { FVIRTKEY | FCONTROL, VK_ADD, IDM_VIEW_INCREASE_TEXT_SIZE }, + { FVIRTKEY | FCONTROL, VK_OEM_PLUS, IDM_VIEW_INCREASE_TEXT_SIZE }, + { FVIRTKEY | FCONTROL | FSHIFT, '1', IDM_VIEW_LARGEICONS }, + { FVIRTKEY | FCONTROL | FSHIFT, '4', IDM_VIEW_LIST }, + { FVIRTKEY, VK_F5, IDM_VIEW_REFRESH }, + { FVIRTKEY | FCONTROL, 'R', IDM_VIEW_REFRESH }, + { FVIRTKEY | FCONTROL, 'H', IDM_VIEW_SHOWHIDDENFILES }, + { FVIRTKEY | FCONTROL | FSHIFT, '3', IDM_VIEW_SMALLICONS }, + { FVIRTKEY | FCONTROL | FSHIFT, '8', IDM_VIEW_THUMBNAILS }, + { FVIRTKEY | FCONTROL | FSHIFT, '9', IDM_VIEW_TILES }, + { FVIRTKEY | FCONTROL | FSHIFT, 'A', IDM_WINDOW_SEARCH_TABS }, + { FVIRTKEY | FCONTROL | FSHIFT, '6', IDM_VIEW_EXTRALARGETHUMBNAILS }, + { FVIRTKEY | FCONTROL | FSHIFT, '7', IDM_VIEW_LARGETHUMBNAILS } +}; +// clang-format on + +} + +AcceleratorManager InitializeAcceleratorManager() +{ + return { g_defaultAccelerators }; +} diff --git a/Explorer++/Explorer++/DefaultAccelerators.h b/Explorer++/Explorer++/DefaultAccelerators.h new file mode 100644 index 000000000..7e8bbe5d8 --- /dev/null +++ b/Explorer++/Explorer++/DefaultAccelerators.h @@ -0,0 +1,10 @@ +// Copyright (C) Explorer++ Project +// SPDX-License-Identifier: GPL-3.0-only +// See LICENSE in the top level directory + +#pragma once + +class AcceleratorManager; + +// Returns an AcceleratorManager instance, initialized with the default set of accelerators. +AcceleratorManager InitializeAcceleratorManager(); diff --git a/Explorer++/Explorer++/Explorer++.vcxproj b/Explorer++/Explorer++/Explorer++.vcxproj index 8b292be5d..dae9b12e6 100644 --- a/Explorer++/Explorer++/Explorer++.vcxproj +++ b/Explorer++/Explorer++/Explorer++.vcxproj @@ -987,6 +987,7 @@ + @@ -1244,6 +1245,7 @@ + diff --git a/Explorer++/Explorer++/Explorer++.vcxproj.filters b/Explorer++/Explorer++/Explorer++.vcxproj.filters index 3f02b51fb..deb406069 100644 --- a/Explorer++/Explorer++/Explorer++.vcxproj.filters +++ b/Explorer++/Explorer++/Explorer++.vcxproj.filters @@ -250,12 +250,6 @@ Core - - Core - - - Core - Core @@ -646,9 +640,6 @@ Core\UI\Views - - Core - Core\UI @@ -682,6 +673,18 @@ Core + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + @@ -822,9 +825,6 @@ Plugins - - Core - Plugins @@ -903,15 +903,6 @@ Tabs - - Core - - - Core - - - Core - Resource Files @@ -1362,9 +1353,6 @@ Core\UI\Views - - Core - Core\UI @@ -1401,6 +1389,24 @@ Core + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + @@ -1557,6 +1563,9 @@ {cadaacd3-aced-4afa-8fbf-e334258b9316} + + {4a6efb22-1fee-46bd-a375-12e500fe4fda} + diff --git a/Explorer++/Explorer++/Explorer++_NoTranslation.rc b/Explorer++/Explorer++/Explorer++_NoTranslation.rc index 27bd65a29..e2804cb76 100644 --- a/Explorer++/Explorer++/Explorer++_NoTranslation.rc +++ b/Explorer++/Explorer++/Explorer++_NoTranslation.rc @@ -48,86 +48,6 @@ IDI_DISPLAYWINDOW ICON "res\\Display Window.ico" IDB_NOPREVIEWAVAILABLE BITMAP "res\\No Preview Available.bmp" -///////////////////////////////////////////////////////////////////////////// -// -// Accelerator -// - -IDR_MAINACCELERATORS ACCELERATORS -BEGIN - "D", IDA_ADDRESSBAR, VIRTKEY, ALT, NOINVERT - "L", IDA_ADDRESSBAR, VIRTKEY, CONTROL, NOINVERT - VK_F4, IDA_COMBODROPDOWN, VIRTKEY, NOINVERT - VK_HOME, IDA_HOME, VIRTKEY, ALT, NOINVERT - "9", IDA_LASTTAB, VIRTKEY, CONTROL, NOINVERT - VK_NEXT, IDA_NEXTTAB, VIRTKEY, CONTROL, NOINVERT - VK_OEM_6, IDA_NEXTTAB, VIRTKEY, CONTROL, NOINVERT - VK_TAB, IDA_NEXTTAB, VIRTKEY, CONTROL, NOINVERT - VK_F6, IDA_NEXTWINDOW, VIRTKEY, NOINVERT - VK_TAB, IDA_NEXTWINDOW, VIRTKEY, NOINVERT - VK_OEM_4, IDA_PREVIOUSTAB, VIRTKEY, CONTROL, NOINVERT - VK_PRIOR, IDA_PREVIOUSTAB, VIRTKEY, CONTROL, NOINVERT - VK_TAB, IDA_PREVIOUSTAB, VIRTKEY, SHIFT, CONTROL, NOINVERT - VK_F6, IDA_PREVIOUSWINDOW, VIRTKEY, SHIFT, NOINVERT - VK_TAB, IDA_PREVIOUSWINDOW, VIRTKEY, SHIFT, NOINVERT - "0", IDA_RESET_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - VK_NUMPAD0, IDA_RESET_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - "T", IDA_RESTORE_LAST_TAB, VIRTKEY, SHIFT, CONTROL, NOINVERT - "1", IDA_TAB1, VIRTKEY, CONTROL, NOINVERT - "2", IDA_TAB2, VIRTKEY, CONTROL, NOINVERT - "3", IDA_TAB3, VIRTKEY, CONTROL, NOINVERT - "4", IDA_TAB4, VIRTKEY, CONTROL, NOINVERT - "5", IDA_TAB5, VIRTKEY, CONTROL, NOINVERT - "6", IDA_TAB6, VIRTKEY, CONTROL, NOINVERT - "7", IDA_TAB7, VIRTKEY, CONTROL, NOINVERT - "8", IDA_TAB8, VIRTKEY, CONTROL, NOINVERT - "E", IDA_TAB_DUPLICATETAB, VIRTKEY, CONTROL, NOINVERT - "N", IDM_ACTIONS_NEWFOLDER, VIRTKEY, CONTROL, NOINVERT - "D", IDM_BOOKMARKS_BOOKMARK_ALL_TABS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "D", IDM_BOOKMARKS_BOOKMARKTHISTAB, VIRTKEY, CONTROL, NOINVERT - "B", IDM_BOOKMARKS_MANAGEBOOKMARKS, VIRTKEY, CONTROL, NOINVERT - "C", IDM_EDIT_COPYTOFOLDER, VIRTKEY, SHIFT, CONTROL, NOINVERT - "M", IDM_EDIT_MOVETOFOLDER, VIRTKEY, SHIFT, CONTROL, NOINVERT - "V", IDM_EDIT_PASTESHORTCUT, VIRTKEY, SHIFT, CONTROL, NOINVERT - "N", IDM_EDIT_SELECTNONE, VIRTKEY, SHIFT, CONTROL, NOINVERT - "D", IDM_EDIT_WILDCARDDESELECT, VIRTKEY, CONTROL, ALT, NOINVERT - "S", IDM_EDIT_WILDCARDSELECTION, VIRTKEY, CONTROL, ALT, NOINVERT - VK_F4, IDM_FILE_CLOSETAB, VIRTKEY, CONTROL, NOINVERT - "W", IDM_FILE_CLOSETAB, VIRTKEY, CONTROL, NOINVERT - "P", IDM_FILE_COPYFOLDERPATH, VIRTKEY, SHIFT, CONTROL, NOINVERT - VK_DELETE, IDM_FILE_DELETEPERMANENTLY, VIRTKEY, SHIFT, NOINVERT - "T", IDM_FILE_NEWTAB, VIRTKEY, CONTROL, NOINVERT - VK_RETURN, IDM_FILE_PROPERTIES, VIRTKEY, ALT, NOINVERT - VK_F2, IDM_FILE_RENAME, VIRTKEY, NOINVERT - "G", IDM_FILTER_APPLYFILTER, VIRTKEY, CONTROL, NOINVERT - "F", IDM_FILTER_FILTERRESULTS, VIRTKEY, SHIFT, CONTROL, NOINVERT - VK_LEFT, IDM_GO_BACK, VIRTKEY, ALT, NOINVERT - VK_RIGHT, IDM_GO_FORWARD, VIRTKEY, ALT, NOINVERT - VK_UP, IDM_GO_UP, VIRTKEY, ALT, NOINVERT - VK_F1, IDM_HELP_ONLINE_DOCUMENTATION, VIRTKEY, NOINVERT - "F", IDM_TOOLS_SEARCH, VIRTKEY, CONTROL, NOINVERT - VK_F3, IDM_TOOLS_SEARCH, VIRTKEY, NOINVERT - VK_OEM_MINUS, IDM_VIEW_DECREASE_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - VK_SUBTRACT, IDM_VIEW_DECREASE_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - "5", IDM_VIEW_DETAILS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "0", IDM_VIEW_EXTRALARGEICONS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "2", IDM_VIEW_ICONS, VIRTKEY, SHIFT, CONTROL, NOINVERT - VK_ADD, IDM_VIEW_INCREASE_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - VK_OEM_PLUS, IDM_VIEW_INCREASE_TEXT_SIZE, VIRTKEY, CONTROL, NOINVERT - "1", IDM_VIEW_LARGEICONS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "4", IDM_VIEW_LIST, VIRTKEY, SHIFT, CONTROL, NOINVERT - "R", IDM_VIEW_REFRESH, VIRTKEY, CONTROL, NOINVERT - VK_F5, IDM_VIEW_REFRESH, VIRTKEY, NOINVERT - "H", IDM_VIEW_SHOWHIDDENFILES, VIRTKEY, CONTROL, NOINVERT - "3", IDM_VIEW_SMALLICONS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "8", IDM_VIEW_THUMBNAILS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "9", IDM_VIEW_TILES, VIRTKEY, SHIFT, CONTROL, NOINVERT - "A", IDM_WINDOW_SEARCH_TABS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "6", IDM_VIEW_EXTRALARGETHUMBNAILS, VIRTKEY, SHIFT, CONTROL, NOINVERT - "7", IDM_VIEW_LARGETHUMBNAILS, VIRTKEY, SHIFT, CONTROL, NOINVERT -END - - #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // diff --git a/Explorer++/Explorer++/NoTranslationResource.h b/Explorer++/Explorer++/NoTranslationResource.h index 321a4a10d..70f830a5f 100644 --- a/Explorer++/Explorer++/NoTranslationResource.h +++ b/Explorer++/Explorer++/NoTranslationResource.h @@ -4,7 +4,6 @@ // #define IDI_MAIN 105 #define IDI_DISPLAYWINDOW 106 -#define IDR_MAINACCELERATORS 152 #define IDB_NOPREVIEWAVAILABLE 236 #define IDB_CUT_16 250 #define IDB_CUT_WINDOWS_10_16 251 diff --git a/Explorer++/Explorer++/WinMain.cpp b/Explorer++/Explorer++/WinMain.cpp index 7e3f82082..e004abc7e 100644 --- a/Explorer++/Explorer++/WinMain.cpp +++ b/Explorer++/Explorer++/WinMain.cpp @@ -12,6 +12,7 @@ #include "CommandLine.h" #include "Console.h" #include "CrashHandlerHelper.h" +#include "DefaultAccelerators.h" #include "Explorer++_internal.h" #include "MainResource.h" #include "ModelessDialogs.h" @@ -250,10 +251,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine InitializeLocale(); - wil::unique_haccel acceleratorTable( - LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_MAINACCELERATORS))); - CHECK(acceleratorTable); - AcceleratorManager acceleratorManager(std::move(acceleratorTable)); + auto acceleratorManager = InitializeAcceleratorManager(); Explorerplusplus::InitializationData initializationData; initializationData.commandLineSettings = &commandLineSettings; diff --git a/Explorer++/TestExplorer++/AcceleratorHelperTest.cpp b/Explorer++/TestExplorer++/AcceleratorHelperTest.cpp index 778005cb5..f882fb7df 100644 --- a/Explorer++/TestExplorer++/AcceleratorHelperTest.cpp +++ b/Explorer++/TestExplorer++/AcceleratorHelperTest.cpp @@ -4,9 +4,12 @@ #include "pch.h" #include "AcceleratorHelper.h" +#include "AcceleratorTestHelper.h" #include -TEST(BuildAcceleratorStringTest, BuildString) +using namespace testing; + +TEST(AcceleratorHelperTest, BuildAcceleratorString) { ACCEL accelerator = {}; accelerator.fVirt = FVIRTKEY; @@ -26,3 +29,14 @@ TEST(BuildAcceleratorStringTest, BuildString) acceleratorText = BuildAcceleratorString(accelerator); EXPECT_EQ(acceleratorText, L"Ctrl+Shift+P"); } + +TEST(AcceleratorHelperTest, AcceleratorTableConversion) +{ + WORD commandIdCounter = 1; + const ACCEL accelerators[] = { { FVIRTKEY | FCONTROL, 'A', commandIdCounter++ }, + { FVIRTKEY | FCONTROL | FSHIFT, VK_F6, commandIdCounter++ }, + { FVIRTKEY | FALT, 'K', commandIdCounter++ }, { FVIRTKEY, VK_F2, commandIdCounter++ } }; + auto table = AcceleratorItemsToTable(accelerators); + auto outputAccelerators = TableToAcceleratorItems(table.get()); + EXPECT_THAT(outputAccelerators, ElementsAreArray(accelerators)); +} diff --git a/Explorer++/TestExplorer++/AcceleratorManagerTest.cpp b/Explorer++/TestExplorer++/AcceleratorManagerTest.cpp index 97d0cd9bd..9e7c6f784 100644 --- a/Explorer++/TestExplorer++/AcceleratorManagerTest.cpp +++ b/Explorer++/TestExplorer++/AcceleratorManagerTest.cpp @@ -4,66 +4,53 @@ #include "pch.h" #include "AcceleratorManager.h" -#include "AcceleratorHelper.h" +#include "AcceleratorTestHelper.h" #include -#include #include using namespace testing; -bool operator==(const ACCEL &first, const ACCEL &second) -{ - return first.cmd == second.cmd && first.fVirt == second.fVirt && first.key == second.key; -} - class AcceleratorManagerTest : public Test { protected: - AcceleratorManagerTest() - { - AddAccelerator(FVIRTKEY, VK_DELETE); - AddAccelerator(FVIRTKEY, VK_F1); - AddAccelerator(FVIRTKEY | FCONTROL, 'C'); - - m_acceleratorManager = - std::make_unique(AcceleratorItemsToTable(m_accelerators)); - } - - void AddAccelerator(BYTE flags, WORD command) + AcceleratorManagerTest() : + m_accelerators({ { FVIRTKEY, VK_DELETE, m_commandIdCounter++ }, + { FVIRTKEY, VK_F1, m_commandIdCounter++ }, + { FVIRTKEY | FCONTROL, 'C', m_commandIdCounter++ } }), + m_acceleratorManager(m_accelerators) { - m_accelerators.push_back({ flags, command, m_commandIdCounter++ }); } WORD m_commandIdCounter = 1; std::vector m_accelerators; - std::unique_ptr m_acceleratorManager; + AcceleratorManager m_acceleratorManager; }; TEST_F(AcceleratorManagerTest, GetAccelerators) { for (const auto &accelerator : m_accelerators) { - auto retrievedAccelerator = m_acceleratorManager->GetAcceleratorForCommand(accelerator.cmd); + auto retrievedAccelerator = m_acceleratorManager.GetAcceleratorForCommand(accelerator.cmd); EXPECT_EQ(retrievedAccelerator, accelerator); } } TEST_F(AcceleratorManagerTest, GetNonExistentAccelerator) { - auto retrievedAccelerator = m_acceleratorManager->GetAcceleratorForCommand(m_commandIdCounter); + auto retrievedAccelerator = m_acceleratorManager.GetAcceleratorForCommand(m_commandIdCounter); EXPECT_EQ(retrievedAccelerator, std::nullopt); } TEST_F(AcceleratorManagerTest, SetAccelerators) { - auto originalAcceleratorTable = m_acceleratorManager->GetAcceleratorTable(); + auto originalAcceleratorTable = m_acceleratorManager.GetAcceleratorTable(); - AddAccelerator(FVIRTKEY | FCONTROL, 'X'); - AddAccelerator(FVIRTKEY | FCONTROL, 'V'); + m_accelerators.push_back({ FVIRTKEY | FCONTROL, 'X', m_commandIdCounter++ }); + m_accelerators.push_back({ FVIRTKEY | FCONTROL, 'V', m_commandIdCounter++ }); - m_acceleratorManager->SetAccelerators(m_accelerators); + m_acceleratorManager.SetAccelerators(m_accelerators); // As the accelerators were updated, the accelerator table should have been rebuilt. - EXPECT_NE(m_acceleratorManager->GetAcceleratorTable(), originalAcceleratorTable); - EXPECT_EQ(m_acceleratorManager->GetAccelerators(), m_accelerators); + EXPECT_NE(m_acceleratorManager.GetAcceleratorTable(), originalAcceleratorTable); + EXPECT_EQ(m_acceleratorManager.GetAccelerators(), m_accelerators); } diff --git a/Explorer++/TestExplorer++/AcceleratorTestHelper.cpp b/Explorer++/TestExplorer++/AcceleratorTestHelper.cpp new file mode 100644 index 000000000..819939ec2 --- /dev/null +++ b/Explorer++/TestExplorer++/AcceleratorTestHelper.cpp @@ -0,0 +1,11 @@ +// Copyright (C) Explorer++ Project +// SPDX-License-Identifier: GPL-3.0-only +// See LICENSE in the top level directory + +#include "pch.h" +#include "AcceleratorTestHelper.h" + +bool operator==(const ACCEL &first, const ACCEL &second) +{ + return first.cmd == second.cmd && first.fVirt == second.fVirt && first.key == second.key; +} diff --git a/Explorer++/TestExplorer++/AcceleratorTestHelper.h b/Explorer++/TestExplorer++/AcceleratorTestHelper.h new file mode 100644 index 000000000..54c22fcea --- /dev/null +++ b/Explorer++/TestExplorer++/AcceleratorTestHelper.h @@ -0,0 +1,9 @@ +// Copyright (C) Explorer++ Project +// SPDX-License-Identifier: GPL-3.0-only +// See LICENSE in the top level directory + +#pragma once + +#include + +bool operator==(const ACCEL &first, const ACCEL &second); diff --git a/Explorer++/TestExplorer++/TestExplorer++.vcxproj b/Explorer++/TestExplorer++/TestExplorer++.vcxproj index f8b9c27fb..120c14708 100644 --- a/Explorer++/TestExplorer++/TestExplorer++.vcxproj +++ b/Explorer++/TestExplorer++/TestExplorer++.vcxproj @@ -185,6 +185,7 @@ + @@ -295,6 +296,7 @@ + diff --git a/Explorer++/TestExplorer++/TestExplorer++.vcxproj.filters b/Explorer++/TestExplorer++/TestExplorer++.vcxproj.filters index fc8b91597..7b14acb65 100644 --- a/Explorer++/TestExplorer++/TestExplorer++.vcxproj.filters +++ b/Explorer++/TestExplorer++/TestExplorer++.vcxproj.filters @@ -182,12 +182,6 @@ Core\UI\Views - - Core - - - Core - Core\UI @@ -217,6 +211,15 @@ Core + + Core\Accelerators + + + Core\Accelerators + + + Core\Accelerators + @@ -300,6 +303,9 @@ {5c4bd6f7-12e3-4e04-a155-20a51b38b5fc} + + {8573b1f7-785d-4e8a-8646-136d0fd51cbd} + @@ -360,6 +366,9 @@ + + Core\Accelerators +