174 lines
5.5 KiB
Go
174 lines
5.5 KiB
Go
/*
|
|
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 (
|
|
"strings"
|
|
|
|
t "git.bullercodeworks.com/brian/tcell-widgets"
|
|
th "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type StatusBar struct {
|
|
id string
|
|
style tcell.Style
|
|
x, y int
|
|
w, h int
|
|
visible bool
|
|
keyMap *t.KeyMap
|
|
|
|
blocks []*StatusBlock
|
|
blockFlags map[*StatusBlock]t.LayoutFlag
|
|
|
|
logger func(string, ...any)
|
|
}
|
|
|
|
func NewStatusBar(id string, s tcell.Style) *StatusBar {
|
|
ret := &StatusBar{}
|
|
ret.Init(id, s)
|
|
return ret
|
|
}
|
|
|
|
func (w *StatusBar) Init(id string, s tcell.Style) {
|
|
w.id = id
|
|
w.style = s
|
|
w.visible = true
|
|
w.blockFlags = make(map[*StatusBlock]t.LayoutFlag)
|
|
}
|
|
|
|
func (w *StatusBar) Id() string { return w.id }
|
|
func (w *StatusBar) HandleResize(ev *tcell.EventResize) {
|
|
w.w, w.h = ev.Size()
|
|
w.updatePosAndSize()
|
|
}
|
|
func (w *StatusBar) GetKeyMap() *t.KeyMap { return w.keyMap }
|
|
func (w *StatusBar) SetKeyMap(km *t.KeyMap) { w.keyMap = km }
|
|
|
|
func (w *StatusBar) HandleKey(ev *tcell.EventKey) bool { return false }
|
|
|
|
func (w *StatusBar) HandleTime(ev *tcell.EventTime) {
|
|
for i := range w.blocks {
|
|
w.blocks[i].HandleTime(ev)
|
|
}
|
|
}
|
|
|
|
func (w *StatusBar) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
th.DrawText(w.x, w.y, strings.Repeat(" ", w.w), w.style, screen)
|
|
for i := range w.blocks {
|
|
w.blocks[i].Draw(screen)
|
|
}
|
|
}
|
|
|
|
func (w *StatusBar) SetStyle(s tcell.Style) { w.style = s }
|
|
func (w *StatusBar) Active() bool { return false }
|
|
func (w *StatusBar) SetActive(a bool) {}
|
|
func (w *StatusBar) Visible() bool { return w.visible }
|
|
func (w *StatusBar) SetVisible(a bool) { w.visible = a }
|
|
func (w *StatusBar) Focusable() bool { return false }
|
|
func (w *StatusBar) SetFocusable(b bool) {}
|
|
func (w *StatusBar) SetX(x int) { w.SetPos(t.Coord{X: x, Y: w.y}) }
|
|
func (w *StatusBar) SetY(y int) { w.SetPos(t.Coord{X: w.x, Y: y}) }
|
|
func (w *StatusBar) GetX() int { return w.x }
|
|
func (w *StatusBar) GetY() int { return w.y }
|
|
func (w *StatusBar) GetPos() t.Coord { return t.Coord{X: w.x, Y: w.y} }
|
|
func (w *StatusBar) SetPos(c t.Coord) {
|
|
w.x, w.y = c.X, c.Y
|
|
w.updatePosAndSize()
|
|
}
|
|
func (w *StatusBar) GetW() int { return w.w }
|
|
func (w *StatusBar) GetH() int { return w.h }
|
|
func (w *StatusBar) SetW(wd int) { w.SetSize(t.Coord{X: wd, Y: w.h}) }
|
|
func (w *StatusBar) SetH(h int) { w.SetSize(t.Coord{X: w.w, Y: h}) }
|
|
func (w *StatusBar) SetSize(c t.Coord) {
|
|
w.w, w.h = c.X, c.Y
|
|
w.updatePosAndSize()
|
|
}
|
|
func (w *StatusBar) WantW() int { return w.w }
|
|
func (w *StatusBar) WantH() int { return w.h }
|
|
func (w *StatusBar) MinW() int { return w.w }
|
|
func (w *StatusBar) MinH() int { return 1 }
|
|
func (w *StatusBar) SetLogger(l func(string, ...any)) { w.logger = l }
|
|
func (w *StatusBar) Log(txt string, args ...any) { w.logger(txt, args...) }
|
|
|
|
func (w *StatusBar) Add(b *StatusBlock) { w.blocks = append(w.blocks, b) }
|
|
func (w *StatusBar) SetFlag(b *StatusBlock, f t.LayoutFlag) {
|
|
if _, ok := w.blockFlags[b]; ok {
|
|
w.blockFlags[b].Add(f)
|
|
} else {
|
|
w.blockFlags[b] = f
|
|
}
|
|
}
|
|
func (w *StatusBar) RemoveFlag(b *StatusBlock, f t.LayoutFlag) { w.blockFlags[b].Remove(f) }
|
|
func (w *StatusBar) ClearFlags(b *StatusBlock) { delete(w.blockFlags, b) }
|
|
|
|
func (w *StatusBar) updatePosAndSize() {
|
|
// First, all blocks that are Left Aligned (or no alignment)
|
|
x := w.x
|
|
for i := range w.blocks {
|
|
f, ok := w.blockFlags[w.blocks[i]]
|
|
if !ok || (f&t.LFAlignLeft != 0 || f&t.LFAlignH == 0) {
|
|
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
|
|
x += w.blocks[i].Width()
|
|
}
|
|
}
|
|
|
|
// Center Aligned
|
|
// First, get the width of all center blocks
|
|
var cW int
|
|
for i := range w.blocks {
|
|
f, ok := w.blockFlags[w.blocks[i]]
|
|
if ok && (f&t.LFAlignCenter != 0) {
|
|
cW += w.blocks[i].Width()
|
|
}
|
|
}
|
|
x = w.x + (w.w / 2) - (cW / 2)
|
|
for i := range w.blocks {
|
|
f, ok := w.blockFlags[w.blocks[i]]
|
|
if ok && (f&t.LFAlignCenter != 0) {
|
|
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
|
|
x += w.blocks[i].Width()
|
|
}
|
|
}
|
|
|
|
// Right Aligned
|
|
// First, get the width of all Right Aligned blocks
|
|
cW = 0
|
|
for i := range w.blocks {
|
|
f, ok := w.blockFlags[w.blocks[i]]
|
|
if ok && (f&t.LFAlignRight != 0) {
|
|
cW += w.blocks[i].Width()
|
|
}
|
|
}
|
|
x = w.x + w.w - cW
|
|
for i := range w.blocks {
|
|
f, ok := w.blockFlags[w.blocks[i]]
|
|
if ok && (f&t.LFAlignRight != 0) {
|
|
w.blocks[i].SetPos(t.Coord{X: x, Y: w.y})
|
|
x += w.blocks[i].Width()
|
|
}
|
|
}
|
|
}
|