This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
/
scroll_bar.go
242 lines (208 loc) · 5.46 KB
/
scroll_bar.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mixins
import (
"github.com/google/gxui"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/base"
)
type ScrollBarOuter interface {
base.ControlOuter
}
type ScrollBar struct {
base.Control
outer ScrollBarOuter
orientation gxui.Orientation
thickness int
minBarLength int
scrollPositionFrom int
scrollPositionTo int
scrollLimit int
railBrush, barBrush gxui.Brush
railPen, barPen gxui.Pen
barRect math.Rect
onScroll gxui.Event
autoHide bool
}
func (s *ScrollBar) positionAt(p math.Point) int {
o := s.orientation
frac := float32(o.Major(p.XY())) / float32(o.Major(s.Size().WH()))
max := s.ScrollLimit()
return int(float32(max) * frac)
}
func (s *ScrollBar) rangeAt(p math.Point) (from, to int) {
width := s.scrollPositionTo - s.scrollPositionFrom
from = math.Clamp(s.positionAt(p), 0, s.scrollLimit-width)
to = from + width
return
}
func (s *ScrollBar) updateBarRect() {
sf, st := s.ScrollFraction()
size := s.Size()
b := size.Rect()
halfMinLen := s.minBarLength / 2
if s.orientation.Horizontal() {
b.Min.X = math.Lerp(0, size.W, sf)
b.Max.X = math.Lerp(0, size.W, st)
if b.W() < s.minBarLength {
c := (b.Min.X + b.Max.X) / 2
c = math.Clamp(c, b.Min.X+halfMinLen, b.Max.X-halfMinLen)
b.Min.X, b.Max.X = c-halfMinLen, c+halfMinLen
}
} else {
b.Min.Y = math.Lerp(0, size.H, sf)
b.Max.Y = math.Lerp(0, size.H, st)
if b.H() < s.minBarLength {
c := (b.Min.Y + b.Max.Y) / 2
c = math.Clamp(c, b.Min.Y+halfMinLen, b.Max.Y-halfMinLen)
b.Min.Y, b.Max.Y = c-halfMinLen, c+halfMinLen
}
}
s.barRect = b
}
func (s *ScrollBar) Init(outer ScrollBarOuter, theme gxui.Theme) {
s.Control.Init(outer, theme)
s.outer = outer
s.thickness = 10
s.minBarLength = 10
s.scrollPositionFrom = 0
s.scrollPositionTo = 100
s.scrollLimit = 100
s.onScroll = gxui.CreateEvent(s.SetScrollPosition)
// Interface compliance test
_ = gxui.ScrollBar(s)
}
func (s *ScrollBar) OnScroll(f func(from, to int)) gxui.EventSubscription {
return s.onScroll.Listen(f)
}
func (s *ScrollBar) ScrollFraction() (from, to float32) {
from = float32(s.scrollPositionFrom) / float32(s.scrollLimit)
to = float32(s.scrollPositionTo) / float32(s.scrollLimit)
return
}
func (s *ScrollBar) DesiredSize(min, max math.Size) math.Size {
if s.orientation.Horizontal() {
return math.Size{W: max.W, H: s.thickness}.Clamp(min, max)
} else {
return math.Size{W: s.thickness, H: max.H}.Clamp(min, max)
}
}
func (s *ScrollBar) Paint(c gxui.Canvas) {
c.DrawRoundedRect(s.outer.Size().Rect(), 3, 3, 3, 3, s.railPen, s.railBrush)
c.DrawRoundedRect(s.barRect, 3, 3, 3, 3, s.barPen, s.barBrush)
}
func (s *ScrollBar) RailBrush() gxui.Brush {
return s.railBrush
}
func (s *ScrollBar) SetRailBrush(b gxui.Brush) {
if s.railBrush != b {
s.railBrush = b
s.Redraw()
}
}
func (s *ScrollBar) BarBrush() gxui.Brush {
return s.barBrush
}
func (s *ScrollBar) SetBarBrush(b gxui.Brush) {
if s.barBrush != b {
s.barBrush = b
s.Redraw()
}
}
func (s *ScrollBar) RailPen() gxui.Pen {
return s.railPen
}
func (s *ScrollBar) SetRailPen(b gxui.Pen) {
if s.railPen != b {
s.railPen = b
s.Redraw()
}
}
func (s *ScrollBar) BarPen() gxui.Pen {
return s.barPen
}
func (s *ScrollBar) SetBarPen(b gxui.Pen) {
if s.barPen != b {
s.barPen = b
s.Redraw()
}
}
func (s *ScrollBar) ScrollPosition() (from, to int) {
return s.scrollPositionFrom, s.scrollPositionTo
}
func (s *ScrollBar) SetScrollPosition(from, to int) {
if s.scrollPositionFrom != from || s.scrollPositionTo != to {
s.scrollPositionFrom, s.scrollPositionTo = from, to
s.updateBarRect()
s.Redraw()
s.onScroll.Fire(from, to)
}
}
func (s *ScrollBar) ScrollLimit() int {
return s.scrollLimit
}
func (s *ScrollBar) SetScrollLimit(l int) {
if s.scrollLimit != l {
s.scrollLimit = l
s.updateBarRect()
s.Redraw()
}
}
func (s *ScrollBar) AutoHide() bool {
return s.autoHide
}
func (s *ScrollBar) SetAutoHide(autoHide bool) {
if s.autoHide != autoHide {
s.autoHide = autoHide
s.Redraw()
}
}
func (s *ScrollBar) IsVisible() bool {
if s.autoHide && s.scrollPositionFrom == 0 && s.scrollPositionTo == s.scrollLimit {
return false
}
return s.Control.IsVisible()
}
func (s *ScrollBar) Orientation() gxui.Orientation {
return s.orientation
}
func (s *ScrollBar) SetOrientation(o gxui.Orientation) {
if s.orientation != o {
s.orientation = o
s.Redraw()
}
}
// InputEventHandler overrides
func (s *ScrollBar) Click(ev gxui.MouseEvent) (consume bool) {
if !s.barRect.Contains(ev.Point) {
p := s.positionAt(ev.Point)
from, to := s.scrollPositionFrom, s.scrollPositionTo
switch {
case p < from:
width := to - from
from = math.Max(from-width, 0)
s.SetScrollPosition(from, from+width)
case p > to:
width := to - from
to = math.Min(to+width, s.scrollLimit)
s.SetScrollPosition(to-width, to)
}
}
return true
}
func (s *ScrollBar) MouseDown(ev gxui.MouseEvent) {
if s.barRect.Contains(ev.Point) {
initialOffset := ev.Point.Sub(s.barRect.Min)
var mms, mus gxui.EventSubscription
mms = ev.Window.OnMouseMove(func(we gxui.MouseEvent) {
p := gxui.WindowToChild(we.WindowPoint, s.outer)
s.SetScrollPosition(s.rangeAt(p.Sub(initialOffset)))
})
mus = ev.Window.OnMouseUp(func(we gxui.MouseEvent) {
mms.Unlisten()
mus.Unlisten()
})
}
s.InputEventHandler.MouseDown(ev)
}