forked from david-vanderson/dvui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.zig
More file actions
238 lines (194 loc) · 7.87 KB
/
app.zig
File metadata and controls
238 lines (194 loc) · 7.87 KB
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
const std = @import("std");
const builtin = @import("builtin");
const dvui = @import("dvui");
const window_icon_png = @embedFile("zig-favicon.png");
// To be a dvui App:
// * declare "dvui_app"
// * expose the backend's main function
// * use the backend's log function
pub const dvui_app: dvui.App = .{
.config = .{
.options = .{
.size = .{ .w = 800.0, .h = 600.0 },
.min_size = .{ .w = 250.0, .h = 350.0 },
.title = "DVUI App Example",
.icon = window_icon_png,
.window_init_options = .{
// Could set a default theme here
// .theme = dvui.Theme.builtin.dracula,
},
},
},
.frameFn = AppFrame,
.initFn = AppInit,
.deinitFn = AppDeinit,
};
pub const main = dvui.App.main;
pub const panic = dvui.App.panic;
pub const std_options: std.Options = .{
.logFn = dvui.App.logFn,
};
var gpa_instance = std.heap.GeneralPurposeAllocator(.{}){};
const gpa = gpa_instance.allocator();
var orig_content_scale: f32 = 1.0;
var warn_on_quit: bool = false;
var warn_on_quit_closing: bool = false;
// Runs before the first frame, after backend and dvui.Window.init()
// - runs between win.begin()/win.end()
pub fn AppInit(win: *dvui.Window) !void {
orig_content_scale = win.content_scale;
// Add your own bundled font files...:
// try dvui.addFont("NOTO", @embedFile("../src/fonts/NotoSansKR-Regular.ttf"), null);
if (false) {
// If you need to set a theme based on the users preferred color scheme, do it here
const theme = switch (win.backend.preferredColorScheme() orelse .light) {
.light => dvui.Theme.builtin.adwaita_light,
.dark => dvui.Theme.builtin.adwaita_dark,
};
win.themeSet(theme);
}
}
// Run as app is shutting down before dvui.Window.deinit()
pub fn AppDeinit() void {}
// Run each frame to do normal UI
pub fn AppFrame() !dvui.App.Result {
return frame();
}
pub fn frame() !dvui.App.Result {
var scaler = dvui.scale(@src(), .{ .scale = &dvui.currentWindow().content_scale, .pinch_zoom = .global }, .{ .rect = .cast(dvui.windowRect()) });
scaler.deinit();
{
var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{ .style = .window, .background = true, .expand = .horizontal });
defer hbox.deinit();
var m = dvui.menu(@src(), .horizontal, .{});
defer m.deinit();
if (dvui.menuItemLabel(@src(), "File", .{ .submenu = true }, .{ .tag = "first-focusable" })) |r| {
var fw = dvui.floatingMenu(@src(), .{ .from = r }, .{});
defer fw.deinit();
if (dvui.menuItemLabel(@src(), "Close Menu", .{}, .{ .expand = .horizontal }) != null) {
m.close();
}
if (dvui.backend.kind != .web) {
if (dvui.menuItemLabel(@src(), "Exit", .{}, .{ .expand = .horizontal }) != null) {
return .close;
}
}
}
}
var scroll = dvui.scrollArea(@src(), .{}, .{ .expand = .both, .style = .window });
defer scroll.deinit();
var tl = dvui.textLayout(@src(), .{}, .{ .expand = .horizontal, .font = .theme(.title) });
const lorem = "This is a dvui.App example that can compile on multiple backends.";
tl.addText(lorem, .{});
tl.addText("\n", .{});
tl.format("Current backend: {s}", .{@tagName(dvui.backend.kind)}, .{});
if (dvui.backend.kind == .web) {
tl.format(" : {s}", .{if (dvui.backend.wasm.wasm_about_webgl2() == 1) "webgl2" else "webgl (no mipmaps)"}, .{});
}
tl.deinit();
var tl2 = dvui.textLayout(@src(), .{}, .{ .expand = .horizontal });
tl2.addText(
\\DVUI
\\- paints the entire window
\\- can show floating windows and dialogs
\\- rest of the window is a scroll area
, .{});
tl2.addText("\n\n", .{});
tl2.addText("Framerate is variable and adjusts as needed for input events and animations.", .{});
tl2.addText("\n\n", .{});
tl2.addText("Framerate is capped by vsync.", .{});
tl2.addText("\n\n", .{});
tl2.addText("Cursor is always being set by dvui.", .{});
tl2.addText("\n\n", .{});
if (dvui.useFreeType) {
tl2.addText("Fonts are being rendered by FreeType 2.", .{});
} else {
tl2.addText("Fonts are being rendered by stb_truetype.", .{});
}
tl2.deinit();
const label = if (dvui.Examples.show_demo_window) "Hide Demo Window" else "Show Demo Window";
if (dvui.button(@src(), label, .{}, .{ .tag = "show-demo-btn" })) {
dvui.Examples.show_demo_window = !dvui.Examples.show_demo_window;
}
if (dvui.button(@src(), "Debug Window", .{}, .{})) {
dvui.toggleDebugWindow();
}
{
var hbox = dvui.box(@src(), .{ .dir = .horizontal }, .{});
defer hbox.deinit();
dvui.label(@src(), "Pinch Zoom or Scale", .{}, .{});
if (dvui.buttonIcon(@src(), "plus", dvui.entypo.plus, .{}, .{}, .{})) {
dvui.currentWindow().content_scale *= 1.1;
}
if (dvui.buttonIcon(@src(), "minus", dvui.entypo.minus, .{}, .{}, .{})) {
dvui.currentWindow().content_scale /= 1.1;
}
if (dvui.currentWindow().content_scale != orig_content_scale) {
if (dvui.button(@src(), "Reset Scale", .{}, .{})) {
dvui.currentWindow().content_scale = orig_content_scale;
}
}
}
if (dvui.backend.kind != .web) {
_ = dvui.checkbox(@src(), &warn_on_quit, "Warn on Quit", .{});
if (warn_on_quit) {
if (warn_on_quit_closing) return .close;
const wd = dvui.currentWindow().data();
for (dvui.events()) |*e| {
if (!dvui.eventMatchSimple(e, wd)) continue;
if ((e.evt == .window and e.evt.window.action == .close) or (e.evt == .app and e.evt.app.action == .quit)) {
e.handle(@src(), wd);
const warnAfter: dvui.DialogCallAfterFn = struct {
fn warnAfter(_: dvui.Id, response: dvui.enums.DialogResponse) !void {
if (response == .ok) warn_on_quit_closing = true;
}
}.warnAfter;
dvui.dialog(@src(), .{}, .{ .message = "Really Quit?", .cancel_label = "Cancel", .callafterFn = warnAfter });
}
}
}
}
// look at demo() for examples of dvui widgets, shows in a floating window
dvui.Examples.demo();
return .ok;
}
test "tab order" {
var t = try dvui.testing.init(.{});
defer t.deinit();
try dvui.testing.settle(frame);
try dvui.testing.expectNotFocused("first-focusable");
try dvui.testing.pressKey(.tab, .none);
try dvui.testing.settle(frame);
try dvui.testing.expectFocused("first-focusable");
}
test "open example window" {
var t = try dvui.testing.init(.{});
defer t.deinit();
try dvui.testing.settle(frame);
// FIXME: The global show_demo_window variable makes tests order dependent
dvui.Examples.show_demo_window = false;
try std.testing.expect(dvui.tagGet(dvui.Examples.demo_window_tag) == null);
try dvui.testing.moveTo("show-demo-btn");
try dvui.testing.click(.left);
try dvui.testing.settle(frame);
try dvui.testing.expectVisible(dvui.Examples.demo_window_tag);
}
// disabling snapshot tests until we figure out a better (less sensitive) way of doing them
//test "snapshot" {
// // snapshot tests are unstable
// var t = try dvui.testing.init(.{});
// defer t.deinit();
//
// // FIXME: The global show_demo_window variable makes tests order dependent
// dvui.Examples.show_demo_window = false;
//
// try dvui.testing.settle(frame);
//
// // Try swapping the names of ./snapshots/app.zig-test.snapshot-X.png
// try t.snapshot(@src(), frame);
//
// try dvui.testing.pressKey(.tab, .none);
// try dvui.testing.settle(frame);
//
// try t.snapshot(@src(), frame);
//}