132 lines
4.1 KiB
Go
132 lines
4.1 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"
|
|
|
|
h "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
|
|
|
|
onPressed func() bool
|
|
}
|
|
|
|
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.onPressed = func() bool { return false }
|
|
}
|
|
func (w *Button) Id() string { return w.id }
|
|
func (w *Button) HandleResize(ev *tcell.EventResize) {}
|
|
func (w *Button) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
if ev.Key() == tcell.KeyEnter {
|
|
return w.onPressed()
|
|
}
|
|
return false
|
|
}
|
|
func (w *Button) HandleTime(ev *tcell.EventTime) {}
|
|
func (w *Button) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
dStyle := w.style
|
|
if w.active {
|
|
dStyle = w.style.Bold(true)
|
|
}
|
|
if w.h == 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]", h.Center(lbl, w.w-2))
|
|
}
|
|
h.DrawText(w.x, w.y, lbl, dStyle, screen)
|
|
} else if w.h == 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)
|
|
}
|
|
h.DrawText(w.x, w.y, lbl, dStyle, screen)
|
|
h.DrawText(w.x, w.y+1, fmt.Sprintf("╰%s╯", strings.Repeat("─", len(lbl)-2)), dStyle, screen)
|
|
return
|
|
}
|
|
if w.w < 2 {
|
|
h.DrawText(w.x, w.y, "╬", dStyle, screen)
|
|
return
|
|
} else if w.w == 2 {
|
|
h.DrawText(w.x, w.y, "[]", dStyle, screen)
|
|
return
|
|
}
|
|
lbl := h.Center(w.label, w.w-2)
|
|
h.DrawText(w.x, w.y, fmt.Sprintf("╭%s╮", strings.Repeat("─", w.w-2)), dStyle, screen)
|
|
h.DrawText(w.x, w.y, fmt.Sprintf("│%s│", h.Center(lbl, w.w-2)), dStyle, screen)
|
|
h.DrawText(w.x, w.y, 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) 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.y }
|
|
func (w *Button) WantW() int { return 2 + 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 true }
|
|
|
|
func (w *Button) SetLabel(l string) { w.label = l }
|
|
func (w *Button) SetOnPressed(p func() bool) { w.onPressed = p }
|