Update buttons as the application state changes.

This commit is contained in:
David Given
2022-02-26 18:10:00 +01:00
parent b45b45b1b3
commit 32f0c5dc09
4 changed files with 58 additions and 26 deletions

View File

@@ -3,6 +3,9 @@
#include <wx/wx.h>
class ExecEvent;
class MainWindow;
extern void runOnUiThread(std::function<void()> callback);
extern void runOnWorkerThread(std::function<void()> callback);
@@ -18,5 +21,26 @@ static inline R runOnUiThread(std::function<R()>& callback)
return retvar;
}
class FluxEngineApp : public wxApp, public wxThreadHelper
{
public:
virtual bool OnInit();
void RunOnWorkerThread(std::function<void()> callback);
private:
void OnExec(const ExecEvent& event);
public:
bool IsWorkerThreadRunning() const;
protected:
virtual wxThread::ExitCode Entry();
private:
std::function<void()> _callback;
MainWindow* _mainWindow;
};
wxDECLARE_APP(FluxEngineApp);
#endif

View File

@@ -3,28 +3,11 @@
#include "mainwindow.h"
#include "utils.h"
class MyApp;
class FluxEngineApp;
class ExecEvent;
static wxSemaphore execSemaphore(0);
class MyApp : public wxApp, public wxThreadHelper
{
public:
virtual bool OnInit();
void RunOnWorkerThread(std::function<void()> callback);
private:
void OnExec(const ExecEvent& event);
protected:
virtual wxThread::ExitCode Entry();
private:
std::function<void()> _callback;
};
wxDECLARE_APP(MyApp);
wxDEFINE_EVENT(EXEC_EVENT_TYPE, ExecEvent);
class ExecEvent : public wxThreadEvent
{
@@ -59,15 +42,15 @@ private:
std::function<void()> _callback;
};
bool MyApp::OnInit()
bool FluxEngineApp::OnInit()
{
Bind(EXEC_EVENT_TYPE, &MyApp::OnExec, this);
MainWindow* frame = new MainWindow();
frame->Show(true);
Bind(EXEC_EVENT_TYPE, &FluxEngineApp::OnExec, this);
_mainWindow = new MainWindow();
_mainWindow->Show(true);
return true;
}
wxThread::ExitCode MyApp::Entry()
wxThread::ExitCode FluxEngineApp::Entry()
{
try
{
@@ -82,12 +65,13 @@ wxThread::ExitCode MyApp::Entry()
runOnUiThread(
[&] {
_callback = nullptr;
_mainWindow->UpdateState();
}
);
return 0;
}
void MyApp::RunOnWorkerThread(std::function<void()> callback)
void FluxEngineApp::RunOnWorkerThread(std::function<void()> callback)
{
if (_callback)
std::cerr << "Cannot start new worker task as one is already running\n";
@@ -99,6 +83,7 @@ void MyApp::RunOnWorkerThread(std::function<void()> callback)
emergencyStop = false;
CreateThread(wxTHREAD_JOINABLE);
GetThread()->Run();
_mainWindow->UpdateState();
}
void runOnWorkerThread(std::function<void()> callback)
@@ -106,7 +91,12 @@ void runOnWorkerThread(std::function<void()> callback)
wxGetApp().RunOnWorkerThread(callback);
}
void MyApp::OnExec(const ExecEvent& event)
bool FluxEngineApp::IsWorkerThreadRunning() const
{
return !!_callback;
}
void FluxEngineApp::OnExec(const ExecEvent& event)
{
event.RunCallback();
execSemaphore.Post();
@@ -120,4 +110,4 @@ void runOnUiThread(std::function<void()> callback)
execSemaphore.Wait();
}
wxIMPLEMENT_APP(MyApp);
wxIMPLEMENT_APP(FluxEngineApp);

View File

@@ -45,6 +45,8 @@ MainWindow::MainWindow(): MainWindowGen(nullptr)
readFluxButton->Bind(wxEVT_BUTTON, &MainWindow::OnReadFluxButton, this);
stopButton->Bind(wxEVT_BUTTON, &MainWindow::OnStopButton, this);
UpdateState();
}
void MainWindow::OnExit(wxCommandEvent& event)
@@ -126,10 +128,24 @@ void MainWindow::OnLogMessage(std::shared_ptr<const AnyLogMessage> message)
{
visualiser->SetTrackData(m.track);
},
[&](const DiskReadLogMessage& m)
{
_currentDisk = m.disk;
},
},
*message);
}
void MainWindow::UpdateState()
{
writeImageButton->Enable(!!_currentDisk);
writeFluxButton->Enable(!!_currentDisk);
stopButton->Enable(wxGetApp().IsWorkerThreadRunning());
readFluxButton->Enable(!wxGetApp().IsWorkerThreadRunning());
readImageButton->Enable(!wxGetApp().IsWorkerThreadRunning());
}
void MainWindow::UpdateDevices()
{
auto candidates = findUsbDevices();

View File

@@ -19,6 +19,8 @@ private:
void OnReadFluxButton(wxCommandEvent&);
void OnLogMessage(std::shared_ptr<const AnyLogMessage> message);
public:
void UpdateState();
void UpdateDevices();
private: