aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/app/components/gallery/index.css33
-rw-r--r--src/app/components/gallery/index.html14
-rw-r--r--src/app/components/gallery/root.zig60
-rw-r--r--src/app/components/image/index.css24
-rw-r--r--src/app/components/image/index.html1
-rw-r--r--src/app/components/image/root.zig16
-rw-r--r--src/app/theme.css1
-rw-r--r--src/http/Response.zig7
-rw-r--r--src/http/handler/root.zig5
-rw-r--r--src/main.zig24
-rw-r--r--src/zilch/App/root.zig1
-rw-r--r--src/zilch/Env/future.zig5
-rw-r--r--src/zilch/component.template.js2
13 files changed, 169 insertions, 24 deletions
diff --git a/src/app/components/gallery/index.css b/src/app/components/gallery/index.css
index c6a5817..35cab60 100644
--- a/src/app/components/gallery/index.css
+++ b/src/app/components/gallery/index.css
@@ -1,3 +1,34 @@
:host {
- background: gray;
+ background: black;
+ --gallery-splits: 5;
+ display: flex;
+ flex-wrap: wrap;
+ overflow: auto;
+}
+
+#control {
+ position: absolute;
+ bottom: 10px;
+ right: 70px;
+ width: 100px;
+ height: 50px;
+ background: var(--primary);
+ border-radius: 50px;
+ display: grid;
+ grid-template-columns: 1fr 1fr;
+ cursor: pointer;
+ user-select: none;
+
+ div {
+ user-select: none;
+ color: var(--color);
+ width: 50px;
+ height: 50px;
+ text-align: center;
+ align-content: center;
+ }
+}
+
+app-image {
+ width: calc(100% / var(--gallery-splits));
}
diff --git a/src/app/components/gallery/index.html b/src/app/components/gallery/index.html
index 8b13789..efa58a3 100644
--- a/src/app/components/gallery/index.html
+++ b/src/app/components/gallery/index.html
@@ -1 +1,13 @@
-
+<div id="control">
+ <div onclick="@zoomIn">
+ <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-plus" viewBox="0 0 16 16">
+ <path d="M8 4a.5.5 0 0 1 .5.5v3h3a.5.5 0 0 1 0 1h-3v3a.5.5 0 0 1-1 0v-3h-3a.5.5 0 0 1 0-1h3v-3A.5.5 0 0 1 8 4">
+ </path>
+ </svg>
+ </div>
+ <div onclick="@zoomOut">
+ <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-dash" viewBox="0 0 16 16">
+ <path d="M4 8a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7A.5.5 0 0 1 4 8"></path>
+ </svg>
+ </div>
+</div>
diff --git a/src/app/components/gallery/root.zig b/src/app/components/gallery/root.zig
index 7a06d5f..1eef707 100644
--- a/src/app/components/gallery/root.zig
+++ b/src/app/components/gallery/root.zig
@@ -1,4 +1,6 @@
const std = @import("std");
+const gpa = std.heap.wasm_allocator;
+
const zilch = @import("zilch");
const Object = zilch.Object;
const components = @import("../root.zig");
@@ -16,23 +18,69 @@ pub const component: zilch.Component = .build(.{
const Self = @This();
element: *Object,
-image: *Image,
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;
- 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 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");
- var append_call = Object.call(body, &env, "append", .{self.image.element});
- append_call.ignore(&env);
+ 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);
+}
diff --git a/src/app/components/image/index.css b/src/app/components/image/index.css
index 034a509..d9a9ff5 100644
--- a/src/app/components/image/index.css
+++ b/src/app/components/image/index.css
@@ -1,8 +1,26 @@
.image {
- width: calc(100% / var(--gallery-splits));
- padding-bottom: calc(100% / var(--gallery-splits));
+ width: 100%;
+ padding-bottom: 100%;
height: 0;
- background: red;
+ background: var(--image);
background-size: cover;
background-position: center;
}
+
+img {
+ content: var(--image);
+ width: 100%;
+ display: none;
+ visibility: hidden;
+}
+
+@container style(--gallery-splits: 1) {
+ .image {
+ padding-bottom: 0px;
+ height: unset;
+ }
+
+ img {
+ display: unset;
+ }
+}
diff --git a/src/app/components/image/index.html b/src/app/components/image/index.html
index bd124ad..2b135dc 100644
--- a/src/app/components/image/index.html
+++ b/src/app/components/image/index.html
@@ -1,2 +1,3 @@
<div class="image">
+ <img>
</div>
diff --git a/src/app/components/image/root.zig b/src/app/components/image/root.zig
index c9aeef4..1d46d30 100644
--- a/src/app/components/image/root.zig
+++ b/src/app/components/image/root.zig
@@ -1,3 +1,4 @@
+const std = @import("std");
const zilch = @import("zilch");
pub const component: zilch.Component = .build(.{
@@ -10,9 +11,22 @@ pub const component: zilch.Component = .build(.{
const Self = @This();
element: *zilch.Object,
+style: *zilch.Object,
pub fn init(self: *Self, element: *zilch.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);
+ };
}
-pub const connect = component.connect;
+pub fn setUrl(self: *Self, env: *zilch.Env, url: []const u8) zilch.Env.Future(void) {
+ var buffer: [1024]u8 = undefined;
+ var writer: std.Io.Writer = .fixed(&buffer);
+ writer.print("url({s})", .{url}) catch unreachable;
+ return .fromAny(self.style.call(env, "setProperty", .{"--image", writer.buffered()}));
+}
diff --git a/src/app/theme.css b/src/app/theme.css
index 1973c82..1034db2 100644
--- a/src/app/theme.css
+++ b/src/app/theme.css
@@ -16,5 +16,4 @@ body {
app-gallery {
width: 100%;
height: 100%;
- display: block;
}
diff --git a/src/http/Response.zig b/src/http/Response.zig
index 69bec65..62c53f5 100644
--- a/src/http/Response.zig
+++ b/src/http/Response.zig
@@ -18,10 +18,8 @@ pub const Message = union(enum) {
return .{ .static = content };
}
- pub fn file(io: std.Io, path: []const u8) !Message {
- return .{ .streaming = try std.Io.Dir.cwd().openFile(io, path, .{
- .allow_directory = false,
- }) };
+ pub fn file(f: std.Io.File) Message {
+ return .{ .streaming = f };
}
};
@@ -97,6 +95,7 @@ pub fn send(
});
},
.streaming => |file| {
+ defer file.close(io);
var write_buffer: [1024]u8 = undefined;
var read_buffer: [1024]u8 = undefined;
diff --git a/src/http/handler/root.zig b/src/http/handler/root.zig
index 33fdb84..634888c 100644
--- a/src/http/handler/root.zig
+++ b/src/http/handler/root.zig
@@ -9,7 +9,6 @@ test "interface" {
const C = Context(.{ "api", "images", .id, "metadata" });
const context: C = .{
.post = post,
- .get = get,
};
const Query = struct {
@@ -25,10 +24,6 @@ test "interface" {
return .ok(.string(request.params.id.as([]const u8)));
}
-
- fn get(request: C.Request) Response {
- return .ok(Response.Message.file(request.io, "./some/path.txt") catch return .with(.not_found, .void));
- }
};
_ = M.context.interface();
diff --git a/src/main.zig b/src/main.zig
index ec3b2fd..308d308 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -14,8 +14,32 @@ const routes: http.RouteSet = .init(&([_]http.handler.Interface{
http.handler.Static(.index, site.app.html, "text/html").interface(),
http.handler.Static(.{"index.wasm"}, wasm, "application/wasm").interface(),
http.handler.Static(.{"icon.png"}, @embedFile("favicon.png"), "image/png").interface(),
+ images.ctx.interface(),
}) ++ api.interfaces);
+const images = struct {
+ const Ctx = http.handler.Context(.{ "image", .id });
+ const ctx: Ctx = .{
+ .get = get,
+ };
+
+ pub fn get(request: Ctx.Request) http.Response {
+ const id = request.params.id.as(usize) catch return .badRequest(.string("invalid id"));
+
+ const storage = std.Io.Dir.cwd().openDir(request.io, "storage", .{}) catch return .with(.internal_server_error, .void);
+ defer storage.close(request.io);
+
+ var allocating: std.Io.Writer.Allocating = .init(request.gpa);
+ defer allocating.deinit();
+
+ allocating.writer.print("{}.jpg", .{id}) catch return .with(.internal_server_error, .void);
+
+ const file = storage.openFile(request.io, allocating.written(), .{}) catch return .badRequest(.string("invalid id"));
+
+ return .ok(.file(file));
+ }
+};
+
pub fn main(init: std.process.Init) !void {
try http.serve(init.gpa, routes, .{
.address = try .parseLiteral("0.0.0.0:8080"),
diff --git a/src/zilch/App/root.zig b/src/zilch/App/root.zig
index 01e2695..e40b4e0 100644
--- a/src/zilch/App/root.zig
+++ b/src/zilch/App/root.zig
@@ -95,6 +95,7 @@ pub fn App(comptime options: Options) type {
.name = "viewport",
.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0",
}),
+ .void("meta", .{ .charset = "utf8" }),
.node("style", .{}, &.{.text(options.theme)}),
.node("script", .{}, &.{.text(@embedFile("symbols.js"))}),
.node("script", .{}, &.{.text(@embedFile("object-store.js"))}),
diff --git a/src/zilch/Env/future.zig b/src/zilch/Env/future.zig
index f829009..43f46c5 100644
--- a/src/zilch/Env/future.zig
+++ b/src/zilch/Env/future.zig
@@ -42,7 +42,10 @@ pub const AnyFuture = union(enum) {
pub fn awaitAny(self: *Self, env: *Env) !Env.Value {
defer self.* = undefined;
switch (self.*) {
- .ignored => @panic("awaited ignored value"),
+ .ignored => {
+ env.flush();
+ return .from({});
+ },
.err => |err| return err,
.resolved => |result| return result,
.unresolved => |result| {
diff --git a/src/zilch/component.template.js b/src/zilch/component.template.js
index 6addef2..bd6d634 100644
--- a/src/zilch/component.template.js
+++ b/src/zilch/component.template.js
@@ -12,7 +12,7 @@ customElements.define("{s}-{s}", class extends HTMLElement {{
wasm_ready.then(() => {{
const id = ObjectStore.generateFreeId();
ObjectStore.registerObject(id, this);
- this[ZsPtr] = wasm.exports['export::{s}::init'](id, this[ZsPtr]);
+ this[ZsPtr] = wasm.exports['export::{s}::init'](id);
}});
}}
}}