First Commit

This commit is contained in:
2025-06-18 07:13:07 -05:00
commit 9688b2930f
13 changed files with 1850 additions and 0 deletions

129
absolute_layout.go Normal file
View File

@@ -0,0 +1,129 @@
/*
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
active bool
visible bool
logger func(string)
}
func (w *AbsoluteLayout) SetLogger(l func(string)) { w.logger = l }
func (w *AbsoluteLayout) Log(txt string) {
if w.logger != nil {
w.logger(txt)
}
}
func NewAbsoluteLayout(id string, s tcell.Style) *AbsoluteLayout {
ret := &AbsoluteLayout{style: s}
ret.Init(id)
return ret
}
func (w *AbsoluteLayout) Init(id string) {
w.id = id
w.visible = true
w.wCoords = make(map[Widget]Coord)
}
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 ok bool
if p, ok = w.wCoords[w.widgets[i]]; !ok {
// Don't know where to put this widget
continue
}
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.widgets = append(w.widgets, n)
w.wCoords[n] = pos
}
func (w *AbsoluteLayout) Clear() {
w.widgets = []Widget{}
w.wCoords = make(map[Widget]Coord)
}