1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright 2019-2022 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
// Package button provides a clickable widget which can be decorated.
package button
import (
"fmt"
"github.com/gcla/gowid"
"github.com/gcla/gowid/gwutil"
tcell "github.com/gdamore/tcell/v2"
)
//======================================================================
// IDecoratedAround is the interface for any type that provides
// "decoration" on its left and right side e.g. for buttons, something like
// "<" and ">".
type IDecoratedAround interface {
LeftDec() string
RightDec() string
}
// IDecoratedMiddle is implemented by any type that provides "decoration"
// in the middle of its render, such as a 'x' or a '-' symbol on a checked
// button.
type IDecoratedMiddle interface {
MiddleDec() string
}
//======================================================================
// Decoration is a simple struct that implements IDecoratedAround.
type Decoration struct {
Left string
Right string
}
func (b *Decoration) LeftDec() string {
return b.Left
}
func (b *Decoration) RightDec() string {
return b.Right
}
func (w *Decoration) SetLeftDec(dec string, app gowid.IApp) {
w.Left = dec
}
func (w *Decoration) SetRightDec(dec string, app gowid.IApp) {
w.Right = dec
}
var (
BareDecoration = Decoration{Left: "", Right: ""}
NormalDecoration = Decoration{Left: "<", Right: ">"}
AltDecoration = Decoration{Left: "[", Right: "]"}
)
//======================================================================
type ICustomKeys interface {
CustomSelectKeys() bool
SelectKeys() []gowid.IKey // can't be nil
}
// IWidget is implemented by any widget that contains exactly one
// exposed subwidget (ICompositeWidget) and that is decorated on its left
// and right (IDecoratedAround).
type IWidget interface {
gowid.ICompositeWidget
IDecoratedAround
}
type Options struct {
Decoration
SelectKeysProvided bool
SelectKeys []gowid.IKey
}
type Widget struct {
inner gowid.IWidget
opts Options
*gowid.Callbacks
gowid.SubWidgetCallbacks
gowid.ClickCallbacks
*Decoration
gowid.AddressProvidesID
gowid.IsSelectable
}
func New(inner gowid.IWidget, opts ...Options) *Widget {
var opt Options
if len(opts) > 0 {
opt = opts[0]
} else {
// Make the default have visible decorators, if none are provided explicitly.
opt.Decoration = NormalDecoration
}
res := &Widget{
inner: inner,
opts: opt,
}
res.SubWidgetCallbacks = gowid.SubWidgetCallbacks{CB: &res.Callbacks}
res.ClickCallbacks = gowid.ClickCallbacks{CB: &res.Callbacks}
res.Decoration = &res.opts.Decoration
var _ gowid.IWidget = res
var _ gowid.ICompositeWidget = res
var _ IWidget = res
var _ ICustomKeys = res
return res
}
func NewAlt(inner gowid.IWidget) *Widget {
return New(inner, Options{
Decoration: AltDecoration,
})
}
func NewBare(inner gowid.IWidget) *Widget {
return New(inner, Options{
Decoration: BareDecoration,
})
}
func NewDecorated(inner gowid.IWidget, decoration Decoration) *Widget {
return New(inner, Options{
Decoration: decoration,
})
}
func (w *Widget) String() string {
return fmt.Sprintf("button[%v]", w.SubWidget())
}
func (w *Widget) Click(app gowid.IApp) {
// No button clicked means a key was pressed
if app.GetMouseState().NoButtonClicked() || app.GetMouseState().LeftIsClicked() {
gowid.RunWidgetCallbacks(w.Callbacks, gowid.ClickCB{}, app, w)
}
}
func (w *Widget) SubWidget() gowid.IWidget {
return w.inner
}
func (w *Widget) SetSubWidget(wi gowid.IWidget, app gowid.IApp) {
w.inner = wi
gowid.RunWidgetCallbacks(w.Callbacks, gowid.SubWidgetCB{}, app, w)
}
func (w *Widget) SetLeftDec(dec string, app gowid.IApp) {
w.Decoration.Left = dec
}
func (w *Widget) SetRightDec(dec string, app gowid.IApp) {
w.Decoration.Right = dec
}
func (w *Widget) SubWidgetSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderSize {
return SubWidgetSize(w, size, focus, app)
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return RenderSize(w, size, focus, app)
}
func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return Render(w, size, focus, app)
}
func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
return UserInput(w, ev, size, focus, app)
}
func (w *Widget) CustomSelectKeys() bool {
return w.opts.SelectKeysProvided
}
func (w *Widget) SelectKeys() []gowid.IKey {
return w.opts.SelectKeys
}
//======================================================================
func SubWidgetSize(w IWidget, size interface{}, focus gowid.Selector, app gowid.IApp) gowid.IRenderSize {
cols, haveCols := size.(gowid.IColumns)
rows, haveRows := size.(gowid.IRows)
switch {
case haveCols && haveRows:
return gowid.RenderBox{C: gwutil.Max(0, cols.Columns()-(len(w.LeftDec())+len(w.RightDec()))), R: rows.Rows()}
case haveCols:
return gowid.RenderFlowWith{C: gwutil.Max(0, cols.Columns()-(len(w.LeftDec())+len(w.RightDec())))}
default:
return gowid.RenderFixed{}
}
}
func RenderSize(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
innerSize := w.SubWidgetSize(size, focus, app)
innerRendered := w.SubWidget().RenderSize(innerSize, focus, app)
boxHeight := innerRendered.BoxRows()
boxWidth := innerRendered.BoxColumns() + len(w.LeftDec()) + len(w.RightDec())
if bsz, ok := size.(gowid.IColumns); ok {
if bsz.Columns() < boxWidth {
boxWidth = bsz.Columns()
}
}
return gowid.RenderBox{boxWidth, boxHeight}
}
type IClickableIdentityWidget interface {
gowid.IClickableWidget
gowid.IIdentity
}
func UserInput(w IClickableIdentityWidget, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
res := false
switch ev := ev.(type) {
case *tcell.EventMouse:
switch ev.Buttons() {
case tcell.Button1, tcell.Button2, tcell.Button3:
app.SetClickTarget(ev.Buttons(), w)
res = true
case tcell.ButtonNone:
if !app.GetLastMouseState().NoButtonClicked() {
clickit := false
app.ClickTarget(func(k tcell.ButtonMask, v gowid.IIdentityWidget) {
if v != nil && v.ID() == w.ID() {
clickit = true
}
})
if clickit {
w.Click(app)
res = true
}
}
}
case *tcell.EventKey:
if wk, ok := w.(ICustomKeys); ok && wk.CustomSelectKeys() {
for _, k := range wk.SelectKeys() {
if gowid.KeysEqual(k, ev) {
w.Click(app)
res = true
break
}
}
} else {
if ev.Key() == tcell.KeyEnter || (ev.Key() == tcell.KeyRune && ev.Rune() == ' ') {
w.Click(app)
res = true
}
}
default:
if wc, ok := w.(gowid.IComposite); ok {
res = wc.SubWidget().UserInput(ev, size, focus, app)
}
}
return res
}
func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
newSize := w.SubWidgetSize(size, focus, app)
res := w.SubWidget().Render(newSize, focus, app)
leftClicker := gowid.CellsFromString(w.LeftDec())
rightClicker := gowid.CellsFromString(w.RightDec())
res.ExtendLeft(leftClicker)
res.ExtendRight(rightClicker)
gowid.MakeCanvasRightSize(res, size)
return res
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|