564 lines
14 KiB
Go
564 lines
14 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"
|
|
"time"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
// LinearLayout lays out all widgets added one after the other
|
|
// It will fill as much space as you give it
|
|
type LinearLayout struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
orientation LinearLayoutOrient
|
|
|
|
x, y int
|
|
w, h int
|
|
widgets []Widget
|
|
layoutFlags map[Widget]LayoutFlag
|
|
layoutWeights map[Widget]int
|
|
totalWeight int
|
|
defFlags LayoutFlag // The default flags applied to widgets if nothing
|
|
// is in layoutFlags
|
|
|
|
// Stacked makes the layout ignore weights and effectively shrink-wrap all
|
|
// fields
|
|
stacked bool
|
|
|
|
active bool
|
|
visible bool
|
|
tabbable bool
|
|
disableTab bool
|
|
insetBorder bool
|
|
|
|
logger func(string, ...any)
|
|
}
|
|
|
|
type LinearLayoutOrient int
|
|
|
|
const (
|
|
LinLayV = LinearLayoutOrient(iota)
|
|
LinLayH
|
|
)
|
|
|
|
var _ Widget = (*LinearLayout)(nil)
|
|
|
|
func NewLinearLayout(id string, s tcell.Style) *LinearLayout {
|
|
ret := &LinearLayout{}
|
|
ret.Init(id, s)
|
|
return ret
|
|
}
|
|
|
|
func (w *LinearLayout) Init(id string, s tcell.Style) {
|
|
w.id = id
|
|
w.style = s
|
|
w.visible = true
|
|
w.tabbable = true
|
|
w.defFlags = LayoutFlag(LFAlignHCenter | LFAlignVCenter)
|
|
w.layoutFlags = make(map[Widget]LayoutFlag)
|
|
w.layoutWeights = make(map[Widget]int)
|
|
}
|
|
|
|
func (w *LinearLayout) Id() string { return w.id }
|
|
func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
|
|
w.w, w.h = ev.Size()
|
|
w.updateWidgetLayouts()
|
|
}
|
|
|
|
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active || w.disableTab {
|
|
return false
|
|
}
|
|
active := w.findActive()
|
|
if active != nil {
|
|
if active.HandleKey(ev) {
|
|
return true
|
|
}
|
|
}
|
|
if ev.Key() == tcell.KeyTab {
|
|
if active == nil && len(w.widgets) > 0 {
|
|
// No widget is active
|
|
w.widgets[0].SetActive(true)
|
|
return true
|
|
}
|
|
return w.activateNext()
|
|
}
|
|
|
|
return false
|
|
}
|
|
func (w *LinearLayout) GetActive() Widget { return w.findActive() }
|
|
|
|
func (w *LinearLayout) HandleTime(ev *tcell.EventTime) {
|
|
for _, wi := range w.widgets {
|
|
wi.HandleTime(ev)
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
pos := w.GetPos()
|
|
if w.insetBorder {
|
|
wh.Border(pos.X, pos.Y, pos.X+w.w, pos.Y+w.h, wh.BRD_CSIMPLE, w.style, screen)
|
|
}
|
|
|
|
for _, wd := range w.widgets {
|
|
pos.DrawOffset(wd, screen)
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) Active() bool { return w.active }
|
|
func (w *LinearLayout) SetActive(a bool) {
|
|
w.active = a
|
|
if w.active {
|
|
act := w.findActiveOrFirst()
|
|
if act != nil {
|
|
act.SetActive(true)
|
|
}
|
|
}
|
|
}
|
|
func (w *LinearLayout) Visible() bool { return w.visible }
|
|
func (w *LinearLayout) SetVisible(a bool) { w.visible = a }
|
|
func (w *LinearLayout) Focusable() bool { return true }
|
|
func (w *LinearLayout) SetTabbable(b bool) { w.tabbable = b }
|
|
func (w *LinearLayout) Tabbable() bool { return w.tabbable }
|
|
func (w *LinearLayout) SetX(x int) { w.x = x }
|
|
func (w *LinearLayout) SetY(y int) { w.y = y }
|
|
func (w *LinearLayout) GetX() int { return w.x }
|
|
func (w *LinearLayout) GetY() int { return w.y }
|
|
func (w *LinearLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *LinearLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *LinearLayout) GetW() int { return w.w }
|
|
func (w *LinearLayout) GetH() int { return w.h }
|
|
func (w *LinearLayout) SetW(wd int) { w.w = wd }
|
|
func (w *LinearLayout) SetH(h int) { w.h = h }
|
|
func (w *LinearLayout) getSize() Coord { return Coord{X: w.w, Y: w.h} }
|
|
func (w *LinearLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
|
func (w *LinearLayout) WantW() int {
|
|
var wantW int
|
|
for _, wd := range w.widgets {
|
|
switch w.orientation {
|
|
case LinLayV:
|
|
// Find the highest want of all widgets
|
|
wantW = wh.Max(wd.WantW(), wantW)
|
|
case LinLayH:
|
|
// Find the sum of all widgets wants
|
|
wantW = wantW + wd.WantW()
|
|
}
|
|
}
|
|
return wantW
|
|
}
|
|
|
|
func (w *LinearLayout) WantH() int {
|
|
var wantH int
|
|
for _, wd := range w.widgets {
|
|
switch w.orientation {
|
|
case LinLayV:
|
|
// Find the sum of all widgets wants
|
|
wantH = wantH + wd.WantH()
|
|
case LinLayH:
|
|
// Find the highest want of all widgets
|
|
wantH = wh.Max(wd.WantH(), wantH)
|
|
}
|
|
}
|
|
return wantH
|
|
}
|
|
|
|
func (w *LinearLayout) MinW() int {
|
|
var minW int
|
|
for _, wd := range w.widgets {
|
|
switch w.orientation {
|
|
case LinLayV:
|
|
// Find the highest minimum width of all widgets
|
|
minW = wh.Max(wd.MinW(), minW)
|
|
case LinLayH:
|
|
// Find the sum of all widget minimum widgets
|
|
minW = minW + wd.MinW()
|
|
}
|
|
}
|
|
return minW
|
|
}
|
|
|
|
func (w *LinearLayout) MinH() int {
|
|
var minH int
|
|
for _, wd := range w.widgets {
|
|
switch w.orientation {
|
|
case LinLayV:
|
|
minH = minH + wd.MinH()
|
|
case LinLayH:
|
|
minH = wh.Max(wd.MinH(), minH)
|
|
}
|
|
}
|
|
return minH
|
|
}
|
|
|
|
// Find the currently active widget, there should be only one.
|
|
func (w *LinearLayout) findActive() Widget {
|
|
for i := range w.widgets {
|
|
if w.widgets[i].Active() {
|
|
return w.widgets[i]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *LinearLayout) findActiveOrFirst() Widget {
|
|
if act := w.findActive(); act != nil {
|
|
return act
|
|
}
|
|
// Didn't find one, return the first
|
|
if len(w.widgets) > 0 {
|
|
return w.widgets[0]
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *LinearLayout) activateNext() bool {
|
|
var found bool
|
|
for i := range w.widgets {
|
|
if found && w.widgets[i].Tabbable() {
|
|
w.Log("%s.activeNext Setting Next Active: %s", w.Id(), w.widgets[i].Id())
|
|
w.widgets[i].SetActive(true)
|
|
return true
|
|
} else if w.widgets[i].Active() {
|
|
found = true
|
|
w.widgets[i].SetActive(false)
|
|
}
|
|
}
|
|
w.Log("%s.activeNext Hit End", w.Id())
|
|
return false
|
|
}
|
|
|
|
func (w *LinearLayout) SetOrientation(o LinearLayoutOrient) { w.orientation = o }
|
|
func (w *LinearLayout) IndexOf(n Widget) int {
|
|
for i := range w.widgets {
|
|
if w.widgets[i] == n {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func (w *LinearLayout) Contains(n Widget) bool {
|
|
return w.IndexOf(n) >= 0
|
|
}
|
|
|
|
func (w *LinearLayout) Replace(n, with Widget) {
|
|
idx := w.IndexOf(n)
|
|
if idx == -1 {
|
|
// 'n' isn't in layout. Bail out.
|
|
return
|
|
}
|
|
pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n]
|
|
w.Delete(n)
|
|
w.Insert(with, idx)
|
|
w.layoutFlags[with], w.layoutWeights[with] = pFlags, pWeight
|
|
}
|
|
|
|
func (w *LinearLayout) Add(n Widget) {
|
|
if w.Contains(n) {
|
|
// If the widget is already in the layout, move it to the end
|
|
pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n]
|
|
w.Delete(n)
|
|
w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight
|
|
}
|
|
w.widgets = append(w.widgets, n)
|
|
// If we don't already have a weight set, set it to 1
|
|
if _, ok := w.layoutWeights[n]; !ok {
|
|
w.layoutWeights[n] = 1
|
|
}
|
|
w.updateTotalWeight()
|
|
}
|
|
|
|
func (w *LinearLayout) Insert(n Widget, idx int) {
|
|
if idx >= len(w.widgets) {
|
|
w.Add(n)
|
|
return
|
|
}
|
|
if pos := w.IndexOf(n); pos >= 0 {
|
|
if pos < idx {
|
|
idx--
|
|
}
|
|
// Preserve the flags & weight
|
|
pFlags, pWeight := w.layoutFlags[n], w.layoutWeights[n]
|
|
w.Delete(n)
|
|
w.layoutFlags[n], w.layoutWeights[n] = pFlags, pWeight
|
|
}
|
|
w.widgets = append(w.widgets[:idx], append([]Widget{n}, w.widgets[idx:]...)...)
|
|
// If we don't already have a weight set, set it to 1
|
|
if _, ok := w.layoutWeights[n]; !ok {
|
|
w.layoutWeights[n] = 1
|
|
}
|
|
w.updateTotalWeight()
|
|
}
|
|
|
|
func (w *LinearLayout) Delete(n Widget) {
|
|
for i := 0; i < len(w.widgets); i++ {
|
|
if w.widgets[i] == n {
|
|
w.DeleteIndex(i)
|
|
delete(w.layoutFlags, n)
|
|
delete(w.layoutWeights, n)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) DeleteIndex(idx int) {
|
|
if idx < len(w.widgets) {
|
|
p := w.widgets[idx]
|
|
w.widgets = append(w.widgets[:idx], w.widgets[idx+1:]...)
|
|
delete(w.layoutFlags, p)
|
|
delete(w.layoutWeights, p)
|
|
w.updateTotalWeight()
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) AddFlag(wd Widget, f LayoutFlag) {
|
|
if f.IsAlignH() {
|
|
w.layoutFlags[wd].ClearAlignH()
|
|
} else if f.IsAlignV() {
|
|
w.layoutFlags[wd].ClearAlignV()
|
|
}
|
|
w.layoutFlags[wd].Add(f)
|
|
}
|
|
|
|
func (w *LinearLayout) RemoveFlag(wd Widget, f LayoutFlag) {
|
|
// Removing an alignment flag centers that direction
|
|
if f.IsAlignH() {
|
|
w.layoutFlags[wd].ClearAlignH()
|
|
} else if f.IsAlignV() {
|
|
w.layoutFlags[wd].ClearAlignV()
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) SetWeight(wd Widget, wt int) {
|
|
if !w.Contains(wd) {
|
|
return
|
|
}
|
|
w.layoutWeights[wd] = wt
|
|
w.updateTotalWeight()
|
|
}
|
|
|
|
func (w *LinearLayout) SetDefaultFlags(f LayoutFlag) { w.defFlags = f }
|
|
|
|
func (w *LinearLayout) updateTotalWeight() {
|
|
w.totalWeight = 0
|
|
for _, v := range w.layoutWeights {
|
|
w.totalWeight += v
|
|
}
|
|
w.HandleResize(w.getSize().ResizeEvent())
|
|
}
|
|
|
|
func (w *LinearLayout) updateWidgetLayouts() {
|
|
switch w.orientation {
|
|
case LinLayV:
|
|
for _, wd := range w.widgets {
|
|
w.updateLLVWidgetSize(wd)
|
|
w.updateLLVWidgetPos(wd)
|
|
}
|
|
case LinLayH:
|
|
for _, wd := range w.widgets {
|
|
w.updateLLHWidgetSize(wd)
|
|
w.updateLLHWidgetPos(wd)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The Layout should have a static Size set at this point that we can use
|
|
// For now we're centering all views in the Layout (on both axes)
|
|
//
|
|
// We need to determine the allowed size of this widget so we can determine
|
|
// it's position
|
|
func (w *LinearLayout) updateLLVWidgetSize(wd Widget) {
|
|
rW := w.w
|
|
if w.stacked {
|
|
rW = wd.MinW()
|
|
} else {
|
|
if wd == w.widgets[len(w.widgets)-1] {
|
|
wrk := float64(w.w) / float64(w.totalWeight)
|
|
if wrk == float64((w.w / w.totalWeight)) {
|
|
rW -= 1
|
|
}
|
|
}
|
|
}
|
|
wd.HandleResize((&Coord{X: rW, Y: w.getWeightedH(wd)}).ResizeEvent())
|
|
}
|
|
|
|
func (w *LinearLayout) updateLLHWidgetSize(wd Widget) {
|
|
rH := w.h
|
|
if w.stacked {
|
|
rH = wd.MinH()
|
|
} else {
|
|
if wd == w.widgets[len(w.widgets)-1] {
|
|
wrk := float64(w.h) / float64(w.totalWeight)
|
|
if wrk == float64((w.h / w.totalWeight)) {
|
|
rH -= 1
|
|
}
|
|
}
|
|
}
|
|
c := &Coord{X: w.getWeightedW(wd), Y: rH}
|
|
w.Log("%s.updateLLHWidgetSize(%s) { %d, %d }", w.Id(), wd.Id(), c.X, c.Y)
|
|
wd.HandleResize(c.ResizeEvent())
|
|
}
|
|
|
|
// The Layout should have a static Size set at this point that we can use
|
|
// For now we're centering all views in the Layout (on both axes)
|
|
// TODO: Use LayoutFlags to determine alignment in each 'cell'
|
|
//
|
|
// The position and size of each widget before this should be correct
|
|
// This widget should also know its size by now. We just need to
|
|
// position it relative to the layout.
|
|
func (w *LinearLayout) updateLLVWidgetPos(wd Widget) {
|
|
c := Coord{}
|
|
for i := range w.widgets {
|
|
if w.widgets[i] == wd {
|
|
break
|
|
}
|
|
c.Y += w.getWeightedH(w.widgets[i])
|
|
}
|
|
|
|
// Do we have a layout flag for this widget?
|
|
var ok bool
|
|
var flgs LayoutFlag
|
|
if flgs, ok = w.layoutFlags[wd]; !ok {
|
|
if w.stacked { // If we're 'stacked', set things to top/left
|
|
flgs = LayoutFlag(LFAlignHLeft | LFAlignVTop)
|
|
} else {
|
|
flgs = w.defFlags
|
|
}
|
|
}
|
|
|
|
// c.Y is the top of this 'cell'
|
|
if wd.GetH() < w.getWeightedH(wd) {
|
|
// But we've got some extra space
|
|
switch flgs.AlignV() {
|
|
case LFAlignVBottom:
|
|
c.Y += w.getWeightedH(wd) - wd.GetH()
|
|
case LFAlignVCenter:
|
|
c.Y += (w.getWeightedH(wd) / 2) - (wd.GetH() / 2)
|
|
}
|
|
}
|
|
if wd.GetW() < w.w {
|
|
c.X = int((float64(w.w) / 2) - (float64(wd.GetW()) / 2))
|
|
} else {
|
|
c.X = 0
|
|
}
|
|
wd.SetPos(c)
|
|
}
|
|
|
|
func (w *LinearLayout) updateLLHWidgetPos(wd Widget) {
|
|
c := Coord{}
|
|
for i := range w.widgets {
|
|
if w.widgets[i] == wd {
|
|
if i > 0 {
|
|
c.X += 1
|
|
}
|
|
break
|
|
}
|
|
c.X += w.getWeightedW(w.widgets[i]) + 2
|
|
}
|
|
|
|
// Do we have a layout flag for this widget?
|
|
var ok bool
|
|
var flgs LayoutFlag
|
|
if flgs, ok = w.layoutFlags[wd]; !ok {
|
|
flgs = w.defFlags
|
|
}
|
|
|
|
// c.X is the left-most of this 'cell'
|
|
if wd.GetW() < w.getWeightedW(wd) {
|
|
// We have extra horizontal space.
|
|
switch flgs.AlignH() {
|
|
case LFAlignHRight:
|
|
c.X += w.getWeightedW(wd) - wd.GetW()
|
|
case LFAlignHCenter:
|
|
c.X += (w.getWeightedW(wd) / 2) - (wd.GetW() / 2)
|
|
}
|
|
}
|
|
c.Y = 0
|
|
if wd.GetW() < w.h {
|
|
// We have extra vertical space.
|
|
switch flgs.AlignV() {
|
|
case LFAlignVBottom:
|
|
c.Y = w.h - wd.GetH()
|
|
case LFAlignVCenter:
|
|
c.Y = int((float64(w.h) / 2) - (float64(wd.GetH()) / 2))
|
|
}
|
|
}
|
|
wd.SetPos(c)
|
|
}
|
|
|
|
func (w *LinearLayout) getWeightedH(wd Widget) int {
|
|
if !w.Contains(wd) {
|
|
return 0
|
|
} else if w.stacked {
|
|
return wd.MinH()
|
|
}
|
|
wght := w.layoutWeights[wd]
|
|
if wght == 0 {
|
|
wght = 1
|
|
}
|
|
retF := float64(w.h) * (float64(wght) / float64(w.totalWeight))
|
|
retI := int(retF)
|
|
if retF-float64(retI) >= 0.5 {
|
|
return retI + 1
|
|
} else {
|
|
return retI
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) getWeightedW(wd Widget) int {
|
|
if !w.Contains(wd) {
|
|
return 0
|
|
} else if w.stacked {
|
|
return wd.MinW()
|
|
}
|
|
wght := w.layoutWeights[wd]
|
|
if wght == 0 {
|
|
wght = 1
|
|
}
|
|
retF := float64(w.w) * (float64(wght) / float64(w.totalWeight))
|
|
retI := int(retF)
|
|
if retF-float64(retI) >= 0.5 {
|
|
return retI + 1
|
|
} else {
|
|
return retI
|
|
}
|
|
}
|
|
|
|
func (w *LinearLayout) SetStacked(s bool) { w.stacked = s }
|
|
func (w *LinearLayout) SetBordered(b bool) { w.insetBorder = b }
|
|
func (w *LinearLayout) SetLogger(l func(string, ...any)) { w.logger = l }
|
|
func (w *LinearLayout) Log(txt string, args ...any) {
|
|
if w.logger != nil {
|
|
txt = fmt.Sprintf("%s:%s", time.Now().Format(time.TimeOnly), txt)
|
|
w.logger(txt, args...)
|
|
}
|
|
}
|