257 lines
6.9 KiB
Go
257 lines
6.9 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, .HETHER 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"
|
|
|
|
wh "git.bullercodeworks.com/brian/tcell-widgets/helpers"
|
|
"github.com/gdamore/tcell"
|
|
)
|
|
|
|
// TopMenuLayout is a greedy widget and will consume all of the space you
|
|
// give it
|
|
type TopMenuLayout struct {
|
|
id string
|
|
style tcell.Style
|
|
|
|
x, y int
|
|
w, h int
|
|
menu *Menu
|
|
widget Widget
|
|
|
|
active bool
|
|
visible bool
|
|
focusable bool
|
|
|
|
layoutFlags LayoutFlag
|
|
keyMap KeyMap
|
|
|
|
logger func(string, ...any)
|
|
}
|
|
|
|
var _ Widget = (*TopMenuLayout)(nil)
|
|
|
|
func NewTopMenuLayout(id string, s tcell.Style) *TopMenuLayout {
|
|
ret := &TopMenuLayout{widget: NewBlankWidget(id)}
|
|
ret.Init(id, s)
|
|
return ret
|
|
}
|
|
|
|
func (w *TopMenuLayout) Init(id string, s tcell.Style) {
|
|
w.id = id
|
|
w.style = s
|
|
w.visible = true
|
|
w.SetActive(true)
|
|
|
|
w.menu = NewMenu(fmt.Sprintf("%s.mainmenu", id), tcell.StyleDefault)
|
|
w.menu.SetActive(false)
|
|
w.menu.SetType(MenuTypeH)
|
|
|
|
w.widget = NewBlankWidget("blank")
|
|
w.keyMap = BlankKeyMap()
|
|
w.keyMap.Add(tcell.KeyEscape, func(ev *tcell.EventKey) bool {
|
|
if w.menu != nil {
|
|
w.menu.SetActive(!w.menu.Active())
|
|
if w.widget != nil {
|
|
w.widget.SetActive(!w.menu.Active())
|
|
}
|
|
return true
|
|
}
|
|
return false
|
|
})
|
|
}
|
|
|
|
func (w *TopMenuLayout) Id() string { return w.id }
|
|
func (w *TopMenuLayout) HandleResize(ev *tcell.EventResize) {
|
|
w.w, w.h = ev.Size()
|
|
if w.menu != nil {
|
|
w.menu.SetPos(Coord{X: 0, Y: 0})
|
|
w.menu.SetSize(Coord{X: w.w, Y: 1})
|
|
}
|
|
|
|
if w.widget != nil {
|
|
w.widget.HandleResize(tcell.NewEventResize(w.w, w.h-1))
|
|
x, y := 0, 1
|
|
|
|
switch w.layoutFlags.AlignH() {
|
|
case LFAlignHRight:
|
|
x = w.w - w.widget.GetW()
|
|
case LFAlignHCenter:
|
|
x = (w.w / 2) - (w.widget.GetW() / 2)
|
|
}
|
|
|
|
switch w.layoutFlags.AlignV() {
|
|
case LFAlignVBottom:
|
|
y = w.h - w.widget.GetH()
|
|
case LFAlignVCenter:
|
|
y = (w.h / 2) - (w.widget.GetH() / 2)
|
|
}
|
|
w.widget.SetPos(Coord{X: x, Y: y})
|
|
}
|
|
}
|
|
|
|
func (w *TopMenuLayout) SetKeyMap(km KeyMap) { w.keyMap = km }
|
|
func (w *TopMenuLayout) AddToKeyMap(km KeyMap) { w.keyMap.Merge(km) }
|
|
func (w *TopMenuLayout) RemoveFromKeyMap(km KeyMap) {
|
|
for k := range km.Keys {
|
|
w.keyMap.Remove(k)
|
|
}
|
|
for r := range km.Runes {
|
|
w.keyMap.RemoveRune(r)
|
|
}
|
|
}
|
|
|
|
func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
|
|
if !w.active {
|
|
return false
|
|
}
|
|
|
|
if w.keyMap.Handle(ev) {
|
|
return true
|
|
}
|
|
if w.menu != nil && w.menu.Active() {
|
|
return w.menu.HandleKey(ev)
|
|
}
|
|
// Pass the key through to the main widget
|
|
if w.widget != nil {
|
|
// If we're here, that means the menu isn't active, but we are. So the
|
|
// widget should be.
|
|
w.widget.SetActive(true)
|
|
ret := w.widget.HandleKey(ev)
|
|
w.widget.SetActive(true)
|
|
return ret
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *TopMenuLayout) HandleTime(ev *tcell.EventTime) {
|
|
w.menu.HandleTime(ev)
|
|
w.widget.HandleTime(ev)
|
|
}
|
|
|
|
func (w *TopMenuLayout) Draw(screen tcell.Screen) {
|
|
if !w.visible {
|
|
return
|
|
}
|
|
if w.widget != nil {
|
|
w.GetPos().DrawOffset(w.widget, screen)
|
|
}
|
|
if w.menu != nil {
|
|
w.GetPos().DrawOffset(w.menu, screen)
|
|
}
|
|
}
|
|
|
|
func (w *TopMenuLayout) Active() bool { return w.active }
|
|
func (w *TopMenuLayout) SetActive(a bool) {
|
|
w.active = a
|
|
if w.widget != nil {
|
|
w.widget.SetActive(a)
|
|
}
|
|
}
|
|
func (w *TopMenuLayout) Visible() bool { return w.visible }
|
|
func (w *TopMenuLayout) SetVisible(a bool) { w.visible = a }
|
|
func (w *TopMenuLayout) SetX(x int) { w.x = x }
|
|
func (w *TopMenuLayout) SetY(y int) { w.y = y }
|
|
func (w *TopMenuLayout) GetX() int { return w.x }
|
|
func (w *TopMenuLayout) GetY() int { return w.y }
|
|
func (w *TopMenuLayout) GetPos() Coord { return Coord{X: w.x, Y: w.y} }
|
|
func (w *TopMenuLayout) SetPos(c Coord) { w.x, w.y = c.X, c.Y }
|
|
func (w *TopMenuLayout) SetW(x int) { w.w = x }
|
|
func (w *TopMenuLayout) SetH(y int) { w.h = y }
|
|
func (w *TopMenuLayout) GetW() int { return w.w }
|
|
func (w *TopMenuLayout) GetH() int { return w.y }
|
|
func (w *TopMenuLayout) SetSize(c Coord) { w.w, w.h = c.X, c.Y }
|
|
func (w *TopMenuLayout) Focusable() bool { return w.focusable }
|
|
func (w *TopMenuLayout) SetFocusable(b bool) { w.focusable = b }
|
|
|
|
func (w *TopMenuLayout) WantW() int { return wh.MaxInt }
|
|
func (w *TopMenuLayout) WantH() int { return wh.MaxInt }
|
|
func (w *TopMenuLayout) MinW() int { return 0 }
|
|
func (w *TopMenuLayout) MinH() int { return 0 }
|
|
|
|
func (w *TopMenuLayout) Menu() *Menu { return w.menu }
|
|
func (w *TopMenuLayout) AddMenuItems(iL ...*MenuItem) { w.menu.AddItems(iL...) }
|
|
func (w *TopMenuLayout) RemoveMenuItems(iL ...*MenuItem) { w.menu.RemoveItems(iL...) }
|
|
|
|
func (w *TopMenuLayout) SetWidget(wd Widget) { w.widget = wd }
|
|
func (w *TopMenuLayout) AddLayoutFlag(f LayoutFlag) {
|
|
if f.IsAlignH() {
|
|
w.layoutFlags.ClearAlignH()
|
|
}
|
|
if f.IsAlignV() {
|
|
w.layoutFlags.ClearAlignV()
|
|
}
|
|
w.layoutFlags.Add(f)
|
|
}
|
|
|
|
func (w *TopMenuLayout) RemoveLayoutFlag(f LayoutFlag) {
|
|
if f.IsAlignH() {
|
|
w.layoutFlags.ClearAlignH()
|
|
}
|
|
if f.IsAlignV() {
|
|
w.layoutFlags.ClearAlignV()
|
|
}
|
|
}
|
|
|
|
func (w *TopMenuLayout) SetLogger(l func(string, ...any)) { w.logger = l }
|
|
func (w *TopMenuLayout) Log(txt string, args ...any) {
|
|
if w.logger != nil {
|
|
w.logger(txt, args...)
|
|
}
|
|
}
|
|
|
|
func (w *TopMenuLayout) CreateMenuItem(id, lbl string, do func() bool, hotKey rune, subItems ...*MenuItem) *MenuItem {
|
|
d := NewMenuItem(id, tcell.StyleDefault)
|
|
d.SetLabel(lbl)
|
|
d.SetOnPressed(do)
|
|
d.SetHotKey(hotKey)
|
|
if len(subItems) > 0 {
|
|
d.AddItems(subItems...)
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (w *TopMenuLayout) SetItemVisible(id string, v bool) bool {
|
|
if mi := w.FindItem(id); mi != nil {
|
|
mi.SetVisible(v)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *TopMenuLayout) SetItemDisabled(id string, d bool) bool {
|
|
if mi := w.FindItem(id); mi != nil {
|
|
mi.SetDisabled(d)
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (w *TopMenuLayout) FindItem(id string) *MenuItem { return w.menu.FindItem(id) }
|
|
func (w *TopMenuLayout) CloseMenu() {
|
|
if w.menu != nil && w.menu.Active() {
|
|
w.menu.SetActive(false)
|
|
w.widget.SetActive(true)
|
|
}
|
|
}
|