Skip to content

Commit a62a508

Browse files
authored
Show current value in menus (#3628)
- **PR Description** Some of our menus let you pick a value for some option (e.g. the sort order menus for branches and commits). It's nice to see which one is the current value when opening such a menu, so this PR implements that. For menus that also have key bindings, the radio button goes between the key and the label. As an alternative, I considered selecting the current value when the menu is opened; however, we currently have no menus where we select a different entry than the first one, and it might be confusing for people who are used to opening a menu and then pressing down-arrow a certain number of times to get to a particular value. - **Please check if the PR fulfills these requirements** * [x] Cheatsheets are up-to-date (run `go generate ./...`) * [x] Code has been formatted (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting)) * [x] Tests have been added/updated (see [here](https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide) * [ ] Text is internationalised (see [here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation)) * [ ] Docs have been updated if necessary * [x] You've read through your own file changes for silly mistakes etc
2 parents a5620eb + 4967e51 commit a62a508

File tree

8 files changed

+77
-5
lines changed

8 files changed

+77
-5
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/controllers/branches_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,8 @@ func (self *BranchesController) createSortMenu() error {
696696
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.BRANCHES}})
697697
}
698698
return nil
699-
})
699+
},
700+
self.c.GetAppState().LocalBranchSortOrder)
700701
}
701702

702703
func (self *BranchesController) createResetMenu(selectedBranch *models.Branch) error {

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func (self *RefsHelper) ResetToRef(ref string, strength string, envVars []string
190190
return nil
191191
}
192192

193-
func (self *RefsHelper) CreateSortOrderMenu(sortOptionsOrder []string, onSelected func(sortOrder string) error) error {
193+
func (self *RefsHelper) CreateSortOrderMenu(sortOptionsOrder []string, onSelected func(sortOrder string) error, currentValue string) error {
194194
type sortMenuOption struct {
195195
key types.Key
196196
label string
@@ -221,7 +221,8 @@ func (self *RefsHelper) CreateSortOrderMenu(sortOptionsOrder []string, onSelecte
221221
OnPress: func() error {
222222
return onSelected(opt.sortOrder)
223223
},
224-
Key: opt.key,
224+
Key: opt.key,
225+
Widget: types.MakeMenuRadioButton(opt.sortOrder == currentValue),
225226
}
226227
})
227228
return self.c.Menu(types.CreateMenuOptions{

pkg/gui/controllers/local_commits_controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ func (self *LocalCommitsController) handleOpenLogMenu() error {
10851085
Label: self.c.Tr.ShowGitGraph,
10861086
OpensMenu: true,
10871087
OnPress: func() error {
1088+
currentValue := self.c.GetAppState().GitLogShowGraph
10881089
onPress := func(value string) func() error {
10891090
return func() error {
10901091
self.c.GetAppState().GitLogShowGraph = value
@@ -1101,14 +1102,17 @@ func (self *LocalCommitsController) handleOpenLogMenu() error {
11011102
{
11021103
Label: "always",
11031104
OnPress: onPress("always"),
1105+
Widget: types.MakeMenuRadioButton(currentValue == "always"),
11041106
},
11051107
{
11061108
Label: "never",
11071109
OnPress: onPress("never"),
1110+
Widget: types.MakeMenuRadioButton(currentValue == "never"),
11081111
},
11091112
{
11101113
Label: "when maximised",
11111114
OnPress: onPress("when-maximised"),
1115+
Widget: types.MakeMenuRadioButton(currentValue == "when-maximised"),
11121116
},
11131117
},
11141118
})
@@ -1118,6 +1122,7 @@ func (self *LocalCommitsController) handleOpenLogMenu() error {
11181122
Label: self.c.Tr.SortCommits,
11191123
OpensMenu: true,
11201124
OnPress: func() error {
1125+
currentValue := self.c.GetAppState().GitLogOrder
11211126
onPress := func(value string) func() error {
11221127
return func() error {
11231128
self.c.GetAppState().GitLogOrder = value
@@ -1139,14 +1144,17 @@ func (self *LocalCommitsController) handleOpenLogMenu() error {
11391144
{
11401145
Label: "topological (topo-order)",
11411146
OnPress: onPress("topo-order"),
1147+
Widget: types.MakeMenuRadioButton(currentValue == "topo-order"),
11421148
},
11431149
{
11441150
Label: "date-order",
11451151
OnPress: onPress("date-order"),
1152+
Widget: types.MakeMenuRadioButton(currentValue == "date-order"),
11461153
},
11471154
{
11481155
Label: "author-date-order",
11491156
OnPress: onPress("author-date-order"),
1157+
Widget: types.MakeMenuRadioButton(currentValue == "author-date-order"),
11501158
},
11511159
},
11521160
})

pkg/gui/controllers/remote_branches_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ func (self *RemoteBranchesController) createSortMenu() error {
145145
return self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.REMOTES}})
146146
}
147147
return nil
148-
})
148+
},
149+
self.c.GetAppState().RemoteBranchSortOrder)
149150
}
150151

151152
func (self *RemoteBranchesController) createResetMenu(selectedBranch *models.RemoteBranch) error {

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

pkg/integration/tests/branch/sort_local_branches.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ var SortLocalBranches = NewIntegrationTest(NewIntegrationTestArgs{
3737
Press(keys.Branches.SortOrder)
3838

3939
t.ExpectPopup().Menu().Title(Equals("Sort order")).
40+
Lines(
41+
Contains("r (•) Recency").IsSelected(),
42+
Contains("a ( ) Alphabetical"),
43+
Contains("d ( ) Date"),
44+
Contains(" Cancel"),
45+
).
4046
Select(Contains("-committerdate")).
4147
Confirm()
4248

@@ -53,6 +59,12 @@ var SortLocalBranches = NewIntegrationTest(NewIntegrationTestArgs{
5359
Press(keys.Branches.SortOrder)
5460

5561
t.ExpectPopup().Menu().Title(Equals("Sort order")).
62+
Lines(
63+
Contains("r ( ) Recency").IsSelected(),
64+
Contains("a ( ) Alphabetical"),
65+
Contains("d (•) Date"),
66+
Contains(" Cancel"),
67+
).
5668
Select(Contains("refname")).
5769
Confirm()
5870

pkg/integration/tests/branch/sort_remote_branches.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ var SortRemoteBranches = NewIntegrationTest(NewIntegrationTestArgs{
4141
Press(keys.Branches.SortOrder)
4242

4343
t.ExpectPopup().Menu().Title(Equals("Sort order")).
44+
Lines(
45+
Contains("a (•) Alphabetical").IsSelected(),
46+
Contains("d ( ) Date"),
47+
Contains(" Cancel"),
48+
).
4449
Select(Contains("-committerdate")).
4550
Confirm()
4651

0 commit comments

Comments
 (0)