-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresizer.go
259 lines (240 loc) · 6.36 KB
/
resizer.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package giowidgets
import (
"fmt"
"gioui.org/gesture"
"gioui.org/io/pointer"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/unit"
"image"
"image/color"
)
// Resize provides a draggable handle in between two widgets for resizing their area.
type Resize struct {
// axis defines how the widgets and the handle are laid out.
axis layout.Axis
initialized bool
length int
totalHandlesLength int
resizables []*Resizable
minLength int
}
type Resizable struct {
// ratio is only calculated during initialization, based on widget's natural size.
// It acts like minimum threshold ratio value beyond which widget size cannot be further reduced.
ratio float32
Widget layout.Widget
DividerHandler layout.Widget
dividerThickness int
float
resize *Resize
prev *Resizable
next *Resizable
}
func NewResizeWidget(axis layout.Axis, resizables []*Resizable) *Resize {
r := &Resize{axis: axis, resizables: resizables}
for _, rz := range resizables {
rz.resize = r
if rz.DividerHandler == nil {
rz.DividerHandler = r.CustomResizeHandleBar
}
}
return r
}
// Layout displays w1 and w2 with handle in between.
//
// The widgets w1 and w2 must be able to gracefully resize their minimum and maximum dimensions
// in order for the resize to be smooth.
func (r *Resize) Layout(gtx layout.Context) layout.Dimensions {
// Compute the first widget's max width/height.
if len(r.resizables) == 0 {
return layout.Dimensions{}
}
if len(r.resizables) == 1 {
return r.resizables[0].Widget(gtx)
}
if !r.initialized {
r.init(gtx)
r.initialized = true
}
// On Window Resize
if r.length != r.axis.Convert(gtx.Constraints.Max).X {
r.onWindowResize(gtx)
}
gtx.Constraints.Min = gtx.Constraints.Max
flex := layout.Flex{Axis: r.axis}
return flex.Layout(gtx,
r.resizables[0].Layout(gtx)...,
)
}
func (r *Resize) init(gtx layout.Context) {
r.length = r.axis.Convert(gtx.Constraints.Max).X
if r.minLength == 0 {
r.minLength = int(0.1 * float32(r.length))
}
allowedMinLength := r.length / len(r.resizables)
if r.minLength > allowedMinLength || r.minLength <= 0 {
r.minLength = allowedMinLength
}
var totalRatio float32
// Obtain the total ration to reset it between 0.0 - 1.00
var totalHandlesLength int
for i, rz := range r.resizables {
if rz.DividerHandler == nil {
rz.DividerHandler = r.CustomResizeHandleBar
}
m := op.Record(gtx.Ops)
d := rz.DividerHandler(gtx)
m.Stop()
totalHandlesLength += r.axis.Convert(d.Size).X
m = op.Record(gtx.Ops)
d = rz.Widget(gtx)
m.Stop()
rz.ratio = float32(r.axis.Convert(d.Size).X) / float32(r.length)
totalRatio += rz.ratio
var prevResizable *Resizable
var nextResizable *Resizable
if i != 0 {
prevResizable = r.resizables[i-1]
}
if i < len(r.resizables)-1 {
nextResizable = r.resizables[i+1]
}
rz.prev = prevResizable
rz.next = nextResizable
rz.resize = r
if i == len(r.resizables)-1 {
if totalRatio <= 1 {
totalRatio -= rz.ratio
rz.ratio = 1 - totalRatio
totalRatio = 1
}
}
}
r.totalHandlesLength = totalHandlesLength
// Reset the ratio between 0.0 - 1.00
var currTotalRatio float32
for _, rz := range r.resizables {
rz.ratio /= totalRatio // reset the total ratio
currTotalRatio += rz.ratio
rz.float.pos = int(float32(r.length) * currTotalRatio)
}
fmt.Println(totalRatio)
fmt.Println(currTotalRatio)
}
func (r *Resize) onWindowResize(gtx layout.Context) {
currMinLength := r.minLength
prevLength := r.length
r.minLength = (currMinLength / prevLength) * r.length
r.length = r.axis.Convert(gtx.Constraints.Max).X
for _, rz := range r.resizables {
rz.float.pos = int((float32(rz.float.pos) / float32(prevLength)) * float32(r.length))
}
}
type float struct {
pos int // position in pixels of the handle
drag gesture.Drag
}
func (r *Resizable) Layout(gtx layout.Context) []layout.FlexChild {
m := op.Record(gtx.Ops)
dims := r.handleDrag(gtx)
c := m.Stop()
children := []layout.FlexChild{
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
prePos := 0
if r.prev != nil {
prePos = r.prev.pos
}
gtx.Constraints.Max = image.Point{X: r.pos - prePos, Y: gtx.Constraints.Max.Y}
if r.resize.axis == layout.Vertical {
gtx.Constraints.Max = r.resize.axis.Convert(gtx.Constraints.Max)
}
d := r.Widget(gtx)
d.Size = gtx.Constraints.Max
return d
}),
layout.Rigid(func(gtx layout.Context) layout.Dimensions {
c.Add(gtx.Ops)
return dims
}),
}
if r.next != nil {
children = append(children, r.next.Layout(gtx)...)
}
return children
}
func (r *Resizable) handleDrag(gtx layout.Context) layout.Dimensions {
if r.next == nil {
return layout.Dimensions{}
}
gtx.Constraints.Min = image.Point{}
dims := r.DividerHandler(gtx)
var de *pointer.Event
for _, e := range r.float.drag.Events(gtx.Metric, gtx, gesture.Axis(r.resize.axis)) {
if e.Type == pointer.Drag {
de = &e
}
}
var posDifference float32
if de != nil {
posDifference = de.Position.X
if r.resize.axis == layout.Vertical {
posDifference = de.Position.Y
}
if posDifference < 0 {
for curr := r; curr != nil; curr = curr.prev {
curr.float.pos += int(posDifference)
minPos := r.resize.minLength
if curr.prev != nil {
minPos = curr.prev.pos + curr.resize.minLength
}
if curr.float.pos < minPos {
curr.float.pos = minPos
} else {
break
}
}
}
if posDifference > 0 {
for curr := r; curr != nil; curr = curr.next {
curr.float.pos += int(posDifference)
maxPos := r.resize.length
if curr.next != nil {
maxPos = curr.next.pos - curr.resize.minLength
}
if curr.float.pos > maxPos {
curr.float.pos = maxPos
} else {
break
}
}
}
}
rect := image.Rectangle{Max: dims.Size}
defer clip.Rect(rect).Push(gtx.Ops).Pop()
r.float.drag.Add(gtx.Ops)
cursor := pointer.CursorRowResize
if r.resize.axis == layout.Horizontal {
cursor = pointer.CursorColResize
}
cursor.Add(gtx.Ops)
return layout.Dimensions{Size: dims.Size}
}
func (r *Resize) CustomResizeHandleBar(gtx Gtx) Dim {
x := gtx.Dp(unit.Dp(4))
y := gtx.Constraints.Max.Y
if r.axis == layout.Vertical {
x = gtx.Constraints.Max.X
y = gtx.Dp(unit.Dp(4))
}
rect := image.Rectangle{
Max: image.Point{
X: x,
Y: y,
},
}
paint.FillShape(gtx.Ops, color.NRGBA{A: 200}, clip.Rect(rect).Op())
return Dim{Size: rect.Max}
}