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();
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++;

View File

@ -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;
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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)

View File

@ -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).

View File

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

View File

@ -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 );
}
} );

View File

@ -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 );

View File

@ -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 );
}

View File

@ -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();
}
}

View File

@ -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);

View File

@ -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);
}