diff options
Diffstat (limited to 'src/app/components/gallery/root.zig')
| -rw-r--r-- | src/app/components/gallery/root.zig | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/app/components/gallery/root.zig b/src/app/components/gallery/root.zig new file mode 100644 index 0000000..1eef707 --- /dev/null +++ b/src/app/components/gallery/root.zig @@ -0,0 +1,86 @@ +const std = @import("std"); +const gpa = std.heap.wasm_allocator; + +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, +body: *Object, +pool: std.heap.MemoryPool(Image), +images: std.ArrayList(*Image), +style: *zilch.Object, +splits: usize, + +pub fn init(self: *Self, element: *Object) !void { + var env: zilch.Env = .default; + defer env.deinit(); + + self.element = element; + self.style = blk: { + var style = self.element.get(&env, "style"); + break :blk try style.await(&env, *zilch.Object); + }; + self.pool = .empty; + self.images = .empty; + self.splits = 5; + + const number_of_boxes: u32 = 1000; + + { + var elements: []zilch.Env.Future(*Object) = try gpa.alloc(zilch.Env.Future(*Object), number_of_boxes); + defer gpa.free(elements); + for (0..number_of_boxes) |index| { + const image = try self.pool.create(gpa); + elements[index] = Image.component.new(image, &env); + try self.images.append(gpa, image); + } + + for (elements[0..], self.images.items) |*el, image| { + try image.init(try el.await(&env)); + var future = image.setUrl(&env, "image/1"); + future.ignore(&env); + } + } + + var body = self.element.get(&env, "shadowRoot"); + + for (self.images.items) |image| { + var append_call = Object.call(body, &env, "append", .{image.element}); + append_call.ignore(&env); + } + + self.body = try body.await(&env, *Object); +} + +pub fn zoomIn(self: *Self) !void { + var env: zilch.Env = .default; + defer env.deinit(); + + self.splits = @max(1, self.splits - 1); + + var future = self.style.call(&env, "setProperty", .{ "--gallery-splits", self.splits }); + future.ignore(&env); +} + +pub fn zoomOut(self: *Self) !void { + var env: zilch.Env = .default; + defer env.deinit(); + + self.splits = @min(10, self.splits + 1); + + var future = self.style.call(&env, "setProperty", .{ "--gallery-splits", self.splits }); + future.ignore(&env); +} |