-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathButtonWidget.zig
176 lines (149 loc) · 5.05 KB
/
ButtonWidget.zig
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
const std = @import("std");
const dvui = @import("../dvui.zig");
const Color = dvui.Color;
const Event = dvui.Event;
const Options = dvui.Options;
const Point = dvui.Point;
const Rect = dvui.Rect;
const RectScale = dvui.RectScale;
const Size = dvui.Size;
const Widget = dvui.Widget;
const WidgetData = dvui.WidgetData;
const ButtonWidget = @This();
pub var defaults: Options = .{
.name = "Button",
.color_fill = .{ .name = .fill_control },
.margin = Rect.all(4),
.corner_radius = Rect.all(5),
.padding = Rect.all(6),
.background = true,
};
pub const InitOptions = struct {
draw_focus: bool = true,
};
wd: WidgetData = undefined,
init_options: InitOptions = undefined,
hover: bool = false,
focus: bool = false,
click: bool = false,
pub fn init(src: std.builtin.SourceLocation, init_opts: InitOptions, opts: Options) ButtonWidget {
var self = ButtonWidget{};
self.init_options = init_opts;
self.wd = WidgetData.init(src, .{}, defaults.override(opts));
return self;
}
pub fn install(self: *ButtonWidget) !void {
try self.wd.register();
dvui.parentSet(self.widget());
if (self.wd.visible()) {
try dvui.tabIndexSet(self.wd.id, self.wd.options.tab_index);
}
}
pub fn matchEvent(self: *ButtonWidget, e: *Event) bool {
return dvui.eventMatchSimple(e, self.data());
}
pub fn processEvents(self: *ButtonWidget) void {
const evts = dvui.events();
for (evts) |*e| {
if (!self.matchEvent(e))
continue;
self.processEvent(e, false);
}
}
pub fn drawBackground(self: *ButtonWidget) !void {
var fill_color: ?Color = null;
if (dvui.captured(self.wd.id)) {
fill_color = self.wd.options.color(.fill_press);
} else if (self.hover) {
fill_color = self.wd.options.color(.fill_hover);
}
try self.wd.borderAndBackground(.{ .fill_color = fill_color });
}
pub fn drawFocus(self: *ButtonWidget) !void {
if (self.init_options.draw_focus and self.focused()) {
try self.wd.focusBorder();
}
}
pub fn focused(self: *ButtonWidget) bool {
return self.wd.id == dvui.focusedWidgetId();
}
pub fn hovered(self: *ButtonWidget) bool {
return self.hover;
}
pub fn pressed(self: *ButtonWidget) bool {
return dvui.captured(self.wd.id);
}
pub fn clicked(self: *ButtonWidget) bool {
return self.click;
}
pub fn widget(self: *ButtonWidget) Widget {
return Widget.init(self, data, rectFor, screenRectScale, minSizeForChild, processEvent);
}
pub fn data(self: *ButtonWidget) *WidgetData {
return &self.wd;
}
pub fn rectFor(self: *ButtonWidget, id: u32, min_size: Size, e: Options.Expand, g: Options.Gravity) Rect {
_ = id;
return dvui.placeIn(self.wd.contentRect().justSize(), min_size, e, g);
}
pub fn screenRectScale(self: *ButtonWidget, rect: Rect) RectScale {
return self.wd.contentRectScale().rectToRectScale(rect);
}
pub fn minSizeForChild(self: *ButtonWidget, s: Size) void {
self.wd.minSizeMax(self.wd.options.padSize(s));
}
pub fn processEvent(self: *ButtonWidget, e: *Event, bubbling: bool) void {
_ = bubbling;
switch (e.evt) {
.mouse => |me| {
if (me.action == .focus) {
e.handled = true;
dvui.focusWidget(self.wd.id, null, e.num);
} else if (me.action == .press and me.button.pointer()) {
e.handled = true;
dvui.captureMouse(self.wd.id);
// drag prestart is just for touch events
dvui.dragPreStart(me.p, .{});
} else if (me.action == .release and me.button.pointer()) {
if (dvui.captured(self.wd.id)) {
e.handled = true;
dvui.captureMouse(null);
dvui.dragEnd();
if (self.data().borderRectScale().r.contains(me.p)) {
self.click = true;
dvui.refresh(null, @src(), self.wd.id);
}
}
} else if (me.action == .motion and me.button.touch()) {
if (dvui.captured(self.wd.id)) {
if (dvui.dragging(me.p)) |_| {
// if we overcame the drag threshold, then that
// means the person probably didn't want to touch
// this button, maybe they were trying to scroll
dvui.captureMouse(null);
dvui.dragEnd();
}
}
} else if (me.action == .position) {
e.handled = true;
self.hover = true;
}
},
.key => |ke| {
if (ke.action == .down and ke.matchBind("activate")) {
e.handled = true;
self.click = true;
dvui.refresh(null, @src(), self.wd.id);
}
},
else => {},
}
if (e.bubbleable()) {
self.wd.parent.processEvent(e, true);
}
}
pub fn deinit(self: *ButtonWidget) void {
self.wd.minSizeSetAndRefresh();
self.wd.minSizeReportToParent();
dvui.parentReset(self.wd.id, self.wd.parent);
}