diff --git a/app/scheduler.go b/app/scheduler.go index 10a38207..72f69d8f 100644 --- a/app/scheduler.go +++ b/app/scheduler.go @@ -17,21 +17,22 @@ func Schedule(widget wtf.Wtfable) { return } - tick := time.NewTicker(interval) - quit := make(chan struct{}) + timer := time.NewTicker(interval) for { select { - case <-tick.C: + case <-timer.C: if widget.Enabled() { widget.Refresh() } else { - tick.Stop() + timer.Stop() + return + } + case quit := <-widget.QuitChan(): + if quit == true { + timer.Stop() return } - case <-quit: - tick.Stop() - return } } } diff --git a/app/wtf_app.go b/app/wtf_app.go index 7d9ad76e..2cd450a2 100644 --- a/app/wtf_app.go +++ b/app/wtf_app.go @@ -60,14 +60,14 @@ func (wtfApp *WtfApp) Start() { // Stop kills all the currently-running widgets in this app func (wtfApp *WtfApp) Stop() { - wtfApp.disableAllWidgets() + wtfApp.stopAllWidgets() } /* -------------------- Unexported Functions -------------------- */ -func (wtfApp *WtfApp) disableAllWidgets() { +func (wtfApp *WtfApp) stopAllWidgets() { for _, widget := range wtfApp.Widgets { - widget.Disable() + widget.Stop() } } diff --git a/modules/spotify/keyboard.go b/modules/spotify/keyboard.go index ba5cf8db..6c650834 100644 --- a/modules/spotify/keyboard.go +++ b/modules/spotify/keyboard.go @@ -18,19 +18,19 @@ func (widget *Widget) initializeKeyboardControls() { } func (widget *Widget) previous() { - widget.SpotifyClient.Previous() + widget.client.Previous() time.Sleep(time.Second * 1) widget.Refresh() } func (widget *Widget) next() { - widget.SpotifyClient.Next() + widget.client.Next() time.Sleep(time.Second * 1) widget.Refresh() } func (widget *Widget) playPause() { - widget.SpotifyClient.PlayPause() + widget.client.PlayPause() time.Sleep(time.Second * 1) widget.Refresh() } diff --git a/modules/spotify/widget.go b/modules/spotify/widget.go index 17493e08..10caeccd 100644 --- a/modules/spotify/widget.go +++ b/modules/spotify/widget.go @@ -13,20 +13,19 @@ type Widget struct { wtf.KeyboardWidget wtf.TextWidget + client spotigopher.SpotifyClient settings *Settings spotigopher.Info - spotigopher.SpotifyClient } // NewWidget creates a new instance of a widget func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget { - spotifyClient := spotigopher.NewClient() widget := Widget{ KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common), TextWidget: wtf.NewTextWidget(app, settings.common, true), - Info: spotigopher.Info{}, - SpotifyClient: spotifyClient, + Info: spotigopher.Info{}, + client: spotigopher.NewClient(), settings: settings, } @@ -45,7 +44,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) * } func (w *Widget) refreshSpotifyInfos() error { - info, err := w.SpotifyClient.GetInfo() + info, err := w.client.GetInfo() w.Info = info return err } diff --git a/wtf/bargraph.go b/wtf/bargraph.go index 41486b6b..ac827524 100644 --- a/wtf/bargraph.go +++ b/wtf/bargraph.go @@ -17,6 +17,7 @@ type BarGraph struct { key string maxStars int name string + quitChan chan bool refreshing bool starChar string @@ -37,9 +38,11 @@ func NewBarGraph(app *tview.Application, name string, settings *cfg.Common, focu focusable: focusable, maxStars: settings.Config.UInt("graphStars", 20), name: settings.Title, + quitChan: make(chan bool), starChar: settings.Config.UString("graphIcon", "|"), - RefreshInt: settings.RefreshInterval, commonSettings: settings, + + RefreshInt: settings.RefreshInterval, } widget.View = widget.addView() @@ -90,6 +93,10 @@ func (widget *BarGraph) Name() string { return widget.name } +func (widget *BarGraph) QuitChan() chan bool { + return widget.quitChan +} + // Refreshing returns TRUE if the widget is currently refreshing its data, FALSE if it is not func (widget *BarGraph) Refreshing() bool { return widget.refreshing @@ -104,6 +111,11 @@ func (widget *BarGraph) SetFocusChar(char string) { return } +func (widget *BarGraph) Stop() { + widget.enabled = false + widget.quitChan <- true +} + func (widget *BarGraph) TextView() *tview.TextView { return widget.View } diff --git a/wtf/stoppable.go b/wtf/stoppable.go new file mode 100644 index 00000000..b194dcc5 --- /dev/null +++ b/wtf/stoppable.go @@ -0,0 +1,6 @@ +package wtf + +// Stoppable is the interface that enforces a stoppable state +type Stoppable interface { + Stop() +} diff --git a/wtf/text_widget.go b/wtf/text_widget.go index 441f3685..eb851918 100644 --- a/wtf/text_widget.go +++ b/wtf/text_widget.go @@ -15,6 +15,7 @@ type TextWidget struct { focusable bool focusChar string name string + quitChan chan bool refreshing bool refreshInterval int app *tview.Application @@ -32,6 +33,7 @@ func NewTextWidget(app *tview.Application, commonSettings *cfg.Common, focusable focusable: focusable, focusChar: commonSettings.FocusChar(), name: commonSettings.Name, + quitChan: make(chan bool), refreshing: false, refreshInterval: commonSettings.RefreshInterval, } @@ -97,6 +99,10 @@ func (widget *TextWidget) HelpText() string { return fmt.Sprintf("\n There is no help available for widget %s", widget.commonSettings.Module.Type) } +func (widget *TextWidget) QuitChan() chan bool { + return widget.quitChan +} + func (widget *TextWidget) Name() string { return widget.name } @@ -115,6 +121,11 @@ func (widget *TextWidget) SetFocusChar(char string) { widget.focusChar = char } +func (widget *TextWidget) Stop() { + widget.enabled = false + widget.quitChan <- true +} + func (widget *TextWidget) String() string { return widget.name } diff --git a/wtf/wtfable.go b/wtf/wtfable.go index d3aac524..cb6fd39a 100644 --- a/wtf/wtfable.go +++ b/wtf/wtfable.go @@ -10,12 +10,14 @@ import ( type Wtfable interface { Enablable Schedulable + Stoppable BorderColor() string ConfigText() string FocusChar() string Focusable() bool HelpText() string + QuitChan() chan bool Name() string SetFocusChar(string) TextView() *tview.TextView