1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-25 08:38:39 +02:00

MenuManager holds a back-reference to project; simplifies calls

This commit is contained in:
Paul Licameli 2019-06-08 11:11:51 -04:00
parent b7386c2db1
commit ef8c100cee
13 changed files with 46 additions and 39 deletions

View File

@ -805,7 +805,7 @@ bool MacroCommands::ApplyCommandInBatchMode( const wxString &friendlyCommand,
AudacityProject *project = GetActiveProject(); AudacityProject *project = GetActiveProject();
auto &settings = ProjectSettings::Get( *project ); auto &settings = ProjectSettings::Get( *project );
// Recalc flags and enable items that may have become enabled. // Recalc flags and enable items that may have become enabled.
MenuManager::Get(*project).UpdateMenus(*project, false); MenuManager::Get(*project).UpdateMenus(false);
// enter batch mode... // enter batch mode...
bool prevShowMode = settings.GetShowId3Dialog(); bool prevShowMode = settings.GetShowId3Dialog();
project->mBatchMode++; project->mBatchMode++;

View File

@ -67,8 +67,8 @@ MenuCreator::~MenuCreator()
} }
static const AudacityProject::AttachedObjects::RegisteredFactory key{ static const AudacityProject::AttachedObjects::RegisteredFactory key{
[]( AudacityProject&){ []( AudacityProject &project ){
return std::make_shared< MenuManager >(); } return std::make_shared< MenuManager >( project ); }
}; };
MenuManager &MenuManager::Get( AudacityProject &project ) MenuManager &MenuManager::Get( AudacityProject &project )
@ -81,9 +81,15 @@ const MenuManager &MenuManager::Get( const AudacityProject &project )
return Get( const_cast< AudacityProject & >( project ) ); return Get( const_cast< AudacityProject & >( project ) );
} }
MenuManager::MenuManager() MenuManager::MenuManager( AudacityProject &project )
: mProject{ project }
{ {
UpdatePrefs(); UpdatePrefs();
}
MenuManager::~MenuManager()
{
} }
void MenuManager::UpdatePrefs() void MenuManager::UpdatePrefs()
@ -408,9 +414,10 @@ CommandFlag MenuManager::GetFocusedFrame(AudacityProject &project)
} }
CommandFlag MenuManager::GetUpdateFlags CommandFlag MenuManager::GetUpdateFlags( bool checkActive )
(AudacityProject &project, bool checkActive)
{ {
auto &project = mProject;
// This method determines all of the flags that determine whether // This method determines all of the flags that determine whether
// certain menu items and commands should be enabled or disabled, // certain menu items and commands should be enabled or disabled,
// and returns them in a bitfield. Note that if none of the flags // 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 // checkActive is a temporary hack that should be removed as soon as we
// get multiple effect preview working // 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? //ANSWER-ME: Why UpdateMenus only does active project?
//JKC: Is this test fixing a bug when multiple projects are open? //JKC: Is this test fixing a bug when multiple projects are open?
//so that menu states work even when different in different projects? //so that menu states work even when different in different projects?
if (&project != GetActiveProject()) if (&project != GetActiveProject())
return; return;
auto flags = MenuManager::Get(project).GetUpdateFlags(project, checkActive); auto flags = GetUpdateFlags(checkActive);
auto flags2 = flags; auto flags2 = flags;
// We can enable some extra items if we have select-all-on-none. // We can enable some extra items if we have select-all-on-none.
@ -772,11 +781,11 @@ void MenuCreator::RebuildAllMenuBars()
} }
} }
bool MenuManager::ReportIfActionNotAllowed bool MenuManager::ReportIfActionNotAllowed(
( AudacityProject &project, const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask )
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 ) if( bAllowed )
return true; return true;
auto &cm = CommandManager::Get( project ); auto &cm = CommandManager::Get( project );
@ -788,14 +797,14 @@ bool MenuManager::ReportIfActionNotAllowed
/// Determines if flags for command are compatible with current state. /// Determines if flags for command are compatible with current state.
/// If not, then try some recovery action to make it so. /// If not, then try some recovery action to make it so.
/// @return whether compatible or not after any actions taken. /// @return whether compatible or not after any actions taken.
bool MenuManager::TryToMakeActionAllowed bool MenuManager::TryToMakeActionAllowed(
( AudacityProject &project, CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask )
CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask )
{ {
auto &project = mProject;
bool bAllowed; bool bAllowed;
if( !flags ) if( !flags )
flags = MenuManager::Get(project).GetUpdateFlags(project); flags = GetUpdateFlags();
bAllowed = ((flags & mask) == (flagsRqd & mask)); bAllowed = ((flags & mask) == (flagsRqd & mask));
if( bAllowed ) if( bAllowed )
@ -808,7 +817,7 @@ bool MenuManager::TryToMakeActionAllowed
if( mStopIfWasPaused && (MissingFlags & AudioIONotBusyFlag ) ){ if( mStopIfWasPaused && (MissingFlags & AudioIONotBusyFlag ) ){
TransportActions::StopIfPaused( project ); TransportActions::StopIfPaused( project );
// Hope this will now reflect stopped audio. // Hope this will now reflect stopped audio.
flags = MenuManager::Get(project).GetUpdateFlags(project); flags = GetUpdateFlags();
bAllowed = ((flags & mask) == (flagsRqd & mask)); bAllowed = ((flags & mask) == (flagsRqd & mask));
if( bAllowed ) if( bAllowed )
return true; return true;
@ -840,7 +849,7 @@ bool MenuManager::TryToMakeActionAllowed
// When autoselect triggers, it might not select all audio in all tracks. // When autoselect triggers, it might not select all audio in all tracks.
// So changed to DoSelectAllAudio. // So changed to DoSelectAllAudio.
SelectActions::DoSelectAllAudio(project); SelectActions::DoSelectAllAudio(project);
flags = MenuManager::Get(project).GetUpdateFlags(project); flags = GetUpdateFlags();
bAllowed = ((flags & mask) == (flagsRqd & mask)); bAllowed = ((flags & mask) == (flagsRqd & mask));
return bAllowed; return bAllowed;
} }

View File

@ -61,7 +61,8 @@ public:
static MenuManager &Get( AudacityProject &project ); static MenuManager &Get( AudacityProject &project );
static const MenuManager &Get( const AudacityProject &project ); static const MenuManager &Get( const AudacityProject &project );
MenuManager(); MenuManager( AudacityProject &project );
~MenuManager();
static void ModifyUndoMenuItems(AudacityProject &project); static void ModifyUndoMenuItems(AudacityProject &project);
static void ModifyToolbarMenus(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 // checkActive is a temporary hack that should be removed as soon as we
// get multiple effect preview working // 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 // If checkActive, do not do complete flags testing on an
// inactive project as it is needlessly expensive. // inactive project as it is needlessly expensive.
CommandFlag GetUpdateFlags( CommandFlag GetUpdateFlags( bool checkActive = false );
AudacityProject &project, bool checkActive = false);
void UpdatePrefs() override; void UpdatePrefs() override;
// Command Handling // Command Handling
bool ReportIfActionNotAllowed( bool ReportIfActionNotAllowed(
AudacityProject &project,
const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd, const wxString & Name, CommandFlag & flags, CommandFlag flagsRqd,
CommandFlag mask ); CommandFlag mask );
bool TryToMakeActionAllowed( bool TryToMakeActionAllowed(
AudacityProject &project,
CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask ); CommandFlag & flags, CommandFlag flagsRqd, CommandFlag mask );
private: private:
CommandFlag GetFocusedFrame(AudacityProject &project); CommandFlag GetFocusedFrame(AudacityProject &project);
AudacityProject &mProject;
// 0 is grey out, 1 is Autoselect, 2 is Give warnings. // 0 is grey out, 1 is Autoselect, 2 is Give warnings.
int mWhatIfNoSelection; int mWhatIfNoSelection;
bool mStopIfWasPaused; bool mStopIfWasPaused;

View File

@ -64,7 +64,7 @@ void ProjectHistory::InitialState()
auto &menuManager = MenuManager::Get( project ); auto &menuManager = MenuManager::Get( project );
menuManager.ModifyUndoMenuItems( project ); menuManager.ModifyUndoMenuItems( project );
menuManager.UpdateMenus( project ); menuManager.UpdateMenus();
} }
bool ProjectHistory::UndoAvailable() bool ProjectHistory::UndoAvailable()
@ -109,7 +109,7 @@ void ProjectHistory::PushState(const wxString &desc,
auto &menuManager = MenuManager::Get( project ); auto &menuManager = MenuManager::Get( project );
menuManager.ModifyUndoMenuItems( project ); menuManager.ModifyUndoMenuItems( project );
menuManager.UpdateMenus( project ); menuManager.UpdateMenus();
if (settings.GetTracksFitVerticallyZoomed()) if (settings.GetTracksFitVerticallyZoomed())
ViewActions::DoZoomFitV( project ); ViewActions::DoZoomFitV( project );
@ -194,7 +194,7 @@ void ProjectHistory::PopState(const UndoState &state)
window.HandleResize(); window.HandleResize();
MenuManager::Get( project ).UpdateMenus( project ); MenuManager::Get( project ).UpdateMenus();
projectFileIO.AutoSave(); projectFileIO.AutoSave();
} }

View File

@ -1277,7 +1277,7 @@ void ProjectWindow::FixScrollbars()
trackPanel.Refresh(false); trackPanel.Refresh(false);
} }
MenuManager::Get( project ).UpdateMenus( project ); MenuManager::Get( project ).UpdateMenus();
if (oldhstate != newhstate || oldvstate != newvstate) { if (oldhstate != newhstate || oldvstate != newvstate) {
UpdateLayout(); UpdateLayout();
@ -1498,7 +1498,7 @@ void ProjectWindow::OnMenu(wxCommandEvent & event)
auto &project = mProject; auto &project = mProject;
auto &commandManager = CommandManager::Get( project ); auto &commandManager = CommandManager::Get( project );
bool handled = commandManager.HandleMenuID( bool handled = commandManager.HandleMenuID(
event.GetId(), MenuManager::Get( project ).GetUpdateFlags( project ), event.GetId(), MenuManager::Get( project ).GetUpdateFlags(),
NoFlagsSpecified); NoFlagsSpecified);
if (handled) if (handled)
@ -1512,7 +1512,7 @@ void ProjectWindow::OnMenu(wxCommandEvent & event)
void ProjectWindow::OnUpdateUI(wxUpdateUIEvent & WXUNUSED(event)) void ProjectWindow::OnUpdateUI(wxUpdateUIEvent & WXUNUSED(event))
{ {
auto &project = mProject; auto &project = mProject;
MenuManager::Get( project ).UpdateMenus( project ); MenuManager::Get( project ).UpdateMenus();
} }
void ProjectWindow::MacShowUndockedToolbars(bool show) void ProjectWindow::MacShowUndockedToolbars(bool show)

View File

@ -1173,7 +1173,7 @@ bool CommandManager::FilterKeyEvent(AudacityProject *project, const wxKeyEvent &
return false; return false;
} }
auto flags = MenuManager::Get(*project).GetUpdateFlags(*project); auto flags = MenuManager::Get(*project).GetUpdateFlags();
wxKeyEvent temp = evt; wxKeyEvent temp = evt;
@ -1265,7 +1265,7 @@ bool CommandManager::HandleCommandEntry(const CommandListEntry * entry,
NiceName.Replace(".","");// remove ... NiceName.Replace(".","");// remove ...
// NB: The call may have the side effect of changing flags. // NB: The call may have the side effect of changing flags.
bool allowed = bool allowed =
MenuManager::Get(*proj).ReportIfActionNotAllowed( *proj, MenuManager::Get(*proj).ReportIfActionNotAllowed(
NiceName, flags, entry->flags, combinedMask ); NiceName, flags, entry->flags, combinedMask );
// If the function was disallowed, it STILL should count as having been // If the function was disallowed, it STILL should count as having been
// handled (by doing nothing or by telling the user of the problem). // handled (by doing nothing or by telling the user of the problem).

View File

@ -3277,7 +3277,6 @@ void EffectUIHost::OnApply(wxCommandEvent & evt)
auto flags = AlwaysEnabledFlag; auto flags = AlwaysEnabledFlag;
bool allowed = bool allowed =
MenuManager::Get(*mProject).ReportIfActionNotAllowed( MenuManager::Get(*mProject).ReportIfActionNotAllowed(
*mProject,
mEffect->GetTranslatedName(), mEffect->GetTranslatedName(),
flags, flags,
WaveTracksSelectedFlag | TimeSelectedFlag, WaveTracksSelectedFlag | TimeSelectedFlag,

View File

@ -466,7 +466,7 @@ bool DoEffect(
// For now, we're limiting realtime preview to a single effect, so // 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 // make sure the menus reflect that fact that one may have just been
// opened. // opened.
MenuManager::Get(project).UpdateMenus(project, false); MenuManager::Get(project).UpdateMenus( false );
} }
} ); } );

View File

@ -512,7 +512,7 @@ void SelectNone( AudacityProject &project )
void SelectAllIfNone( AudacityProject &project ) void SelectAllIfNone( AudacityProject &project )
{ {
auto &viewInfo = ViewInfo::Get( project ); auto &viewInfo = ViewInfo::Get( project );
auto flags = MenuManager::Get( project ).GetUpdateFlags( project ); auto flags = MenuManager::Get( project ).GetUpdateFlags();
if(!(flags & TracksSelectedFlag) || if(!(flags & TracksSelectedFlag) ||
viewInfo.selectedRegion.isPoint()) viewInfo.selectedRegion.isPoint())
DoSelectAllAudio( project ); DoSelectAllAudio( project );

View File

@ -208,7 +208,7 @@ namespace TransportActions {
// Stop playing or recording, if paused. // Stop playing or recording, if paused.
void StopIfPaused( AudacityProject &project ) void StopIfPaused( AudacityProject &project )
{ {
auto flags = MenuManager::Get( project ).GetUpdateFlags( project ); auto flags = MenuManager::Get( project ).GetUpdateFlags();
if( flags & PausedFlag ) if( flags & PausedFlag )
DoStop( project ); DoStop( project );
} }

View File

@ -46,7 +46,7 @@ void DoMacMinimize(AudacityProject *project)
#endif #endif
// So that the Minimize menu command disables // So that the Minimize menu command disables
MenuManager::Get(*project).UpdateMenus(*project); MenuManager::Get(*project).UpdateMenus();
} }
} }

View File

@ -1095,7 +1095,6 @@ bool ControlToolBar::DoRecord(AudacityProject &project,
// NB: The call may have the side effect of changing flags. // NB: The call may have the side effect of changing flags.
bool allowed = MenuManager::Get(project).TryToMakeActionAllowed( bool allowed = MenuManager::Get(project).TryToMakeActionAllowed(
project,
flags, flags,
AudioIONotBusyFlag | CanStopAudioStreamFlag, AudioIONotBusyFlag | CanStopAudioStreamFlag,
AudioIONotBusyFlag | CanStopAudioStreamFlag); AudioIONotBusyFlag | CanStopAudioStreamFlag);

View File

@ -298,7 +298,7 @@ void EditToolBar::OnButton(wxCommandEvent &event)
if (!p) return; if (!p) return;
auto &cm = CommandManager::Get( *p ); auto &cm = CommandManager::Get( *p );
auto flags = MenuManager::Get(*p).GetUpdateFlags(*p); auto flags = MenuManager::Get(*p).GetUpdateFlags();
const CommandContext context( *GetActiveProject() ); const CommandContext context( *GetActiveProject() );
cm.HandleTextualCommand(EditToolbarButtonList[id].commandName, context, flags, NoFlagsSpecified); cm.HandleTextualCommand(EditToolbarButtonList[id].commandName, context, flags, NoFlagsSpecified);
} }