akiyosi.goneovim/editor/scrollbar.go

193 lines
4.2 KiB
Go
Raw Normal View History

2018-09-16 21:48:17 +09:00
package editor
import (
2018-12-27 21:03:21 +09:00
"fmt"
2020-03-09 23:48:46 +09:00
"math"
2020-03-10 23:48:34 +09:00
"sync"
2018-12-27 21:03:21 +09:00
2020-03-09 23:48:46 +09:00
"github.com/akiyosi/goneovim/util"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
2020-09-26 12:04:36 +09:00
"github.com/therecipe/qt/widgets"
2018-09-16 21:48:17 +09:00
)
// ScrollBar is
type ScrollBar struct {
2020-09-26 12:04:36 +09:00
mu sync.Mutex
2020-03-10 23:48:34 +09:00
2020-03-09 23:48:46 +09:00
ws *Workspace
widget *widgets.QWidget
thumb *widgets.QWidget
pos int
height int
isPressed bool
beginPosY int
2018-09-16 21:48:17 +09:00
}
func newScrollBar() *ScrollBar {
widget := widgets.NewQWidget(nil, 0)
widget.SetContentsMargins(0, 0, 0, 0)
2018-09-22 07:44:44 +09:00
widget.SetFixedWidth(10)
2018-09-16 21:48:17 +09:00
thumb := widgets.NewQWidget(widget, 0)
2020-03-11 00:32:10 +09:00
thumb.SetFixedWidth(8)
2018-09-16 21:48:17 +09:00
scrollBar := &ScrollBar{
widget: widget,
thumb: thumb,
}
2020-09-26 12:04:36 +09:00
scrollBar.thumb.ConnectMousePressEvent(scrollBar.thumbPress)
2020-03-12 19:53:02 +09:00
scrollBar.thumb.ConnectMouseMoveEvent(scrollBar.thumbScroll)
scrollBar.thumb.ConnectMouseReleaseEvent(scrollBar.thumbRelease)
scrollBar.thumb.ConnectEnterEvent(scrollBar.thumbEnter)
scrollBar.thumb.ConnectLeaveEvent(scrollBar.thumbLeave)
2020-03-09 23:48:46 +09:00
2020-03-12 19:53:02 +09:00
scrollBar.widget.Hide()
2018-09-16 21:48:17 +09:00
return scrollBar
}
2020-03-12 19:53:02 +09:00
func (s *ScrollBar) thumbEnter(e *core.QEvent) {
2020-03-11 00:32:10 +09:00
color := editor.config.SideBar.AccentColor
2020-03-09 23:48:46 +09:00
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", color))
}
2020-03-12 19:53:02 +09:00
func (s *ScrollBar) thumbLeave(e *core.QEvent) {
2020-03-09 23:48:46 +09:00
color := editor.colors.scrollBarFg.String()
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", color))
}
2020-03-12 19:53:02 +09:00
func (s *ScrollBar) thumbPress(e *gui.QMouseEvent) {
2020-03-11 12:01:30 +09:00
switch e.Button() {
case core.Qt__LeftButton:
s.beginPosY = e.GlobalPos().Y()
s.isPressed = true
default:
}
2020-03-09 23:48:46 +09:00
}
2020-03-12 19:53:02 +09:00
func (s *ScrollBar) thumbScroll(e *gui.QMouseEvent) {
2020-03-10 23:48:34 +09:00
s.mu.Lock()
defer s.mu.Unlock()
2020-03-09 23:48:46 +09:00
win, ok := s.ws.screen.getWindow(s.ws.cursor.gridid)
if !ok {
return
}
font := win.getFont()
2020-03-11 00:32:10 +09:00
thumbHeight := s.height
if s.height < 20 {
thumbHeight = 20
}
2020-09-26 12:04:36 +09:00
ratio := float64((s.ws.maxLine*font.lineHeight)+thumbHeight) / float64(s.widget.Height())
2020-03-09 23:48:46 +09:00
v := s.beginPosY - e.GlobalPos().Y()
if v == 0 {
return
}
v2 := int(math.Ceil(float64(v) * ratio))
s.beginPosY = e.GlobalPos().Y()
2020-03-10 23:48:34 +09:00
s.scroll(v2, 0)
2020-03-09 23:48:46 +09:00
s.update()
}
2020-03-12 19:53:02 +09:00
func (s *ScrollBar) thumbRelease(e *gui.QMouseEvent) {
2020-03-09 23:48:46 +09:00
s.isPressed = false
s.scroll(0, 0)
}
// for smooth scroll, but it has some probrem
func (s *ScrollBar) scroll(v, h int) {
var vert int
var vertKey string
win, ok := s.ws.screen.getWindow(s.ws.cursor.gridid)
if !ok {
return
}
// Detect current mode
mode := win.s.ws.mode
if mode == "terminal-input" {
win.s.ws.nvim.Input(`<C-\><C-n>`)
} else if mode != "normal" {
win.s.ws.nvim.Input(win.s.ws.escKeyInInsert)
}
2020-03-09 23:48:46 +09:00
font := win.getFont()
isStopScroll := !s.isPressed
if int(math.Abs(float64(v))) >= font.lineHeight {
vert = v / font.lineHeight
} else {
vert, _ = win.smoothUpdate(v, h, isStopScroll)
}
if vert == 0 {
return
}
if vert > 0 {
vertKey = "Up"
} else {
vertKey = "Down"
}
if win.s.ws.isMappingScrollKey {
if vert != 0 {
win.s.ws.nvim.Input(fmt.Sprintf("<ScrollWheel%s>", vertKey))
}
} else {
if vert > 0 {
win.s.ws.nvim.Input(fmt.Sprintf("%v<C-y>", int(math.Abs(float64(vert)))))
} else if vert < 0 {
win.s.ws.nvim.Input(fmt.Sprintf("%v<C-e>", int(math.Abs(float64(vert)))))
}
}
}
2018-12-27 21:03:21 +09:00
func (s *ScrollBar) setColor() {
2019-02-17 18:00:09 +09:00
fg := editor.colors.scrollBarFg.String()
2018-12-27 21:03:21 +09:00
s.thumb.SetStyleSheet(fmt.Sprintf(" * { background: %s;}", fg))
2019-03-02 23:31:40 +09:00
s.widget.SetStyleSheet(" * { background: rgba(0, 0, 0, 0);}")
2018-12-27 21:03:21 +09:00
}
2018-09-16 21:48:17 +09:00
func (s *ScrollBar) update() {
2020-03-07 17:53:16 +09:00
win, ok := s.ws.screen.getWindow(s.ws.cursor.gridid)
if !ok {
return
}
top := win.scrollRegion[0]
bot := win.scrollRegion[1]
2018-09-16 21:48:17 +09:00
if top == 0 && bot == 0 {
2018-09-17 15:31:34 +09:00
top = 0
bot = s.ws.rows - 1
}
2020-03-10 23:48:34 +09:00
font := win.getFont()
relativeCursorY := int(float64(s.ws.cursor.y) / float64(font.lineHeight))
2018-09-17 15:31:34 +09:00
if s.ws.maxLine == 0 {
2018-09-30 23:43:21 +09:00
lnITF, err := s.ws.nvimEval("line('$')")
if err != nil {
s.ws.maxLine = 0
} else {
2020-03-09 23:48:46 +09:00
s.ws.maxLine = util.ReflectToInt(lnITF)
2018-09-30 23:43:21 +09:00
}
2018-09-16 21:48:17 +09:00
}
2020-03-09 23:48:46 +09:00
2018-09-16 21:48:17 +09:00
if s.ws.maxLine > bot-top {
2020-03-11 00:32:10 +09:00
s.height = int(float64(bot-top) / float64(s.ws.maxLine) * float64(s.ws.screen.widget.Height()))
thumbHeight := s.height
2018-09-22 07:44:44 +09:00
if s.height < 20 {
2020-03-11 00:32:10 +09:00
thumbHeight = 20
2018-09-22 07:44:44 +09:00
}
2020-03-11 00:32:10 +09:00
s.thumb.SetFixedHeight(thumbHeight)
2021-01-11 02:04:50 +09:00
s.pos = int(float64(s.ws.viewport[2]-relativeCursorY) / float64(s.ws.maxLine) * float64(s.ws.screen.widget.Height()))
2018-09-17 15:31:34 +09:00
s.thumb.Move2(0, s.pos)
2018-09-16 21:48:17 +09:00
s.widget.Show()
} else {
s.widget.Hide()
}
}