1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

Add DisabledReason field to MenuItem

This is useful to disable items that are not applicable right now because of
some condition (e.g. the "delete branch" menu item when the currently
checked-out branch is selected).

When a DisabledReason is set on a menu item, we
- show it in a tooltip (below the regular tooltip of the item, if it has one)
- strike through the item's key, if it has one
- show an error message with the DisabledReason if the user tries to invoke the
  command
This commit is contained in:
Stefan Haller
2023-09-05 21:49:33 +02:00
parent 679148449a
commit 7f9818cfa2
5 changed files with 26 additions and 2 deletions

View File

@ -90,6 +90,9 @@ func (self *MenuViewModel) GetDisplayStrings(_ int, _ int) [][]string {
return lo.Map(menuItems, func(item *types.MenuItem, _ int) []string { return lo.Map(menuItems, func(item *types.MenuItem, _ int) []string {
displayStrings := item.LabelColumns displayStrings := item.LabelColumns
if item.DisabledReason != "" {
displayStrings[0] = style.FgDefault.SetStrikethrough().Sprint(displayStrings[0])
}
if !showKeys { if !showKeys {
return displayStrings return displayStrings
@ -169,6 +172,10 @@ func (self *MenuContext) GetKeybindings(opts types.KeybindingsOpts) []*types.Bin
} }
func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error { func (self *MenuContext) OnMenuPress(selectedItem *types.MenuItem) error {
if selectedItem != nil && selectedItem.DisabledReason != "" {
return self.c.ErrorMsg(selectedItem.DisabledReason)
}
if err := self.c.PopContext(); err != nil { if err := self.c.PopContext(); err != nil {
return err return err
} }

View File

@ -333,7 +333,7 @@ func (self *ConfirmationHelper) resizeMenu() {
tooltip := "" tooltip := ""
selectedItem := self.c.Contexts().Menu.GetSelected() selectedItem := self.c.Contexts().Menu.GetSelected()
if selectedItem != nil { if selectedItem != nil {
tooltip = selectedItem.Tooltip tooltip = self.TooltipForMenuItem(selectedItem)
} }
tooltipHeight := getMessageHeight(true, tooltip, panelWidth) + 2 // plus 2 for the frame tooltipHeight := getMessageHeight(true, tooltip, panelWidth) + 2 // plus 2 for the frame
_, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0) _, _ = self.c.GocuiGui().SetView(self.c.Views().Tooltip.Name(), x0, tooltipTop, x1, tooltipTop+tooltipHeight-1, 0)
@ -382,3 +382,14 @@ func (self *ConfirmationHelper) IsPopupPanel(viewName string) bool {
func (self *ConfirmationHelper) IsPopupPanelFocused() bool { func (self *ConfirmationHelper) IsPopupPanelFocused() bool {
return self.IsPopupPanel(self.c.CurrentContext().GetViewName()) return self.IsPopupPanel(self.c.CurrentContext().GetViewName())
} }
func (self *ConfirmationHelper) TooltipForMenuItem(menuItem *types.MenuItem) string {
tooltip := menuItem.Tooltip
if menuItem.DisabledReason != "" {
if tooltip != "" {
tooltip += "\n\n"
}
tooltip += style.FgRed.Sprintf(self.c.Tr.DisabledMenuItemPrefix) + menuItem.DisabledReason
}
return tooltip
}

View File

@ -54,7 +54,7 @@ func (self *MenuController) GetOnFocus() func(types.OnFocusOpts) error {
return func(types.OnFocusOpts) error { return func(types.OnFocusOpts) error {
selectedMenuItem := self.context().GetSelected() selectedMenuItem := self.context().GetSelected()
if selectedMenuItem != nil { if selectedMenuItem != nil {
self.c.Views().Tooltip.SetContent(selectedMenuItem.Tooltip) self.c.Views().Tooltip.SetContent(self.c.Helpers().Confirmation.TooltipForMenuItem(selectedMenuItem))
} }
return nil return nil
} }

View File

@ -204,6 +204,10 @@ type MenuItem struct {
// The tooltip will be displayed upon highlighting the menu item // The tooltip will be displayed upon highlighting the menu item
Tooltip string Tooltip string
// If non-empty, show this in a tooltip, style the menu item as disabled,
// and refuse to invoke the command
DisabledReason string
// Can be used to group menu items into sections with headers. MenuItems // Can be used to group menu items into sections with headers. MenuItems
// with the same Section should be contiguous, and will automatically get a // with the same Section should be contiguous, and will automatically get a
// section header. If nil, the item is not part of a section. // section header. If nil, the item is not part of a section.

View File

@ -612,6 +612,7 @@ type TranslationSet struct {
MarkAsBaseCommitTooltip string MarkAsBaseCommitTooltip string
MarkedCommitMarker string MarkedCommitMarker string
PleaseGoToURL string PleaseGoToURL string
DisabledMenuItemPrefix string
Actions Actions Actions Actions
Bisect Bisect Bisect Bisect
Log Log Log Log
@ -1403,6 +1404,7 @@ func EnglishTranslationSet() TranslationSet {
MarkAsBaseCommitTooltip: "Select a base commit for the next rebase; this will effectively perform a 'git rebase --onto'.", MarkAsBaseCommitTooltip: "Select a base commit for the next rebase; this will effectively perform a 'git rebase --onto'.",
MarkedCommitMarker: "↑↑↑ Will rebase from here ↑↑↑", MarkedCommitMarker: "↑↑↑ Will rebase from here ↑↑↑",
PleaseGoToURL: "Please go to {{.url}}", PleaseGoToURL: "Please go to {{.url}}",
DisabledMenuItemPrefix: "Disabled: ",
Actions: Actions{ Actions: Actions{
// TODO: combine this with the original keybinding descriptions (those are all in lowercase atm) // TODO: combine this with the original keybinding descriptions (those are all in lowercase atm)
CheckoutCommit: "Checkout commit", CheckoutCommit: "Checkout commit",