1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-05-01 16:19:43 +02:00

Begin to rewrite AudacityLogger...

... Singleton members become statics; smart pointers and null checks for safety
This commit is contained in:
Paul Licameli 2021-04-16 20:15:56 -04:00
parent 347cb7c294
commit 7685d92a0c
2 changed files with 28 additions and 27 deletions

View File

@ -33,6 +33,7 @@ Provides thread-safe logging based on the wxWidgets log facility.
#include <wx/settings.h>
#include <wx/textctrl.h>
#include <wx/tokenzr.h>
#include <wx/weakref.h>
#include "../images/AudacityLogoAlpha.xpm"
#include "widgets/AudacityMessageBox.h"
@ -56,6 +57,11 @@ enum
LoggerID_Close
};
namespace {
Destroy_ptr<wxFrame> sFrame;
wxWeakRef<wxTextCtrl> sText;
}
AudacityLogger *AudacityLogger::Get()
{
static std::once_flag flag;
@ -76,7 +82,6 @@ AudacityLogger::AudacityLogger()
: wxEvtHandler(),
wxLog()
{
mText = NULL;
mUpdated = false;
}
@ -84,9 +89,10 @@ AudacityLogger::~AudacityLogger() = default;
void AudacityLogger::Flush()
{
if (mUpdated && mFrame && mFrame->IsShown()) {
if (mUpdated && sFrame && sFrame->IsShown()) {
mUpdated = false;
mText->ChangeValue(mBuffer);
if (sText)
sText->ChangeValue(mBuffer);
}
}
@ -140,21 +146,21 @@ void AudacityLogger::Show(bool show)
{
// Hide the frame if created, otherwise do nothing
if (!show) {
if (mFrame) {
mFrame->Show(false);
if (sFrame) {
sFrame->Show(false);
}
return;
}
// If the frame already exists, refresh its contents and show it
if (mFrame) {
if (!mFrame->IsShown()) {
mText->ChangeValue(mBuffer);
mText->SetInsertionPointEnd();
mText->ShowPosition(mText->GetLastPosition());
if (sFrame) {
if (!sFrame->IsShown() && sText) {
sText->ChangeValue(mBuffer);
sText->SetInsertionPointEnd();
sText->ShowPosition(sText->GetLastPosition());
}
mFrame->Show();
mFrame->Raise();
sFrame->Show();
sFrame->Raise();
return;
}
@ -186,7 +192,7 @@ void AudacityLogger::Show(bool show)
{
S.StartVerticalLay(true);
{
mText = S.Style(wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY | wxTE_RICH)
sText = S.Style(wxTE_MULTILINE | wxHSCROLL | wxTE_READONLY | wxTE_RICH)
.AddTextWindow(mBuffer);
S.AddSpace(0, 5);
@ -234,9 +240,9 @@ void AudacityLogger::Show(bool show)
&AudacityLogger::OnClose,
this, LoggerID_Close);
mFrame = std::move( frame );
sFrame = std::move( frame );
mFrame->Show();
sFrame->Show();
Flush();
}
@ -265,7 +271,7 @@ void AudacityLogger::OnCloseWindow(wxCloseEvent & WXUNUSED(e))
// On the Mac, destroy the window rather than hiding it since the
// log menu will override the root windows menu if there is no
// project window open.
mFrame.reset();
sFrame.reset();
#else
Show(false);
#endif
@ -293,30 +299,30 @@ void AudacityLogger::OnSave(wxCommandEvent & WXUNUSED(e))
wxT("txt"),
{ FileNames::TextFiles },
wxFD_SAVE | wxFD_OVERWRITE_PROMPT | wxRESIZE_BORDER,
mFrame.get());
sFrame.get());
if (fName.empty()) {
return;
}
if (!mText->SaveFile(fName)) {
if (!(sText && sText->SaveFile(fName))) {
AudacityMessageBox(
XO("Couldn't save log to file: %s").Format( fName ),
XO("Warning"),
wxICON_EXCLAMATION,
mFrame.get());
sFrame.get());
return;
}
}
void AudacityLogger::UpdatePrefs()
{
if (mFrame) {
bool shown = mFrame->IsShown();
if (sFrame) {
bool shown = sFrame->IsShown();
if (shown) {
Show(false);
}
mFrame.reset();
sFrame.reset();
if (shown) {
Show(true);
}

View File

@ -6,9 +6,6 @@
Dominic Mazzoni
This is the main source file for Audacity which handles
initialization and termination by subclassing wxApp.
**********************************************************************/
#ifndef __AUDACITY_LOGGER__
@ -59,8 +56,6 @@ class AUDACITY_DLL_API AudacityLogger final : public wxEvtHandler,
// PrefsListener implementation
void UpdatePrefs() override;
Destroy_ptr<wxFrame> mFrame;
wxTextCtrl *mText;
wxString mBuffer;
bool mUpdated;
};