-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.v
108 lines (92 loc) · 2.17 KB
/
switch.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
// 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 gx
const (
sw_height = 20
sw_width = 40
sw_dot_size = 16
sw_open_bg_color = gx.rgb(19, 206, 102)
sw_close_bg_color = gx.rgb(220, 223, 230)
)
type SwitchClickFn = fn (arg_1 voidptr, arg_2 voidptr)
pub struct Switch {
pub mut:
idx int
height int
width int
x int
y int
parent Layout
is_focused bool
open bool
ui &UI
onclick SwitchClickFn
}
pub struct SwitchConfig {
onclick SwitchClickFn
open bool
}
fn (mut s Switch) init(parent Layout) {
s.parent = parent
ui := parent.get_ui()
s.ui = ui
mut subscriber := parent.get_subscriber()
subscriber.subscribe_method(events.on_click, sw_click, s)
}
pub fn switcher(c SwitchConfig) &Switch {
mut s := &Switch{
height: sw_height
width: sw_width
open: c.open
onclick: c.onclick
ui: 0
}
return s
}
fn (mut s Switch) set_pos(x int, y int) {
s.x = x
s.y = y
}
fn (mut s Switch) size() (int, int) {
return s.width, s.height
}
fn (mut s Switch) propose_size(w int, h int) (int, int) {
return s.width, s.height
}
fn (mut s Switch) draw() {
padding := (s.height - sw_dot_size) / 2
if s.open {
s.ui.gg.draw_rect(s.x, s.y, s.width, s.height, sw_open_bg_color)
s.ui.gg.draw_rect(s.x - padding + s.width - sw_dot_size, s.y + padding, sw_dot_size,
sw_dot_size, gx.white)
} else {
s.ui.gg.draw_rect(s.x, s.y, s.width, s.height, sw_close_bg_color)
s.ui.gg.draw_rect(s.x + padding, s.y + padding, sw_dot_size, sw_dot_size, gx.white)
}
}
fn (s &Switch) point_inside(x f64, y f64) bool {
return x >= s.x && x <= s.x + s.width && y >= s.y && y <= s.y + s.height
}
fn sw_click(mut s Switch, e &MouseEvent, w &Window) {
if !s.point_inside(e.x, e.y) {
return
}
// <===== mouse position test added
if int(e.action) == 0 {
s.open = !s.open
if s.onclick != voidptr(0) {
s.onclick(w.state, s)
}
}
}
fn (mut s Switch) focus() {
s.is_focused = true
}
fn (mut s Switch) unfocus() {
s.is_focused = false
}
fn (s &Switch) is_focused() bool {
return s.is_focused
}