From 7bf05dfca47e175d92bb24a256f1a0e036800be5 Mon Sep 17 00:00:00 2001 From: Stefan Haller Date: Tue, 12 Aug 2025 14:54:49 +0200 Subject: [PATCH] Avoid showing in options map when it doesn't do anything The code duplication between Escape and EscapeEnabled is unfortunate, but I don't see a better way to solve this. --- pkg/gui/controllers/global_controller.go | 21 +++++++--- pkg/gui/controllers/quit_actions.go | 40 +++++++++++++++++++ ...inding_suggestions_when_switching_repos.go | 4 +- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/pkg/gui/controllers/global_controller.go b/pkg/gui/controllers/global_controller.go index 9dfbeb454..bf9923182 100644 --- a/pkg/gui/controllers/global_controller.go +++ b/pkg/gui/controllers/global_controller.go @@ -115,11 +115,12 @@ func (self *GlobalController) GetKeybindings(opts types.KeybindingsOpts) []*type Handler: self.quitWithoutChangingDirectory, }, { - Key: opts.GetKey(opts.Config.Universal.Return), - Modifier: gocui.ModNone, - Handler: self.escape, - Description: self.c.Tr.Cancel, - DisplayOnScreen: true, + Key: opts.GetKey(opts.Config.Universal.Return), + Modifier: gocui.ModNone, + Handler: self.escape, + Description: self.c.Tr.Cancel, + GetDisabledReason: self.escapeEnabled, + DisplayOnScreen: true, }, { Key: opts.GetKey(opts.Config.Universal.ToggleWhitespaceInDiffView), @@ -190,6 +191,16 @@ func (self *GlobalController) escape() error { return (&QuitActions{c: self.c}).Escape() } +func (self *GlobalController) escapeEnabled() *types.DisabledReason { + if (&QuitActions{c: self.c}).EscapeEnabled() { + return nil + } + + // The empty error text is intentional. We don't want to show an error + // toast for this, but only hide it from the options map. + return &types.DisabledReason{Text: ""} +} + func (self *GlobalController) toggleWhitespace() error { return (&ToggleWhitespaceAction{c: self.c}).Call() } diff --git a/pkg/gui/controllers/quit_actions.go b/pkg/gui/controllers/quit_actions.go index 381828c1f..f9b047357 100644 --- a/pkg/gui/controllers/quit_actions.go +++ b/pkg/gui/controllers/quit_actions.go @@ -48,6 +48,8 @@ func (self *QuitActions) confirmQuitDuringUpdate() error { } func (self *QuitActions) Escape() error { + // If you make changes to this function, be sure to update EscapeEnabled accordingly. + currentContext := self.c.Context().Current() if listContext, ok := currentContext.(types.IListContext); ok { @@ -90,3 +92,41 @@ func (self *QuitActions) Escape() error { return nil } + +func (self *QuitActions) EscapeEnabled() bool { + currentContext := self.c.Context().Current() + + if listContext, ok := currentContext.(types.IListContext); ok { + if listContext.GetList().IsSelectingRange() { + return true + } + } + + if ctx, ok := currentContext.(types.IFilterableContext); ok { + if ctx.IsFiltering() { + return true + } + } + + parentContext := currentContext.GetParentContext() + if parentContext != nil { + return true + } + + for _, mode := range self.c.Helpers().Mode.Statuses() { + if mode.IsActive() { + return true + } + } + + repoPathStack := self.c.State().GetRepoPathStack() + if !repoPathStack.IsEmpty() { + return true + } + + if self.c.UserConfig().QuitOnTopLevelReturn { + return true + } + + return false +} diff --git a/pkg/integration/tests/ui/keybinding_suggestions_when_switching_repos.go b/pkg/integration/tests/ui/keybinding_suggestions_when_switching_repos.go index d9c24aeb7..def9a53c7 100644 --- a/pkg/integration/tests/ui/keybinding_suggestions_when_switching_repos.go +++ b/pkg/integration/tests/ui/keybinding_suggestions_when_switching_repos.go @@ -31,12 +31,12 @@ var KeybindingSuggestionsWhenSwitchingRepos = NewIntegrationTest(NewIntegrationT t.Views().Files().Focus() t.Views().Options().Content( - Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: ")) + Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?")) switchToRepo("other") switchToRepo("repo") t.Views().Options().Content( - Equals("Commit: c | Stash: s | Reset: D | Keybindings: ? | Cancel: ")) + Equals("Commit: c | Stash: s | Reset: D | Keybindings: ?")) }, })