Skip to content

Commit

Permalink
work
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Licameli committed Oct 25, 2023
1 parent 0b5a964 commit 2603467
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 69 deletions.
113 changes: 58 additions & 55 deletions src/prefs/KeyConfigPrefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void KeyConfigPrefs::Populate()

mCommandSelected = wxNOT_FOUND;

mManager = &CommandManager::Get( *mProject );
mEditor.mManager = &CommandManager::Get( *mProject );

// For speed, don't sort here. We're just creating.
// Instead sort when we do SetView later in this function.
Expand Down Expand Up @@ -320,12 +320,8 @@ void KeyConfigPrefs::PopulateOrExchange(ShuttleGui & S)
Layout();
}

void KeyConfigPrefs::RefreshBindings(bool bSort)
void KeyConfigEditor::RefreshBindings()
{
TranslatableStrings Labels;
TranslatableStrings Categories;
TranslatableStrings Prefixes;

mNames.clear();
mKeys.clear();
mDefaultKeys.clear();
Expand All @@ -341,22 +337,27 @@ void KeyConfigPrefs::RefreshBindings(bool bSort)

mStandardDefaultKeys = mDefaultKeys;
FilterKeys( mStandardDefaultKeys );
}

void KeyConfigPrefs::RefreshBindings(bool bSort)
{
mEditor.RefreshBindings();

mView->RefreshBindings(mNames,
Categories,
Prefixes,
Labels,
mKeys,
mView->RefreshBindings(mEditor.mNames,
mEditor.Categories,
mEditor.Prefixes,
mEditor.Labels,
mEditor.mKeys,
bSort);
//Not needed as NEW nodes are already shown expanded.
//mView->ExpandAll();

mNewKeys = mKeys;
mEditor.mNewKeys = mEditor.mKeys;
}

// RefreshKeyInfo is used to update mKeys vector only
// Introduced for efficiency purposes to avoid unnecessary usage of RefreshBinding
void KeyConfigPrefs::RefreshKeyInfo()
void KeyConfigEditor::RefreshKeyInfo()
{
mKeys.clear();

Expand All @@ -366,7 +367,7 @@ void KeyConfigPrefs::RefreshKeyInfo()

// Removes all shortcuts
// Doesn't call RefreshBindings()
void KeyConfigPrefs::ClearAllKeys()
void KeyConfigEditor::ClearAllKeys()
{
const NormalizedKeyString noKey{ NO_SHORTCUT };
for (const auto & command : mNames)
Expand All @@ -378,7 +379,7 @@ void KeyConfigPrefs::ClearAllKeys()
// with illegal key duplicates in fMatching and sMatching.
// Search for duplicates fully implemented here
// to avoid possible problems with legal shortcut duplicates.
bool KeyConfigPrefs::ContainsIllegalDups(
bool KeyConfigEditor::ContainsIllegalDups(
TranslatableString & fMatching, TranslatableString & sMatching) const
{
using IndexesArray = std::vector<int>;
Expand Down Expand Up @@ -418,7 +419,7 @@ bool KeyConfigPrefs::ContainsIllegalDups(
// 2. the added shortcut doesn't create illegal shortcut duplicate
// The names of operations for which the second condition was violated
// are returned in a single error message
TranslatableString KeyConfigPrefs::MergeWithExistingKeys(
TranslatableString KeyConfigEditor::MergeWithExistingKeys(
const std::vector<NormalizedKeyString> &toAdd)
{
TranslatableString disabledShortcuts;
Expand Down Expand Up @@ -503,37 +504,37 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))

// this RefreshKeyInfo is here to account for
// potential OnSet() function executions before importing
RefreshKeyInfo();
mEditor.RefreshKeyInfo();

// saving pre-import settings
const std::vector<NormalizedKeyString> oldKeys{ mKeys };
const std::vector<NormalizedKeyString> oldKeys{ mEditor.mKeys };

// clearing all pre-import settings
ClearAllKeys();
mEditor.ClearAllKeys();

// getting new settings
XMLFileReader reader;
if (!reader.Parse(mManager, file)) {
if (!reader.Parse(mEditor.mManager, file)) {
AudacityMessageBox(
reader.GetErrorStr(),
XO("Error Importing Keyboard Shortcuts"),
wxOK | wxCENTRE,
this);
}

RefreshKeyInfo();
mEditor.RefreshKeyInfo();

// checking new setting for duplicates
// if there are duplicates, throwing error and returning to pre-import state
TranslatableString fMatching;
TranslatableString sMatching;

if (ContainsIllegalDups(fMatching, sMatching))
if (mEditor.ContainsIllegalDups(fMatching, sMatching))
{
// restore the old pre-import hotkeys stored in oldKeys
for (size_t k{ 0 }; k < mNames.size(); k++)
mManager->SetKeyFromName(mNames[k], oldKeys[k]);
mKeys = oldKeys;
for (size_t k{ 0 }; k < mEditor.mNames.size(); k++)
mEditor.mManager->SetKeyFromName(mEditor.mNames[k], oldKeys[k]);
mEditor.mKeys = oldKeys;

// output an error message
AudacityMessageBox(
Expand All @@ -548,12 +549,14 @@ void KeyConfigPrefs::OnImport(wxCommandEvent & WXUNUSED(event))
}

// adding possible old settings to the new settings and recording the conflicting ones
TranslatableString disabledShortcuts{ MergeWithExistingKeys(oldKeys) };
TranslatableString disabledShortcuts{
mEditor.MergeWithExistingKeys(oldKeys) };

RefreshBindings(true);

TranslatableString message{
XO("Loaded %d keyboard shortcuts\n").Format(mManager->GetNumberOfKeysRead()) };
XO("Loaded %d keyboard shortcuts\n")
.Format(mEditor.mManager->GetNumberOfKeysRead()) };

if (disabledShortcuts.Translation() != (""))
message += XO("\nThe following commands are not mentioned in the imported file, "
Expand Down Expand Up @@ -582,7 +585,7 @@ void KeyConfigPrefs::OnExport(wxCommandEvent & WXUNUSED(event))

GuardedCall( [&] {
XMLFileWriter prefFile{ file, XO("Error Exporting Keyboard Shortcuts") };
mManager->WriteXML(prefFile);
mEditor.mManager->WriteXML(prefFile);
prefFile.Commit();
} );
}
Expand All @@ -600,7 +603,7 @@ void KeyConfigPrefs::OnDefaults(wxCommandEvent & WXUNUSED(event))
BasicMenu::Handle( &Menu ).Popup( wxWidgetsWindowPlacement{ this } );
}

void KeyConfigPrefs::FilterKeys( std::vector<NormalizedKeyString> & arr )
void KeyConfigEditor::FilterKeys( std::vector<NormalizedKeyString> & arr )
{
const auto &MaxListOnly = CommandManager::ExcludedList();

Expand All @@ -616,12 +619,12 @@ void KeyConfigPrefs::OnImportDefaults(wxCommandEvent & event)
gPrefs->DeleteEntry(wxT("/GUI/Shortcuts/FullDefaults"));
gPrefs->Flush();

mNewKeys = mDefaultKeys;
mEditor.mNewKeys = mEditor.mDefaultKeys;
if( event.GetId() == 1 )
FilterKeys( mNewKeys );
mEditor.FilterKeys( mEditor.mNewKeys );

for (size_t i = 0; i < mNewKeys.size(); i++) {
mManager->SetKeyFromIndex(i, mNewKeys[i]);
for (size_t i = 0; i < mEditor.mNewKeys.size(); i++) {
mEditor.mManager->SetKeyFromIndex(i, mEditor.mNewKeys[i]);
}

RefreshBindings(true);
Expand Down Expand Up @@ -752,8 +755,8 @@ void KeyConfigPrefs::SetKeyForSelected(const NormalizedKeyString & key)
}

mView->SetKey(mCommandSelected, key);
mManager->SetKeyFromName(name, key);
mNewKeys[ make_iterator_range( mNames ).index( name ) ] = key;
mEditor.mManager->SetKeyFromName(name, key);
mEditor.mNewKeys[ make_iterator_range( mEditor.mNames ).index( name ) ] = key;
}


Expand All @@ -771,22 +774,22 @@ void KeyConfigPrefs::OnSet(wxCommandEvent & WXUNUSED(event))
CommandID newCommand{ mView->GetName(mCommandSelected) };
NormalizedKeyString enteredKey{ mKey->GetValue() };
NormalizedKeyString newComDefaultKey{
mManager->GetDefaultKeyFromName(newCommand) };
mEditor.mManager->GetDefaultKeyFromName(newCommand) };
CommandIDs oldCommands;

// collecting commands competing for the same shortcut
for (size_t i{ 0 }; i < mNames.size(); i++)
for (size_t i{ 0 }; i < mEditor.mNames.size(); i++)
{
if (mNewKeys[i] == enteredKey)
if (mEditor.mNewKeys[i] == enteredKey)
{
// ignore the Set button if the same shortcut is used
if (mNames[i] == newCommand)
if (mEditor.mNames[i] == newCommand)
return;

if (newComDefaultKey == EMPTY_SHORTCUT ||
mDefaultKeys[i] != newComDefaultKey)
mEditor.mDefaultKeys[i] != newComDefaultKey)
{
oldCommands.push_back(mNames[i]);
oldCommands.push_back(mEditor.mNames[i]);
}
}
}
Expand All @@ -795,18 +798,18 @@ void KeyConfigPrefs::OnSet(wxCommandEvent & WXUNUSED(event))
if (!oldCommands.empty()) {
auto newlabel = Verbatim( wxT("'%s - %s'") )
.Format(
mManager->GetCategoryFromName(newCommand),
mManager->GetPrefixedLabelFromName(newCommand) );
mEditor.mManager->GetCategoryFromName(newCommand),
mEditor.mManager->GetPrefixedLabelFromName(newCommand) );
auto oldlabel = Verbatim(wxT("'%s - %s'"))
.Format(
mManager->GetCategoryFromName(oldCommands[0]),
mManager->GetPrefixedLabelFromName(oldCommands[0]));
mEditor.mManager->GetCategoryFromName(oldCommands[0]),
mEditor.mManager->GetPrefixedLabelFromName(oldCommands[0]));

for (size_t i{ 1 }; i < oldCommands.size(); i++)
oldlabel += XO("\n\n\t and\n\n\t") +
Verbatim(wxT("'%s - %s'")).Format(
mManager->GetCategoryFromName(oldCommands[i]),
mManager->GetPrefixedLabelFromName(oldCommands[i]));
mEditor.mManager->GetCategoryFromName(oldCommands[i]),
mEditor.mManager->GetPrefixedLabelFromName(oldCommands[i]));

if (wxCANCEL == AudacityMessageBox(
XO(
Expand All @@ -826,8 +829,8 @@ void KeyConfigPrefs::OnSet(wxCommandEvent & WXUNUSED(event))
for (const auto & command : oldCommands)
{
mView->SetKeyByName(command, {});
mManager->SetKeyFromName(command, {});
mNewKeys[make_iterator_range(mNames).index(command)] = {};
mEditor.mManager->SetKeyFromName(command, {});
mEditor.mNewKeys[make_iterator_range(mEditor.mNames).index(command)] = {};
}
}

Expand Down Expand Up @@ -899,11 +902,11 @@ bool KeyConfigPrefs::Commit()
PopulateOrExchange(S);

bool bFull = gPrefs->ReadBool(wxT("/GUI/Shortcuts/FullDefaults"), false);
for (size_t i = 0; i < mNames.size(); i++) {
const auto &dkey = bFull ? mDefaultKeys[i] : mStandardDefaultKeys[i];
for (size_t i = 0; i < mEditor.mNames.size(); i++) {
const auto &dkey = bFull ? mEditor.mDefaultKeys[i] : mEditor.mStandardDefaultKeys[i];
// using GET to interpret CommandID as a config path component
auto name = wxT("/NewKeys/") + mNames[i].GET();
const auto &key = mNewKeys[i];
auto name = wxT("/NewKeys/") + mEditor.mNames[i].GET();
const auto &key = mEditor.mNewKeys[i];

if (gPrefs->HasEntry(name)) {
if (key != NormalizedKeyString{ gPrefs->ReadObject(name, key) } ) {
Expand All @@ -926,8 +929,8 @@ bool KeyConfigPrefs::Commit()
void KeyConfigPrefs::Cancel()
{
// Restore original key values
for (size_t i = 0; i < mNames.size(); i++) {
mManager->SetKeyFromIndex(i, mKeys[i]);
for (size_t i = 0; i < mEditor.mNames.size(); i++) {
mEditor.mManager->SetKeyFromIndex(i, mEditor.mKeys[i]);
}

return;
Expand Down
37 changes: 23 additions & 14 deletions src/prefs/KeyConfigPrefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,28 @@ enum ViewByType : int;

#define KEY_CONFIG_PREFS_PLUGIN_SYMBOL ComponentInterfaceSymbol{ XO("Key Config") }

struct KeyConfigEditor
{
void RefreshBindings();
void FilterKeys( std::vector<NormalizedKeyString> & arr );
void RefreshKeyInfo();
void ClearAllKeys();
bool ContainsIllegalDups(TranslatableString & fMatching,
TranslatableString & sMatching) const;
TranslatableString MergeWithExistingKeys(
const std::vector<NormalizedKeyString> &toAdd);

CommandManager *mManager;
CommandIDs mNames;
std::vector<NormalizedKeyString> mDefaultKeys; // The full set.
std::vector<NormalizedKeyString> mStandardDefaultKeys; // The reduced set.
std::vector<NormalizedKeyString> mKeys;
std::vector<NormalizedKeyString> mNewKeys; // Used for work in progress.
TranslatableStrings Labels;
TranslatableStrings Categories;
TranslatableStrings Prefixes;
};

class KeyConfigPrefs final : public PrefsPanel
{
public:
Expand All @@ -46,13 +68,6 @@ class KeyConfigPrefs final : public PrefsPanel
private:
void Populate();
void RefreshBindings(bool bSort);
void RefreshKeyInfo();
void ClearAllKeys();
bool ContainsIllegalDups(TranslatableString & fMatching,
TranslatableString & sMatching) const;
TranslatableString MergeWithExistingKeys(
const std::vector<NormalizedKeyString> &toAdd);
void FilterKeys( std::vector<NormalizedKeyString> & arr );
CommandID NameFromKey(const NormalizedKeyString & key);
void SetKeyForSelected(const NormalizedKeyString & key);

Expand Down Expand Up @@ -95,19 +110,13 @@ class KeyConfigPrefs final : public PrefsPanel

AudacityProject *mProject{};

CommandManager *mManager;
int mCommandSelected;

CommandIDs mNames;
std::vector<NormalizedKeyString> mDefaultKeys; // The full set.
std::vector<NormalizedKeyString> mStandardDefaultKeys; // The reduced set.
std::vector<NormalizedKeyString> mKeys;
std::vector<NormalizedKeyString> mNewKeys; // Used for work in progress.
KeyConfigEditor mEditor;

DECLARE_EVENT_TABLE()
};


/// A PrefsPanel::Factory that creates one KeyConfigPrefs panel.
/// This factory can be parametrized by name, which specifies a command to be
/// focused initially
Expand Down

0 comments on commit 2603467

Please sign in to comment.