Skip to content

Commit 20a4aea

Browse files
committed
Support showing checkboxes or radio buttons in menus
For checkboxes it probably doesn't really make sense to use them yet, because we'd have to find a way how you can toggle them without closing the dialog; but we already provide rendering for them to lay the ground. But radio buttons can be used already, because for those it is ok to close the dialog when choosing a different option (as long as there is only one grounp of radio buttons in the panel, that is).
1 parent a5620eb commit 20a4aea

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

pkg/gui/context/menu_context.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,21 @@ func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
107107
keyLabel = style.FgCyan.Sprint(keybindings.LabelFromKey(item.Key))
108108
}
109109

110-
displayStrings = utils.Prepend(displayStrings, keyLabel)
110+
checkMark := ""
111+
switch item.Widget {
112+
case types.MenuWidgetNone:
113+
// do nothing
114+
case types.MenuWidgetRadioButtonSelected:
115+
checkMark = "(•)"
116+
case types.MenuWidgetRadioButtonUnselected:
117+
checkMark = "( )"
118+
case types.MenuWidgetCheckboxSelected:
119+
checkMark = "[✓]"
120+
case types.MenuWidgetCheckboxUnselected:
121+
checkMark = "[ ]"
122+
}
123+
124+
displayStrings = utils.Prepend(displayStrings, keyLabel, checkMark)
111125
return displayStrings
112126
})
113127
}

pkg/gui/types/common.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,30 @@ type DisabledReason struct {
217217
ShowErrorInPanel bool
218218
}
219219

220+
type MenuWidget int
221+
222+
const (
223+
MenuWidgetNone MenuWidget = iota
224+
MenuWidgetRadioButtonSelected
225+
MenuWidgetRadioButtonUnselected
226+
MenuWidgetCheckboxSelected
227+
MenuWidgetCheckboxUnselected
228+
)
229+
230+
func MakeMenuRadioButton(value bool) MenuWidget {
231+
if value {
232+
return MenuWidgetRadioButtonSelected
233+
}
234+
return MenuWidgetRadioButtonUnselected
235+
}
236+
237+
func MakeMenuCheckBox(value bool) MenuWidget {
238+
if value {
239+
return MenuWidgetCheckboxSelected
240+
}
241+
return MenuWidgetCheckboxUnselected
242+
}
243+
220244
type MenuItem struct {
221245
Label string
222246

@@ -232,6 +256,12 @@ type MenuItem struct {
232256
// item, as opposed to having to navigate to it
233257
Key Key
234258

259+
// A widget to show in front of the menu item. Supported widget types are
260+
// checkboxes and radio buttons,
261+
// This only handles the rendering of the widget; the behavior needs to be
262+
// provided by the client.
263+
Widget MenuWidget
264+
235265
// The tooltip will be displayed upon highlighting the menu item
236266
Tooltip string
237267

0 commit comments

Comments
 (0)