1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/list_controller.go

184 lines
5.8 KiB
Go
Raw Normal View History

2022-02-05 08:04:10 +02:00
package controllers
import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
type ListControllerFactory struct {
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-02-05 08:04:10 +02:00
}
2023-03-23 09:47:29 +02:00
func NewListControllerFactory(c *ControllerCommon) *ListControllerFactory {
2022-02-05 08:04:10 +02:00
return &ListControllerFactory{
2023-03-23 09:47:29 +02:00
c: c,
2022-02-05 08:04:10 +02:00
}
}
func (self *ListControllerFactory) Create(context types.IListContext) *ListController {
return &ListController{
2023-03-23 09:47:29 +02:00
baseController: baseController{},
c: self.c,
context: context,
2022-02-05 08:04:10 +02:00
}
}
type ListController struct {
baseController
2023-03-23 09:47:29 +02:00
c *ControllerCommon
2022-02-05 08:04:10 +02:00
context types.IListContext
}
func (self *ListController) Context() types.Context {
return self.context
}
func (self *ListController) HandlePrevLine() error {
return self.handleLineChange(-1)
}
func (self *ListController) HandleNextLine() error {
return self.handleLineChange(1)
}
func (self *ListController) HandleScrollLeft() error {
2022-04-16 07:27:56 +02:00
return self.scrollHorizontal(self.context.GetViewTrait().ScrollLeft)
2022-02-05 08:04:10 +02:00
}
func (self *ListController) HandleScrollRight() error {
2022-04-16 07:27:56 +02:00
return self.scrollHorizontal(self.context.GetViewTrait().ScrollRight)
2022-02-05 08:04:10 +02:00
}
2022-04-16 07:27:56 +02:00
func (self *ListController) HandleScrollUp() error {
scrollHeight := self.c.UserConfig.Gui.ScrollHeight
self.context.GetViewTrait().ScrollUp(scrollHeight)
2022-04-16 07:27:56 +02:00
return nil
}
func (self *ListController) HandleScrollDown() error {
scrollHeight := self.c.UserConfig.Gui.ScrollHeight
self.context.GetViewTrait().ScrollDown(scrollHeight)
2022-04-16 07:27:56 +02:00
return nil
}
func (self *ListController) scrollHorizontal(scrollFunc func()) error {
2022-02-05 08:04:10 +02:00
scrollFunc()
return self.context.HandleFocus(types.OnFocusOpts{})
2022-02-05 08:04:10 +02:00
}
func (self *ListController) handleLineChange(change int) error {
before := self.context.GetList().GetSelectedLineIdx()
self.context.GetList().MoveSelectedLine(change)
after := self.context.GetList().GetSelectedLineIdx()
2022-02-27 02:42:22 +02:00
if err := self.pushContextIfNotFocused(); err != nil {
return err
}
2022-02-05 08:04:10 +02:00
// 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 {
if change == -1 {
checkScrollUp(self.context.GetViewTrait(), self.c.UserConfig,
self.context.ModelIndexToViewIndex(before), self.context.ModelIndexToViewIndex(after))
} else if change == 1 {
checkScrollDown(self.context.GetViewTrait(), self.c.UserConfig,
self.context.ModelIndexToViewIndex(before), self.context.ModelIndexToViewIndex(after))
}
return self.context.HandleFocus(types.OnFocusOpts{})
2022-02-05 08:04:10 +02:00
}
return nil
}
func (self *ListController) HandlePrevPage() error {
return self.handleLineChange(-self.context.GetViewTrait().PageDelta())
}
func (self *ListController) HandleNextPage() error {
return self.handleLineChange(self.context.GetViewTrait().PageDelta())
}
func (self *ListController) HandleGotoTop() error {
2022-03-19 00:31:52 +02:00
return self.handleLineChange(-self.context.GetList().Len())
2022-02-05 08:04:10 +02:00
}
func (self *ListController) HandleGotoBottom() error {
2022-03-19 00:31:52 +02:00
return self.handleLineChange(self.context.GetList().Len())
2022-02-05 08:04:10 +02:00
}
2022-02-27 02:42:22 +02:00
func (self *ListController) HandleClick(opts gocui.ViewMouseBindingOpts) error {
2022-02-05 08:04:10 +02:00
prevSelectedLineIdx := self.context.GetList().GetSelectedLineIdx()
newSelectedLineIdx := self.context.ViewIndexToModelIndex(opts.Y)
2022-02-27 02:42:22 +02:00
alreadyFocused := self.isFocused()
2022-02-05 08:04:10 +02:00
2022-02-27 02:42:22 +02:00
if err := self.pushContextIfNotFocused(); err != nil {
return err
2022-02-05 08:04:10 +02:00
}
2022-03-19 00:31:52 +02:00
if newSelectedLineIdx > self.context.GetList().Len()-1 {
2022-02-05 08:04:10 +02:00
return nil
}
self.context.GetList().SetSelectedLineIdx(newSelectedLineIdx)
2022-02-27 02:42:22 +02:00
if prevSelectedLineIdx == newSelectedLineIdx && alreadyFocused && self.context.GetOnClick() != nil {
return self.context.GetOnClick()()
2022-02-05 08:04:10 +02:00
}
return self.context.HandleFocus(types.OnFocusOpts{})
2022-02-05 08:04:10 +02:00
}
2022-02-27 02:42:22 +02:00
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()
}
2022-02-05 08:04:10 +02:00
func (self *ListController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
return []*types.Binding{
2022-02-27 02:42:22 +02:00
{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.PrevPage},
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.NextPage), Handler: self.HandleNextPage, Description: self.c.Tr.NextPage},
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoTop), Handler: self.HandleGotoTop, Description: self.c.Tr.GotoTop},
2022-02-27 02:42:22 +02:00
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollLeft), Handler: self.HandleScrollLeft},
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.ScrollRight), Handler: self.HandleScrollRight},
{Tag: "navigation", Key: opts.GetKey(opts.Config.Universal.GotoBottom), Handler: self.HandleGotoBottom, Description: self.c.Tr.GotoBottom},
2022-02-05 08:04:10 +02:00
}
}
2022-02-27 02:42:22 +02:00
func (self *ListController) GetMouseKeybindings(opts types.KeybindingsOpts) []*gocui.ViewMouseBinding {
return []*gocui.ViewMouseBinding{
{
ViewName: self.context.GetViewName(),
Key: gocui.MouseWheelUp,
Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleScrollUp() },
2022-02-27 02:42:22 +02:00
},
{
ViewName: self.context.GetViewName(),
Key: gocui.MouseLeft,
Handler: func(opts gocui.ViewMouseBindingOpts) error { return self.HandleClick(opts) },
2022-02-27 02:42:22 +02:00
},
{
ViewName: self.context.GetViewName(),
Key: gocui.MouseWheelDown,
Handler: func(gocui.ViewMouseBindingOpts) error { return self.HandleScrollDown() },
2022-02-27 02:42:22 +02:00
},
}
}