Fix TopMenu
Add Shrinkwrap
This commit is contained in:
BIN
example/example
BIN
example/example
Binary file not shown.
@@ -72,14 +72,32 @@ func (s *UiScreen) Init(ui *Ui) {
|
|||||||
ll.Add(btnL)
|
ll.Add(btnL)
|
||||||
ll.AddFlag(btnL, w.LFAlignVBottom)
|
ll.AddFlag(btnL, w.LFAlignVBottom)
|
||||||
|
|
||||||
|
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 := w.NewDebugWidget("debug", ui.style)
|
||||||
dw.SetWidget(ll)
|
dw.SetWidget(ll)
|
||||||
dw.SetSize(w.Coord{X: 20, Y: 30})
|
dw.SetSize(w.Coord{X: 20, Y: 30})
|
||||||
dw.SetActive(true)
|
dw.SetActive(true)
|
||||||
s.widget = dw
|
s.widget = dw
|
||||||
|
*/
|
||||||
|
ll.Add(s.log)
|
||||||
|
|
||||||
s.widgets = append(s.widgets, dw)
|
// s.widgets = append(s.widgets, ml)
|
||||||
s.widgets = append(s.widgets, s.log)
|
// s.widgets = append(s.widgets, s.log)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *UiScreen) HandleResize(ev *tcell.EventResize) {
|
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'
|
// Ctrl+J is the keypad 'Enter'
|
||||||
ev = tcell.NewEventKey(tcell.KeyEnter, 0, 0)
|
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)
|
return s.widget.HandleKey(ev)
|
||||||
} else {
|
|
||||||
return s.log.HandleKey(ev)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
func (s *UiScreen) HandleTime(ev *tcell.EventTime) {}
|
func (s *UiScreen) HandleTime(ev *tcell.EventTime) {}
|
||||||
func (s *UiScreen) Draw() {
|
func (s *UiScreen) Draw() {
|
||||||
s.widget.Draw(s.ui.tScreen)
|
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) {}
|
func (s *UiScreen) Log(txt string, args ...any) {}
|
||||||
|
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ func (w *LinearLayout) Init(id string, s tcell.Style) {
|
|||||||
w.id = id
|
w.id = id
|
||||||
w.style = s
|
w.style = s
|
||||||
w.visible = true
|
w.visible = true
|
||||||
|
w.tabbable = true
|
||||||
w.layoutFlags = make(map[Widget]LayoutFlag)
|
w.layoutFlags = make(map[Widget]LayoutFlag)
|
||||||
w.layoutWeights = make(map[Widget]int)
|
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 {
|
func (w *LinearLayout) HandleKey(ev *tcell.EventKey) bool {
|
||||||
// First, see if the active widget handles the key
|
// First, see if the active widget handles the key
|
||||||
|
w.Log("LL: Handling Event: %s", ev.Name())
|
||||||
if len(w.widgets) > w.cursor {
|
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) {
|
if w.widgets[w.cursor].HandleKey(ev) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -195,7 +198,13 @@ func (w *LinearLayout) MinH() int {
|
|||||||
// All others to inactive
|
// All others to inactive
|
||||||
// If this layout is not active, everything is inactive
|
// If this layout is not active, everything is inactive
|
||||||
func (w *LinearLayout) updateActive() {
|
func (w *LinearLayout) updateActive() {
|
||||||
|
if !w.active {
|
||||||
|
w.Log("LL: Not Active")
|
||||||
|
}
|
||||||
for i, wd := range w.widgets {
|
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)
|
wd.SetActive(i == w.cursor && w.active)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
66
wdgt_shrinkwrap.go
Normal file
66
wdgt_shrinkwrap.go
Normal 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() }
|
||||||
@@ -86,8 +86,8 @@ func (w *TopMenuLayout) HandleKey(ev *tcell.EventKey) bool {
|
|||||||
w.menu.SetActive(!w.menu.Active())
|
w.menu.SetActive(!w.menu.Active())
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if w.menu.HandleKey(ev) {
|
if w.menu.Active() {
|
||||||
return true
|
return w.menu.HandleKey(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pass the key through to the main widget
|
// Pass the key through to the main widget
|
||||||
@@ -115,7 +115,10 @@ func (w *TopMenuLayout) Draw(screen tcell.Screen) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (w *TopMenuLayout) Active() bool { return w.active }
|
func (w *TopMenuLayout) Active() bool { return w.active }
|
||||||
func (w *TopMenuLayout) SetActive(a bool) { w.active = a }
|
func (w *TopMenuLayout) SetActive(a bool) {
|
||||||
|
w.active = a
|
||||||
|
w.widget.SetActive(a)
|
||||||
|
}
|
||||||
func (w *TopMenuLayout) Visible() bool { return w.visible }
|
func (w *TopMenuLayout) Visible() bool { return w.visible }
|
||||||
func (w *TopMenuLayout) SetVisible(a bool) { w.visible = a }
|
func (w *TopMenuLayout) SetVisible(a bool) { w.visible = a }
|
||||||
func (w *TopMenuLayout) SetX(x int) { w.x = x }
|
func (w *TopMenuLayout) SetX(x int) { w.x = x }
|
||||||
|
|||||||
Reference in New Issue
Block a user