-
Notifications
You must be signed in to change notification settings - Fork 50
/
frame.go
219 lines (171 loc) · 3.86 KB
/
frame.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
package clui
import (
xs "github.com/huandu/xstrings"
"math"
)
/*
Frame is a decorative control and container - frame with optional title.
All area inside a frame is transparent. Frame can be used as spacer element
- set border to BorderNone and use that control in any place where a spacer
is required
*/
type Frame struct {
BaseControl
border BorderStyle
children []Control
pack PackType
scrollable bool
lastScrollProp int
}
/*
NewFrame creates a new frame.
view - is a View that manages the control
parent - is container that keeps the control. The same View can be a view and a parent at the same time.
width and heigth - are minimal size of the control.
bs - type of border: no border, single or double.
scale - the way of scaling the control when the parent is resized. Use DoNotScale constant if the
control should keep its original size.
*/
func CreateFrame(parent Control, width, height int, bs BorderStyle, scale int) *Frame {
f := new(Frame)
f.BaseControl = NewBaseControl()
if width == AutoSize {
width = 5
}
if height == AutoSize {
height = 3
}
if bs == BorderAuto {
bs = BorderNone
}
f.SetSize(width, height)
f.SetConstraints(width, height)
f.border = bs
f.parent = parent
f.SetTabStop(false)
f.scale = scale
f.gapX, f.gapY = 0, 0
if bs == BorderNone {
f.padX, f.padY = 0, 0
} else {
f.padX, f.padY = 1, 1
}
if parent != nil {
parent.AddChild(f)
}
return f
}
func (f *Frame) SetScrollable(scrollable bool) {
f.scrollable = scrollable
if scrollable {
px, py := f.Paddings()
if f.Pack() == Vertical {
px += 1
}
if f.Pack() == Horizontal {
py += 1
}
f.SetPaddings(px, py)
}
f.SetClipped(scrollable)
}
func (f *Frame) Scrollable() bool {
return f.scrollable
}
// Repaint draws the control on its View surface
func (f *Frame) Draw() {
if f.hidden {
return
}
PushAttributes()
defer PopAttributes()
x, y, w, h := f.Clipper()
fx, fy := f.Pos()
fw, fh := f.Size()
if f.scrollable {
_, fpy := f.Paddings()
var dist float64
prop := 0
ctrl := ActiveControl(f)
if ctrl != nil {
var frameProp float64
_, ty := ctrl.Pos()
dist = (float64(fy) + float64(fpy)) - float64(ty)
dist = math.Sqrt(dist * dist)
if dist > 0 {
frameProp = (dist * 100) / float64(fh)
}
if frameProp > 0 {
prop = int(math.Round((float64(h-2) / (100 / frameProp))))
}
f.lastScrollProp = prop
}
DrawScrollBar(x+w, y, 1, h, f.lastScrollProp)
}
fg, bg := RealColor(f.fg, f.Style(), ColorViewText), RealColor(f.bg, f.Style(), ColorViewBack)
if f.border == BorderNone {
if bg != ColorDefault {
SetBackColor(bg)
FillRect(x, y, w, h, ' ')
}
f.DrawChildren()
return
}
SetTextColor(fg)
SetBackColor(bg)
DrawFrame(fx, fy, fw, fh, f.border)
if f.title != "" {
str := f.title
raw := UnColorizeText(str)
if xs.Len(raw) > fw-2 {
str = SliceColorized(str, 0, fw-2-3) + "..."
}
DrawText(fx+1, fy, str)
}
f.DrawChildren()
}
// ScrollTo in case of a scrollable frame this api will scroll the content
// without adjusting the clipper
func (f *Frame) ScrollTo(x int, y int) {
if !f.scrollable {
return
}
f.x = x
f.y = y
f.ResizeChildren()
f.PlaceChildren()
}
func (f *Frame) ProcessEvent(ev Event) bool {
if ev.Type != EventActivateChild || (!f.scrollable || ev.Target == nil) {
return false
}
x, y := f.Pos()
px, py := f.Paddings()
cx, cy, cw, ch := f.Clipper()
tw, th := ev.Target.Size()
tx, ty := ev.Target.Pos()
if ControlInRect(ev.Target, cx, cy, cw, ch) {
return false
}
xx := x
yy := y
if (ty+th)-(py/2) > cy+ch {
delta := (ty + th) - (cy + ch)
yy = y - delta
} else if ty < cy {
delta := cy - ty
yy = y + delta
}
if (tx+tw)-(px/2) > cx+cw {
delta := (tx + tw) - (cx + cw)
xx = (x - delta)
} else if tx < cx {
delta := cx - tx
xx = x + delta
}
f.x = xx
f.y = yy
f.ResizeChildren()
f.PlaceChildren()
return false
}