Skip to content

Commit cbb63a4

Browse files
authored
Merge pull request #530 from akiyosi/improve-smoothscroll
Improve smooth scroll
2 parents 6f5e787 + a974023 commit cbb63a4

File tree

6 files changed

+896
-467
lines changed

6 files changed

+896
-467
lines changed

editor/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (c *gonvimConfig) init() {
356356

357357
c.Editor.LineToScroll = 1
358358
c.Editor.SmoothScroll = false
359-
c.Editor.SmoothScrollDuration = 300
359+
c.Editor.SmoothScrollDuration = 750
360360
c.Editor.DisableHorizontalScroll = false
361361

362362
c.Editor.DrawBorderForFloatWindow = false
@@ -370,7 +370,7 @@ func (c *gonvimConfig) init() {
370370
c.Editor.DiffDeletePattern = 1
371371
c.Editor.DiffChangePattern = 1
372372

373-
c.Cursor.Duration = 70
373+
c.Cursor.Duration = 180
374374

375375
// ----
376376

editor/cursor.go

Lines changed: 55 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
// Cursor is
1313
type Cursor struct {
1414
widgets.QWidget
15+
16+
smoothMoveAnimation *core.QPropertyAnimation
1517
charCache *Cache
1618
font *Font
1719
fallbackfonts []*Font
@@ -692,9 +694,10 @@ func (c *Cursor) redraw() {
692694

693695
// paint() is to request update cursor widget.
694696
// NOTE: This function execution may not be necessary.
695-
// This is because move() is performed in the redraw() of the cursor,
696-
// and it seems that paintEvent is fired inside
697-
// the cursor widget in conjunction with this move processing.
697+
//
698+
// This is because move() is performed in the redraw() of the cursor,
699+
// and it seems that paintEvent is fired inside
700+
// the cursor widget in conjunction with this move processing.
698701
func (c *Cursor) paint() {
699702
if editor.isKeyAutoRepeating {
700703
return
@@ -703,6 +706,50 @@ func (c *Cursor) paint() {
703706
c.Update()
704707
}
705708

709+
func (c *Cursor) initializeOrReuseSmoothMoveAnimation() {
710+
if c.smoothMoveAnimation == nil {
711+
c.smoothMoveAnimation = core.NewQPropertyAnimation2(c, core.NewQByteArray2("animationProp", len("animationProp")), c)
712+
c.smoothMoveAnimation.SetDuration(int(editor.config.Cursor.Duration))
713+
c.smoothMoveAnimation.SetStartValue(core.NewQVariant10(float64(0.01)))
714+
c.smoothMoveAnimation.SetEndValue(core.NewQVariant10(1))
715+
c.smoothMoveAnimation.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutExpo))
716+
717+
c.smoothMoveAnimation.ConnectValueChanged(func(value *core.QVariant) {
718+
if !c.doAnimate {
719+
c.delta = 0
720+
c.deltax = 0
721+
c.deltay = 0
722+
c.move()
723+
c.paint()
724+
return
725+
}
726+
ok := false
727+
v := value.ToDouble(&ok)
728+
if !ok {
729+
return
730+
}
731+
732+
c.delta = v
733+
c.deltax = (c.x - c.xprime) * v
734+
c.deltay = (c.y - c.yprime) * v
735+
736+
if v == 1.0 {
737+
c.delta = 0
738+
c.deltax = 0
739+
c.deltay = 0
740+
c.doAnimate = false
741+
}
742+
743+
if c.doAnimate {
744+
c.animationStartX = c.xprime
745+
c.animationStartY = c.yprime
746+
}
747+
748+
c.move()
749+
})
750+
}
751+
}
752+
706753
func (c *Cursor) animateMove() {
707754
if !c.doAnimate {
708755
return
@@ -711,61 +758,13 @@ func (c *Cursor) animateMove() {
711758
return
712759
}
713760

714-
// process smooth scroll
715-
a := core.NewQPropertyAnimation2(c, core.NewQByteArray2("animationProp", len("animationProp")), c)
716-
a.ConnectValueChanged(func(value *core.QVariant) {
717-
if !c.doAnimate {
718-
c.delta = 0
719-
c.deltax = 0
720-
c.deltay = 0
721-
c.move()
722-
c.paint()
723-
return
724-
}
725-
ok := false
726-
v := value.ToDouble(&ok)
727-
if !ok {
728-
return
729-
}
761+
c.initializeOrReuseSmoothMoveAnimation()
730762

731-
c.delta = v
732-
c.deltax = (c.x - c.xprime) * v
733-
c.deltay = (c.y - c.yprime) * v
734-
735-
if v == 1.0 {
736-
c.delta = 0
737-
c.deltax = 0
738-
c.deltay = 0
739-
c.doAnimate = false
740-
}
741-
742-
if c.doAnimate {
743-
c.animationStartX = c.xprime
744-
c.animationStartY = c.yprime
745-
}
763+
if c.smoothMoveAnimation.State() == core.QAbstractAnimation__Running {
764+
c.smoothMoveAnimation.Stop()
765+
}
746766

747-
c.move()
748-
})
749-
duration := editor.config.Cursor.Duration
750-
a.SetDuration(int(duration))
751-
a.SetStartValue(core.NewQVariant10(float64(0.01)))
752-
a.SetEndValue(core.NewQVariant10(1))
753-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InOutCirc))
754-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutQuart))
755-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutExpo))
756-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutQuint))
757-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InOutCubic))
758-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InOutQuint))
759-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__Linear))
760-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InQuart))
761-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutCubic))
762-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InOutQuart))
763-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutInQuart))
764-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InOutExpo))
765-
a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__OutCirc))
766-
// a.SetEasingCurve(core.NewQEasingCurve(core.QEasingCurve__InCubic))
767-
768-
a.Start(core.QAbstractAnimation__DeletionPolicy(core.QAbstractAnimation__DeleteWhenStopped))
767+
c.smoothMoveAnimation.Start(core.QAbstractAnimation__DeletionPolicy(core.QAbstractAnimation__KeepWhenStopped))
769768
}
770769

771770
func (c *Cursor) resize(width, height int) {

editor/input.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ func (e *Editor) keyRelease(event *gui.QKeyEvent) {
1515
return
1616
}
1717

18-
if editor.config.Editor.SmoothScroll {
19-
ws := e.workspaces[e.active]
20-
win, ok := ws.screen.getWindow(ws.cursor.gridid)
21-
if !ok {
22-
return
23-
}
24-
if win.scrollPixels2 != 0 {
25-
return
26-
}
27-
win.grabScreenSnapshot()
28-
}
18+
// if editor.config.Editor.SmoothScroll {
19+
// ws := e.workspaces[e.active]
20+
// win, ok := ws.screen.getWindow(ws.cursor.gridid)
21+
// if !ok {
22+
// return
23+
// }
24+
// if win.scrollPixels2 != 0 {
25+
// return
26+
// }
27+
// win.grabScreenSnapshot()
28+
// }
2929

3030
e.isKeyAutoRepeating = false
3131
}
@@ -38,9 +38,12 @@ func (e *Editor) keyPress(event *gui.QKeyEvent) {
3838
if ws.nvim == nil {
3939
return
4040
}
41-
if !e.isKeyAutoRepeating {
42-
ws.getSnapshot()
41+
if event.IsAutoRepeat() {
42+
e.isKeyAutoRepeating = true
4343
}
44+
// if !e.isKeyAutoRepeating {
45+
// ws.getSnapshot()
46+
// }
4447
if !e.isHideMouse && e.config.Editor.HideMouseWhenTyping {
4548
bc := gui.NewQCursor2(core.Qt__BlankCursor)
4649
gui.QGuiApplication_SetOverrideCursor(bc)
@@ -51,9 +54,6 @@ func (e *Editor) keyPress(event *gui.QKeyEvent) {
5154
input := e.convertKey(event)
5255

5356
e.putLog("key input for nvim::", "input:", input)
54-
if event.IsAutoRepeat() {
55-
e.isKeyAutoRepeating = true
56-
}
5757
if input != "" {
5858
ws.nvim.Input(input)
5959
}

0 commit comments

Comments
 (0)