1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-10-20 17:41:13 +02:00

NormalizedKeyString redefined as TaggedIdentifier

This commit is contained in:
Paul Licameli
2019-02-28 07:37:10 -05:00
parent 3eeb91f23a
commit 11c8377460
7 changed files with 45 additions and 54 deletions

View File

@@ -535,7 +535,8 @@ const std::vector<NormalizedKeyString> &CommandManager::ExcludedList()
}; };
std::vector<NormalizedKeyString> result( std::vector<NormalizedKeyString> result(
strings, strings + sizeof(strings)/sizeof(*strings) ); std::begin(strings), std::end(strings)
);
std::sort( result.begin(), result.end() ); std::sort( result.begin(), result.end() );
return result; return result;
}(); }();
@@ -1053,7 +1054,7 @@ CommandListEntry *CommandManager::NewIdentifier(const CommandID & nameIn,
gPrefs->SetPath(wxT("/NewKeys")); gPrefs->SetPath(wxT("/NewKeys"));
if (gPrefs->HasEntry(entry->name)) { if (gPrefs->HasEntry(entry->name)) {
entry->key = entry->key =
NormalizedKeyString{ gPrefs->Read(entry->name, entry->key.Raw()) }; NormalizedKeyString{ gPrefs->ReadObject(entry->name, entry->key) };
} }
gPrefs->SetPath(wxT("/")); gPrefs->SetPath(wxT("/"));
@@ -1098,7 +1099,8 @@ wxString CommandManager::GetLabel(const CommandListEntry *entry) const
wxString label = entry->label; wxString label = entry->label;
if (!entry->key.empty()) if (!entry->key.empty())
{ {
label += wxT("\t") + entry->key.Raw(); // using GET to compose menu item name for wxWidgets
label += wxT("\t") + entry->key.GET();
} }
return label; return label;
@@ -1120,7 +1122,8 @@ wxString CommandManager::GetLabelWithDisabledAccel(const CommandListEntry *entry
// Dummy accelerator that looks Ok in menus but is non functional. // Dummy accelerator that looks Ok in menus but is non functional.
// Note the space before the key. // Note the space before the key.
#ifdef __WXMSW__ #ifdef __WXMSW__
auto key = entry->key.Raw(); // using GET to compose menu item name for wxWidgets
auto key = entry->key.GET();
Accel = wxString("\t ") + key; Accel = wxString("\t ") + key;
if( key.StartsWith("Left" )) break; if( key.StartsWith("Left" )) break;
if( key.StartsWith("Right")) break; if( key.StartsWith("Right")) break;
@@ -1149,7 +1152,8 @@ wxString CommandManager::GetLabelWithDisabledAccel(const CommandListEntry *entry
#endif #endif
//wxLogDebug("Added Accel:[%s][%s]", entry->label, entry->key ); //wxLogDebug("Added Accel:[%s][%s]", entry->label, entry->key );
// Normal accelerator. // Normal accelerator.
Accel = wxString("\t") + entry->key.Raw(); // using GET to compose menu item name for wxWidgets
Accel = wxString("\t") + entry->key.GET();
} }
} while (false ); } while (false );
label += Accel; label += Accel;
@@ -1847,7 +1851,7 @@ void CommandManager::WriteXML(XMLWriter &xmlFile) const
xmlFile.StartTag(wxT("command")); xmlFile.StartTag(wxT("command"));
xmlFile.WriteAttr(wxT("name"), entry->name); xmlFile.WriteAttr(wxT("name"), entry->name);
xmlFile.WriteAttr(wxT("label"), label); xmlFile.WriteAttr(wxT("label"), label);
xmlFile.WriteAttr(wxT("key"), entry->key.Raw()); xmlFile.WriteAttr(wxT("key"), entry->key);
xmlFile.EndTag(wxT("command")); xmlFile.EndTag(wxT("command"));
} }
@@ -1905,7 +1909,8 @@ void CommandManager::CheckDups()
if (mCommandList[i]->key == mCommandList[j]->key) { if (mCommandList[i]->key == mCommandList[j]->key) {
wxString msg; wxString msg;
msg.Printf(wxT("key combo '%s' assigned to '%s' and '%s'"), msg.Printf(wxT("key combo '%s' assigned to '%s' and '%s'"),
mCommandList[i]->key.Raw(), // using GET to form debug message
mCommandList[i]->key.GET(),
mCommandList[i]->label.BeforeFirst(wxT('\t')), mCommandList[i]->label.BeforeFirst(wxT('\t')),
mCommandList[j]->label.BeforeFirst(wxT('\t'))); mCommandList[j]->label.BeforeFirst(wxT('\t')));
wxASSERT_MSG(mCommandList[i]->key != mCommandList[j]->key, msg); wxASSERT_MSG(mCommandList[i]->key != mCommandList[j]->key, msg);

View File

@@ -93,18 +93,6 @@ using SubMenuList = std::vector < std::unique_ptr<SubMenuListEntry> >;
// so we don't want the structures to relocate with vector operations. // so we don't want the structures to relocate with vector operations.
using CommandList = std::vector<std::unique_ptr<CommandListEntry>>; using CommandList = std::vector<std::unique_ptr<CommandListEntry>>;
namespace std
{
template<> struct hash< NormalizedKeyString > {
size_t operator () (const NormalizedKeyString &str) const // noexcept
{
auto &stdstr = str.Raw(); // no allocations, a cheap fetch
using Hasher = std::hash< wxString >;
return Hasher{}( stdstr );
}
};
}
using CommandKeyHash = std::unordered_map<NormalizedKeyString, CommandListEntry*>; using CommandKeyHash = std::unordered_map<NormalizedKeyString, CommandListEntry*>;
using CommandNameHash = std::unordered_map<wxString, CommandListEntry*>; using CommandNameHash = std::unordered_map<wxString, CommandListEntry*>;
using CommandIDHash = std::unordered_map<int, CommandListEntry*>; using CommandIDHash = std::unordered_map<int, CommandListEntry*>;

View File

@@ -14,7 +14,8 @@
#include <wx/event.h> #include <wx/event.h>
NormalizedKeyString::NormalizedKeyString(const wxString & key) NormalizedKeyString::NormalizedKeyString( const wxString & key )
: NormalizedKeyStringBase( key )
{ {
#if defined(__WXMAC__) #if defined(__WXMAC__)
wxString newkey; wxString newkey;
@@ -45,16 +46,19 @@ NormalizedKeyString::NormalizedKeyString(const wxString & key)
newkey += wxT("Ctrl+"); newkey += wxT("Ctrl+");
} }
(wxString&)*this = newkey + temp.AfterLast(wxT('+')); (NormalizedKeyStringBase&)*this =
newkey + temp.AfterLast(wxT('+'));
#else #else
(wxString&)*this = key; (NormalizedKeyStringBase&)*this = key;
#endif #endif
} }
wxString NormalizedKeyString::Display(bool usesSpecialChars) const wxString NormalizedKeyString::Display(bool usesSpecialChars) const
{ {
(void)usesSpecialChars;//compiler food (void)usesSpecialChars;//compiler food
wxString newkey = *this; // using GET to manipulate key string as needed for macOS differences
// in displaying of it
auto newkey = this->GET();
#if defined(__WXMAC__) #if defined(__WXMAC__)
if (!usesSpecialChars) { if (!usesSpecialChars) {

View File

@@ -12,38 +12,28 @@
#ifndef __AUDACITY_KEYBOARD__ #ifndef __AUDACITY_KEYBOARD__
#define __AUDACITY_KEYBOARD__ #define __AUDACITY_KEYBOARD__
#include <audacity/Types.h>
#include <wx/defs.h> #include <wx/defs.h>
#include <wx/string.h> // to inherit
class wxKeyEvent; class wxKeyEvent;
struct NormalizedKeyString : private wxString struct NormalizedKeyStringTag;
// Case insensitive comparisons
using NormalizedKeyStringBase = TaggedIdentifier<NormalizedKeyStringTag, false>;
struct NormalizedKeyString : NormalizedKeyStringBase
{ {
NormalizedKeyString() = default; NormalizedKeyString() = default;
explicit NormalizedKeyString( const wxString &key );
explicit NormalizedKeyString( const wxString &str );
wxString Display(bool usesSpecialChars = false) const; wxString Display(bool usesSpecialChars = false) const;
const wxString &Raw() const { return *this; }
bool NoCaseEqual( const NormalizedKeyString &other ) const
{ return 0 == this->Raw() .CmpNoCase( other.Raw() ); }
using wxString::empty;
}; };
inline bool operator == namespace std
( const NormalizedKeyString &a, const NormalizedKeyString &b) {
{ return a.Raw () == b.Raw(); } template<> struct hash< NormalizedKeyString >
: hash< NormalizedKeyStringBase > {};
inline bool operator != }
( const NormalizedKeyString &a, const NormalizedKeyString &b)
{ return a.Raw () != b.Raw(); }
inline bool operator <
( const NormalizedKeyString &a, const NormalizedKeyString &b)
{ return a.Raw () < b.Raw(); }
NormalizedKeyString KeyEventToKeyString(const wxKeyEvent & keyEvent); NormalizedKeyString KeyEventToKeyString(const wxKeyEvent & keyEvent);

View File

@@ -661,8 +661,8 @@ bool KeyConfigPrefs::Commit()
const auto &key = mNewKeys[i]; const auto &key = mNewKeys[i];
if (gPrefs->HasEntry(name)) { if (gPrefs->HasEntry(name)) {
if (key != NormalizedKeyString{ gPrefs->Read(name, key.Raw()) } ) { if (key != NormalizedKeyString{ gPrefs->ReadObject(name, key) } ) {
gPrefs->Write(name, key.Raw()); gPrefs->Write(name, key);
} }
if (key == dkey) { if (key == dkey) {
gPrefs->DeleteEntry(name); gPrefs->DeleteEntry(name);
@@ -670,7 +670,7 @@ bool KeyConfigPrefs::Commit()
} }
else { else {
if (key != dkey) { if (key != dkey) {
gPrefs->Write(name, key.Raw()); gPrefs->Write(name, key);
} }
} }
} }

View File

@@ -142,25 +142,29 @@ BEGIN_POPUP_MENU(TrackMenuTable)
OnMoveUpID, OnMoveUpID,
_("Move Track &Up") + wxT("\t") + _("Move Track &Up") + wxT("\t") +
(GetActiveProject()->GetCommandManager()-> (GetActiveProject()->GetCommandManager()->
GetKeyFromName(wxT("TrackMoveUp")).Raw()), // using GET to compose menu item name for wxWidgets
GetKeyFromName(wxT("TrackMoveUp")).GET()),
OnMoveTrack) OnMoveTrack)
POPUP_MENU_ITEM( POPUP_MENU_ITEM(
OnMoveDownID, OnMoveDownID,
_("Move Track &Down") + wxT("\t") + _("Move Track &Down") + wxT("\t") +
(GetActiveProject()->GetCommandManager()-> (GetActiveProject()->GetCommandManager()->
GetKeyFromName(wxT("TrackMoveDown")).Raw()), // using GET to compose menu item name for wxWidgets
GetKeyFromName(wxT("TrackMoveDown")).GET()),
OnMoveTrack) OnMoveTrack)
POPUP_MENU_ITEM( POPUP_MENU_ITEM(
OnMoveTopID, OnMoveTopID,
_("Move Track to &Top") + wxT("\t") + _("Move Track to &Top") + wxT("\t") +
(GetActiveProject()->GetCommandManager()-> (GetActiveProject()->GetCommandManager()->
GetKeyFromName(wxT("TrackMoveTop")).Raw()), // using GET to compose menu item name for wxWidgets
GetKeyFromName(wxT("TrackMoveTop")).GET()),
OnMoveTrack) OnMoveTrack)
POPUP_MENU_ITEM( POPUP_MENU_ITEM(
OnMoveBottomID, OnMoveBottomID,
_("Move Track to &Bottom") + wxT("\t") + _("Move Track to &Bottom") + wxT("\t") +
(GetActiveProject()->GetCommandManager()-> (GetActiveProject()->GetCommandManager()->
GetKeyFromName(wxT("TrackMoveBottom")).Raw()), // using GET to compose menu item name for wxWidgets
GetKeyFromName(wxT("TrackMoveBottom")).GET()),
OnMoveTrack) OnMoveTrack)
END_POPUP_MENU() END_POPUP_MENU()

View File

@@ -288,7 +288,7 @@ KeyView::GetNameByKey(const NormalizedKeyString & key) const
// Search the nodes for the key // Search the nodes for the key
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
if (key.NoCaseEqual( mNodes[i].key)) if ( key == mNodes[i].key )
{ {
return mNodes[i].name; return mNodes[i].name;
} }
@@ -308,7 +308,7 @@ KeyView::GetIndexByKey(const NormalizedKeyString & key) const
// Search the nodes for the key // Search the nodes for the key
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
{ {
if (key.NoCaseEqual( mNodes[i].key)) if ( key == mNodes[i].key )
{ {
return mNodes[i].index; return mNodes[i].index;
} }