From 83008d3b01f28f759a6a3728bc94da9b26695b59 Mon Sep 17 00:00:00 2001 From: William JCM Date: Fri, 10 Jan 2020 00:01:24 +0100 Subject: [PATCH] MainFrame: replace the timer with a file watcher. This will lead to less UI flickering and faster response times. --- GUI/EvtMainFrame.cpp | 44 ++++++++++++++++++++++++++++++++++---------- GUI/EvtMainFrame.h | 7 ++++++- GUI/MainFrame.cpp | 3 --- GUI/MainFrame.fbp | 9 --------- GUI/MainFrame.h | 3 --- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/GUI/EvtMainFrame.cpp b/GUI/EvtMainFrame.cpp index 51744ff..53d5099 100644 --- a/GUI/EvtMainFrame.cpp +++ b/GUI/EvtMainFrame.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "EvtMainFrame.h" @@ -55,6 +56,10 @@ EvtMainFrame::EvtMainFrame(wxWindow* parent): MainFrame(parent) { "Please avoid using this application while the game is running. Bad Thingsā„¢ could happen to your data.\n\n" "DISCLAIMER: The developer of this application cannot be held responsible for data loss or corruption. PLEASE USE AT YOUR OWN RISK!\n\n" "Last but not least, this application is released under the terms of the GNU General Public Licence version 3. Please see the COPYING file for more details.")); + + _watcher.Connect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler(EvtMainFrame::fileUpdateEvent), nullptr, this); + _watcher.AddTree(wxFileName(Utility::Directory::toNativeSeparators(_saveDirectory), wxPATH_WIN), + wxFSW_EVENT_CREATE|wxFSW_EVENT_DELETE|wxFSW_EVENT_MODIFY|wxFSW_EVENT_RENAME, wxString::Format("Unit??%s.sav", _localSteamId)); } EvtMainFrame::~EvtMainFrame() { @@ -62,6 +67,7 @@ EvtMainFrame::~EvtMainFrame() { _installedListView->Disconnect(wxEVT_LIST_ITEM_DESELECTED, wxListEventHandler(EvtMainFrame::installedSelectionEvent), nullptr, this); _installedListView->Disconnect(wxEVT_LIST_BEGIN_DRAG, wxListEventHandler(EvtMainFrame::listColumnDragEvent), nullptr, this); _installedListView->Disconnect(wxEVT_LIST_COL_BEGIN_DRAG, wxListEventHandler(EvtMainFrame::listColumnDragEvent), nullptr, this); + _watcher.Disconnect(wxEVT_FSWATCHER, wxFileSystemWatcherEventHandler(EvtMainFrame::fileUpdateEvent), nullptr, this); } void EvtMainFrame::importEvent(wxCommandEvent&) { @@ -117,8 +123,6 @@ void EvtMainFrame::importEvent(wxCommandEvent&) { *(iter + i) = _localSteamId[i]; } } - - refreshListView(); } void EvtMainFrame::moveEvent(wxCommandEvent&) { @@ -147,8 +151,6 @@ void EvtMainFrame::moveEvent(wxCommandEvent&) { if(dest_status != "") { Utility::Directory::move(dest_file + ".tmp", orig_file); } - - refreshListView(); } void EvtMainFrame::deleteEvent(wxCommandEvent&) { @@ -162,8 +164,6 @@ void EvtMainFrame::deleteEvent(wxCommandEvent&) { if(Utility::Directory::exists(file)) { Utility::Directory::rm(file); } - - refreshListView(); } void EvtMainFrame::openSaveDirEvent(wxCommandEvent&) { @@ -178,8 +178,34 @@ void EvtMainFrame::listColumnDragEvent(wxListEvent& event) { event.Veto(); } -void EvtMainFrame::timerEvent(wxTimerEvent&) { - refreshListView(); +void EvtMainFrame::fileUpdateEvent(wxFileSystemWatcherEvent& event) { + int event_type = event.GetChangeType(); + + if(event_type == wxFSW_EVENT_MODIFY && _lastWatcherEventType == wxFSW_EVENT_RENAME) { + _lastWatcherEventType = event_type; + return; + } + + _lastWatcherEventType = event_type; + + std::string event_file =(Utility::Directory::fromNativeSeparators(event.GetNewPath().GetFullName().ToStdString())); + + if(!Utility::String::endsWith(event_file, ".sav")) { + return; + } + + long slot; + + try { + slot = std::stol(std::string(event_file.data() + 4, 2)); + } + catch(const std::invalid_argument&) { + return; + } + + _installedListView->SetItem(slot, 1, getSlotMassName(slot)); + + updateCommandsState(); } void EvtMainFrame::getSaveDirectory() { @@ -228,8 +254,6 @@ void EvtMainFrame::initialiseListView() { _installedListView->SetColumnWidth(1, wxLIST_AUTOSIZE_USEHEADER); refreshListView(); - - _refreshTimer.Start(1000); } void EvtMainFrame::refreshListView() { diff --git a/GUI/EvtMainFrame.h b/GUI/EvtMainFrame.h index 1e21857..ee9e397 100644 --- a/GUI/EvtMainFrame.h +++ b/GUI/EvtMainFrame.h @@ -19,6 +19,8 @@ #include +#include + #include "MainFrame.h" class EvtMainFrame: public MainFrame { @@ -33,7 +35,7 @@ class EvtMainFrame: public MainFrame { void openSaveDirEvent(wxCommandEvent&); void installedSelectionEvent(wxListEvent&); void listColumnDragEvent(wxListEvent&); - void timerEvent(wxTimerEvent&); + void fileUpdateEvent(wxFileSystemWatcherEvent& event); private: void getSaveDirectory(); @@ -50,6 +52,9 @@ class EvtMainFrame: public MainFrame { std::string _saveDirectory; std::string _localSteamId; + + wxFileSystemWatcher _watcher; + int _lastWatcherEventType = 0; }; #endif // __EvtMainFrame__ diff --git a/GUI/MainFrame.cpp b/GUI/MainFrame.cpp index 1795ed2..53f78e5 100644 --- a/GUI/MainFrame.cpp +++ b/GUI/MainFrame.cpp @@ -79,7 +79,6 @@ MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, co this->SetSizer( bSizerMain ); this->Layout(); bSizerMain->Fit( this ); - _refreshTimer.SetOwner( this, wxID_ANY ); this->Centre( wxBOTH ); @@ -88,7 +87,6 @@ MainFrame::MainFrame( wxWindow* parent, wxWindowID id, const wxString& title, co _moveButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::moveEvent ), NULL, this ); _deleteButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::deleteEvent ), NULL, this ); _openSaveDirButton->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::openSaveDirEvent ), NULL, this ); - this->Connect( wxID_ANY, wxEVT_TIMER, wxTimerEventHandler( MainFrame::timerEvent ) ); } MainFrame::~MainFrame() @@ -98,6 +96,5 @@ MainFrame::~MainFrame() _moveButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::moveEvent ), NULL, this ); _deleteButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::deleteEvent ), NULL, this ); _openSaveDirButton->Disconnect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( MainFrame::openSaveDirEvent ), NULL, this ); - this->Disconnect( wxID_ANY, wxEVT_TIMER, wxTimerEventHandler( MainFrame::timerEvent ) ); } diff --git a/GUI/MainFrame.fbp b/GUI/MainFrame.fbp index a181f8d..18390ef 100644 --- a/GUI/MainFrame.fbp +++ b/GUI/MainFrame.fbp @@ -603,15 +603,6 @@ - - 0 - wxID_ANY - _refreshTimer - 0 - 1000 - protected - timerEvent - diff --git a/GUI/MainFrame.h b/GUI/MainFrame.h index 1237a2f..b95ff64 100644 --- a/GUI/MainFrame.h +++ b/GUI/MainFrame.h @@ -22,7 +22,6 @@ #include #include #include -#include #include /////////////////////////////////////////////////////////////////////////// @@ -43,14 +42,12 @@ class MainFrame : public wxFrame wxButton* _openSaveDirButton; wxStaticText* _riskLabel; wxStaticText* _aboutText; - wxTimer _refreshTimer; // Virtual event handlers, overide them in your derived class virtual void importEvent( wxCommandEvent& event ) { event.Skip(); } virtual void moveEvent( wxCommandEvent& event ) { event.Skip(); } virtual void deleteEvent( wxCommandEvent& event ) { event.Skip(); } virtual void openSaveDirEvent( wxCommandEvent& event ) { event.Skip(); } - virtual void timerEvent( wxTimerEvent& event ) { event.Skip(); } public: