First Commit
This commit is contained in:
148
menu_item.go
Normal file
148
menu_item.go
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
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 (
|
||||
h "git.bullercodeworks.com/brian/dhcli/helpers"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type MenuItem struct {
|
||||
id string
|
||||
label string
|
||||
style tcell.Style
|
||||
active bool
|
||||
visible bool
|
||||
x, y int
|
||||
w, h int
|
||||
|
||||
menuType MenuType
|
||||
items []*MenuItem
|
||||
onPressed func() bool
|
||||
|
||||
manualExpand bool
|
||||
expanded bool
|
||||
disabled bool
|
||||
}
|
||||
|
||||
func NewMenuItem(id string, style tcell.Style) *MenuItem {
|
||||
ret := &MenuItem{style: style}
|
||||
ret.Init(id)
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *MenuItem) Init(id string) {
|
||||
w.id = id
|
||||
w.visible = true
|
||||
}
|
||||
func (w *MenuItem) Id() string { return w.id }
|
||||
func (w *MenuItem) HandleResize(ev *tcell.EventResize) {}
|
||||
func (w *MenuItem) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
return false
|
||||
}
|
||||
if ev.Key() == tcell.KeyEnter {
|
||||
if w.onPressed != nil {
|
||||
return w.onPressed()
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
func (w *MenuItem) HandleTime(ev *tcell.EventTime) {}
|
||||
func (w *MenuItem) Draw(screen tcell.Screen) {
|
||||
if !w.visible {
|
||||
return
|
||||
}
|
||||
st := w.style
|
||||
if w.active {
|
||||
st = w.style.Reverse(true)
|
||||
}
|
||||
|
||||
x, y := w.x, w.y
|
||||
wd := w.w
|
||||
h.DrawText(x, y, h.PadR(w.label, wd), st, screen)
|
||||
if w.expanded {
|
||||
x += 2
|
||||
for i := range w.items {
|
||||
w.items[i].SetPos(Coord{X: x, Y: y})
|
||||
w.items[i].Draw(screen)
|
||||
y++
|
||||
}
|
||||
}
|
||||
}
|
||||
func (w *MenuItem) Active() bool { return w.active }
|
||||
func (w *MenuItem) SetActive(a bool) {
|
||||
w.active = a
|
||||
if !w.manualExpand {
|
||||
w.expanded = a
|
||||
}
|
||||
}
|
||||
func (w *MenuItem) Visible() bool { return w.visible }
|
||||
func (w *MenuItem) SetVisible(a bool) { w.visible = a }
|
||||
func (w *MenuItem) SetX(x int) { w.x = x }
|
||||
func (w *MenuItem) SetY(y int) { w.y = y }
|
||||
func (w *MenuItem) GetX() int { return w.x }
|
||||
func (w *MenuItem) GetY() int { return w.y }
|
||||
func (w *MenuItem) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
||||
func (w *MenuItem) SetW(x int) { w.w = x }
|
||||
func (w *MenuItem) SetH(y int) { w.h = y }
|
||||
func (w *MenuItem) GetW() int { return w.w }
|
||||
func (w *MenuItem) GetH() int { return w.y }
|
||||
func (w *MenuItem) WantW() int {
|
||||
ret := len(w.label) + 2
|
||||
if len(w.items) > 0 {
|
||||
for i := range w.items {
|
||||
if w.items[i].WantW() > ret {
|
||||
ret = w.items[i].WantW()
|
||||
// TODO: Figure offset of subitems
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (w *MenuItem) WantH() int {
|
||||
ret := 1
|
||||
if len(w.items) > 0 {
|
||||
for i := range len(w.items) {
|
||||
ret += w.items[i].WantH()
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
func (w *MenuItem) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
||||
func (w *MenuItem) Focusable() bool { return !w.disabled }
|
||||
|
||||
// How much width this item wants
|
||||
func (w *MenuItem) Expand(e bool) {
|
||||
if w.manualExpand {
|
||||
w.expanded = e
|
||||
}
|
||||
}
|
||||
|
||||
func (w *MenuItem) SetLabel(lbl string) { w.label = lbl }
|
||||
func (w *MenuItem) SetDisabled(d bool) { w.disabled = d }
|
||||
|
||||
func (w *MenuItem) AddItems(iL ...*MenuItem) {
|
||||
w.items = append(w.items, iL...)
|
||||
}
|
||||
func (w *MenuItem) SetOnPressed(p func() bool) { w.onPressed = p }
|
||||
Reference in New Issue
Block a user