summaryrefslogtreecommitdiffstats
path: root/pkg/gui
diff options
context:
space:
mode:
authorJesse Duffield <jessedduffield@gmail.com>2022-02-27 11:42:22 +1100
committerJesse Duffield <jessedduffield@gmail.com>2022-03-17 19:13:40 +1100
commit59d4df2a4483993eeebaa0e79feb6c62493bcfe0 (patch)
tree6155e28bc8c0d405bb64c0b7b126571547f82623 /pkg/gui
parentcf00949b85b72e4d4726c127a285b748a6a4ba55 (diff)
fix click handling
Diffstat (limited to 'pkg/gui')
-rw-r--r--pkg/gui/context/base_context.go11
-rw-r--r--pkg/gui/controllers.go1
-rw-r--r--pkg/gui/controllers/attach.go1
-rw-r--r--pkg/gui/controllers/base_controller.go4
-rw-r--r--pkg/gui/controllers/commitish_controller.go4
-rw-r--r--pkg/gui/controllers/commits_files_controller.go10
-rw-r--r--pkg/gui/controllers/files_controller.go28
-rw-r--r--pkg/gui/controllers/list_controller.go83
-rw-r--r--pkg/gui/controllers/local_commits_controller.go4
-rw-r--r--pkg/gui/controllers/menu_controller.go8
-rw-r--r--pkg/gui/controllers/remotes_controller.go8
-rw-r--r--pkg/gui/controllers/sub_commits_switch_controller.go4
-rw-r--r--pkg/gui/controllers/submodules_controller.go8
-rw-r--r--pkg/gui/keybindings.go7
-rw-r--r--pkg/gui/types/context.go6
15 files changed, 120 insertions, 67 deletions
diff --git a/pkg/gui/context/base_context.go b/pkg/gui/context/base_context.go
index b4beb293d..9b006662f 100644
--- a/pkg/gui/context/base_context.go
+++ b/pkg/gui/context/base_context.go
@@ -14,6 +14,7 @@ type BaseContext struct {
keybindingsFns []types.KeybindingsFn
mouseKeybindingsFns []types.MouseKeybindingsFn
+ onClickFn func() error
focusable bool
@@ -90,6 +91,16 @@ func (self *BaseContext) AddMouseKeybindingsFn(fn types.MouseKeybindingsFn) {
self.mouseKeybindingsFns = append(self.mouseKeybindingsFns, fn)
}
+func (self *BaseContext) AddOnClickFn(fn func() error) {
+ if fn != nil {
+ self.onClickFn = fn
+ }
+}
+
+func (self *BaseContext) GetOnClick() func() error {
+ return self.onClickFn
+}
+
func (self *BaseContext) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
bindings := []*gocui.ViewMouseBinding{}
for i := range self.mouseKeybindingsFns {
diff --git a/pkg/gui/controllers.go b/pkg/gui/controllers.go
index 0755bbe8e..04abf1103 100644
--- a/pkg/gui/controllers.go
+++ b/pkg/gui/controllers.go
@@ -173,6 +173,7 @@ func (gui *Gui) resetControllers() {
controllers.AttachControllers(gui.State.Contexts.RemoteBranches, remoteBranchesController)
controllers.AttachControllers(gui.State.Contexts.Global, gui.Controllers.Sync, gui.Controllers.Undo, gui.Controllers.Global)
+ // this must come last so that we've got our click handlers defined against the context
listControllerFactory := controllers.NewListControllerFactory(gui.c)
for _, context := range gui.getListContexts() {
controllers.AttachControllers(context, listControllerFactory.Create(context))
diff --git a/pkg/gui/controllers/attach.go b/pkg/gui/controllers/attach.go
index 008c15505..3e621c54c 100644
--- a/pkg/gui/controllers/attach.go
+++ b/pkg/gui/controllers/attach.go
@@ -6,5 +6,6 @@ func AttachControllers(context types.Context, controllers ...types.IController)
for _, controller := range controllers {
context.AddKeybindingsFn(controller.GetKeybindings)
context.AddMouseKeybindingsFn(controller.GetMouseKeybindings)
+ context.AddOnClickFn(controller.GetOnClick())
}
}
diff --git a/pkg/gui/controllers/base_controller.go b/pkg/gui/controllers/base_controller.go
index e510c1a9f..db7ad7a40 100644
--- a/pkg/gui/controllers/base_controller.go
+++ b/pkg/gui/controllers/base_controller.go
@@ -14,3 +14,7 @@ func (self *baseController) GetKeybindings(opts types.KeybindingsOpts) []*types.
func (self *baseController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return nil
}
+
+func (self *baseController) GetOnClick() func() error {
+ return nil
+}
diff --git a/pkg/gui/controllers/commitish_controller.go b/pkg/gui/controllers/commitish_controller.go
index b570e4aba..04e271253 100644
--- a/pkg/gui/controllers/commitish_controller.go
+++ b/pkg/gui/controllers/commitish_controller.go
@@ -58,6 +58,10 @@ func (self *CommitishController) GetKeybindings(opts types.KeybindingsOpts) []*t
return bindings
}
+func (self *CommitishController) GetOnClick() func() error {
+ return self.checkSelected(self.enter)
+}
+
func (self *CommitishController) checkSelected(callback func(string) error) func() error {
return func() error {
refName := self.context.GetSelectedRefName()
diff --git a/pkg/gui/controllers/commits_files_controller.go b/pkg/gui/controllers/commits_files_controller.go
index 5eed10883..d0015faff 100644
--- a/pkg/gui/controllers/commits_files_controller.go
+++ b/pkg/gui/controllers/commits_files_controller.go
@@ -70,9 +70,10 @@ func (self *CommitFilesController) GetKeybindings(opts types.KeybindingsOpts) []
func (self *CommitFilesController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
- ViewName: "main",
- Key: gocui.MouseLeft,
- Handler: self.onClickMain,
+ ViewName: "main",
+ Key: gocui.MouseLeft,
+ Handler: self.onClickMain,
+ FromContext: string(self.context().GetKey()),
},
}
}
@@ -97,12 +98,11 @@ func (self *CommitFilesController) context() *context.CommitFilesContext {
}
func (self *CommitFilesController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
- clickedViewLineIdx := opts.Cy + opts.Oy
node := self.context().GetSelectedFileNode()
if node == nil {
return nil
}
- return self.enterCommitFile(node, types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: clickedViewLineIdx})
+ return self.enterCommitFile(node, types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: opts.Y})
}
func (self *CommitFilesController) checkout(node *filetree.CommitFileNode) error {
diff --git a/pkg/gui/controllers/files_controller.go b/pkg/gui/controllers/files_controller.go
index 5f6ccec7e..b10efc15c 100644
--- a/pkg/gui/controllers/files_controller.go
+++ b/pkg/gui/controllers/files_controller.go
@@ -50,10 +50,6 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
Handler: self.checkSelectedFileNode(self.press),
Description: self.c.Tr.LcToggleStaged,
},
- // {
- // Key: gocui.MouseLeft,
- // Handler: func() error { return self.context().HandleClick(self.checkSelectedFileNode(self.press)) },
- // },
{
Key: opts.GetKey("<c-b>"), // TODO: softcode
Handler: self.handleStatusFilterPressed,
@@ -153,18 +149,24 @@ func (self *FilesController) GetKeybindings(opts types.KeybindingsOpts) []*types
func (self *FilesController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
- ViewName: "main",
- Key: gocui.MouseLeft,
- Handler: self.onClickMain,
+ ViewName: "main",
+ Key: gocui.MouseLeft,
+ Handler: self.onClickMain,
+ FromContext: string(self.context().GetKey()),
},
{
- ViewName: "secondary",
- Key: gocui.MouseLeft,
- Handler: self.onClickSecondary,
+ ViewName: "secondary",
+ Key: gocui.MouseLeft,
+ Handler: self.onClickSecondary,
+ FromContext: string(self.context().GetKey()),
},
}
}
+func (self *FilesController) GetOnClick() func() error {
+ return self.checkSelectedFileNode(self.press)
+}
+
func (self *FilesController) press(node *filetree.FileNode) error {
if node.IsLeaf() {
file := node.File
@@ -631,13 +633,11 @@ func (self *FilesController) handleStashSave(stashFunc func(message string) erro
}
func (self *FilesController) onClickMain(opts gocui.ViewMouseBindingOpts) error {
- clickedViewLineIdx := opts.Cy + opts.Oy
- return self.EnterFile(types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: clickedViewLineIdx})
+ return self.EnterFile(types.OnFocusOpts{ClickedViewName: "main", ClickedViewLineIdx: opts.Y})
}
func (self *FilesController) onClickSecondary(opts gocui.ViewMouseBindingOpts) error {
- clickedViewLineIdx := opts.Cy + opts.Oy
- return self.EnterFile(types.OnFocusOpts{ClickedViewName: "secondary", ClickedViewLineIdx: clickedViewLineIdx})
+ return self.EnterFile(types.OnFocusOpts{ClickedViewName: "secondary", ClickedViewLineIdx: opts.Y})
}
func (self *FilesController) fetch() error {
diff --git a/pkg/gui/controllers/list_controller.go b/pkg/gui/controllers/list_controller.go
index 5b1d2e04a..c9898f908 100644
--- a/pkg/gui/controllers/list_controller.go
+++ b/pkg/gui/controllers/list_controller.go
@@ -61,6 +61,10 @@ func (self *ListController) handleLineChange(change int) error {
self.context.GetList().MoveSelectedLine(change)
after := self.context.GetList().GetSelectedLineIdx()
+ if err := self.pushContextIfNotFocused(); err != nil {
+ return err
+ }
+
// doing this check so that if we're holding the up key at the start of the list
// we're not constantly re-rendering the main view.
if before != after {
@@ -86,20 +90,13 @@ func (self *ListController) HandleGotoBottom() error {
return self.handleLineChange(self.context.GetList().GetItemsLength())
}
-func (self *ListController) HandleClick(onClick func() error) error {
+func (self *ListController) HandleClick(opts gocui.ViewMouseBindingOpts) error {
prevSelectedLineIdx := self.context.GetList().GetSelectedLineIdx()
- // because we're handling a click, we need to determine the new line idx based
- // on the view itself.
- newSelectedLineIdx := self.context.GetViewTrait().SelectedLineIdx()
+ newSelectedLineIdx := opts.Y
+ alreadyFocused := self.isFocused()
- currentContextKey := self.c.CurrentContext().GetKey()
- alreadyFocused := currentContextKey == self.context.GetKey()
-
- // we need to focus the view
- if !alreadyFocused {
- if err := self.c.PushContext(self.context); err != nil {
- return err
- }
+ if err := self.pushContextIfNotFocused(); err != nil {
+ return err
}
if newSelectedLineIdx > self.context.GetList().GetItemsLength()-1 {
@@ -108,26 +105,37 @@ func (self *ListController) HandleClick(onClick func() error) error {
self.context.GetList().SetSelectedLineIdx(newSelectedLineIdx)
- if prevSelectedLineIdx == newSelectedLineIdx && alreadyFocused && onClick != nil {
- return onClick()
+ if prevSelectedLineIdx == newSelectedLineIdx && alreadyFocused && self.context.GetOnClick() != nil {
+ return self.context.GetOnClick()()
}
return self.context.HandleFocus()
}
+func (self *ListController) pushContextIfNotFocused() error {
+ if !self.isFocused() {
+ if err := self.c.PushContext(self.context); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+func (self *ListController) isFocused() bool {
+ return self.c.CurrentContext().GetKey() == self.context.GetKey()
+}
+
func (self *ListController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
return []*types.Binding{
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItemAlt), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItem), Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
- {Tag: "navigation", Key: gocui.MouseWheelUp, Modifier: gocui.ModNone, Handler: self.HandlePrevLine},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItemAlt), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItem), Modifier: gocui.ModNone, Handler: self.HandleNextLine},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevPage), Modifier: gocui.ModNone, Handler: self.HandlePrevPage, Description: self.c.Tr.LcPrevPage},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Modifier: gocui.ModNone, Handler: self.HandleNextPage, Description: self.c.Tr.LcNextPage},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Modifier: gocui.ModNone, Handler: self.HandleGotoTop, Description: self.c.Tr.LcGotoTop},
- {Key: gocui.MouseLeft, Modifier: gocui.ModNone, Handler: func() error { return self.HandleClick(nil) }},
- {Tag: "navigation", Key: gocui.MouseWheelDown, Modifier: gocui.ModNone, Handler: self.HandleNextLine},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollLeft), Modifier: gocui.ModNone, Handler: self.HandleScrollLeft},
- {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollRight), Modifier: gocui.ModNone, Handler: self.HandleScrollRight},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItemAlt), Handler: self.HandlePrevLine},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevItem), Handler: self.HandlePrevLine},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItemAlt), Handler: self.HandleNextLine},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextItem), Handler: self.HandleNextLine},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.PrevPage), Handler: self.HandlePrevPage, Description: self.c.Tr.LcPrevPage},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Handler: self.HandleNextPage, Description: self.c.Tr.LcNextPage},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Handler: self.HandleGotoTop, Description: self.c.Tr.LcGotoTop},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollLeft), Handler: self.HandleScrollLeft},
+ {Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollRight), Handler: self.HandleScrollRight},
{
Key: opts.GetKey(opts.Config.Universal.StartSearch),
Handler: func() error { self.c.OpenSearch(); return nil },
@@ -142,3 +150,26 @@ func (self *ListController) GetKeybindings(opts types.KeybindingsOpts) []*types.
},
}
}
+
+func (self *ListController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
+ return []*gocui.ViewMouseBinding{
+ {
+ ViewName: self.context.GetViewName(),
+ ToContext: string(self.context.GetKey()),
+ Key: gocui.MouseWheelUp,
+ Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandlePrevLine() },
+ },
+ {
+ ViewName: self.context.GetViewName(),
+ ToContext: string(self.context.GetKey()),
+ Key: gocui.MouseLeft,
+ Handler: func(opts gocui.ViewMouseBindingOpts) error { return self.HandleClick(opts) },
+ },
+ {
+ ViewName: self.context.GetViewName(),
+ ToContext: string(self.context.GetKey()),
+ Key: gocui.MouseWheelDown,
+ Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleNextLine() },
+ },
+ }
+}
diff --git a/pkg/gui/controllers/local_commits_controller.go b/pkg/gui/controllers/local_commits_controller.go
index 1693f19c3..b54cfa3c0 100644
--- a/pkg/gui/controllers/local_commits_controller.go
+++ b/pkg/gui/controllers/local_commits_controller.go
@@ -136,10 +136,6 @@ func (self *LocalCommitsController) GetKeybindings(opts types.KeybindingsOpts) [
Description: self.c.Tr.LcGotoBottom,
Tag: "navigation",
},
- // {
- // Key: gocui.MouseLeft,
- // Handler: func() error { return self.context().HandleClick(self.checkSelected(self.enter)) },
- // },
}
for _, binding := range outsideFilterModeBindings {
diff --git a/pkg/gui/controllers/menu_controller.go b/pkg/gui/controllers/menu_controller.go
index 91e85dec5..f217c993a 100644
--- a/pkg/gui/controllers/menu_controller.go
+++ b/pkg/gui/controllers/menu_controller.go
@@ -35,15 +35,15 @@ func (self *MenuController) GetKeybindings(opts types.KeybindingsOpts) []*types.
Key: opts.GetKey(opts.Config.Universal.ConfirmAlt1),
Handler: self.press,
},
- // {
- // Key: gocui.MouseLeft,
- // Handler: func() error { return self.context.HandleClick(self.press) },
- // },
}
return bindings
}
+func (self *MenuController) GetOnClick() func() error {
+ return self.press
+}
+
func (self *MenuController) press() error {
selectedItem := self.context().GetSelected()
diff --git a/pkg/gui/controllers/remotes_controller.go b/pkg/gui/controllers/remotes_controller.go
index 489454f89..fd4b34297 100644
--- a/pkg/gui/controllers/remotes_controller.go
+++ b/pkg/gui/controllers/remotes_controller.go
@@ -35,10 +35,6 @@ func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*typ
Key: opts.GetKey(opts.Config.Universal.GoInto),
Handler: self.checkSelected(self.enter),
},
- // {
- // Key: gocui.MouseLeft,
- // Handler: func() error { return self.context.HandleClick(self.checkSelected(self.enter)) },
- // },
{
Key: opts.GetKey(opts.Config.Branches.FetchRemote),
Handler: self.checkSelected(self.fetch),
@@ -64,6 +60,10 @@ func (self *RemotesController) GetKeybindings(opts types.KeybindingsOpts) []*typ
return bindings
}
+func (self *RemotesController) GetOnClick() func() error {
+ return self.checkSelected(self.enter)
+}
+
func (self *RemotesController) enter(remote *models.Remote) error {
// naive implementation: get the branches from the remote and render them to the list, change the context
self.setRemoteBranches(remote.Branches)
diff --git a/pkg/gui/controllers/sub_commits_switch_controller.go b/pkg/gui/controllers/sub_commits_switch_controller.go
index cbc9ce137..4c8f086a5 100644
--- a/pkg/gui/controllers/sub_commits_switch_controller.go
+++ b/pkg/gui/controllers/sub_commits_switch_controller.go
@@ -57,6 +57,10 @@ func (self *SubCommitsSwitchController) GetKeybindings(opts types.KeybindingsOpt
return bindings
}
+func (self *SubCommitsSwitchController) GetOnClick() func() error {
+ return self.viewCommits
+}
+
func (self *SubCommitsSwitchController) viewCommits() error {
refName := self.context.GetSelectedRefName()
if refName == "" {
diff --git a/pkg/gui/controllers/submodules_controller.go b/pkg/gui/controllers/submodules_controller.go
index 408536960..83c05da4b 100644
--- a/pkg/gui/controllers/submodules_controller.go
+++ b/pkg/gui/controllers/submodules_controller.go
@@ -69,13 +69,13 @@ func (self *SubmodulesController) GetKeybindings(opts types.KeybindingsOpts) []*
Description: self.c.Tr.LcViewBulkSubmoduleOptions,
OpensMenu: true,
},
- // {
- // Key: gocui.MouseLeft,
- // Handler: func() error { return self.context().HandleClick(self.checkSelected(self.enter)) },
- // },
}
}
+func (self *SubmodulesController) GetOnClick() func() error {
+ return self.checkSelected(self.enter)
+}
+
func (self *SubmodulesController) enter(submodule *models.SubmoduleConfig) error {
return self.enterSubmodule(submodule)
}
diff --git a/pkg/gui/keybindings.go b/pkg/gui/keybindings.go
index 12ceff538..d6a586a10 100644
--- a/pkg/gui/keybindings.go
+++ b/pkg/gui/keybindings.go
@@ -988,12 +988,7 @@ func (self *Gui) GetInitialKeybindings() ([]*types.Binding, []*gocui.ViewMouseBi
bindings = append(bindings, binding)
}
- for _, binding := range c.GetMouseKeybindings(opts) {
- if contextKey != context.GLOBAL_CONTEXT_KEY {
- binding.FromContext = string(contextKey)
- }
- mouseKeybindings = append(mouseKeybindings, binding)
- }
+ mouseKeybindings = append(mouseKeybindings, c.GetMouseKeybindings(opts)...)
}
for _, viewName := range []string{"status", "branches", "files", "commits", "commitFiles", "stash", "menu"} {
diff --git a/pkg/gui/types/context.go b/pkg/gui/types/context.go
index ed971d348..5e588da0d 100644
--- a/pkg/gui/types/context.go
+++ b/pkg/gui/types/context.go
@@ -38,6 +38,11 @@ type IBaseContext interface {
AddKeybindingsFn(KeybindingsFn)
AddMouseKeybindingsFn(MouseKeybindingsFn)
+
+ // This is a bit of a hack at the moment: we currently only set an onclick function so that
+ // our list controller can come along and wrap it in a list-specific click handler.
+ // We'll need to think of a better way to do this.
+ AddOnClickFn(func() error)
}
type Context interface {
@@ -94,6 +99,7 @@ type MouseKeybindingsFn func(opts KeybindingsOpts) []*gocui.ViewMouseBinding
type HasKeybindings interface {
GetKeybindings(opts KeybindingsOpts) []*Binding
GetMouseKeybindings(opts KeybindingsOpts) []*gocui.ViewMouseBinding
+ GetOnClick() func() error
}
type IController interface {