miqt: port timers from Qt to native Go timers

This commit is contained in:
mappu 2025-01-06 18:33:14 +13:00
parent dcc8edd3f8
commit 90fe6b862e
3 changed files with 25 additions and 17 deletions

2
go.mod
View File

@ -2,4 +2,4 @@ module code.ivysaur.me/qocker-miqt
go 1.19 go 1.19
require github.com/mappu/miqt v0.7.1 // indirect require github.com/mappu/miqt v0.7.2-0.20250104001511-4c0d782bd34c // indirect

2
go.sum
View File

@ -1,2 +1,4 @@
github.com/mappu/miqt v0.7.1 h1:CIegOqnF9sxSHs4eyqOgAHbuhFwCu1hth4b989ZTP1k= 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.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=

36
main.go
View File

@ -5,10 +5,14 @@ import (
"os/exec" "os/exec"
"runtime" "runtime"
"strings" "strings"
"time"
qt "github.com/mappu/miqt/qt6" qt "github.com/mappu/miqt/qt6"
"github.com/mappu/miqt/qt6/mainthread"
) )
const AutoRefreshInterval = 1 * time.Second
func NewStatusDelegate(status string) *qt.QWidget { func NewStatusDelegate(status string) *qt.QWidget {
mw := qt.NewQWidget2() mw := qt.NewQWidget2()
@ -101,7 +105,7 @@ type DockerGUI struct {
networks_searchbar *qt.QLineEdit networks_searchbar *qt.QLineEdit
volumes_searchbar *qt.QLineEdit volumes_searchbar *qt.QLineEdit
refresh_timer *qt.QTimer refresh_timer *time.Ticker
} }
func NewDockerGUI() *DockerGUI { func NewDockerGUI() *DockerGUI {
@ -254,9 +258,6 @@ func (self *DockerGUI) create_toolbar() {
// Add auto-refresh checkbox // Add auto-refresh checkbox
self.auto_refresh_checkbox = qt.NewQCheckBox3("Auto-refresh") self.auto_refresh_checkbox = qt.NewQCheckBox3("Auto-refresh")
self.auto_refresh_checkbox.SetChecked(true) 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) self.toolbar.AddWidget(self.auto_refresh_checkbox.QWidget)
// Add separator // Add separator
@ -419,19 +420,24 @@ func (self *DockerGUI) show_context_menu(position *qt.QPoint) {
} }
func (self *DockerGUI) setup_auto_refresh() { func (self *DockerGUI) setup_auto_refresh() {
self.refresh_timer = qt.NewQTimer2(self.QObject) self.refresh_timer = time.NewTicker(AutoRefreshInterval)
self.refresh_timer.OnTimeout(self.refresh_data)
if self.auto_refresh_checkbox.IsChecked() {
self.refresh_timer.Start(1000) // 1000 ms = 1 second
}
}
func (self *DockerGUI) toggle_auto_refresh(state int) { go func() {
if state == int(qt.Checked) { for {
self.refresh_timer.Start(1000) _, ok := <-self.refresh_timer.C
} else { if !ok {
self.refresh_timer.Stop() return
} }
mainthread.Wait(func() {
if !self.auto_refresh_checkbox.IsChecked() {
return
}
self.refresh_data()
})
}
}()
} }
func (self *DockerGUI) handle_action(action string) { func (self *DockerGUI) handle_action(action string) {