Files
tcell-widgets/absolute_layout.go
2025-07-09 16:14:11 -05:00

191 lines
5.7 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 (
"fmt"
"github.com/gdamore/tcell"
)
type AbsoluteLayout struct {
id string
style tcell.Style
x, y int
w, h int
widgets []Widget
wCoords map[Widget]Coord
wAnchor map[Widget]AbsoluteAnchor
defAnchor AbsoluteAnchor
active bool
visible bool
logger func(string)
}
type AbsoluteAnchor int
const (
AnchorTL = AbsoluteAnchor(iota) // x,y starts at <startX> <startY>
AnchorT // x,y starts at <middleX>, <startY>
AnchorTR // x,y starts at <endX>, <startY>
AnchorR // x,y starts at <endX>, <middleY>
AnchorBR // x,y starts at <endX>, <endY>
AnchorB // x,y starts at <middleX>, <endY>
AnchorBL // x,y starts at <endX>, <startY>
AnchorL // x,y starts at <startX>, <middleY>
AnchorErr
)
func NewAbsoluteLayout(id string, s tcell.Style) *AbsoluteLayout {
ret := &AbsoluteLayout{}
ret.Init(id, s)
return ret
}
func (w *AbsoluteLayout) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.defAnchor = AnchorTL
w.wCoords = make(map[Widget]Coord)
w.wAnchor = make(map[Widget]AbsoluteAnchor)
}
func (w *AbsoluteLayout) Id() string { return w.id }
func (w *AbsoluteLayout) HandleResize(ev *tcell.EventResize) {
for _, wi := range w.widgets {
wi.HandleResize(ev)
}
}
func (w *AbsoluteLayout) HandleKey(ev *tcell.EventKey) bool {
for _, wi := range w.widgets {
w.Log(fmt.Sprintf("Passing key (%s) to %s", ev.Name(), wi.Id()))
if wi.HandleKey(ev) {
return true
}
}
return false
}
func (w *AbsoluteLayout) HandleTime(ev *tcell.EventTime) {
for _, wi := range w.widgets {
wi.HandleTime(ev)
}
}
func (w *AbsoluteLayout) Draw(screen tcell.Screen) {
if !w.visible {
return
}
for i := len(w.widgets) - 1; i >= 0; i-- {
var p Coord
var a AbsoluteAnchor
var ok bool
if p, ok = w.wCoords[w.widgets[i]]; !ok {
// Don't know where to put this widget
continue
}
if a, ok = w.wAnchor[w.widgets[i]]; !ok {
a = w.defAnchor
}
midX := (w.x + (w.x + w.w)) / 2
// midY := (w.y + (w.y + w.h)) / 2
switch a {
case AnchorTL:
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorT:
wrk := w.widgets[i].GetW() / 2
w.widgets[i].SetPos(p.Add(Coord{X: (midX - wrk), Y: w.y}))
case AnchorTR:
wrk := w.widgets[i].GetW()
w.widgets[i].SetPos(p.Add(Coord{X: w.x + w.w - wrk, Y: w.y}))
case AnchorR:
wrkYmid := w.widgets[i].GetH() / 2
wrkX := w.widgets[i].GetW()
w.widgets[i].SetPos(p.Add(Coord{X: w.x + w.w - wrkX, Y: w.y - (w.h / 2) - wrkYmid}))
case AnchorBR:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorB:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
case AnchorBL:
wrkX, wrkY := (w.x+w.h)-w.widgets[i].GetH(), 0
w.widgets[i].SetPos(p.Add(Coord{X: wrkX, Y: wrkY}))
case AnchorL:
// TODO
w.widgets[i].SetPos(p.Add(Coord{X: w.x, Y: w.y}))
}
w.widgets[i].Draw(screen)
}
}
func (w *AbsoluteLayout) Active() bool { return w.active }
func (w *AbsoluteLayout) SetActive(a bool) { w.active = a }
func (w *AbsoluteLayout) Visible() bool { return w.visible }
func (w *AbsoluteLayout) SetVisible(a bool) { w.visible = a }
func (w *AbsoluteLayout) Focusable() bool { return true }
func (w *AbsoluteLayout) SetX(x int) { w.x = x }
func (w *AbsoluteLayout) SetY(y int) { w.y = y }
func (w *AbsoluteLayout) GetX() int { return w.x }
func (w *AbsoluteLayout) GetY() int { return w.y }
func (w *AbsoluteLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *AbsoluteLayout) GetW() int { return w.w }
func (w *AbsoluteLayout) GetH() int { return w.h }
func (w *AbsoluteLayout) SetW(wd int) { w.w = wd }
func (w *AbsoluteLayout) SetH(h int) { w.h = h }
func (w *AbsoluteLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *AbsoluteLayout) WantW() int { return w.w }
func (w *AbsoluteLayout) WantH() int { return w.h }
// Add a widget at x/y
func (w *AbsoluteLayout) Add(n Widget, pos Coord) { w.AddAnchored(n, pos, w.defAnchor) }
func (w *AbsoluteLayout) AddAnchored(n Widget, pos Coord, anchor AbsoluteAnchor) {
w.widgets = append(w.widgets, n)
w.wCoords[n] = pos
w.wAnchor[n] = anchor
}
func (w *AbsoluteLayout) Clear() {
w.widgets = []Widget{}
w.wCoords = make(map[Widget]Coord)
}
func (w *AbsoluteLayout) SetDefaultAnchor(d AbsoluteAnchor) {
if d < 0 || d > AnchorErr {
d = AnchorTL
}
w.defAnchor = d
}
func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
func (w *AbsoluteLayout) Log(txt string) {
if w.logger != nil {
w.logger(txt)
}
}