1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-30 03:23:08 +03:00

Enforce single-item selection in various actions

We want to show an error when the user tries to invoke an action that expects only
a single item to be selected.

We're using the GetDisabledReason field to enforce this (as well as DisabledReason
on menu items).

I've created a ListControllerTrait to store some shared convenience functions for this.
This commit is contained in:
Jesse Duffield
2024-01-14 13:51:20 +11:00
parent 280b4d60f8
commit 51fb82d6bf
45 changed files with 854 additions and 757 deletions

View File

@ -8,34 +8,43 @@ import (
var _ types.IController = &SwitchToSubCommitsController{}
type CanSwitchToSubCommits interface {
types.Context
types.IListContext
GetSelectedRef() types.Ref
ShowBranchHeadsInSubCommits() bool
}
// Not using our ListControllerTrait because our 'selected' item is not a list item
// but an attribute on it i.e. the ref of an item.
type SwitchToSubCommitsController struct {
baseController
*ListControllerTrait[types.Ref]
c *ControllerCommon
context CanSwitchToSubCommits
}
func NewSwitchToSubCommitsController(
controllerCommon *ControllerCommon,
c *ControllerCommon,
context CanSwitchToSubCommits,
) *SwitchToSubCommitsController {
return &SwitchToSubCommitsController{
baseController: baseController{},
c: controllerCommon,
context: context,
ListControllerTrait: NewListControllerTrait[types.Ref](
c,
context,
context.GetSelectedRef,
),
c: c,
context: context,
}
}
func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Handler: self.viewCommits,
Key: opts.GetKey(opts.Config.Universal.GoInto),
Description: self.c.Tr.ViewCommits,
Handler: self.viewCommits,
GetDisabledReason: self.require(self.singleItemSelected()),
Key: opts.GetKey(opts.Config.Universal.GoInto),
Description: self.c.Tr.ViewCommits,
},
}
@ -59,7 +68,3 @@ func (self *SwitchToSubCommitsController) viewCommits() error {
ShowBranchHeads: self.context.ShowBranchHeadsInSubCommits(),
})
}
func (self *SwitchToSubCommitsController) Context() types.Context {
return self.context
}