aboutsummaryrefslogtreecommitdiff
path: root/src/zilch/App/root.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/zilch/App/root.zig')
-rw-r--r--src/zilch/App/root.zig104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig
new file mode 100644
index 0000000..52cbfc2
--- /dev/null
+++ b/src/zilch/App/root.zig
@@ -0,0 +1,104 @@
+const builtin = @import("builtin");
+const std = @import("std");
+const Element = @import("../root.zig").html.Element;
+const Document = @import("../root.zig").html.Document;
+const Fragment = @import("../root.zig").html.Fragment;
+const Component = @import("../root.zig").Component;
+
+pub const Options = struct {
+ name: []const u8,
+ globals: []const Global,
+ components: []const Component,
+ body: Fragment,
+ theme: []const u8,
+
+ pub const Global = struct {
+ name: []const u8,
+ type: type,
+ };
+};
+
+pub fn App(comptime options: Options) type {
+ if (builtin.target.cpu.arch.isWasm()) {
+ var names: [options.globals.len][]const u8 = undefined;
+ var types: [options.globals.len]type = undefined;
+ var attrs: [options.globals.len]std.builtin.Type.StructField.Attributes = undefined;
+
+ for (options.globals, 0..) |field, index| {
+ names[index] = field.name;
+ types[index] = field.type;
+ attrs[index] = .{};
+ }
+
+ const G = @Struct(.auto, null, &names, &types, &attrs);
+ return struct {
+ globals: G = g: {
+ var gl: G = undefined;
+
+ for (options.globals, 1..) |field, id| {
+ @field(gl, field.name) = @ptrFromInt(id);
+ }
+
+ break :g gl;
+ },
+
+ pub const init: @This() = .{};
+
+ comptime {
+ for (options.components) |component| {
+ component.exportLinks();
+ }
+ }
+ };
+ } else {
+ const components: []const Element = (comp: {
+ var cs: []const Element = &.{};
+
+ for (options.components) |component| {
+ cs = cs ++ component.toElement();
+ }
+
+ break :comp cs;
+ });
+
+ return struct {
+ const node: Document = .{
+ .root = .node("html", .{}, &.{
+ .node("head", .{}, &.{
+ .node("title", .{}, &.{.text(options.name)}),
+ .void("meta", .{
+ .name = "viewport",
+ .content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0",
+ }),
+ .node("style", .{}, &.{.text(options.theme)}),
+ .node("script", .{}, &.{.text(@embedFile("symbols.js"))}),
+ .node("script", .{}, &.{.text(@embedFile("object-store.js"))}),
+ .node("script", .{}, &.{.text(@embedFile("main.js"))}),
+ .node("script", .{}, &.{.text(reg: {
+ var register_code: []const u8 = "";
+
+ for (options.globals, 1..) |field, id| {
+ register_code = register_code ++ std.fmt.comptimePrint(
+ "ObjectStore.registerObject({}, {s});",
+ .{ id, field.name },
+ );
+ }
+
+ break :reg register_code;
+ })}),
+ }),
+ .node(
+ "body",
+ .{},
+ &[_]Element{
+ } ++ components ++ options.body.elements,
+ ),
+ }),
+ };
+
+ html: []const u8 = std.fmt.comptimePrint("{f}", .{node}),
+
+ pub const init: @This() = .{};
+ };
+ }
+}