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

@ -10,13 +10,16 @@ import (
var _ types.IController = &SwitchToDiffFilesController{}
type CanSwitchToDiffFiles interface {
types.Context
types.IListContext
CanRebase() bool
GetSelectedRef() types.Ref
}
// 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 SwitchToDiffFilesController struct {
baseController
*ListControllerTrait[types.Ref]
c *ControllerCommon
context CanSwitchToDiffFiles
diffFilesContext *context.CommitFilesContext
@ -28,7 +31,12 @@ func NewSwitchToDiffFilesController(
diffFilesContext *context.CommitFilesContext,
) *SwitchToDiffFilesController {
return &SwitchToDiffFilesController{
baseController: baseController{},
baseController: baseController{},
ListControllerTrait: NewListControllerTrait[types.Ref](
c,
context,
context.GetSelectedRef,
),
c: c,
context: context,
diffFilesContext: diffFilesContext,
@ -38,9 +46,10 @@ func NewSwitchToDiffFilesController(
func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
bindings := []*types.Binding{
{
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
Description: self.c.Tr.ViewItemFiles,
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.withItem(self.enter),
GetDisabledReason: self.require(self.singleItemSelected()),
Description: self.c.Tr.ViewItemFiles,
},
}
@ -48,18 +57,7 @@ func (self *SwitchToDiffFilesController) GetKeybindings(opts types.KeybindingsOp
}
func (self *SwitchToDiffFilesController) GetOnClick() func() error {
return self.checkSelected(self.enter)
}
func (self *SwitchToDiffFilesController) checkSelected(callback func(types.Ref) error) func() error {
return func() error {
ref := self.context.GetSelectedRef()
if ref == nil {
return nil
}
return callback(ref)
}
return self.withItemGraceful(self.enter)
}
func (self *SwitchToDiffFilesController) enter(ref types.Ref) error {
@ -70,10 +68,6 @@ func (self *SwitchToDiffFilesController) enter(ref types.Ref) error {
})
}
func (self *SwitchToDiffFilesController) Context() types.Context {
return self.context
}
func (self *SwitchToDiffFilesController) viewFiles(opts SwitchToCommitFilesContextOpts) error {
diffFilesContext := self.diffFilesContext