Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions pkg/gui/controllers/helpers/drag_autoscroller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package helpers

import (
"time"

"github.com/jesseduffield/lazygit/pkg/gui/types"
)

const (
dragAutoscrollInitialDelay = 300 * time.Millisecond
dragAutoscrollSlowInterval = 250 * time.Millisecond
dragAutoscrollFastInterval = 100 * time.Millisecond
dragAutoscrollVeryFastInterval = 50 * time.Millisecond
)

// All state is UI-thread-owned. Timer goroutines only enqueue tick back onto
// the UI thread, where generation changes and scroll callbacks are serialized
// with mouse handlers and focus changes.
type DragAutoscroller struct {
c *HelperCommon
context types.Context

canScroll func(direction int) bool
onScroll func(viewIndex int) bool

// Incremented whenever the scroll direction changes or the autoscroller
// is canceled. A scheduled tick carries the generation it was created
// for, so stale ticks can be told apart from the one that is current.
generation uint64
direction int
interval time.Duration
// Last known pointer position relative to the viewport; used by ticks to
// compute which line ends up under the pointer after scrolling.
pointerViewportY int
}

func NewDragAutoscroller(
c *HelperCommon,
context types.Context,
canScroll func(direction int) bool,
onScroll func(viewIndex int) bool,
) *DragAutoscroller {
return &DragAutoscroller{
c: c,
context: context,
canScroll: canScroll,
onScroll: onScroll,
}
}

// Update is called with the pointer position of every drag event. Entering a
// scroll zone arms a timer (with an initial delay, so that merely passing
// through the zone doesn't scroll); once armed, scrolling continues on its
// own until the pointer leaves the zone, the drag ends, or a callback stops
// it.
func (self *DragAutoscroller) Update(pointerViewportY int) {
_, viewportHeight := self.context.GetViewTrait().ViewPortYBounds()
direction, interval := dragAutoscrollZone(viewportHeight, pointerViewportY)
if direction != 0 && self.canScroll != nil && !self.canScroll(direction) {
direction = 0
interval = 0
}

self.pointerViewportY = pointerViewportY
generation, schedule := self.updateState(direction, interval)
if schedule {
self.schedule(generation, dragAutoscrollInitialDelay)
}
}

func (self *DragAutoscroller) Direction() int {
return self.direction
}

func (self *DragAutoscroller) updateState(direction int, interval time.Duration) (uint64, bool) {
if direction == self.direction {
self.interval = interval
return self.generation, false
}

self.generation++
self.direction = direction
self.interval = interval
return self.generation, direction != 0
}

func (self *DragAutoscroller) Cancel() {
self.generation++
self.direction = 0
self.interval = 0
}

func (self *DragAutoscroller) schedule(generation uint64, delay time.Duration) {
time.AfterFunc(delay, func() {
self.c.OnUIThreadBackground(func() error {
self.tick(generation)
return nil
})
})
}

func (self *DragAutoscroller) tick(generation uint64) {
if generation != self.generation {
return
}
if self.direction == 0 ||
self.canScroll != nil && !self.canScroll(self.direction) {
self.Cancel()
return
}

view := self.context.GetViewTrait()
oldOriginY, _ := view.ViewPortYBounds()
if self.direction < 0 {
view.ScrollUp(1)
} else {
view.ScrollDown(1)
}
newOriginY, _ := view.ViewPortYBounds()
if newOriginY == oldOriginY {
self.Cancel()
return
}

if !self.onScroll(newOriginY + self.pointerViewportY) {
self.Cancel()
return
}

self.schedule(generation, self.interval)
}

// dragAutoscrollZone returns the scroll direction and tick interval for a
// pointer position: anything beyond the view scrolls very fast, the outermost
// viewport row scrolls fast, the row just inside it scrolls slowly, and anything
// further inside doesn't scroll at all.
func dragAutoscrollZone(viewportHeight int, pointerViewportY int) (int, time.Duration) {
switch {
case pointerViewportY < 0:
return -1, dragAutoscrollVeryFastInterval
case pointerViewportY == 0:
return -1, dragAutoscrollFastInterval
case pointerViewportY == 1:
return -1, dragAutoscrollSlowInterval
case pointerViewportY > viewportHeight-1:
return 1, dragAutoscrollVeryFastInterval
case pointerViewportY == viewportHeight-1:
return 1, dragAutoscrollFastInterval
case pointerViewportY == viewportHeight-2:
return 1, dragAutoscrollSlowInterval
default:
return 0, 0
}
}
62 changes: 62 additions & 0 deletions pkg/gui/controllers/helpers/drag_autoscroller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package helpers

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestDragAutoscrollZone(t *testing.T) {
testCases := []struct {
name string
pointerViewportY int
expectedDirection int
expectedInterval time.Duration
}{
{name: "above view", pointerViewportY: -1, expectedDirection: -1, expectedInterval: dragAutoscrollVeryFastInterval},
{name: "top outer row", pointerViewportY: 0, expectedDirection: -1, expectedInterval: dragAutoscrollFastInterval},
{name: "top inner row", pointerViewportY: 1, expectedDirection: -1, expectedInterval: dragAutoscrollSlowInterval},
{name: "middle", pointerViewportY: 5},
{name: "bottom inner row", pointerViewportY: 8, expectedDirection: 1, expectedInterval: dragAutoscrollSlowInterval},
{name: "bottom outer row", pointerViewportY: 9, expectedDirection: 1, expectedInterval: dragAutoscrollFastInterval},
{name: "below view", pointerViewportY: 10, expectedDirection: 1, expectedInterval: dragAutoscrollVeryFastInterval},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
direction, interval := dragAutoscrollZone(10, testCase.pointerViewportY)

assert.Equal(t, testCase.expectedDirection, direction)
assert.Equal(t, testCase.expectedInterval, interval)
})
}
}

func TestDragAutoscrollerDoesNotRestartWhenMovingToOuterEdge(t *testing.T) {
self := &DragAutoscroller{
generation: 1,
direction: 1,
interval: dragAutoscrollSlowInterval,
}

generation, schedule := self.updateState(1, dragAutoscrollFastInterval)

assert.Equal(t, uint64(1), generation)
assert.False(t, schedule)
assert.Equal(t, dragAutoscrollFastInterval, self.interval)
}

func TestStaleDragAutoscrollTickDoesNotCancelCurrentGeneration(t *testing.T) {
self := &DragAutoscroller{
generation: 4,
direction: 1,
interval: dragAutoscrollFastInterval,
}

self.tick(2)

assert.Equal(t, uint64(4), self.generation)
assert.Equal(t, 1, self.direction)
assert.Equal(t, dragAutoscrollFastInterval, self.interval)
}
90 changes: 79 additions & 11 deletions pkg/gui/controllers/patch_explorer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"github.com/jesseduffield/lazygit/pkg/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/samber/lo"
)
Expand All @@ -19,18 +20,27 @@ func NewPatchExplorerControllerFactory(c *ControllerCommon) *PatchExplorerContro
}

func (self *PatchExplorerControllerFactory) Create(context types.IPatchExplorerContext) *PatchExplorerController {
return &PatchExplorerController{
controller := &PatchExplorerController{
baseController: baseController{},
c: self.c,
context: context,
}
controller.dragAutoscroller = helpers.NewDragAutoscroller(
self.c.HelperCommon,
context,
controller.canDragAutoscroll,
controller.handleDragAutoscroll,
)
return controller
}

type PatchExplorerController struct {
baseController
c *ControllerCommon

context types.IPatchExplorerContext
context types.IPatchExplorerContext
dragAutoscroller *helpers.DragAutoscroller
draggingWithMouse bool
}

func (self *PatchExplorerController) Context() types.Context {
Expand Down Expand Up @@ -153,13 +163,77 @@ func (self *PatchExplorerController) GetMouseKeybindings(opts types.KeybindingsO
ViewName: self.context.GetViewName(),
Key: gocui.MouseLeft,
Modifier: gocui.ModMotion,
Handler: func(gocui.ViewMouseBindingOpts) error {
return self.withRenderAndFocus(self.HandleMouseDrag)()
},
Handler: self.handleMouseDrag,
},
{
ViewName: self.context.GetViewName(),
Key: gocui.MouseRelease,
Handler: func(gocui.ViewMouseBindingOpts) error { return self.handleDragRelease() },
},
}
}

func (self *PatchExplorerController) handleMouseDrag(opts gocui.ViewMouseBindingOpts) error {
if err := self.withLock(func() error {
self.context.GetState().DragSelectLine(opts.Y)
self.renderDragSelection()
return nil
})(); err != nil {
return err
}

self.draggingWithMouse = true
originY, _ := self.context.GetViewTrait().ViewPortYBounds()
self.dragAutoscroller.Update(opts.Y - originY)
return nil
}

func (self *PatchExplorerController) canDragAutoscroll(int) bool {
state := self.context.GetState()
return state != nil && state.SelectingRange()
}

func (self *PatchExplorerController) handleDragAutoscroll(viewIndex int) bool {
if !self.canDragAutoscroll(0) {
return false
}

if err := self.withLock(func() error {
self.context.GetState().DragSelectLine(viewIndex)
self.renderDragSelection()
return nil
})(); err != nil {
return false
}
return true
}

func (self *PatchExplorerController) renderDragSelection() {
view := self.context.GetView()
state := self.context.GetState()
originY := view.OriginY()
startIndex, _ := state.SelectedViewRange()
view.SetRangeSelectStart(startIndex)
view.SetCursorY(state.GetSelectedViewLineIdx() - originY)
self.context.Render()
}

func (self *PatchExplorerController) handleDragRelease() error {
self.draggingWithMouse = false
self.dragAutoscroller.Cancel()
return nil
}

func (self *PatchExplorerController) GetOnFocusLost() func(types.OnFocusLostOpts) {
return func(types.OnFocusLostOpts) {
self.dragAutoscroller.Cancel()
if self.draggingWithMouse {
self.draggingWithMouse = false
self.c.GocuiGui().CancelMouseCapture()
}
}
}

func (self *PatchExplorerController) HandlePrevLine() error {
before := self.context.GetState().GetSelectedViewLineIdx()
self.context.GetState().CycleSelection(false)
Expand Down Expand Up @@ -266,12 +340,6 @@ func (self *PatchExplorerController) HandleMouseDown() error {
return nil
}

func (self *PatchExplorerController) HandleMouseDrag() error {
self.context.GetState().DragSelectLine(self.context.GetViewTrait().SelectedLineIdx())

return nil
}

func (self *PatchExplorerController) CopySelectedToClipboard() error {
selected := self.context.GetState().PlainRenderSelected()

Expand Down
32 changes: 26 additions & 6 deletions pkg/gui/gui_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,33 @@ func (self *GuiDriver) PressKeysRapidly(keyStrs ...string) {
func (self *GuiDriver) Click(x, y int) {
self.CheckAllToastsAcknowledged()

self.replayMouseEvent(x, y, tcell.ButtonPrimary)
self.replayMouseEvent(x, y, tcell.ButtonNone)
}

func (self *GuiDriver) ClickAndHold(x, y int) {
self.CheckAllToastsAcknowledged()
self.replayMouseEvent(x, y, tcell.ButtonPrimary)
}

// MouseMove reports the mouse at a new position with the left button still
// held down, i.e. a drag movement. (No test needs pointer motion without a
// button held, so that variant doesn't exist.)
func (self *GuiDriver) MouseMove(x, y int) {
self.replayMouseEvent(x, y, tcell.ButtonPrimary)
}

func (self *GuiDriver) MouseRelease(x, y int) {
self.replayMouseEvent(x, y, tcell.ButtonNone)
}

func (self *GuiDriver) OnUIThreadAndWait(f func()) {
_ = self.gui.g.OnUIThreadAndWait(func() error { f(); return nil })
}

func (self *GuiDriver) replayMouseEvent(x, y int, buttons tcell.ButtonMask) {
self.gui.g.ReplayMouseEvent(gocui.NewTcellMouseEventWrapper(
tcell.NewEventMouse(x, y, tcell.ButtonPrimary, 0),
0,
))
self.waitTillIdle()
self.gui.g.ReplayMouseEvent(gocui.NewTcellMouseEventWrapper(
tcell.NewEventMouse(x, y, tcell.ButtonNone, 0),
tcell.NewEventMouse(x, y, buttons, 0),
0,
))
self.waitTillIdle()
Expand Down
Loading
Loading