Fix TopMenu

Add Shrinkwrap
This commit is contained in:
2025-08-14 08:29:58 -05:00
parent 0c6f212e18
commit 566f35e655
5 changed files with 109 additions and 35 deletions

Binary file not shown.

View File

@@ -72,14 +72,32 @@ func (s *UiScreen) Init(ui *Ui) {
ll.Add(btnL)
ll.AddFlag(btnL, w.LFAlignVBottom)
dw := w.NewDebugWidget("debug", ui.style)
dw.SetWidget(ll)
dw.SetSize(w.Coord{X: 20, Y: 30})
dw.SetActive(true)
s.widget = dw
ml := w.NewTopMenuLayout("menu", ui.style)
m := ml.Menu()
m.SetLabel("Widget Example")
ml.AddMenuItems(
m.CreateMenuItem("File", nil,
m.CreateMenuItem("Exit", func() bool {
s.ui.stop()
return true
}),
),
)
ml.SetLogger(s.log.Log)
ml.SetWidget(ll)
ml.SetActive(true)
s.widget = ml
/*
dw := w.NewDebugWidget("debug", ui.style)
dw.SetWidget(ll)
dw.SetSize(w.Coord{X: 20, Y: 30})
dw.SetActive(true)
s.widget = dw
*/
ll.Add(s.log)
s.widgets = append(s.widgets, dw)
s.widgets = append(s.widgets, s.log)
// s.widgets = append(s.widgets, ml)
// s.widgets = append(s.widgets, s.log)
}
func (s *UiScreen) HandleResize(ev *tcell.EventResize) {
@@ -95,34 +113,12 @@ func (s *UiScreen) HandleKey(ev *tcell.EventKey) bool {
// Ctrl+J is the keypad 'Enter'
ev = tcell.NewEventKey(tcell.KeyEnter, 0, 0)
}
if s.cursor < len(s.widgets) {
if s.widgets[s.cursor].HandleKey(ev) {
return true
}
}
if ev.Key() == tcell.KeyTab {
s.cursor = (s.cursor + 1) % len(s.widgets)
if s.cursor == 0 {
s.widget.SetActive(true)
s.log.SetActive(false)
} else {
s.widget.SetActive(false)
s.log.SetActive(true)
}
return true
}
if s.cursor == 0 {
return s.widget.HandleKey(ev)
} else {
return s.log.HandleKey(ev)
}
return s.widget.HandleKey(ev)
}
func (s *UiScreen) HandleTime(ev *tcell.EventTime) {}
func (s *UiScreen) Draw() {
s.widget.Draw(s.ui.tScreen)
s.log.Draw(s.ui.tScreen)
// s.log.Draw(s.ui.tScreen)
}
func (s *UiScreen) Log(txt string, args ...any) {}

View File

@@ -69,6 +69,7 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
w.id = id
w.style = s
w.visible = true
w.tabbable = true
w.layoutFlags = make(map[Widget]LayoutFlag)
w.layoutWeights = make(map[Widget]int)
}
@@ -83,7 +84,9 @@ func (w *LinearLayout) HandleResize(ev *tcell.EventResize) {
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
// First, see if the active widget handles the key
w.Log("LL: Handling Event: %s", ev.Name())
if len(w.widgets) > w.cursor {
w.Log("LL: Pushing to Widget: %s", w.widgets[w.cursor].Id())
if w.widgets[w.cursor].HandleKey(ev) {
return true
}
@@ -195,7 +198,13 @@ func (w *LinearLayout) MinH() int {
// All others to inactive
// If this layout is not active, everything is inactive
func (w *LinearLayout) updateActive() {
if !w.active {
w.Log("LL: Not Active")
}
for i, wd := range w.widgets {
if i == w.cursor {
w.Log("LL: Setting Active: %s", w.widgets[i].Id())
}
wd.SetActive(i == w.cursor && w.active)
}
}

66
wdgt_shrinkwrap.go Normal file
View File

@@ -0,0 +1,66 @@
/*
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 "github.com/gdamore/tcell"
// ShrinkWrap is a widget that limits the size of it's child
type ShrinkWrap struct {
widget Widget
}
var _ Widget = (*ShrinkWrap)(nil)
func NewShrinkWrap(w Widget) *ShrinkWrap {
ret := &ShrinkWrap{widget: w}
return ret
}
func (w *ShrinkWrap) Init(id string, st tcell.Style) { w.widget.Init(id, st) }
func (w *ShrinkWrap) Id() string { return w.widget.Id() }
func (w *ShrinkWrap) HandleResize(ev *tcell.EventResize) { w.widget.HandleResize(ev) }
func (w *ShrinkWrap) HandleKey(ev *tcell.EventKey) bool { return w.widget.HandleKey(ev) }
func (w *ShrinkWrap) HandleTime(ev *tcell.EventTime) { w.widget.HandleTime(ev) }
func (w *ShrinkWrap) Draw(screen tcell.Screen) { w.widget.Draw(screen) }
func (w *ShrinkWrap) Active() bool { return w.widget.Active() }
func (w *ShrinkWrap) SetActive(a bool) { w.widget.SetActive(a) }
func (w *ShrinkWrap) Visible() bool { return w.widget.Visible() }
func (w *ShrinkWrap) SetVisible(v bool) { w.widget.SetVisible(v) }
func (w *ShrinkWrap) Focusable() bool { return w.widget.Focusable() }
func (w *ShrinkWrap) Tabbable() bool { return w.widget.Tabbable() }
func (w *ShrinkWrap) SetTabbable(t bool) { w.widget.SetTabbable(t) }
func (w *ShrinkWrap) SetX(x int) { w.widget.SetX(x) }
func (w *ShrinkWrap) SetY(y int) { w.widget.SetY(y) }
func (w *ShrinkWrap) GetX() int { return w.widget.GetX() }
func (w *ShrinkWrap) GetY() int { return w.widget.GetY() }
func (w *ShrinkWrap) GetPos() Coord { return w.widget.GetPos() }
func (w *ShrinkWrap) SetPos(pos Coord) { w.widget.SetPos(pos) }
func (w *ShrinkWrap) SetSize(size Coord) { w.widget.SetSize(size) }
func (w *ShrinkWrap) SetW(wd int) { w.widget.SetW(wd) }
func (w *ShrinkWrap) SetH(h int) { w.widget.SetH(h) }
func (w *ShrinkWrap) GetW() int { return w.widget.GetW() }
func (w *ShrinkWrap) GetH() int { return w.widget.GetH() }
func (w *ShrinkWrap) WantW() int { return w.widget.MinW() }
func (w *ShrinkWrap) WantH() int { return w.widget.MinH() }
func (w *ShrinkWrap) MinW() int { return w.widget.MinW() }
func (w *ShrinkWrap) MinH() int { return w.widget.MinH() }

View File

@@ -86,8 +86,8 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
w.menu.SetActive(!w.menu.Active())
return true
}
if w.menu.HandleKey(ev) {
return true
if w.menu.Active() {
return w.menu.HandleKey(ev)
}
// Pass the key through to the main widget
@@ -114,8 +114,11 @@ func (w *TopMenuLayout) Draw(screen tcell.Screen) {
}
}
func (w *TopMenuLayout) Active() bool { return w.active }
func (w *TopMenuLayout) SetActive(a bool) { w.active = a }
func (w *TopMenuLayout) Active() bool { return w.active }
func (w *TopMenuLayout) SetActive(a bool) {
w.active = a
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 }