-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup.v
146 lines (127 loc) · 2.93 KB
/
group.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
// Copyright (c) 2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module ui
import math
import gx
import eventbus
pub struct Group {
pub mut:
title string
height int
width int
x int
y int
parent Layout
ui &UI
children []Widget
margin_left int = 5
margin_top int = 10
margin_right int = 5
margin_bottom int = 5
spacing int = 5
}
pub struct GroupConfig {
pub mut:
title string
x int
y int
width int
height int
}
fn (mut g Group) init(parent Layout) {
g.parent = parent
ui := parent.get_ui()
g.ui = ui
for child in g.children {
child.init(g)
}
g.calculate_child_positions()
}
pub fn group(c GroupConfig, children []Widget) &Group {
mut g := &Group{
title: c.title
x: c.x
y: c.y
width: c.width
height: c.height
children: children
ui: 0
}
return g
}
fn (mut g Group) set_pos(x int, y int) {
g.x = x
g.y = y
g.calculate_child_positions()
}
fn (mut g Group) calculate_child_positions() {
mut widgets := g.children
mut start_x := g.x + g.margin_left
mut start_y := g.y + g.margin_top
for widget in widgets {
mut wid_w, wid_h := widget.size()
widget.set_pos(start_x, start_y)
start_y = start_y + wid_h + g.spacing
if wid_w > g.width - g.margin_left - g.margin_right {
g.width = wid_w + g.margin_left + g.margin_right
}
if start_y + g.margin_bottom > g.height {
g.height = start_y - wid_h
}
}
}
fn (mut g Group) propose_size(w int, h int) (int, int) {
g.width = w
g.height = h
return g.width, g.height
}
fn (mut g Group) draw() {
// Border
g.ui.gg.draw_empty_rect(g.x, g.y, g.width, g.height, gx.gray)
mut title := g.title
mut text_width := g.ui.gg.text_width(title)
if text_width > (g.width - check_mark_size - 3) {
proportion := f32(g.width) / f32(text_width)
target_len := int(math.floor(title.len * proportion)) - 5
title = if target_len < 0 { '' } else { title.substr(0, target_len) + '..' }
text_width = g.ui.gg.text_width(title)
}
// Title
g.ui.gg.draw_rect(g.x + check_mark_size, g.y - 5, text_width + 5, 10, g.ui.window.bg_color)
g.ui.gg.draw_text_def(g.x + check_mark_size + 3, g.y - 7, title)
for child in g.children {
child.draw()
}
}
fn (g &Group) point_inside(x f64, y f64) bool {
return x >= g.x && x <= g.x + g.width && y >= g.y && y <= g.y + g.height
}
fn (mut g Group) focus() {
}
fn (mut g Group) unfocus() {
}
fn (g &Group) is_focused() bool {
return false
}
fn (g &Group) get_ui() &UI {
return g.ui
}
fn (g &Group) unfocus_all() {
for child in g.children {
child.unfocus()
}
}
fn (g &Group) resize(width int, height int) {
}
fn (g &Group) get_state() voidptr {
parent := g.parent
return parent.get_state()
}
fn (g &Group) get_subscriber() &eventbus.Subscriber {
parent := g.parent
return parent.get_subscriber()
}
fn (g &Group) size() (int, int) {
return g.width, g.height
}