summaryrefslogtreecommitdiffstats
path: root/pkg/gui/context
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-08-08 13:26:26 +0200
committerStefan Haller <stefan@haller-berlin.de>2023-08-29 08:04:47 +0200
commit6d4df573936e16fa3366f670269538aebef35276 (patch)
tree730dafa0746d22bf3306e15c2341a412511c66bf /pkg/gui/context
parent3df01aaff08b68a860b52aee5acee411c8ad63d5 (diff)
Add option to add sections to menus
Diffstat (limited to 'pkg/gui/context')
-rw-r--r--pkg/gui/context/menu_context.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/pkg/gui/context/menu_context.go b/pkg/gui/context/menu_context.go
index 30fe8a4b4..287ed92ec 100644
--- a/pkg/gui/context/menu_context.go
+++ b/pkg/gui/context/menu_context.go
@@ -38,6 +38,7 @@ func NewMenuContext(
list: viewModel,
getDisplayStrings: viewModel.GetDisplayStrings,
getColumnAlignments: func() []utils.Alignment { return viewModel.columnAlignment },
+ getNonModelItems: viewModel.GetNonModelItems,
},
c: c,
},
@@ -113,6 +114,40 @@ func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
})
}
+func (self *MenuViewModel) GetNonModelItems() []*NonModelItem {
+ // Don't display section headers when we are filtering. The reason is that
+ // filtering changes the order of the items (they are sorted by best match),
+ // so all the sections would be messed up.
+ if self.FilteredListViewModel.IsFiltering() {
+ return []*NonModelItem{}
+ }
+
+ result := []*NonModelItem{}
+ menuItems := self.FilteredListViewModel.GetItems()
+ var prevSection *types.MenuSection = nil
+ for i, menuItem := range menuItems {
+ menuItem := menuItem
+ if menuItem.Section != nil && menuItem.Section != prevSection {
+ if prevSection != nil {
+ result = append(result, &NonModelItem{
+ Index: i,
+ Column: 1,
+ Content: "",
+ })
+ }
+
+ result = append(result, &NonModelItem{
+ Index: i,
+ Column: 1,
+ Content: style.FgGreen.SetBold().Sprintf("--- %s ---", menuItem.Section.Title),
+ })
+ prevSection = menuItem.Section
+ }
+ }
+
+ return result
+}
+
func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
basicBindings := self.ListContextTrait.GetKeybindings(opts)
menuItemsWithKeys := lo.Filter(self.menuItems, func(item *types.MenuItem, _ int) bool {