aboutsummaryrefslogtreecommitdiff
path: root/src/z/App/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/z/App/root.zig')
-rw-r--r--src/z/App/root.zig66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/z/App/root.zig b/src/z/App/root.zig
new file mode 100644
index 0000000..d684248
--- /dev/null
+++ b/src/z/App/root.zig
@@ -0,0 +1,66 @@
+const builtin = @import("builtin");
+const std = @import("std");
+const h = @import("html");
+
+pub const Options = struct { name: []const u8 };
+
+pub fn App(comptime options: Options, globals: anytype) type {
+ const info = @typeInfo(@TypeOf(globals)).@"struct";
+
+ if (builtin.target.cpu.arch.isWasm()) {
+ var names: [info.fields.len][]const u8 = undefined;
+ var types: [info.fields.len]type = undefined;
+ var attrs: [info.fields.len]std.builtin.Type.StructField.Attributes = undefined;
+
+ for (info.fields, 0..) |field, index| {
+ names[index] = field.name;
+ types[index] = @field(globals, field.name);
+ attrs[index] = .{};
+ }
+
+ const G = @Struct(.auto, null, &names, &types, &attrs);
+ return struct {
+ globals: G = g: {
+ var gl: G = undefined;
+
+ for (info.fields, 1..) |field, id| {
+ @field(gl, field.name) = @ptrFromInt(id);
+ }
+
+ break :g gl;
+ },
+
+ pub const init: @This() = .{};
+ };
+ } else {
+ return struct {
+ html: []const u8 = h.Element.native(.html, .{}, .children(&.{
+ .native(.head, .{}, .children(&.{
+ .native(.title, .{}, .content(options.name)),
+ .native(.meta, .{
+ .name = "viewport",
+ .content = "width=device-width, initial-scale=1.0",
+ }, .void),
+ })),
+ .native(.body, .{}, .children(&.{
+ .native(.script, .{}, .content(@embedFile("object-store.js"))),
+ .native(.script, .{}, .content(reg: {
+ var register_code: []const u8 = "";
+
+ for (info.fields, 1..) |field, id| {
+ register_code = register_code ++ std.fmt.comptimePrint(
+ "ObjectStore.registerObject({}, {s});",
+ .{ id, field.name },
+ );
+ }
+
+ break :reg register_code;
+ })),
+ .native(.script, .{}, .content(@embedFile("main.js"))),
+ })),
+ })).toDocument(),
+
+ pub const init: @This() = .{};
+ };
+ }
+}