From ef8c100ceec6976cfa6ab6a742cd86e6762bbb29 Mon Sep 17 00:00:00 2001 From: Paul Licameli Date: Sat, 8 Jun 2019 11:11:51 -0400 Subject: [PATCH] MenuManager holds a back-reference to project; simplifies calls --- src/BatchCommands.cpp | 2 +- src/Menus.cpp | 43 ++++++++++++++++++++------------- src/Menus.h | 12 ++++----- src/ProjectHistory.cpp | 6 ++--- src/ProjectWindow.cpp | 6 ++--- src/commands/CommandManager.cpp | 4 +-- src/effects/Effect.cpp | 1 - src/menus/PluginMenus.cpp | 2 +- src/menus/SelectMenus.cpp | 2 +- src/menus/TransportMenus.cpp | 2 +- src/menus/WindowMenus.cpp | 2 +- src/toolbars/ControlToolBar.cpp | 1 - src/toolbars/EditToolBar.cpp | 2 +- 13 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/BatchCommands.cpp b/src/BatchCommands.cpp index 6015422b5..5725b9d45 100644 --- a/src/BatchCommands.cpp +++ b/src/BatchCommands.cpp @@ -805,7 +805,7 @@ bool MacroCommands::ApplyCommandInBatchMode( const wxString &friendlyCommand, AudacityProject *project = GetActiveProject(); auto &settings = ProjectSettings::Get( *project ); // Recalc flags and enable items that may have become enabled. - MenuManager::Get(*project).UpdateMenus(*project, false); + MenuManager::Get(*project).UpdateMenus(false); // enter batch mode... bool prevShowMode = settings.GetShowId3Dialog(); project->mBatchMode++; diff --git a/src/Menus.cpp b/src/Menus.cpp index b235ef5b4..f2e26b7df 100644 --- a/src/Menus.cpp +++ b/src/Menus.cpp @@ -67,8 +67,8 @@ MenuCreator::~MenuCreator() } static const AudacityProject::AttachedObjects::RegisteredFactory key{ - []( AudacityProject&){ - return std::make_shared< MenuManager >(); } + []( AudacityProject &project ){ + return std::make_shared< MenuManager >( project ); } }; MenuManager &MenuManager::Get( AudacityProject &project ) @@ -81,9 +81,15 @@ const MenuManager &MenuManager::Get( const AudacityProject &project ) return Get( const_cast< AudacityProject & >( project ) ); } -MenuManager::MenuManager() +MenuManager::MenuManager( AudacityProject &project ) + : mProject{ project } { UpdatePrefs(); + +} + +MenuManager::~MenuManager() +{ } void MenuManager::UpdatePrefs() @@ -408,9 +414,10 @@ CommandFlag MenuManager::GetFocusedFrame(AudacityProject &project) } -CommandFlag MenuManager::GetUpdateFlags -(AudacityProject &project, bool checkActive) +CommandFlag MenuManager::GetUpdateFlags( bool checkActive ) { + auto &project = mProject; + // This method determines all of the flags that determine whether // certain menu items and commands should be enabled or disabled, // and returns them in a bitfield. Note that if none of the flags @@ -668,15 +675,17 @@ void MenuManager::ModifyToolbarMenus(AudacityProject &project) // checkActive is a temporary hack that should be removed as soon as we // get multiple effect preview working -void MenuManager::UpdateMenus(AudacityProject &project, bool checkActive) +void MenuManager::UpdateMenus( bool checkActive ) { + auto &project = mProject; + //ANSWER-ME: Why UpdateMenus only does active project? //JKC: Is this test fixing a bug when multiple projects are open? //so that menu states work even when different in different projects? if (&project != GetActiveProject()) return; - auto flags = MenuManager::Get(project).GetUpdateFlags(project, checkActive); + auto flags = GetUpdateFlags(checkActive); auto flags2 = flags; // We can enable some extra items if we have select-all-on-none. @@ -772,11 +781,11 @@ void MenuCreator::RebuildAllMenuBars() } } -bool MenuManager::ReportIfActionNotAllowed -( AudacityProject &project, - const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ) +bool MenuManager::ReportIfActionNotAllowed( + const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ) { - bool bAllowed = TryToMakeActionAllowed( project, flags, flagsRqd, mask ); + auto &project = mProject; + bool bAllowed = TryToMakeActionAllowed( flags, flagsRqd, mask ); if( bAllowed ) return true; auto &cm = CommandManager::Get( project ); @@ -788,14 +797,14 @@ bool MenuManager::ReportIfActionNotAllowed /// Determines if flags for command are compatible with current state. /// If not, then try some recovery action to make it so. /// @return whether compatible or not after any actions taken. -bool MenuManager::TryToMakeActionAllowed -( AudacityProject &project, - CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ) +bool MenuManager::TryToMakeActionAllowed( + CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ) { + auto &project = mProject; bool bAllowed; if( !flags ) - flags = MenuManager::Get(project).GetUpdateFlags(project); + flags = GetUpdateFlags(); bAllowed = ((flags & mask) == (flagsRqd & mask)); if( bAllowed ) @@ -808,7 +817,7 @@ bool MenuManager::TryToMakeActionAllowed if( mStopIfWasPaused && (MissingFlags & AudioIONotBusyFlag ) ){ TransportActions::StopIfPaused( project ); // Hope this will now reflect stopped audio. - flags = MenuManager::Get(project).GetUpdateFlags(project); + flags = GetUpdateFlags(); bAllowed = ((flags & mask) == (flagsRqd & mask)); if( bAllowed ) return true; @@ -840,7 +849,7 @@ bool MenuManager::TryToMakeActionAllowed // When autoselect triggers, it might not select all audio in all tracks. // So changed to DoSelectAllAudio. SelectActions::DoSelectAllAudio(project); - flags = MenuManager::Get(project).GetUpdateFlags(project); + flags = GetUpdateFlags(); bAllowed = ((flags & mask) == (flagsRqd & mask)); return bAllowed; } diff --git a/src/Menus.h b/src/Menus.h index 53e4d9326..a7081f575 100644 --- a/src/Menus.h +++ b/src/Menus.h @@ -61,7 +61,8 @@ public: static MenuManager &Get( AudacityProject &project ); static const MenuManager &Get( const AudacityProject &project ); - MenuManager(); + MenuManager( AudacityProject &project ); + ~MenuManager(); static void ModifyUndoMenuItems(AudacityProject &project); static void ModifyToolbarMenus(AudacityProject &project); @@ -70,27 +71,26 @@ public: // checkActive is a temporary hack that should be removed as soon as we // get multiple effect preview working - void UpdateMenus(AudacityProject &project, bool checkActive = true); + void UpdateMenus( bool checkActive = true ); // If checkActive, do not do complete flags testing on an // inactive project as it is needlessly expensive. - CommandFlag GetUpdateFlags( - AudacityProject &project, bool checkActive = false); + CommandFlag GetUpdateFlags( bool checkActive = false ); void UpdatePrefs() override; // Command Handling bool ReportIfActionNotAllowed( - AudacityProject &project, const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ); bool TryToMakeActionAllowed( - AudacityProject &project, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ); private: CommandFlag GetFocusedFrame(AudacityProject &project); + AudacityProject &mProject; + // 0 is grey out, 1 is Autoselect, 2 is Give warnings. int mWhatIfNoSelection; bool mStopIfWasPaused; diff --git a/src/ProjectHistory.cpp b/src/ProjectHistory.cpp index f3a299eac..5dff9db3a 100644 --- a/src/ProjectHistory.cpp +++ b/src/ProjectHistory.cpp @@ -64,7 +64,7 @@ void ProjectHistory::InitialState() auto &menuManager = MenuManager::Get( project ); menuManager.ModifyUndoMenuItems( project ); - menuManager.UpdateMenus( project ); + menuManager.UpdateMenus(); } bool ProjectHistory::UndoAvailable() @@ -109,7 +109,7 @@ void ProjectHistory::PushState(const wxString &desc, auto &menuManager = MenuManager::Get( project ); menuManager.ModifyUndoMenuItems( project ); - menuManager.UpdateMenus( project ); + menuManager.UpdateMenus(); if (settings.GetTracksFitVerticallyZoomed()) ViewActions::DoZoomFitV( project ); @@ -194,7 +194,7 @@ void ProjectHistory::PopState(const UndoState &state) window.HandleResize(); - MenuManager::Get( project ).UpdateMenus( project ); + MenuManager::Get( project ).UpdateMenus(); projectFileIO.AutoSave(); } diff --git a/src/ProjectWindow.cpp b/src/ProjectWindow.cpp index 1b6a597ae..aa9c19368 100644 --- a/src/ProjectWindow.cpp +++ b/src/ProjectWindow.cpp @@ -1277,7 +1277,7 @@ void ProjectWindow::FixScrollbars() trackPanel.Refresh(false); } - MenuManager::Get( project ).UpdateMenus( project ); + MenuManager::Get( project ).UpdateMenus(); if (oldhstate != newhstate || oldvstate != newvstate) { UpdateLayout(); @@ -1498,7 +1498,7 @@ void ProjectWindow::OnMenu(wxCommandEvent & event) auto &project = mProject; auto &commandManager = CommandManager::Get( project ); bool handled = commandManager.HandleMenuID( - event.GetId(), MenuManager::Get( project ).GetUpdateFlags( project ), + event.GetId(), MenuManager::Get( project ).GetUpdateFlags(), NoFlagsSpecified); if (handled) @@ -1512,7 +1512,7 @@ void ProjectWindow::OnMenu(wxCommandEvent & event) void ProjectWindow::OnUpdateUI(wxUpdateUIEvent & WXUNUSED(event)) { auto &project = mProject; - MenuManager::Get( project ).UpdateMenus( project ); + MenuManager::Get( project ).UpdateMenus(); } void ProjectWindow::MacShowUndockedToolbars(bool show) diff --git a/src/commands/CommandManager.cpp b/src/commands/CommandManager.cpp index 82f373ecb..8b17d02c9 100644 --- a/src/commands/CommandManager.cpp +++ b/src/commands/CommandManager.cpp @@ -1173,7 +1173,7 @@ bool CommandManager::FilterKeyEvent(AudacityProject *project, const wxKeyEvent & return false; } - auto flags = MenuManager::Get(*project).GetUpdateFlags(*project); + auto flags = MenuManager::Get(*project).GetUpdateFlags(); wxKeyEvent temp = evt; @@ -1265,7 +1265,7 @@ bool CommandManager::HandleCommandEntry(const CommandListEntry * entry, NiceName.Replace(".","");// remove ... // NB: The call may have the side effect of changing flags. bool allowed = - MenuManager::Get(*proj).ReportIfActionNotAllowed( *proj, + MenuManager::Get(*proj).ReportIfActionNotAllowed( NiceName, flags, entry->flags, combinedMask ); // If the function was disallowed, it STILL should count as having been // handled (by doing nothing or by telling the user of the problem). diff --git a/src/effects/Effect.cpp b/src/effects/Effect.cpp index e6f98e516..e0482a09e 100644 --- a/src/effects/Effect.cpp +++ b/src/effects/Effect.cpp @@ -3277,7 +3277,6 @@ void EffectUIHost::OnApply(wxCommandEvent & evt) auto flags = AlwaysEnabledFlag; bool allowed = MenuManager::Get(*mProject).ReportIfActionNotAllowed( - *mProject, mEffect->GetTranslatedName(), flags, WaveTracksSelectedFlag | TimeSelectedFlag, diff --git a/src/menus/PluginMenus.cpp b/src/menus/PluginMenus.cpp index 60813cea8..26383e2db 100644 --- a/src/menus/PluginMenus.cpp +++ b/src/menus/PluginMenus.cpp @@ -466,7 +466,7 @@ bool DoEffect( // For now, we're limiting realtime preview to a single effect, so // make sure the menus reflect that fact that one may have just been // opened. - MenuManager::Get(project).UpdateMenus(project, false); + MenuManager::Get(project).UpdateMenus( false ); } } ); diff --git a/src/menus/SelectMenus.cpp b/src/menus/SelectMenus.cpp index f35fbc7cd..282a8c7fe 100644 --- a/src/menus/SelectMenus.cpp +++ b/src/menus/SelectMenus.cpp @@ -512,7 +512,7 @@ void SelectNone( AudacityProject &project ) void SelectAllIfNone( AudacityProject &project ) { auto &viewInfo = ViewInfo::Get( project ); - auto flags = MenuManager::Get( project ).GetUpdateFlags( project ); + auto flags = MenuManager::Get( project ).GetUpdateFlags(); if(!(flags & TracksSelectedFlag) || viewInfo.selectedRegion.isPoint()) DoSelectAllAudio( project ); diff --git a/src/menus/TransportMenus.cpp b/src/menus/TransportMenus.cpp index a55bda429..47f423af4 100644 --- a/src/menus/TransportMenus.cpp +++ b/src/menus/TransportMenus.cpp @@ -208,7 +208,7 @@ namespace TransportActions { // Stop playing or recording, if paused. void StopIfPaused( AudacityProject &project ) { - auto flags = MenuManager::Get( project ).GetUpdateFlags( project ); + auto flags = MenuManager::Get( project ).GetUpdateFlags(); if( flags & PausedFlag ) DoStop( project ); } diff --git a/src/menus/WindowMenus.cpp b/src/menus/WindowMenus.cpp index c448eb30a..f75b82474 100644 --- a/src/menus/WindowMenus.cpp +++ b/src/menus/WindowMenus.cpp @@ -46,7 +46,7 @@ void DoMacMinimize(AudacityProject *project) #endif // So that the Minimize menu command disables - MenuManager::Get(*project).UpdateMenus(*project); + MenuManager::Get(*project).UpdateMenus(); } } diff --git a/src/toolbars/ControlToolBar.cpp b/src/toolbars/ControlToolBar.cpp index b03a1b18f..069d0bdcc 100644 --- a/src/toolbars/ControlToolBar.cpp +++ b/src/toolbars/ControlToolBar.cpp @@ -1095,7 +1095,6 @@ bool ControlToolBar::DoRecord(AudacityProject &project, // NB: The call may have the side effect of changing flags. bool allowed = MenuManager::Get(project).TryToMakeActionAllowed( - project, flags, AudioIONotBusyFlag | CanStopAudioStreamFlag, AudioIONotBusyFlag | CanStopAudioStreamFlag); diff --git a/src/toolbars/EditToolBar.cpp b/src/toolbars/EditToolBar.cpp index 85ac81260..d69225f26 100644 --- a/src/toolbars/EditToolBar.cpp +++ b/src/toolbars/EditToolBar.cpp @@ -298,7 +298,7 @@ void EditToolBar::OnButton(wxCommandEvent &event) if (!p) return; auto &cm = CommandManager::Get( *p ); - auto flags = MenuManager::Get(*p).GetUpdateFlags(*p); + auto flags = MenuManager::Get(*p).GetUpdateFlags(); const CommandContext context( *GetActiveProject() ); cm.HandleTextualCommand(EditToolbarButtonList[id].commandName, context, flags, NoFlagsSpecified); }