akiyosi.goneovim/editor/scrollbar.go

84 lines
1.8 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"
2018-09-16 21:48:17 +09:00
"github.com/therecipe/qt/widgets"
)
// ScrollBar is
type ScrollBar struct {
ws *Workspace
widget *widgets.QWidget
thumb *widgets.QWidget
pos int
height int
}
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)
thumb.SetFixedWidth(5)
scrollBar := &ScrollBar{
widget: widget,
thumb: thumb,
}
scrollBar.widget.Hide()
return scrollBar
}
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()
2019-03-02 23:31:40 +09:00
// bg := editor.colors.scrollBarBg.StringTransparent()
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(fmt.Sprintf(" * { background: %s;}", bg))
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
}
relativeCursorY := int(float64(s.ws.cursor.y) / float64(s.ws.font.lineHeight))
if s.ws.maxLine == 0 {
2018-09-30 23:43:21 +09:00
//s.ws.nvim.Eval("line('$')", &s.ws.maxLine)
lnITF, err := s.ws.nvimEval("line('$')")
if err != nil {
s.ws.maxLine = 0
} else {
2019-11-21 11:37:17 +09:00
switch lnITF := lnITF.(type) {
2018-10-01 20:57:51 +09:00
case uint64:
case uint:
2018-09-30 23:43:21 +09:00
case int64:
2019-11-21 11:37:17 +09:00
s.ws.maxLine = int(lnITF)
2018-09-30 23:43:21 +09:00
case int:
2019-11-21 11:37:17 +09:00
s.ws.maxLine = lnITF
2018-09-30 23:43:21 +09:00
}
}
2018-09-16 21:48:17 +09:00
}
if s.ws.maxLine > bot-top {
s.height = int(float64(bot-top) / float64(s.ws.maxLine) * float64(s.ws.screen.widget.Height()))
2018-09-22 07:44:44 +09:00
if s.height < 20 {
s.height = 20
}
2018-09-16 21:48:17 +09:00
s.thumb.SetFixedHeight(s.height)
2018-09-21 22:04:24 +09:00
s.pos = int(float64(s.ws.curLine-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()
}
}