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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
|
// 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 menu is a widget that presents a drop-down menu.
package menu
import (
"fmt"
"github.com/gcla/gowid"
"github.com/gcla/gowid/widgets/holder"
"github.com/gcla/gowid/widgets/null"
"github.com/gcla/gowid/widgets/overlay"
tcell "github.com/gdamore/tcell/v2"
)
//======================================================================
type IWidget interface {
gowid.ICompositeWidget
Overlay() overlay.IWidgetSettable
Open(ISite, gowid.IApp)
Close(gowid.IApp)
IsOpen() bool
CloseKeys() []gowid.IKey // Keys that should close the current submenu (e.g. left arrow)
IgnoreKeys() []gowid.IKey // Keys that shouldn't close submenu but should be passed back to main app
AutoClose() bool
SetAutoClose(bool, gowid.IApp)
Width() gowid.IWidgetDimension
SetWidth(gowid.IWidgetDimension, gowid.IApp)
Name() string
}
type IOpener interface {
OpenMenu(*Widget, ISite, gowid.IApp) bool
CloseMenu(*Widget, gowid.IApp)
}
type Options struct {
CloseKeysProvided bool
CloseKeys []gowid.IKey
IgnoreKeysProvided bool
IgnoreKeys []gowid.IKey
NoAutoClose bool
Modal bool
OpenCloser IOpener
}
var (
DefaultIgnoreKeys = []gowid.IKey{
gowid.MakeKeyExt(tcell.KeyLeft),
gowid.MakeKeyExt(tcell.KeyRight),
gowid.MakeKeyExt(tcell.KeyUp),
gowid.MakeKeyExt(tcell.KeyDown),
}
DefaultCloseKeys = []gowid.IKey{
gowid.MakeKeyExt(tcell.KeyLeft),
gowid.MakeKeyExt(tcell.KeyEscape),
}
)
// Widget overlays one widget on top of another. The bottom widget
// is rendered without the focus at full size. The bottom widget is
// rendered between a horizontal and vertical padding widget set up with
// the sizes provided.
type Widget struct {
overlay *overlay.Widget // So that I can set the "top" widget in the overlay to "open" the menu
baseHolder *holder.Widget // Holds the actual base widget
modal *rejectKeyInput // Allow/disallow keys to lower when menu is open
top *NavWrapperWidget // So that I can reinstate it in the overlay to "open" the menu
name string // The name uses for the canvas anchor
site ISite // If open, provides the name of the canvas anchor at which the top widget is rendered
width gowid.IWidgetDimension // For rendering the top widget
autoClose bool // If true, then close the menu if it was open, and another widget takes the input
opts Options
Callbacks *gowid.Callbacks
}
type rejectKeyInput struct {
gowid.IWidget
on bool
}
// New takes a widget, rather than a menu model, so that I can potentially style
// the menu. TODO - consider adding styling to menu model?
//
func New(name string, menuw gowid.IWidget, width gowid.IWidgetDimension, opts ...Options) *Widget {
var opt Options
if len(opts) > 0 {
opt = opts[0]
}
if opt.OpenCloser == nil {
opt.OpenCloser = OpenerFunc(OpenSimple)
}
res := &Widget{
name: name,
width: width,
autoClose: !opt.NoAutoClose,
opts: opt,
Callbacks: gowid.NewCallbacks(),
}
var _ IWidget = res
var _ ISiteName = res
baseHolder := holder.New(null.New())
closerBase := &rejectKeyInput{
IWidget: baseHolder,
}
// We don't have the base widget at this point
base := &AutoCloserWidget{
IWidget: closerBase,
menu: res,
}
// // Makes sure submenu doesn't take keyboard input unless it is "selected"
top := &NavWrapperWidget{menuw, res}
ov := overlay.New(
nil, base,
gowid.VAlignTop{0}, gowid.RenderFixed{}, //gowid.RenderWithRatio{1.0},
gowid.HAlignLeft{}, width,
overlay.Options{
BottomGetsFocus: true,
},
)
res.overlay = ov
res.baseHolder = baseHolder
res.modal = closerBase
res.top = top
return res
}
func (w *Widget) String() string {
return fmt.Sprintf("menu[%v]", w.Name())
}
func (w *Widget) AutoClose() bool {
return w.autoClose
}
func (w *Widget) SetAutoClose(autoClose bool, app gowid.IApp) {
w.autoClose = autoClose
}
func (w *Widget) Width() gowid.IWidgetDimension {
return w.overlay.Width()
}
func (w *Widget) SetWidth(width gowid.IWidgetDimension, app gowid.IApp) {
w.overlay.SetWidth(width, app)
}
func (w *Widget) Height() gowid.IWidgetDimension {
return w.overlay.Height()
}
func (w *Widget) SetHeight(width gowid.IWidgetDimension, app gowid.IApp) {
w.overlay.SetHeight(width, app)
}
func (w *Widget) Name() string {
return w.name
}
func (w *Widget) Open(site ISite, app gowid.IApp) {
w.opts.OpenCloser.OpenMenu(w, site, app)
}
func (w *Widget) OpenImpl(site ISite, app gowid.IApp) {
w.site = site
site.SetNamer(w, app)
w.overlay.SetTop(w.top, app)
if w.opts.Modal {
w.modal.on = true
}
}
func (w *Widget) Close(app gowid.IApp) {
w.opts.OpenCloser.CloseMenu(w, app)
}
func (w *Widget) CloseImpl(app gowid.IApp) {
// protect against case where it's closed already
if w.site != nil {
w.site.SetNamer(nil, app)
w.site = nil
}
w.overlay.SetTop(nil, app)
w.modal.on = false
}
func (w *Widget) Overlay() overlay.IWidgetSettable {
return w.overlay
}
func (w *Widget) SetSubWidget(widget gowid.IWidget, app gowid.IApp) {
w.baseHolder.IWidget = widget
}
func (w *Widget) SubWidget() gowid.IWidget {
return w.baseHolder.IWidget
}
func (w *Widget) SubWidgetSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderSize {
return w.RenderSize(size, focus, app)
}
func (w *Widget) IsOpen() bool {
return w.overlay.Top() != nil
}
func (w *Widget) CloseKeys() []gowid.IKey {
closeKeys := w.opts.CloseKeys
if !w.opts.CloseKeysProvided && len(w.opts.CloseKeys) == 0 {
closeKeys = DefaultCloseKeys
}
return closeKeys
}
func (w *Widget) IgnoreKeys() []gowid.IKey {
ignoreKeys := w.opts.IgnoreKeys
if !w.opts.IgnoreKeysProvided && len(w.opts.IgnoreKeys) == 0 {
ignoreKeys = DefaultIgnoreKeys
}
return ignoreKeys
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return gowid.CalculateRenderSizeFallback(w, size, focus, app)
}
func (w *Widget) Selectable() bool {
return true
}
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) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return Render(w, size, focus, app)
}
func (w *rejectKeyInput) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
if _, ok := ev.(*tcell.EventKey); ok && w.on {
return false
}
if _, ok := ev.(*tcell.EventPaste); ok && w.on {
return false
}
return w.IWidget.UserInput(ev, size, focus, app)
}
//======================================================================
type CachedWidget struct {
gowid.IWidget
c gowid.ICanvas
}
func (w *CachedWidget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return w.c
}
type CachedOverlay struct {
overlay.IWidget
c gowid.ICanvas
}
func (w *CachedOverlay) Bottom() gowid.IWidget {
return &CachedWidget{w.IWidget.Bottom(), w.c}
}
func (w *CachedOverlay) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return overlay.Render(w, size, focus, app)
}
//======================================================================
func UserInput(w IWidget, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
return gowid.UserInputIfSelectable(w.Overlay(), ev, size, focus, app)
}
func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
bfocus := focus.And(w.Overlay().BottomGetsFocus())
bottomC := w.Overlay().Bottom().Render(size, bfocus, app)
off, ok := bottomC.GetMark(w.Name())
if !ok {
// Means menu is closed
return bottomC
}
w.Overlay().SetVAlign(gowid.VAlignTop{off.Y}, app)
w.Overlay().SetHAlign(gowid.HAlignLeft{Margin: off.X}, app)
// So we don't need to render the bottom canvas twice
fakeOverlay := &CachedOverlay{w.Overlay(), bottomC}
return fakeOverlay.Render(size, focus, app)
}
//======================================================================
type ISiteName interface {
Name() string
}
type ISite interface {
Namer() ISiteName
SetNamer(ISiteName, gowid.IApp)
}
// SiteWidget is a zero-width widget which acts as the coordinates at which a submenu will open
type SiteWidget struct {
gowid.IWidget
Options SiteOptions
}
type SiteOptions struct {
Namer ISiteName
XOffset int
YOffset int
}
func NewSite(opts ...SiteOptions) *SiteWidget {
var opt SiteOptions
if len(opts) > 0 {
opt = opts[0]
}
res := &SiteWidget{
IWidget: null.New(),
Options: opt,
}
var _ gowid.IWidget = res
var _ ISite = res
return res
}
func (w *SiteWidget) Selectable() bool {
return false
}
func (w *SiteWidget) Namer() ISiteName {
return w.Options.Namer
}
func (w *SiteWidget) SetNamer(m ISiteName, app gowid.IApp) {
w.Options.Namer = m
}
func (w *SiteWidget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
res := w.IWidget.Render(size, focus, app)
if w.Options.Namer != nil {
res.SetMark(w.Options.Namer.Name(), w.Options.XOffset, w.Options.YOffset)
}
return res
}
func (w *SiteWidget) String() string {
return fmt.Sprintf("menusite[->%v]", w.Options.Namer)
}
//======================================================================
// AutoCloserWidget is used to detect if a given menu is open when a widget responds to user
// input. Then some action can be taken after that user input (e.g. closing the menu)
type AutoCloserWidget struct {
gowid.IWidget
menu IWidget
}
func (w *AutoCloserWidget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
wasOpen := w.menu.IsOpen()
res := w.IWidget.UserInput(ev, size, focus, app)
// Close the menu if it was open prior to this input operation (i.e. not just opened) and
// if the non-menu part of the UI took the current input - but only if the input was mouse.
// This makes it harder to accidentally close the menu by hitting e.g. a right cursor key and
// it not being accepted by the menu, instead being accepted by the base widget
if w.menu.AutoClose() && wasOpen && res {
if _, ok := ev.(*tcell.EventMouse); ok {
w.menu.Close(app)
}
}
return res
}
//======================================================================
// NavWrapperWidget is used to detect if a given menu is open when a widget responds to user
// input. Then some action can be taken after that user input (e.g. closing the menu)
type NavWrapperWidget struct {
gowid.IWidget
menu IWidget
//index int
}
func (w *NavWrapperWidget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
res := false
// if _, ok := ev.(*tcell.EventKey); ok {
// if w.index != w.menu.Active() {
// return res
// }
// }
// Test the subwidget first. It might want to capture certain keys
res = w.IWidget.UserInput(ev, size, focus, app)
// If the submenu itself didn't claim the input, check the close keys
if !res {
if evk, ok := ev.(*tcell.EventKey); ok {
for _, k := range w.menu.CloseKeys() {
if gowid.KeysEqual(k, evk) {
w.menu.Close(app)
res = true
break
}
}
if !res {
for _, k := range w.menu.IgnoreKeys() {
if gowid.KeysEqual(k, evk) {
res = true
break
}
}
}
}
}
return res
}
//======================================================================
// Return false if it was already open
type OpenerFunc func(bool, *Widget, ISite, gowid.IApp) bool
func (m OpenerFunc) OpenMenu(mu *Widget, site ISite, app gowid.IApp) bool {
return m(true, mu, site, app)
}
func (m OpenerFunc) CloseMenu(mu *Widget, app gowid.IApp) {
m(false, mu, nil, app)
}
func OpenSimple(open bool, mu *Widget, site ISite, app gowid.IApp) bool {
if open {
mu.OpenImpl(site, app)
return true
} else {
mu.CloseImpl(app)
return true
}
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|