mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add a global Redraw method for TextWidget
Partially addresses #429, by centralizing widget drawing
This commit is contained in:
@@ -29,13 +29,12 @@ func (widget *Widget) display() {
|
||||
return
|
||||
}
|
||||
|
||||
widget.View.SetTitle(widget.ContextualTitle(widget.settings.common.Title))
|
||||
widget.View.SetText(widget.contentFrom(widget.calEvents))
|
||||
widget.TextWidget.Redraw(widget.settings.common.Title, widget.contentFrom(widget.calEvents), false)
|
||||
}
|
||||
|
||||
func (widget *Widget) contentFrom(calEvents []*CalEvent) string {
|
||||
if (calEvents == nil) || (len(calEvents) == 0) {
|
||||
return ""
|
||||
return "No calendar events"
|
||||
}
|
||||
|
||||
var str string
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package gcal
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/wtf"
|
||||
)
|
||||
@@ -12,7 +10,6 @@ type Widget struct {
|
||||
|
||||
app *tview.Application
|
||||
calEvents []*CalEvent
|
||||
ch chan struct{}
|
||||
settings *Settings
|
||||
}
|
||||
|
||||
@@ -21,27 +18,21 @@ func NewWidget(app *tview.Application, settings *Settings) *Widget {
|
||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
||||
|
||||
app: app,
|
||||
ch: make(chan struct{}),
|
||||
settings: settings,
|
||||
}
|
||||
|
||||
go updateLoop(&widget)
|
||||
|
||||
return &widget
|
||||
}
|
||||
|
||||
/* -------------------- Exported Functions -------------------- */
|
||||
|
||||
func (widget *Widget) Disable() {
|
||||
close(widget.ch)
|
||||
widget.TextWidget.Disable()
|
||||
}
|
||||
|
||||
func (widget *Widget) Refresh() {
|
||||
if isAuthenticated() {
|
||||
widget.app.QueueUpdateDraw(func() {
|
||||
widget.fetchAndDisplayEvents()
|
||||
})
|
||||
widget.fetchAndDisplayEvents()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -59,27 +50,5 @@ func (widget *Widget) fetchAndDisplayEvents() {
|
||||
widget.calEvents = calEvents
|
||||
}
|
||||
|
||||
widget.app.QueueUpdateDraw(func() {
|
||||
widget.display()
|
||||
})
|
||||
}
|
||||
|
||||
func updateLoop(widget *Widget) {
|
||||
if widget.settings.textInterval == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
tick := time.NewTicker(time.Duration(widget.settings.textInterval) * time.Second)
|
||||
defer tick.Stop()
|
||||
outer:
|
||||
for {
|
||||
select {
|
||||
case <-tick.C:
|
||||
widget.app.QueueUpdateDraw(func() {
|
||||
widget.display()
|
||||
})
|
||||
case <-widget.ch:
|
||||
break outer
|
||||
}
|
||||
}
|
||||
widget.display()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user