1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-06-23 15:50:05 +02:00

Begin to separate UI from internals of AudacityLogger...

... use more indirect notifications to the frame that it needs updates.

One notification to update the window when text is flushed in the logger.

Another to update the window for changes in language preferences.
This commit is contained in:
Paul Licameli 2021-07-23 11:11:35 -04:00
parent 7685d92a0c
commit 03f77429a0
2 changed files with 53 additions and 16 deletions

View File

@ -25,6 +25,7 @@ Provides thread-safe logging based on the wxWidgets log facility.
#include "ShuttleGui.h"
#include <mutex>
#include <optional>
#include <wx/filedlg.h>
#include <wx/log.h>
#include <wx/ffile.h>
@ -60,6 +61,15 @@ enum
namespace {
Destroy_ptr<wxFrame> sFrame;
wxWeakRef<wxTextCtrl> sText;
struct LogWindowUpdater : public PrefsListener
{
// PrefsListener implementation
void UpdatePrefs() override;
};
// Unique PrefsListener can't be statically constructed before the application
// object initializes, so use Optional
std::optional<LogWindowUpdater> pUpdater;
}
AudacityLogger *AudacityLogger::Get()
@ -89,11 +99,15 @@ AudacityLogger::~AudacityLogger() = default;
void AudacityLogger::Flush()
{
if (mUpdated && sFrame && sFrame->IsShown()) {
if (mUpdated && mListener && mListener())
mUpdated = false;
if (sText)
sText->ChangeValue(mBuffer);
}
}
auto AudacityLogger::SetListener(Listener listener) -> Listener
{
auto result = std::move(mListener);
mListener = std::move(listener);
return result;
}
void AudacityLogger::DoLogText(const wxString & str)
@ -245,6 +259,21 @@ void AudacityLogger::Show(bool show)
sFrame->Show();
Flush();
// Also create the listeners
if (!pUpdater)
pUpdater.emplace();
SetListener([]{
if (auto pLogger = AudacityLogger::Get()) {
if (sFrame && sFrame->IsShown()) {
if (sText)
sText->ChangeValue(pLogger->GetBuffer());
return true;
}
}
return false;
});
}
wxString AudacityLogger::GetLog(int count)
@ -315,16 +344,17 @@ void AudacityLogger::OnSave(wxCommandEvent & WXUNUSED(e))
}
}
void AudacityLogger::UpdatePrefs()
void LogWindowUpdater::UpdatePrefs()
{
//! Re-create the non-modal window in case of change of preferred language
if (sFrame) {
bool shown = sFrame->IsShown();
if (shown) {
Show(false);
AudacityLogger::Get()->Show(false);
}
sFrame.reset();
if (shown) {
Show(true);
AudacityLogger::Get()->Show(true);
}
}
}

View File

@ -11,10 +11,7 @@
#ifndef __AUDACITY_LOGGER__
#define __AUDACITY_LOGGER__
#include <functional>
#include "MemoryX.h"
#include "Prefs.h"
#include <wx/log.h> // to inherit
@ -24,8 +21,7 @@ class wxFrame;
class wxTextCtrl;
class AUDACITY_DLL_API AudacityLogger final : public wxEvtHandler,
public wxLog,
public PrefsListener
public wxLog
{
public:
@ -41,8 +37,21 @@ class AUDACITY_DLL_API AudacityLogger final : public wxEvtHandler,
wxString GetLog(int count = 0);
//! Get all the accumulated text since program start or last ClearLog()
const wxString &GetBuffer() const { return mBuffer; }
protected:
void Flush() override;
//! Type of function called by Flush
/*! @return true if flush completed
*/
using Listener = std::function< bool() >;
//! Set the unique listener, returning any previous one
Listener SetListener(Listener listener);
protected:
void DoLogText(const wxString & msg) override;
private:
@ -53,9 +62,7 @@ class AUDACITY_DLL_API AudacityLogger final : public wxEvtHandler,
void OnClear(wxCommandEvent & e);
void OnSave(wxCommandEvent & e);
// PrefsListener implementation
void UpdatePrefs() override;
Listener mListener;
wxString mBuffer;
bool mUpdated;
};