319 lines
7.6 KiB
Go
319 lines
7.6 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"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
type Form struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
cursor int
|
|
visible bool
|
|
active bool
|
|
focusable bool
|
|
x, y int
|
|
w, h int
|
|
|
|
title string
|
|
border []rune
|
|
fields []Widget
|
|
|
|
submit *Button
|
|
cancel *Button
|
|
hasSubmit bool
|
|
hasCancel bool
|
|
|
|
keyMap, customKeyMap KeyMap
|
|
}
|
|
|
|
var _ Widget = (*Form)(nil)
|
|
|
|
func NewForm(id string, style tcell.Style) *Form {
|
|
f := &Form{}
|
|
f.Init(id, style)
|
|
return f
|
|
}
|
|
|
|
func (w *Form) Init(id string, style tcell.Style) {
|
|
w.id = id
|
|
w.style = style
|
|
w.visible = true
|
|
w.focusable = true
|
|
w.submit = NewButton(fmt.Sprintf("%s-submit", id), style)
|
|
w.cancel = NewButton(fmt.Sprintf("%s-cancel", id), style)
|
|
w.keyMap = BlankKeyMap()
|
|
w.customKeyMap = BlankKeyMap()
|
|
}
|
|
|
|
func (w *Form) Id() string { return w.id }
|
|
func (w *Form) HandleResize(ev *tcell.EventResize) {
|
|
w.w, w.h = ev.Size()
|
|
y := 0
|
|
for i := range w.fields {
|
|
w.fields[i].SetPos(Coord{X: 0, Y: y})
|
|
wantH := w.fields[i].WantH()
|
|
w.fields[i].HandleResize(Coord{X: w.w, Y: wantH}.ResizeEvent())
|
|
y += wantH
|
|
}
|
|
half := w.w / 2
|
|
if w.hasCancel && w.hasSubmit {
|
|
w.cancel.SetPos(Coord{X: 0, Y: y})
|
|
w.cancel.SetSize(Coord{X: half, Y: 3})
|
|
w.submit.SetPos(Coord{X: 0, Y: y})
|
|
w.submit.SetSize(Coord{X: half, Y: 3})
|
|
} else if w.hasCancel {
|
|
w.cancel.SetPos(Coord{X: half, Y: y})
|
|
w.cancel.SetSize(Coord{X: half, Y: 3})
|
|
} else if w.hasSubmit {
|
|
w.submit.SetPos(Coord{X: w.w / 2, Y: y})
|
|
w.submit.SetSize(Coord{X: half, Y: 3})
|
|
}
|
|
}
|
|
|
|
func (w *Form) SetKeyMap(km KeyMap, def bool) {
|
|
if def {
|
|
w.keyMap = km
|
|
} else {
|
|
w.customKeyMap = km
|
|
}
|
|
}
|
|
func (w *Form) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
|
|
func (w *Form) RemoveFromKeyMap(km KeyMap) {
|
|
for k := range km.Keys {
|
|
w.customKeyMap.Remove(k)
|
|
}
|
|
for r := range km.Runes {
|
|
w.customKeyMap.RemoveRune(r)
|
|
}
|
|
}
|
|
|
|
func (w *Form) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
if w.getActive().HandleKey(ev) {
|
|
return true
|
|
}
|
|
if ev.Key() == tcell.KeyTab {
|
|
fldCnt := len(w.fields)
|
|
if w.hasCancel {
|
|
fldCnt += 1
|
|
}
|
|
if w.hasSubmit {
|
|
fldCnt += 1
|
|
}
|
|
pre := w.cursor
|
|
w.cursor = (w.cursor + 1) % fldCnt
|
|
w.updateWidgets()
|
|
return w.cursor > pre
|
|
}
|
|
b1 := w.keyMap.Handle(ev)
|
|
b2 := w.customKeyMap.Handle(ev)
|
|
return b1 || b2
|
|
}
|
|
func (w *Form) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Form) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
useStyle := w.style.Dim(!w.active)
|
|
if len(w.title) > 0 {
|
|
wh.TitledBorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.title, w.border, useStyle, screen)
|
|
} else {
|
|
wh.BorderFilled(w.x, w.y, w.x+w.w, w.y+w.h, w.border, useStyle, screen)
|
|
}
|
|
p := w.GetPos()
|
|
for _, wdgt := range w.fields {
|
|
if !w.Active() {
|
|
wdAct := wdgt.Active()
|
|
wdgt.SetActive(false)
|
|
p.DrawOffset(wdgt, screen)
|
|
wdgt.SetActive(wdAct)
|
|
} else {
|
|
p.DrawOffset(wdgt, screen)
|
|
}
|
|
}
|
|
if w.hasCancel {
|
|
p.DrawOffset(w.cancel, screen)
|
|
}
|
|
if w.hasSubmit {
|
|
p.DrawOffset(w.submit, screen)
|
|
}
|
|
}
|
|
|
|
func (w *Form) Active() bool { return w.active }
|
|
func (w *Form) SetActive(a bool) {
|
|
w.active = a
|
|
w.updateWidgets()
|
|
}
|
|
func (w *Form) Visible() bool { return w.visible }
|
|
func (w *Form) SetVisible(a bool) { w.visible = a }
|
|
func (w *Form) SetX(x int) { w.x = x }
|
|
func (w *Form) SetY(y int) { w.y = y }
|
|
func (w *Form) GetX() int { return w.x }
|
|
func (w *Form) GetY() int { return w.y }
|
|
func (w *Form) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *Form) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *Form) SetW(wd int) { w.w = wd }
|
|
func (w *Form) SetH(h int) { w.h = h }
|
|
func (w *Form) GetW() int { return w.w }
|
|
func (w *Form) GetH() int { return w.h }
|
|
func (w *Form) WantW() int {
|
|
var ret int
|
|
if len(w.border) > 0 {
|
|
ret += 2
|
|
}
|
|
maxFld := wh.MaxFor(func(f Widget) int { return f.WantW() }, w.fields...)
|
|
ret += maxFld.WantW()
|
|
|
|
return 1
|
|
}
|
|
|
|
func (w *Form) WantH() int {
|
|
var ret int
|
|
if len(w.border) > 0 {
|
|
ret += 2
|
|
}
|
|
for i := range w.fields {
|
|
ret += w.fields[i].WantH()
|
|
}
|
|
if w.hasSubmit || w.hasCancel {
|
|
ret += 3
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func (w *Form) SetSize(c Coord) {
|
|
w.SetW(c.X)
|
|
w.SetH(c.Y)
|
|
}
|
|
func (w *Form) Focusable() bool { return w.focusable }
|
|
func (w *Form) SetFocusable(b bool) { w.focusable = b }
|
|
func (w *Form) MinW() int { return 1 }
|
|
func (w *Form) MinH() int { return 1 }
|
|
|
|
// Non-Widget Functions
|
|
func (w *Form) updateWidgets() {
|
|
for i := 0; i < len(w.fields); i++ {
|
|
w.fields[i].SetActive(w.Active() && i == w.cursor)
|
|
}
|
|
if w.hasCancel {
|
|
w.cancel.SetActive(w.Active() && w.cursor == len(w.fields))
|
|
if w.hasSubmit {
|
|
w.submit.SetActive(w.Active() && w.cursor == len(w.fields)+1)
|
|
}
|
|
} else if w.hasSubmit {
|
|
w.submit.SetActive(w.Active() && w.cursor == len(w.fields))
|
|
}
|
|
}
|
|
|
|
func (w *Form) getActive() Widget {
|
|
if w.cursor < len(w.fields) {
|
|
return w.fields[w.cursor]
|
|
}
|
|
if w.cursor == len(w.fields) {
|
|
if w.hasCancel {
|
|
return w.cancel
|
|
}
|
|
if w.hasSubmit {
|
|
return w.submit
|
|
}
|
|
} else if w.cursor > len(w.fields) && w.hasSubmit {
|
|
return w.submit
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (w *Form) IndexOf(n Widget) int {
|
|
for i := range w.fields {
|
|
if w.fields[i] == n {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
func (w *Form) Contains(n Widget) bool { return w.IndexOf(n) >= 0 }
|
|
func (w *Form) Replace(n, with Widget) {
|
|
idx := w.IndexOf(n)
|
|
if idx == -1 {
|
|
return
|
|
}
|
|
w.Delete(n)
|
|
w.Insert(with, idx)
|
|
}
|
|
|
|
func (w *Form) Add(n Widget) {
|
|
if w.Contains(n) {
|
|
// If the widget is already in the layout, move it to the end
|
|
w.Delete(n)
|
|
}
|
|
w.fields = append(w.fields, n)
|
|
}
|
|
|
|
func (w *Form) Insert(n Widget, idx int) {
|
|
if idx >= len(w.fields) {
|
|
w.Add(n)
|
|
return
|
|
}
|
|
if pos := w.IndexOf(n); pos >= 0 {
|
|
if pos < idx {
|
|
idx--
|
|
}
|
|
w.Delete(n)
|
|
}
|
|
w.fields = append(w.fields[:idx], append([]Widget{n}, w.fields[idx:]...)...)
|
|
}
|
|
|
|
func (w *Form) Delete(n Widget) {
|
|
for i := 0; i < len(w.fields); i++ {
|
|
if w.fields[i] == n {
|
|
w.DeleteIndex(i)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (w *Form) DeleteIndex(idx int) {
|
|
if idx < len(w.fields) {
|
|
w.fields = append(w.fields[:idx], w.fields[idx+1:]...)
|
|
}
|
|
}
|
|
func (w *Form) Clear() { w.fields = []Widget{} }
|
|
|
|
func (w *Form) SetTitle(ttl string) { w.title = ttl }
|
|
func (w *Form) SetBorder(b []rune) { w.border = b }
|
|
|
|
func (w *Form) SetSubmitLabel(lbl string) { w.submit.SetLabel(lbl) }
|
|
func (w *Form) SetOnSubmit(sb func() bool) {
|
|
w.submit.SetOnPressed(sb)
|
|
w.hasSubmit = sb != nil
|
|
}
|
|
func (w *Form) SetCancelLabel(lbl string) { w.cancel.SetLabel(lbl) }
|
|
func (w *Form) SetOnCancel(sb func() bool) {
|
|
w.cancel.SetOnPressed(sb)
|
|
w.hasCancel = sb != nil
|
|
}
|