Skip to content

Commit

Permalink
Make menus using the gowid list and not pile
Browse files Browse the repository at this point in the history
This makes for a friendlier menu because the contents can scroll if the
widget is not able to be rendered in the space available.
  • Loading branch information
gcla committed Feb 7, 2021
1 parent d35d833 commit d3b7336
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 53 deletions.
7 changes: 3 additions & 4 deletions ui/filterconvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ func makeFilterConvsMenuWidget() gowid.IWidget {
)
}

convListBox := menuutil.MakeMenuWithHotKeys(menuItems)

return convListBox
lb, _ := menuutil.MakeMenuWithHotKeys(menuItems)
return lb
}

func makeFilterConvs2MenuWidget() gowid.IWidget {
Expand Down Expand Up @@ -122,7 +121,7 @@ func makeFilterConvs2MenuWidget() gowid.IWidget {
)
}

convListBox := menuutil.MakeMenuWithHotKeys(menuItems)
convListBox, _ := menuutil.MakeMenuWithHotKeys(menuItems)

return convListBox
}
Expand Down
75 changes: 43 additions & 32 deletions ui/menuutil/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
"github.com/gcla/gowid/widgets/framed"
"github.com/gcla/gowid/widgets/hpadding"
"github.com/gcla/gowid/widgets/keypress"
"github.com/gcla/gowid/widgets/list"
"github.com/gcla/gowid/widgets/menu"
"github.com/gcla/gowid/widgets/pile"
"github.com/gcla/gowid/widgets/selectable"
"github.com/gcla/gowid/widgets/styled"
"github.com/gcla/gowid/widgets/text"
"github.com/gcla/gowid/widgets/vpadding"
"github.com/gcla/termshark/v2/widgets/appkeys"
"github.com/gdamore/tcell"
)
Expand All @@ -37,25 +38,38 @@ func MakeMenuDivider() SimpleMenuItem {
return SimpleMenuItem{}
}

func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {
func MakeMenuWithHotKeys(items []SimpleMenuItem) (gowid.IWidget, int) {
return makeMenuWithHotKeys(items, true)
}

func MakeMenu(items []SimpleMenuItem) (gowid.IWidget, int) {
return makeMenuWithHotKeys(items, false)
}

func makeMenuWithHotKeys(items []SimpleMenuItem, showKeys bool) (gowid.IWidget, int) {
menu1Widgets := make([]gowid.IWidget, len(items))
menu1HotKeys := make([]gowid.IWidget, len(items))

// Figure out the length of the longest hotkey string representation
max := 0
maxHK := 0
for _, w := range items {
if w.Txt != "" {
k := fmt.Sprintf("%v", w.Key)
if len(k) > max {
max = len(k)
if len(k) > maxHK {
maxHK = len(k)
}
}
}

maxVal := 0

// Construct the hotkey widget and menu item widget for each menu entry
for i, w := range items {
if w.Txt != "" {
load1B := button.NewBare(text.New(w.Txt))
if len(w.Txt) > maxVal {
maxVal = len(w.Txt)
}
var ks string
if w.Key != nil {
ks = fmt.Sprintf("%v", w.Key)
Expand Down Expand Up @@ -83,14 +97,20 @@ func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {

fixed := gowid.RenderFixed{}

startCol := 0
if showKeys {
startCol = 2
}

// Build the menu "row" for each menu entry
menu1Widgets2 := make([]gowid.IWidget, len(menu1Widgets))
for i, w := range menu1Widgets {
if w == nil {
menu1Widgets2[i] = divider.NewUnicode()
menu1Widgets2[i] = vpadding.New(divider.NewUnicode(), gowid.VAlignTop{}, gowid.RenderFlow{})
} else {
menu1Widgets2[i] = columns.New(
[]gowid.IContainerWidget{
containerWidgets := make([]gowid.IContainerWidget, 0, 3)
if showKeys {
containerWidgets = append(containerWidgets,
&gowid.ContainerWidget{
IWidget: hpadding.New(
// size is translated from flowwith{20} to fixed; fixed gives size 6, flowwith aligns right to 12
Expand All @@ -102,40 +122,33 @@ func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {
fixed,
),
gowid.HAlignLeft{},
gowid.RenderFlowWith{C: max},
gowid.RenderFlowWith{C: maxHK},
),
D: fixed,
},
&gowid.ContainerWidget{
IWidget: text.New("| "),
D: fixed,
},
&gowid.ContainerWidget{
IWidget: w,
D: fixed,
},
)
}
containerWidgets = append(containerWidgets,
&gowid.ContainerWidget{
IWidget: w,
D: fixed,
//D: gowid.RenderWithUnits{U: 20},
},
)

menu1Widgets2[i] = columns.New(
containerWidgets,
columns.Options{
StartColumn: 2,
StartColumn: startCol,
},
)
}
}

menu1cwidgets := make([]gowid.IContainerWidget, len(menu1Widgets2))
for i, w := range menu1Widgets2 {
var dim gowid.IWidgetDimension
if menu1Widgets[i] != nil {
dim = fixed
} else {
dim = gowid.RenderFlow{}
}
menu1cwidgets[i] = &gowid.ContainerWidget{
IWidget: w,
D: dim,
}
}

keys := make([]gowid.IKey, 0)
for _, i := range items {
if i.Key != nil {
Expand All @@ -148,9 +161,7 @@ func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {
cellmod.Opaque(
styled.New(
framed.NewUnicode(
pile.New(menu1cwidgets, pile.Options{
Wrap: true,
}),
list.New(list.NewSimpleListWalker(menu1Widgets2)),
),
gowid.MakePaletteRef("default"),
),
Expand All @@ -169,7 +180,7 @@ func MakeMenuWithHotKeys(items []SimpleMenuItem) gowid.IWidget {
}
}))

return menuListBox1
return menuListBox1, maxHK + maxVal + 2 + 2 // "| " + border
}

//======================================================================
Expand Down
23 changes: 12 additions & 11 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ func progMax(x, y Prog) Prog {

//======================================================================

func makeRecentMenuWidget() gowid.IWidget {
func makeRecentMenuWidget() (gowid.IWidget, int) {
savedItems := make([]menuutil.SimpleMenuItem, 0)
cfiles := termshark.ConfStringSlice("main.recent-files", []string{})
if cfiles != nil {
Expand All @@ -2475,13 +2475,11 @@ func makeRecentMenuWidget() gowid.IWidget {
)
}
}
savedListBox := menuutil.MakeMenuWithHotKeys(savedItems)

return savedListBox
return menuutil.MakeMenuWithHotKeys(savedItems)
}

func UpdateRecentMenu(app gowid.IApp) {
savedListBox := makeRecentMenuWidget()
savedListBox, _ := makeRecentMenuWidget()
savedListBoxWidgetHolder.SetSubWidget(savedListBox, app)
}

Expand Down Expand Up @@ -2830,7 +2828,7 @@ func Build() (*gowid.App, error) {
)
}

generalMenuListBox := menuutil.MakeMenuWithHotKeys(generalMenuItems)
generalMenuListBox, generalMenuWidth := menuutil.MakeMenuWithHotKeys(generalMenuItems)

var generalNext menuutil.NextMenu

Expand All @@ -2842,7 +2840,10 @@ func Build() (*gowid.App, error) {
),
)

generalMenu = menu.New("main", generalMenuListBoxWithKeys, fixed, menu.Options{
// Hack. What's a more general way of doing this? The length of the <Menu> button I suppose
openMenuSite.Options.XOffset = -generalMenuWidth + 8

generalMenu = menu.New("main", generalMenuListBoxWithKeys, units(generalMenuWidth), menu.Options{
Modal: true,
CloseKeysProvided: true,
CloseKeys: []gowid.IKey{
Expand All @@ -2862,7 +2863,7 @@ func Build() (*gowid.App, error) {
),
)

openAnalysisSite = menu.NewSite(menu.SiteOptions{YOffset: 1})
openAnalysisSite = menu.NewSite(menu.SiteOptions{XOffset: -12, YOffset: 1})
openAnalysis.OnClick(gowid.MakeWidgetCallback(gowid.ClickCB{}, func(app gowid.IApp, target gowid.IWidget) {
analysisMenu.Open(openAnalysisSite, app)
}))
Expand Down Expand Up @@ -2894,7 +2895,7 @@ func Build() (*gowid.App, error) {
},
}

analysisMenuListBox := menuutil.MakeMenuWithHotKeys(analysisMenuItems)
analysisMenuListBox, analysisMenuWidth := menuutil.MakeMenuWithHotKeys(analysisMenuItems)

var analysisNext menuutil.NextMenu

Expand All @@ -2906,7 +2907,7 @@ func Build() (*gowid.App, error) {
),
)

analysisMenu = menu.New("analysis", analysisMenuListBoxWithKeys, fixed, menu.Options{
analysisMenu = menu.New("analysis", analysisMenuListBoxWithKeys, units(analysisMenuWidth), menu.Options{
Modal: true,
CloseKeysProvided: true,
CloseKeys: []gowid.IKey{
Expand All @@ -2928,7 +2929,7 @@ func Build() (*gowid.App, error) {
Styler: gowid.MakePaletteRef("progress-spinner"),
})

savedListBox := makeRecentMenuWidget()
savedListBox, _ := makeRecentMenuWidget()
savedListBoxWidgetHolder = holder.New(savedListBox)

savedMenu = menu.New("saved", savedListBoxWidgetHolder, fixed, menu.Options{
Expand Down
2 changes: 1 addition & 1 deletion widgets/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func New(opt Options) *Widget {
}
}

drop := menu.New("filter", menuListBox2, gowid.RenderWithUnits{U: opt.MaxCompletions + 2},
drop := menu.New("filter", menuListBox2, fixed,
menu.Options{
IgnoreKeysProvided: true,
IgnoreKeys: ign,
Expand Down
8 changes: 3 additions & 5 deletions widgets/streamwidget/streamwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,13 @@ func MakeConvMenu() (*holder.Widget, *menu.Widget) {
}

func (w *Widget) updateConvMenuWidget(app gowid.IApp) {
convListBox := w.makeConvMenuWidget()
convListBox, _ := w.makeConvMenuWidget()
w.convMenuHolder.SetSubWidget(convListBox, app)
w.setConvButtonText(app)
w.setTurnText(app)
}

func (w *Widget) makeConvMenuWidget() gowid.IWidget {
func (w *Widget) makeConvMenuWidget() (gowid.IWidget, int) {
savedItems := make([]menuutil.SimpleMenuItem, 0)

savedItems = append(savedItems,
Expand Down Expand Up @@ -295,9 +295,7 @@ func (w *Widget) makeConvMenuWidget() gowid.IWidget {
)
}

convListBox := menuutil.MakeMenuWithHotKeys(savedItems)

return convListBox
return menuutil.MakeMenuWithHotKeys(savedItems)
}

// Turns an array of stream chunks into a pair of (a) a scrollable table
Expand Down

0 comments on commit d3b7336

Please sign in to comment.