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
|
// 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 isselected provides a widget that acts differently if selected (or focused)
package isselected
import (
"fmt"
"github.com/gcla/gowid"
)
//======================================================================
type Widget struct {
Not gowid.IWidget // Must not be nil
Selected gowid.IWidget // If nil, then Not used
Focused gowid.IWidget // If nil, then Selected used
}
var _ gowid.IWidget = (*Widget)(nil)
func New(w1, w2, w3 gowid.IWidget) *Widget {
return &Widget{
Not: w1,
Selected: w2,
Focused: w3,
}
}
func (w *Widget) pick(focus gowid.Selector) gowid.IWidget {
if focus.Focus {
if w.Focused == nil && w.Selected == nil {
return w.Not
} else if w.Focused == nil {
return w.Selected
} else {
return w.Focused
}
} else if focus.Selected {
if w.Selected == nil {
return w.Not
} else {
return w.Selected
}
} else {
return w.Not
}
}
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
return gowid.RenderSize(w.pick(focus), size, focus, app)
}
func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
return w.pick(focus).Render(size, focus, app)
}
func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
return w.pick(focus).UserInput(ev, size, focus, app)
}
// TODO - this isn't right. Should Selectable be conditioned on focus?
func (w *Widget) Selectable() bool {
return w.Not.Selectable()
}
func (w *Widget) String() string {
return fmt.Sprintf("issel[%v#%v#%v]", w.Not, w.Selected, w.Focused)
}
//======================================================================
// For uses that require IComposite
type WidgetExt struct {
*Widget
}
func NewExt(w1, w2, w3 gowid.IWidget) *WidgetExt {
return &WidgetExt{New(w1, w2, w3)}
}
var _ gowid.IWidget = (*WidgetExt)(nil)
var _ gowid.IComposite = (*WidgetExt)(nil)
// Return Focused because UserInput operations that change state
// will apply when the widget is in focus - so this is likely the
// one we want. But this looks like a rich source of bugs...
func (w *WidgetExt) SubWidget() gowid.IWidget {
return w.pick(gowid.Focused)
}
//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End:
|