Menu tweaks
Spinner?
This commit is contained in:
106
wdgt_spinner.go
Normal file
106
wdgt_spinner.go
Normal file
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
Copyright © Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package widgets
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
// Spinner is just blank. It's a good placeholder, if needed.
|
||||
type Spinner struct {
|
||||
id string
|
||||
x, y int
|
||||
style tcell.Style
|
||||
currentFrame int
|
||||
frames []rune
|
||||
lastTick time.Time
|
||||
tickInterval time.Duration
|
||||
keyMap KeyMap
|
||||
}
|
||||
|
||||
var _ Widget = (*Spinner)(nil)
|
||||
|
||||
func NewSpinner(id string, style tcell.Style) *Spinner {
|
||||
ret := &Spinner{id: id}
|
||||
ret.Init(id, style)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *Spinner) Init(id string, st tcell.Style) {
|
||||
w.frames = []rune{'⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'}
|
||||
w.tickInterval = time.Second
|
||||
w.keyMap = BlankKeyMap()
|
||||
}
|
||||
func (w *Spinner) Id() string { return w.id }
|
||||
func (w *Spinner) HandleResize(ev *tcell.EventResize) {}
|
||||
func (w *Spinner) SetKeyMap(km KeyMap) { w.keyMap = km }
|
||||
func (w *Spinner) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
||||
func (w *Spinner) RemoveFromKeyMap(km KeyMap) {
|
||||
for k := range km.Keys {
|
||||
w.keyMap.Remove(k)
|
||||
}
|
||||
for r := range km.Runes {
|
||||
w.keyMap.RemoveRune(r)
|
||||
}
|
||||
}
|
||||
func (w *Spinner) HandleKey(ev *tcell.EventKey) bool { return false }
|
||||
func (w *Spinner) HandleTime(ev *tcell.EventTime) {
|
||||
for w.lastTick.Before(ev.When()) {
|
||||
w.lastTick = w.lastTick.Add(w.tickInterval)
|
||||
w.currentFrame = (w.currentFrame + 1) % len(w.frames)
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Spinner) Draw(screen tcell.Screen) {
|
||||
screen.SetContent(w.x, w.y, w.frames[w.currentFrame], nil, w.style)
|
||||
}
|
||||
|
||||
func (w *Spinner) Active() bool { return false }
|
||||
func (w *Spinner) SetActive(a bool) {}
|
||||
func (w *Spinner) Visible() bool { return true }
|
||||
func (w *Spinner) SetVisible(v bool) {}
|
||||
func (w *Spinner) Focusable() bool { return false }
|
||||
func (w *Spinner) SetFocusable(t bool) {}
|
||||
func (w *Spinner) SetX(x int) {}
|
||||
func (w *Spinner) SetY(y int) {}
|
||||
func (w *Spinner) GetX() int { return w.x }
|
||||
func (w *Spinner) GetY() int { return w.y }
|
||||
func (w *Spinner) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
||||
func (w *Spinner) SetPos(pos Coord) {}
|
||||
func (w *Spinner) SetSize(size Coord) {}
|
||||
func (w *Spinner) SetW(wd int) {}
|
||||
func (w *Spinner) SetH(h int) {}
|
||||
func (w *Spinner) GetW() int { return 0 }
|
||||
func (w *Spinner) GetH() int { return 0 }
|
||||
func (w *Spinner) WantW() int { return 0 }
|
||||
func (w *Spinner) WantH() int { return 0 }
|
||||
func (w *Spinner) MinW() int { return 0 }
|
||||
func (w *Spinner) MinH() int { return 0 }
|
||||
|
||||
func (w *Spinner) SetFrames(frames []rune) {
|
||||
w.frames = frames
|
||||
w.currentFrame = 0
|
||||
}
|
||||
|
||||
func (w *Spinner) SetTickInterface(d time.Duration) { w.tickInterval = d }
|
||||
Reference in New Issue
Block a user