1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-07 15:49:42 +02:00

Reimplement PrefsListener without wx/app.h or wxCommandEvent...

... so that Prefs depends only on the allowed subset of wxBase
This commit is contained in:
Paul Licameli 2021-07-19 22:08:27 -04:00
parent a0ad72d967
commit a2f109de2e
7 changed files with 41 additions and 25 deletions

View File

@ -1932,8 +1932,7 @@ void AdornedRulerPanel::OnAutoScroll(wxCommandEvent&)
gPrefs->Flush();
wxTheApp->AddPendingEvent(wxCommandEvent{
EVT_PREFS_UPDATE, ViewInfo::UpdateScrollPrefsID() });
PrefsListener::Broadcast(ViewInfo::UpdateScrollPrefsID());
}

View File

@ -50,7 +50,6 @@
*//*******************************************************************/
#include "Prefs.h"
#include <wx/defs.h>
@ -61,7 +60,7 @@
#include "Internat.h"
#include "MemoryX.h"
#include <memory>
#include "BasicUI.h"
BoolSetting DefaultUpdatesCheckingFlag{
L"/Update/DefaultUpdatesChecking", true };
@ -71,20 +70,39 @@ std::unique_ptr<FileConfig> ugPrefs {};
FileConfig *gPrefs = nullptr;
int gMenusDirty = 0;
wxDEFINE_EVENT(EVT_PREFS_UPDATE, wxCommandEvent);
struct MyEvent : wxEvent
{
public:
explicit MyEvent(int id) : mId{id} {}
virtual wxEvent *Clone() const override { return new MyEvent{mId}; }
int mId;
};
wxDECLARE_EVENT(EVT_PREFS_UPDATE, MyEvent);
wxDEFINE_EVENT(EVT_PREFS_UPDATE, MyEvent);
struct PrefsListener::Impl : wxEvtHandler
{
Impl( PrefsListener &owner );
~Impl();
void OnEvent(wxCommandEvent&);
void OnEvent(wxEvent&);
PrefsListener &mOwner;
};
static wxEvtHandler hub;
void PrefsListener::Broadcast(int id)
{
BasicUI::CallAfter([id]{
MyEvent event{ id };
hub.ProcessEvent(event);
});
}
PrefsListener::Impl::Impl( PrefsListener &owner )
: mOwner{ owner }
{
wxTheApp->Bind(EVT_PREFS_UPDATE, &PrefsListener::Impl::OnEvent, this);
hub.Bind(EVT_PREFS_UPDATE, &PrefsListener::Impl::OnEvent, this);
}
PrefsListener::Impl::~Impl()
@ -104,7 +122,7 @@ void PrefsListener::UpdateSelectedPrefs( int )
{
}
void PrefsListener::Impl::OnEvent( wxCommandEvent &evt )
void PrefsListener::Impl::OnEvent( wxEvent &evt )
{
evt.Skip();
auto id = evt.GetId();

View File

@ -29,8 +29,6 @@
#ifndef __AUDACITY_PREFS__
#define __AUDACITY_PREFS__
// Increment this every time the prefs need to be reset
// the first part (before the r) indicates the version the reset took place
// the second part (after the r) indicates the number of times the prefs have been reset within the same version
@ -385,15 +383,20 @@ public:
};
// An event emitted by the application when the Preference dialog commits
// changes
wxDECLARE_EXPORTED_EVENT(AUDACITY_DLL_API,
EVT_PREFS_UPDATE, wxCommandEvent);
// Invoke UpdatePrefs() when Preference dialog commits changes.
//! A listener notified of changes in preferences
class AUDACITY_DLL_API PrefsListener
{
public:
//! Call this static function to notify all PrefsListener objects
/*!
@param id when positive, passed to UpdateSelectedPrefs() of all listeners,
meant to indicate that only a certain subset of preferences have changed;
else their UpdatePrefs() methods are called. (That is supposed to happen
when the user OK's changes in the Preferences dialog.)
Callbacks are delayed, in the main thread, using BasicUI::CallAfter
*/
static void Broadcast(int id = 0);
PrefsListener();
virtual ~PrefsListener();

View File

@ -331,8 +331,7 @@ void OnShowClipping(const CommandContext &context)
gPrefs->Flush();
commandManager.Check(wxT("ShowClipping"), checked);
wxTheApp->AddPendingEvent(wxCommandEvent{
EVT_PREFS_UPDATE, ShowClippingPrefsID() });
PrefsListener::Broadcast(ShowClippingPrefsID());
trackPanel.Refresh(false);
}
@ -348,8 +347,7 @@ void OnShowNameOverlay(const CommandContext &context)
gPrefs->Flush();
commandManager.Check(wxT("ShowTrackNameInWaveform"), checked);
wxTheApp->AddPendingEvent(wxCommandEvent{
EVT_PREFS_UPDATE, ShowTrackNameInWaveformPrefsID() });
PrefsListener::Broadcast(ShowTrackNameInWaveformPrefsID());
trackPanel.Refresh(false);
}

View File

@ -766,7 +766,7 @@ void PrefsDialog::OnOK(wxCommandEvent & WXUNUSED(event))
// so AudacityProject::UpdatePrefs() or any of the routines it calls must
// not cause MenuCreator::RebuildMenuBar() to be executed.
wxTheApp->AddPendingEvent(wxCommandEvent{ EVT_PREFS_UPDATE });
PrefsListener::Broadcast();
if( IsModal() )
EndModal(true);

View File

@ -681,8 +681,7 @@ void DeviceToolBar::OnChoice(wxCommandEvent &event)
gAudioIO->HandleDeviceChange();
}
wxTheApp->AddPendingEvent(wxCommandEvent{
EVT_PREFS_UPDATE, DeviceToolbarPrefsID() });
PrefsListener::Broadcast(DeviceToolbarPrefsID());
}
void DeviceToolBar::ShowInputDialog()

View File

@ -2103,8 +2103,7 @@ void MeterPanel::OnPreferences(wxCommandEvent & WXUNUSED(event))
// Currently, there are 2 playback meters and 2 record meters and any number of
// mixerboard meters, so we have to send out an preferences updated message to
// ensure they all update themselves.
wxTheApp->AddPendingEvent(wxCommandEvent{
EVT_PREFS_UPDATE, MeterPrefsID() });
PrefsListener::Broadcast(MeterPrefsID());
}
}