From 6ee8d96a6e8c1e95b8ca2ab19fcc6a3a856d50c8 Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Wed, 12 Mar 2014 17:31:13 -0400 Subject: Added GTK+ indeterminate ProgressBars. --- sysdata_unix.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'sysdata_unix.go') diff --git a/sysdata_unix.go b/sysdata_unix.go index 6947e20..91b301c 100644 --- a/sysdata_unix.go +++ b/sysdata_unix.go @@ -4,7 +4,7 @@ package ui import ( - // ... + "time" ) type sysData struct { @@ -12,6 +12,7 @@ type sysData struct { widget *gtkWidget container *gtkWidget // for moving + pulse chan bool // for sysData.progressPulse() } type classData struct { @@ -285,7 +286,56 @@ func (s *sysData) delete(index int) { <-ret } +// With GTK+, we must manually pulse the indeterminate progressbar ourselves. This goroutine does that. +func (s *sysData) progressPulse() { + pulse := func() { + ret := make(chan struct{}) + defer close(ret) + uitask <- func() { + gtk_progress_bar_pulse(s.widget) + ret <- struct{}{} + } + <-ret + } + + var ticker *time.Ticker + var tickchan <-chan time.Time + + // the default on Windows + const pulseRate = 30 * time.Millisecond + + for { + select { + case start := <-s.pulse: + if start { + ticker = time.NewTicker(pulseRate) + tickchan = ticker.C + pulse() // start the pulse animation now, not 30ms later + } else { + if ticker != nil { + ticker.Stop() + } + ticker = nil + tickchan = nil + s.pulse <- true // notify sysData.setProgress() + } + case <-tickchan: + pulse() + } + } +} + func (s *sysData) setProgress(percent int) { + if s.pulse == nil { + s.pulse = make(chan bool) + go s.progressPulse() + } + if percent == -1 { + s.pulse <- true + return + } + s.pulse <- false + <-s.pulse // wait for sysData.progressPulse() to register that ret := make(chan struct{}) defer close(ret) uitask <- func() { -- cgit v1.2.3