-
Notifications
You must be signed in to change notification settings - Fork 0
/
component_fontchooser.v
78 lines (66 loc) · 1.65 KB
/
component_fontchooser.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
module uicomponent
import ui
import os
const (
fontchooser_row_id = '_row_sw_font'
fontchooser_lb_id = '_lb_sw_font'
)
[heap]
struct FontChooser {
pub mut:
layout &ui.Stack // required
dtw ui.DrawTextWidget
// To become a component of a parent component
component voidptr
}
[params]
pub struct FontChooserConfig {
id string = uicomponent.fontchooser_lb_id
draw_lines bool = true
dtw ui.DrawTextWidget = ui.canvas_plus() // since it requires an intialisation
}
pub fn fontchooser(c FontChooserConfig) &ui.Stack {
mut lb := ui.listbox(
id: c.id
scrollview: true
draw_lines: c.draw_lines
on_change: fontchooser_lb_change
)
fontchooser_add_fonts_items(mut lb)
layout := ui.row(
id: uicomponent.fontchooser_row_id
widths: 300.0
heights: 200.0
children: [lb]
)
mut fc := &FontChooser{
layout: layout
dtw: c.dtw
}
ui.component_connect(fc, layout, lb)
return layout
}
pub fn component_fontchooser(w ui.ComponentChild) &FontChooser {
return &FontChooser(w.component)
}
fn fontchooser_add_fonts_items(mut lb ui.ListBox) {
font_paths := ui.font_path_list()
for fp in font_paths {
lb.append_item(fp, os.file_name(fp), 0)
}
}
pub fn fontchooser_connect(w &ui.Window, dtw ui.DrawTextWidget) {
fc_layout := w.stack(uicomponent.fontchooser_row_id)
mut fc := component_fontchooser(fc_layout)
fc.dtw = dtw
}
fn fontchooser_lb_change(a voidptr, lb &ui.ListBox) {
mut w := lb.ui.window
fc := component_fontchooser(lb)
// println('fc_lb_change: $lb.id')
mut dtw := ui.DrawTextWidget(fc.dtw)
fp, id := lb.selected() or { 'classic', '' }
// println("$id, $fp")
w.add_font(id, fp)
dtw.update_text_style(font_name: id)
}