-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_extra_text.v
439 lines (389 loc) · 9.6 KB
/
ui_extra_text.v
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
module ui
import gx
const (
word_wrap_id = '\n'
)
pub fn text_x_from_pos<T>(w &T, text string, x int) int {
ustr := text.runes()
if x > ustr.len {
// println('warning: text_x_from_pos $x > $ustr.len')
x = ustr.len
}
left := ustr[..x].string()
return text_width(w, left)
}
pub fn text_xminmax_from_pos<T>(w &T, text string, x1 int, x2 int) (int, int) {
ustr := text.runes()
mut x_min, mut x_max := if x1 < x2 { x1, x2 } else { x2, x1 }
if x_max > ustr.len {
// println('warning: text_xminmax_from_pos $x_max > $ustr.len')
x_max = ustr.len
}
if x_min < 0 {
// println('warning: text_xminmax_from_pos $x_min < 0')
x_min = 0
}
// println("xminmax: ${ustr.len} $x_min $x_max")
left := ustr[..x_min].string()
right := ustr[x_max..].string()
ww, lw, rw := text_width(w, text), text_width(w, left), text_width(w, right)
return lw, ww - lw - rw
}
pub fn text_pos_from_x<T>(w &T, text string, x int) int {
if x <= 0 {
return 0
}
mut prev_width := 0
ustr := text.runes()
for i in 0 .. ustr.len {
width := text_width(w, ustr[..i].string())
width2 := if i < ustr.len { text_width(w, ustr[..(i + 1)].string()) } else { width }
if (prev_width + width) / 2 <= x && x <= (width + width2) / 2 {
return i
}
prev_width = width
}
return ustr.len
}
fn word_wrap_to_line_by_width<T>(w &T, s string, max_line_width int) ([]string, []int) {
words := s.split(' ')
mut line := ''
mut line_width := 0
mut text_lines := []string{}
mut word_wrap, mut cpt := []int{}, 0
for i, word in words {
if i == 0 { // at least the first
line = word
line_width = text_width(w, word)
} else {
word_width := text_width(w, ' ' + word)
if line_width + word_width < max_line_width {
line += ' ' + word
line_width += word_width
} else {
text_lines << line
word_wrap << cpt
line = word
line_width = word_width
cpt++
}
}
}
if line_width > 0 {
text_lines << line
word_wrap << cpt
}
return text_lines, word_wrap
}
fn word_wrap_text_to_lines_by_width<T>(w &T, s string, max_line_width int) ([]string, []int) {
lines := s.split('\n')
mut word_wrapped_lines, mut word_wrap_ind := []string{}, []int{}
for line in lines {
ww_lines, ww_ind := word_wrap_to_line_by_width(w, line, max_line_width)
word_wrapped_lines << ww_lines
word_wrap_ind << ww_ind
}
// println('tl: $word_wrapped_lines ww: $word_wrap_ind')
return word_wrapped_lines, word_wrap_ind
}
fn word_wrap_join(lines []string, ind []int) string {
mut res := ''
// println("lines: $lines, ind: $ind")
for i, line in lines {
sp := if i == 0 {
''
} else if ind[i] > 0 {
' '
} else {
'\n'
}
res += sp + line
}
// println("res: $res")
return res
}
// get text position from row i and column j
pub fn text_lines_pos_at(lines []string, i int, j int) int {
mut pos := 0
for k in 0 .. j {
pos += lines[k].runes().len + 1 // +1 for \n or space
}
pos += i
// println('text_lines_pos_at: ($i, $j) -> $pos ')
return pos
}
// get row and column from text position
pub fn text_lines_row_column_at(lines []string, pos int) (int, int) {
if pos == 0 {
return 0, 0
}
mut j := 0
mut total_len, mut ustr_len := 0, 0
for line in lines {
ustr_len = line.runes().len + 1
total_len += ustr_len
if pos > total_len {
j++
} else {
total_len -= ustr_len
break
}
}
// println('text_lines_row_column_at: $pos -> ($pos - $total_len, $j)')
return pos - total_len, j
}
// Initially inside ui_linux_c.v
fn word_wrap_to_lines(s string, max_line_length int) []string {
words := s.split(' ')
mut line := []string{}
mut line_len := 0
mut text_lines := []string{}
for word in words {
word_len := word.runes().len
if line_len + word_len < max_line_length {
line << word
line_len += word_len + 1
continue
} else {
text_lines << line.join(' ')
line = []
line_len = 0
}
}
if line_len > 0 {
text_lines << line.join(' ')
}
return text_lines
}
fn word_wrap_text_to_lines(s string, max_line_length int) []string {
lines := s.split('\n')
mut word_wrapped_lines := []string{}
for line in lines {
word_wrapped_lines << word_wrap_to_lines(line, max_line_length)
}
return word_wrapped_lines
}
fn text_lines_size(lines []string, ui &UI) (int, int) {
mut width, mut height := 0, 0
mut tw, mut th := 0, 0
for line in lines {
tw, th = ui.gg.text_size(line)
// println("tt line: $line -> ($tw, $th)")
if tw > width {
width = tw
}
height += th
}
return width, height
}
//=== Tooltip ===//
// 1) From now, consider that widgets having tooltip are always on top and without intersecting other widgets.
// As a first try, this makes sense for visible widgets.
// Rmk: if we introduce hover event, this would be to consider.
// 2) It is assumed that there is only one tooltip drawn at the same time
// Rmk: popups are a bit different.
// 3) This is also devoted to simple widgets needing a quick system of help.
const (
tooltip_margin = 5
)
struct TextLines {
mut:
lines []string
x int
y int
width int
height int
text_cfg gx.TextCfg
text_size f64
}
struct Tooltip {
TextLines
mut:
id string
active bool
color gx.Color = gx.black
bg_color gx.Color = gx.Color{255, 220, 127, 220}
side Side = .right
ui &UI = 0
}
pub struct TooltipMessage {
text string
side Side = .right
}
[unsafe]
pub fn (t &Tooltip) free() {
unsafe {
for line in t.lines {
line.free()
}
t.lines.free()
// t.id.free()
}
$if free ? {
println('\tTooltip -> freed')
}
}
[unsafe]
pub fn (t &TooltipMessage) free() {
unsafe {
t.text.free()
// t.id.free()
}
$if free ? {
println('\tTooltipMessage -> freed')
}
}
pub fn start_tooltip(mut w Widget, id string, msg TooltipMessage, wui &UI) {
mut win := wui.window
win.tooltip.id = id
if !win.tooltip.active { // only once
// println("start tooltip $win.tooltip.id: $msg")
if win.tooltip.ui == 0 {
win.tooltip.ui = wui
}
win.tooltip.lines = word_wrap_text_to_lines(msg.text, 70)
win.tooltip.width, win.tooltip.height = text_lines_size(win.tooltip.lines, wui)
win.tooltip.width += 2 * ui.tooltip_margin
win.tooltip.height += 2 * ui.tooltip_margin
set_text_cfg_color(mut win.tooltip, win.tooltip.color)
set_text_cfg_style(mut win.tooltip, true, true, false)
win.tooltip.active = true
width, height := w.size()
match msg.side {
// TODO: the other sides
.top {
win.tooltip.x = w.x + w.offset_x + width / 2 - win.tooltip.width / 2
win.tooltip.y = w.y + w.offset_y - win.tooltip.height - ui.tooltip_margin
}
.right {
win.tooltip.x = w.x + w.offset_x + width + ui.tooltip_margin
win.tooltip.y = w.y + w.offset_y + height / 2 - win.tooltip.height / 2
}
else {}
}
}
}
fn stop_tooltip(id string, wui &UI) {
mut win := wui.window
if win.tooltip.active && win.tooltip.id == id {
// println("tooltip stop $win.tooltip.id")
win.tooltip.active = false
}
}
fn draw_tooltip(win Window) {
if win.tooltip.active {
// TODO: add triangle to connect the rectangle
// win.ui.gg.draw_rect(win.tooltip.x, win.tooltip.y, win.tooltip.width, win.tooltip.height,
// gx.yellow)
win.ui.gg.draw_rounded_rect_filled(win.tooltip.x, win.tooltip.y, win.tooltip.width,
win.tooltip.height, .3, win.tooltip.bg_color)
draw_text_lines(win.tooltip, win.tooltip.x + ui.tooltip_margin, win.tooltip.y,
win.tooltip.lines)
}
}
pub fn (mut w Window) append_tooltip(child Widget, tooltip TooltipMessage) {
w.widgets_tooltip << child
w.tooltips << tooltip
}
pub fn (mut w Window) update_tooltip(e &MouseMoveEvent) {
for i, mut child in w.widgets_tooltip {
id := child.id()
if !child.hidden {
if child.point_inside(e.x, e.y) {
start_tooltip(mut child, id, w.tooltips[i], w.ui)
} else {
stop_tooltip(id, w.ui)
}
if w.dragger.activated {
stop_tooltip(id, w.ui)
}
}
}
}
//=== Basic Message Dialog ===/
// Before sokol deals with multiple window (soon)
fn (mut win Window) add_message_dialog() {
mut dlg := column(
id: '_msg_dlg_col'
alignment: .center
widths: compact
heights: compact
spacing: 10
margin: Margin{5, 5, 5, 5}
bg_color: gx.Color{140, 210, 240, 100}
bg_radius: .3
children: [
label(id: '_msg_dlg_lab', text: ' Hello World'),
button(
id: '_msg_dlg_btn'
text: 'OK'
width: 100
radius: .3
onclick: message_dialog_click
),
]
)
dlg.is_root_layout = false
win.children << dlg
dlg.set_visible(false)
}
fn message_dialog_click(app voidptr, b &Button) {
mut dlg := b.ui.window.stack('_msg_dlg_col')
dlg.set_visible(false)
}
pub fn (win &Window) message(s string) {
if win.native_message {
message_box(s)
} else {
mut dlg := win.stack('_msg_dlg_col')
mut msg := win.label('_msg_dlg_lab')
msg.set_text(s)
mut tw, mut th := text_lines_size(s.split('\n'), win.ui)
msg.propose_size(tw, th)
if tw < 200 {
tw = 200
}
th += 50
// println("msg: ($tw, $th) $s")
dlg.propose_size(tw, th)
ww, wh := win.size()
dlg.set_pos(ww / 2 - tw / 2, wh / 2 - th / 2)
dlg.set_visible(true)
dlg.update_layout()
}
}
/*
// Playing with Styled Text
struct TextChunk {
text string
start int
stop int
cfg gx.TextCfg
}
pub struct TextContext {
chunks []TextChunk
colors map[string]gx.Color
styles map[string]gx.TextCfg
}
struct TextView {
x int
y int
width int
height int
context &TextContext
}
* default: {style: "", size: 10, color: black}
* start:
- style: normal "", italic {i], bold {b], underline {u]
- size: uint8 (ex: {12])
- color: r,g,b,a or hexa (0x00000000) string lowercase (ex: {red])
- font-family: string capitalized
- combined: {...|...|...]
end:
- idem with closing [...} or [...|...|...}
- empty [} means last opened
current:
custom style: blurr
stack of style operations:
{b] {t] [b} [t}
*/