Files
tcell-widgets/wdgt_button.go
2025-09-25 09:49:52 -05:00

163 lines
5.0 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"
"strings"
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
"github.com/gdamore/tcell"
)
type Button struct {
id string
label string
style tcell.Style
x, y int
w, h int
active bool
visible bool
focusable bool
keyMap KeyMap
onPressed func() bool
logger func(string, ...any)
}
var _ Widget = (*Button)(nil)
func NewButton(id string, style tcell.Style) *Button {
b := &Button{}
b.Init(id, style)
return b
}
func (w *Button) Init(id string, style tcell.Style) {
w.id = id
w.style = style
w.visible = true
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
tcell.KeyEnter: func(ev *tcell.EventKey) bool { return w.onPressed() },
})
w.onPressed = func() bool { return false }
w.focusable = true
}
func (w *Button) Id() string { return w.id }
func (w *Button) HandleResize(ev *tcell.EventResize) {
w.w, w.h = ev.Size()
}
func (w *Button) SetKeyMap(km KeyMap) { w.keyMap = km }
func (w *Button) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
func (w *Button) RemoveFromKeyMap(km KeyMap) {
for k := range km.Keys {
w.keyMap.Remove(k)
}
for r := range km.Runes {
w.keyMap.RemoveRune(r)
}
}
func (w *Button) HandleKey(ev *tcell.EventKey) bool {
if !w.active {
return false
}
return w.keyMap.Handle(ev)
}
func (w *Button) HandleTime(ev *tcell.EventTime) {}
func (w *Button) Draw(screen tcell.Screen) {
if !w.visible {
return
}
dStyle := w.style.Dim(!w.active)
switch w.h {
case 1:
lbl := w.label
if w.w < len(lbl) {
lbl = lbl[:w.w]
} else if w.w == len(w.label)+2 {
lbl = fmt.Sprintf("[%s]", lbl)
} else if w.w > len(w.label)+2 {
lbl = fmt.Sprintf("[%s]", wh.Center(lbl, w.w-2))
}
wh.DrawText(w.x, w.y, lbl, dStyle, screen)
return
case 2:
lbl := w.label
if w.w < len(lbl) {
lbl = lbl[:w.w]
} else if w.w == len(lbl)+2 {
lbl = fmt.Sprintf("╭%s╮", lbl)
}
wh.DrawText(w.x, w.y, lbl, dStyle, screen)
wh.DrawText(w.x, w.y+1, fmt.Sprintf("╰%s╯", strings.Repeat("─", len(lbl)-2)), dStyle, screen)
return
}
if w.w < 2 {
wh.DrawText(w.x, w.y, "╬", dStyle, screen)
return
} else if w.w == 2 {
wh.DrawText(w.x, w.y, "[]", dStyle, screen)
return
}
lbl := wh.Center(w.label, w.w-2)
wh.DrawText(w.x, w.y, fmt.Sprintf("╭%s╮", strings.Repeat("─", w.w-2)), dStyle, screen)
wh.DrawText(w.x, w.y+1, fmt.Sprintf("│%s│", wh.Center(lbl, w.w-2)), dStyle, screen)
wh.DrawText(w.x, w.y+2, fmt.Sprintf("╰%s╯", strings.Repeat("─", w.w-2)), dStyle, screen)
}
func (w *Button) Active() bool { return w.active }
func (w *Button) SetActive(a bool) { w.active = a }
func (w *Button) Visible() bool { return w.visible }
func (w *Button) SetVisible(a bool) { w.visible = a }
func (w *Button) SetX(x int) { w.x = x }
func (w *Button) SetY(y int) { w.y = y }
func (w *Button) GetX() int { return w.x }
func (w *Button) GetY() int { return w.y }
func (w *Button) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
func (w *Button) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
func (w *Button) SetW(x int) { w.w = x }
func (w *Button) SetH(y int) { w.h = y }
func (w *Button) GetW() int { return w.w }
func (w *Button) GetH() int { return w.h }
func (w *Button) WantW() int { return 4 + len(w.label) }
func (w *Button) WantH() int { return 3 }
func (w *Button) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
func (w *Button) Focusable() bool { return w.focusable }
func (w *Button) SetFocusable(b bool) { w.focusable = b }
func (w *Button) MinW() int { return len(w.label) + 2 }
func (w *Button) MinH() int { return 1 }
func (w *Button) SetLabel(l string) { w.label = l }
func (w *Button) SetOnPressed(p func() bool) { w.onPressed = p }
func (w *Button) SetLogger(l func(string, ...any)) { w.logger = l }
func (w *Button) Log(txt string, args ...any) {
if w.logger != nil {
w.logger(txt, args...)
}
}