From 90fe6b862e894b6b651377b99a25968e0fd6e2bd Mon Sep 17 00:00:00 2001 From: mappu Date: Mon, 6 Jan 2025 18:33:14 +1300 Subject: [PATCH] miqt: port timers from Qt to native Go timers --- go.mod | 2 +- go.sum | 2 ++ main.go | 38 ++++++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index b13988f..c85231f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module code.ivysaur.me/qocker-miqt go 1.19 -require github.com/mappu/miqt v0.7.1 // indirect +require github.com/mappu/miqt v0.7.2-0.20250104001511-4c0d782bd34c // indirect diff --git a/go.sum b/go.sum index 25293f8..0fc788a 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/mappu/miqt v0.7.1 h1:CIegOqnF9sxSHs4eyqOgAHbuhFwCu1hth4b989ZTP1k= github.com/mappu/miqt v0.7.1/go.mod h1:xFg7ADaO1QSkmXPsPODoKe/bydJpRG9fgCYyIDl/h1U= +github.com/mappu/miqt v0.7.2-0.20250104001511-4c0d782bd34c h1:4oJzer4B//aJ8B/dCFF9yLPgSLobqXVhUpfQh/Tdc5U= +github.com/mappu/miqt v0.7.2-0.20250104001511-4c0d782bd34c/go.mod h1:xFg7ADaO1QSkmXPsPODoKe/bydJpRG9fgCYyIDl/h1U= diff --git a/main.go b/main.go index 24b8006..43e3470 100644 --- a/main.go +++ b/main.go @@ -5,10 +5,14 @@ import ( "os/exec" "runtime" "strings" + "time" qt "github.com/mappu/miqt/qt6" + "github.com/mappu/miqt/qt6/mainthread" ) +const AutoRefreshInterval = 1 * time.Second + func NewStatusDelegate(status string) *qt.QWidget { mw := qt.NewQWidget2() @@ -101,7 +105,7 @@ type DockerGUI struct { networks_searchbar *qt.QLineEdit volumes_searchbar *qt.QLineEdit - refresh_timer *qt.QTimer + refresh_timer *time.Ticker } func NewDockerGUI() *DockerGUI { @@ -254,9 +258,6 @@ func (self *DockerGUI) create_toolbar() { // Add auto-refresh checkbox self.auto_refresh_checkbox = qt.NewQCheckBox3("Auto-refresh") self.auto_refresh_checkbox.SetChecked(true) - self.auto_refresh_checkbox.OnStateChanged(func(state int) { - self.toggle_auto_refresh(state) - }) self.toolbar.AddWidget(self.auto_refresh_checkbox.QWidget) // Add separator @@ -419,19 +420,24 @@ func (self *DockerGUI) show_context_menu(position *qt.QPoint) { } func (self *DockerGUI) setup_auto_refresh() { - self.refresh_timer = qt.NewQTimer2(self.QObject) - self.refresh_timer.OnTimeout(self.refresh_data) - if self.auto_refresh_checkbox.IsChecked() { - self.refresh_timer.Start(1000) // 1000 ms = 1 second - } -} + self.refresh_timer = time.NewTicker(AutoRefreshInterval) -func (self *DockerGUI) toggle_auto_refresh(state int) { - if state == int(qt.Checked) { - self.refresh_timer.Start(1000) - } else { - self.refresh_timer.Stop() - } + go func() { + for { + _, ok := <-self.refresh_timer.C + if !ok { + return + } + + mainthread.Wait(func() { + if !self.auto_refresh_checkbox.IsChecked() { + return + } + + self.refresh_data() + }) + } + }() } func (self *DockerGUI) handle_action(action string) {