diff options
| -rw-r--r-- | src/app/components/gallery/index.css | 3 | ||||
| -rw-r--r-- | src/app/components/gallery/index.html | 1 | ||||
| -rw-r--r-- | src/app/components/gallery/root.zig | 38 | ||||
| -rw-r--r-- | src/app/components/icons/root.zig | 4 | ||||
| -rw-r--r-- | src/app/components/image/index.css | 10 | ||||
| -rw-r--r-- | src/app/components/image/root.zig | 14 | ||||
| -rw-r--r-- | src/app/components/navbutton/root.zig | 4 | ||||
| -rw-r--r-- | src/app/components/root.zig | 9 | ||||
| -rw-r--r-- | src/app/index.html | 2 | ||||
| -rw-r--r-- | src/app/root.zig | 3 | ||||
| -rw-r--r-- | src/app/theme.css | 5 | ||||
| -rw-r--r-- | src/zilch/App/root.zig | 16 | ||||
| -rw-r--r-- | src/zilch/Component.zig | 71 | ||||
| -rw-r--r-- | src/zilch/component.template.js | 18 | ||||
| -rw-r--r-- | src/zilch/panic.zig | 9 | ||||
| -rw-r--r-- | src/zilch/root.zig | 7 |
16 files changed, 154 insertions, 60 deletions
diff --git a/src/app/components/gallery/index.css b/src/app/components/gallery/index.css new file mode 100644 index 0000000..c6a5817 --- /dev/null +++ b/src/app/components/gallery/index.css @@ -0,0 +1,3 @@ +:host { + background: gray; +} diff --git a/src/app/components/gallery/index.html b/src/app/components/gallery/index.html new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/app/components/gallery/index.html @@ -0,0 +1 @@ + diff --git a/src/app/components/gallery/root.zig b/src/app/components/gallery/root.zig new file mode 100644 index 0000000..7a06d5f --- /dev/null +++ b/src/app/components/gallery/root.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const zilch = @import("zilch"); +const Object = zilch.Object; +const components = @import("../root.zig"); +const Image = components.Image; + +const app = @import("root").app; + +pub const component: zilch.Component = .build(.{ + .name = "gallery", + .html = .parse(@embedFile("index.html")), + .css = @embedFile("index.css"), + .link = @This(), +}); + +const Self = @This(); + +element: *Object, +image: *Image, +body: *Object, + +pub fn init(self: *Self, element: *Object) !void { + var env: zilch.Env = .default; + defer env.deinit(); + + self.element = element; + + self.image = try std.heap.wasm_allocator.create(Image); + var el = Image.component.new(self.image, &env); + try self.image.init(try el.await(&env)); + + var body = self.element.get(&env, "shadowRoot"); + + var append_call = Object.call(body, &env, "append", .{self.image.element}); + append_call.ignore(&env); + + self.body = try body.await(&env, *Object); +} diff --git a/src/app/components/icons/root.zig b/src/app/components/icons/root.zig index be814d8..0328472 100644 --- a/src/app/components/icons/root.zig +++ b/src/app/components/icons/root.zig @@ -7,11 +7,11 @@ pub const icons: []const zilch.Component = &.{ }; pub fn icon(name: []const u8) zilch.Component { - return .{ + return .build(.{ .namespace = "icon", .name = name, .html = .parse(@embedFile(name ++ ".svg")), .css = @embedFile("index.css"), .link = struct {}, - }; + }); } diff --git a/src/app/components/image/index.css b/src/app/components/image/index.css index 86ba44e..034a509 100644 --- a/src/app/components/image/index.css +++ b/src/app/components/image/index.css @@ -1,12 +1,8 @@ -:host { - --src: attr(src) -} - .image { - width: 100%; - padding-bottom: 100%; + width: calc(100% / var(--gallery-splits)); + padding-bottom: calc(100% / var(--gallery-splits)); height: 0; - background-image: url("" ++ var(--src)); + background: red; background-size: cover; background-position: center; } diff --git a/src/app/components/image/root.zig b/src/app/components/image/root.zig index 971713d..c9aeef4 100644 --- a/src/app/components/image/root.zig +++ b/src/app/components/image/root.zig @@ -1,8 +1,18 @@ const zilch = @import("zilch"); -pub const component: zilch.Component = .{ +pub const component: zilch.Component = .build(.{ .name = "image", .html = .parse(@embedFile("index.html")), .css = @embedFile("index.css"), .link = @This(), -}; +}); + +const Self = @This(); + +element: *zilch.Object, + +pub fn init(self: *Self, element: *zilch.Object) !void { + self.element = element; +} + +pub const connect = component.connect; diff --git a/src/app/components/navbutton/root.zig b/src/app/components/navbutton/root.zig index 2f26116..a512d1d 100644 --- a/src/app/components/navbutton/root.zig +++ b/src/app/components/navbutton/root.zig @@ -6,12 +6,12 @@ const Promise = zilch.Promise; const Self = @This(); -pub const component: zilch.Component = .{ +pub const component: zilch.Component = .build(.{ .name = "navbutton", .html = .parse(@embedFile("index.html")), .css = @embedFile("index.css"), .link = @This(), -}; +}); element: *zilch.Object, shadow_root: *zilch.Object, diff --git a/src/app/components/root.zig b/src/app/components/root.zig index f51e3c1..999a052 100644 --- a/src/app/components/root.zig +++ b/src/app/components/root.zig @@ -2,7 +2,12 @@ const zilch = @import("zilch"); const icons = @import("icons/root.zig").icons; +pub const NavButton = @import("navbutton/root.zig"); +pub const Image = @import("image/root.zig"); +pub const Gallery = @import("gallery/root.zig"); + pub const components = &[_]zilch.Component{ - @import("navbutton/root.zig").component, - @import("image/root.zig").component, + NavButton.component, + Image.component, + Gallery.component, } ++ icons; diff --git a/src/app/index.html b/src/app/index.html index b96a445..caf9703 100644 --- a/src/app/index.html +++ b/src/app/index.html @@ -1,2 +1,2 @@ <app-navbutton></app-navbutton> -<app-image src="icon.png"></app-image> +<app-gallery></app-gallery> diff --git a/src/app/root.zig b/src/app/root.zig index d57faf9..1a04b21 100644 --- a/src/app/root.zig +++ b/src/app/root.zig @@ -14,7 +14,8 @@ pub const app: zilch.App(.{ .favicon = "icon.png", .functions = &(zilch.Object.functions ++ zilch.Promise.functions ++ - zilch.Callable.functions), + zilch.Callable.functions ++ + zilch.Component.functions), .globals = &.{ zilch.Object.global(.window), zilch.Object.global(.Object), diff --git a/src/app/theme.css b/src/app/theme.css index cfe5205..1973c82 100644 --- a/src/app/theme.css +++ b/src/app/theme.css @@ -13,7 +13,8 @@ body { overflow: hidden; } -app-image { +app-gallery { + width: 100%; + height: 100%; display: block; - width: 20vw; } diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig index a7700d9..01e2695 100644 --- a/src/zilch/App/root.zig +++ b/src/zilch/App/root.zig @@ -45,7 +45,6 @@ pub fn App(comptime options: Options) type { break :g gl; }, - Methods: type = enum(u16) {}, pub const init: @This() = .{}; @@ -56,21 +55,29 @@ pub fn App(comptime options: Options) type { } }; } else { - const components: []const Element = (comp: { + const components: []const Element, const registry: []const u8 = (comp: { var cs: []const Element = &.{}; + var registry: []const u8 = "const component_registry = {"; for (options.components) |component| { cs = cs ++ component.toElement(); + registry = registry ++ std.fmt.comptimePrint("{}: '{s}-{s}',", .{ + component.id, + component.namespace, + component.name, + }); } - break :comp cs; + registry = registry ++ "};"; + + break :comp .{ cs, registry }; }); const functions: []const u8 = func: { var buf: []const u8 = "ObjectStore.methods = {"; for (options.functions) |function| { - buf = buf ++ std.fmt.comptimePrint("{}: {s},", .{function.id, function.content}); + buf = buf ++ std.fmt.comptimePrint("{}: {s},", .{ function.id, function.content }); } buf = buf ++ "}"; @@ -111,6 +118,7 @@ pub fn App(comptime options: Options) type { "body", .{}, &[_]Element{ + .node("script", .{}, &.{.text(registry)}), } ++ components ++ options.body.elements, ), }), diff --git a/src/zilch/Component.zig b/src/zilch/Component.zig index 35fafbe..592b464 100644 --- a/src/zilch/Component.zig +++ b/src/zilch/Component.zig @@ -3,14 +3,37 @@ const Fragment = @import("root.zig").html.Fragment; const Element = @import("root.zig").html.Element; const Attribute = @import("root.zig").html.Attribute; const Object = @import("Object.zig"); +const Env = @import("root.zig").Env; +const Function = Env.Function; +const Future = Env.Future; const Self = @This(); -namespace: []const u8 = "app", +pub const Options = struct { + namespace: []const u8 = "app", + name: []const u8, + html: Fragment, + css: []const u8, + link: type, +}; + +namespace: []const u8, name: []const u8, html: Fragment, css: []const u8, link: type, +id: u32, + +pub fn build(comptime options: Options) Self { + return .{ + .namespace = options.namespace, + .name = options.name, + .html = options.html, + .css = options.css, + .link = options.link, + .id = std.hash.XxHash32.hash(4654, options.namespace ++ "-" ++ options.name), + }; +} fn handlerNeedsEvent(func: anytype) bool { const T = @TypeOf(func); @@ -93,25 +116,6 @@ pub fn exportLinks(self: *const Self) void { }, ); - const deinit = struct { - pub fn deinit(obj: *self.link) callconv(.c) void { - if (@hasDecl(self.link, "deinit")) { - wrapHandler(obj.deinit()); - } - std.heap.wasm_allocator.destroy(obj); - } - }.deinit; - - @export( - &deinit, - .{ - .name = std.fmt.comptimePrint( - "export::{s}::deinit", - .{self.name}, - ), - }, - ); - for (self.html.elements) |element| { comptime self.exportFromHtml(element); } @@ -188,9 +192,34 @@ pub fn toElement(self: *const Self) []const Element { &.{.text( std.fmt.comptimePrint( @embedFile("component.template.js"), - .{ self.namespace, self.name, self.name, self.name, self.name }, + .{ + self.namespace, + self.name, + self.name, + self.name, + }, ), )}, ), }; } + +pub fn new(self: Self, obj: *self.link, env: *Env) Future(*Object) { + return .fromAny(env.call(bindings.new, .{ @as(usize, @intCast(self.id)), @intFromPtr(obj) })); +} + +const bindings = struct { + pub const new: Function = .from( + \\function(name_id, ptr) { + \\ const el = document.createElement(component_registry[name_id]); + \\ el[ZsPtr] = ptr; + \\ const id = ObjectStore.generateFreeId(); + \\ ObjectStore.registerObject(id, el); + \\ return el; + \\} + ); +}; + +pub const functions = [_]Function{ + bindings.new, +}; diff --git a/src/zilch/component.template.js b/src/zilch/component.template.js index e8cb914..6addef2 100644 --- a/src/zilch/component.template.js +++ b/src/zilch/component.template.js @@ -8,14 +8,12 @@ customElements.define("{s}-{s}", class extends HTMLElement {{ this[ZsPtr] = null; }} connectedCallback() {{ - wasm_ready.then(() => {{ - const id = ObjectStore.generateFreeId(); - ObjectStore.registerObject(id, this); - this[ZsPtr] = wasm.exports['export::{s}::init'](id); - }}); + if (this[ZsPtr] === null) {{ + wasm_ready.then(() => {{ + const id = ObjectStore.generateFreeId(); + ObjectStore.registerObject(id, this); + this[ZsPtr] = wasm.exports['export::{s}::init'](id, this[ZsPtr]); + }}); + }} }} - disconnectedCallback() {{ - wasm.exports['export::{s}::deinit'](this[ZsId]); - ObjectStore.free(this); - }} -}}) +}}); diff --git a/src/zilch/panic.zig b/src/zilch/panic.zig new file mode 100644 index 0000000..7163c6d --- /dev/null +++ b/src/zilch/panic.zig @@ -0,0 +1,9 @@ +const log = @import("log/root.zig"); +const std = @import("std"); + +fn defaultPanic(msg: []const u8, _: ?usize) noreturn { + log.js.err(msg); + @trap(); +} + +pub const panic = std.debug.FullPanic(defaultPanic); diff --git a/src/zilch/root.zig b/src/zilch/root.zig index 9bca612..25dfae7 100644 --- a/src/zilch/root.zig +++ b/src/zilch/root.zig @@ -18,12 +18,7 @@ pub const App = @import("App/root.zig").App; pub const Options = @import("App/root.zig").Options; pub const Component = @import("Component.zig"); -fn defaultPanic(msg: []const u8, _: ?usize) noreturn { - log.js.err(msg); - @trap(); -} - -pub const panic = std.debug.FullPanic(defaultPanic); +pub const panic = @import("panic.zig").panic; const Ptr = struct { o: *anyopaque }; comptime { |