Skip to content

Commit

Permalink
update deps, zig, etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
cztomsik committed Jul 1, 2023
1 parent 64f5a36 commit 2d94711
Show file tree
Hide file tree
Showing 13 changed files with 37 additions and 31 deletions.
2 changes: 1 addition & 1 deletion deps/napigen
8 changes: 7 additions & 1 deletion examples/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ const styles = {
margin: '10px 0',
},

buttons: {
display: 'flex',
justifyContent: 'space-between',
},

button: {
display: 'block',
width: 50,
height: 25, // TODO: remove this
},
}

Expand Down
8 changes: 4 additions & 4 deletions src/css/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ pub const Parser = struct {
/// Parse an integer.
pub fn parseInt(self: *Parser, comptime T: type) !T {
const num = try self.expect(.number);
return std.math.cast(T, @intFromFloat(u32, num)) orelse error.InvalidInt;
return std.math.cast(T, @as(u32, @intFromFloat(num))) orelse error.InvalidInt;
}

/// Parse a float.
pub fn parseFloat(self: *Parser, comptime T: type) !T {
return @floatCast(T, try self.expect(.number));
return @floatCast(try self.expect(.number));
}

/// Parse an enum.
Expand All @@ -73,7 +73,7 @@ pub const Parser = struct {

inline for (std.meta.fields(T)) |f| {
if (std.mem.eql(u8, cssName(f.name), ident)) {
return @enumFromInt(T, f.value);
return @enumFromInt(f.value);
}
}

Expand All @@ -97,7 +97,7 @@ pub const Parser = struct {

inline for (std.meta.fields(T)) |f| {
if (f.default_value) |ptr| {
const v = @ptrCast(*const f.type, @alignCast(f.alignment, ptr)).*;
const v = @as(*const f.type, @ptrCast(@alignCast(ptr))).*;
@field(res, f.name) = self.parseOptional(f.type) orelse v;
} else {
@field(res, f.name) = try self.parse(f.type);
Expand Down
2 changes: 1 addition & 1 deletion src/css/style_declaration.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn StyleDeclaration(comptime Property: type, comptime Shorthand: type) type

/// Returns the number of longhand properties.
pub fn length(self: *Self) u32 {
return @truncate(u32, self.properties.items.len);
return @intCast(self.properties.items.len);
}

/// Returns the name of the property at the given index.
Expand Down
6 changes: 3 additions & 3 deletions src/css/values/color.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ pub const Color = struct {
}
},
.hash => |s| switch (s.len) {
8 => return rgba(hex(s[0..2]), hex(s[2..4]), hex(s[4..6]), .{ .value = @floatFromInt(f32, hex(s[6..8])) / 255.0 }),
8 => return rgba(hex(s[0..2]), hex(s[2..4]), hex(s[4..6]), .{ .value = @as(f32, @floatFromInt(hex(s[6..8]))) / 255.0 }),
6 => return rgba(hex(s[0..2]), hex(s[2..4]), hex(s[4..6]), .{ .value = 1.0 }),
4 => return rgba((hex(s[0..1])) * 17, (hex(s[1..2])) * 17, (hex(s[2..3])) * 17, .{ .value = @floatFromInt(f32, (hex(s[3..4])) * 17) / 255.0 }),
4 => return rgba((hex(s[0..1])) * 17, (hex(s[1..2])) * 17, (hex(s[2..3])) * 17, .{ .value = @as(f32, @floatFromInt((hex(s[3..4])) * 17)) / 255.0 }),
3 => return rgba((hex(s[0..1])) * 17, (hex(s[1..2])) * 17, (hex(s[2..3])) * 17, .{ .value = 1.0 }),
else => {},
},
Expand Down Expand Up @@ -111,7 +111,7 @@ fn hue_to_rgb(hue: f32, m1: f32, m2: f32) f32 {
}

fn f32_to_u8_clamped(f: f32) u8 {
return @intFromFloat(u8, std.math.clamp(f, 0, 255));
return @intFromFloat(std.math.clamp(f, 0, 255));
}

const NAMED_COLORS = .{
Expand Down
12 changes: 6 additions & 6 deletions src/dom/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub const Document = struct {
.owner_document = document,
.node_type = .document,
.layout = .{
.style = .{ .display = .block, .width = .{ .fraction = 1 }, .height = .{ .fraction = 1 } },
.style = .{ .display = .block, .width = .{ .percent = 100 }, .height = .{ .percent = 100 } },
},
},
.allocator = allocator,
Expand Down Expand Up @@ -70,8 +70,8 @@ pub const Document = struct {
const Cx = struct {
fn measure(node: *std.meta.FieldType(Node, .layout), _: [2]f32) [2]f32 {
// TODO: use node.context.parent_node to get the parent element and use font size from the computed style
const text = @ptrCast(*CharacterData, @alignCast(@alignOf(CharacterData), node.context));
return .{ @floatFromInt(f32, text.data.len) * 7, 19.5 };
const text = @as(*CharacterData, @ptrCast(@alignCast(node.context)));
return .{ @as(f32, @floatFromInt(text.data.len)) * 7, 19.5 };
}
};

Expand All @@ -81,7 +81,7 @@ pub const Document = struct {
.owner_document = self,
.node_type = .text,
.layout = .{
.context = @ptrCast(*anyopaque, text),
.context = @ptrCast(text),
.measure_fn = &Cx.measure,
},
},
Expand Down Expand Up @@ -161,7 +161,7 @@ pub const Document = struct {

while (childNodes.next()) |child| {
if (child.node_type == .text) {
try writer.writeAll(@ptrCast(*CharacterData, child).data);
try writer.writeAll(@as(*CharacterData, @ptrCast(child)).data);
}
}

Expand Down Expand Up @@ -200,7 +200,7 @@ pub const Document = struct {
fn updateTree(self: *Document, node: *Node, force: bool) void {
if (force or node.is_dirty) {
switch (node.node_type) {
.element => updateElement(self, @ptrCast(*Element, node)),
.element => updateElement(self, @ptrCast(node)),
.text => {}, // TODO: node.layout.markDirty();
else => {},
}
Expand Down
10 changes: 5 additions & 5 deletions src/dom/element.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ pub const Element = struct {
};

fn convertDim(value: css.Dimension) emlay.Dimension {
switch (value) {
.auto => return .auto,
.px => |v| return .{ .px = v },
.percent => |v| return .{ .fraction = v / 100 },
return switch (value) {
.auto => .auto,
.px => |v| .{ .px = v },
.percent => |v| .{ .percent = v },
else => @panic("TODO"),
}
};
}
2 changes: 1 addition & 1 deletion src/dom/node.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const Node = struct {

/// Returns the node as an element, or null otherwise.
pub fn element(self: *Node) ?*Element {
return if (self.node_type == .element) @ptrCast(*Element, self) else null;
return if (self.node_type == .element) @ptrCast(self) else null;
}

/// Returns whether the node has any children.
Expand Down
8 changes: 4 additions & 4 deletions src/platform.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ pub const Window = opaque {
c.glfwMakeContextCurrent(window);
_ = gladLoadGL();

return @ptrCast(*Self, window);
return @ptrCast(window);
}

fn handle(self: *Self) *c.GLFWwindow {
return @ptrCast(*c.GLFWwindow, self);
return @ptrCast(self);
}

pub fn deinit(self: *Self) void {
Expand All @@ -51,7 +51,7 @@ pub const Window = opaque {
pub fn size(self: *Self) [2]f32 {
var res: [2]i32 = .{ 0, 0 };
if (!builtin.is_test) c.glfwGetWindowSize(self.handle(), &res[0], &res[1]);
return .{ @floatFromInt(f32, res[0]), @floatFromInt(f32, res[1]) };
return .{ @floatFromInt(res[0]), @floatFromInt(res[1]) };
}

// TODO: resize, show, hide, focus, blur, ...
Expand Down Expand Up @@ -139,7 +139,7 @@ fn handleGlfwMouseButton(w: ?*c.GLFWwindow, _: c_int, action: c_int, _: c_int) c

fn handleGlfwKey(w: ?*c.GLFWwindow, key: c_int, _: c_int, action: c_int, _: c_int) callconv(.C) void {
// TODO: key -> which
pushEvent(.{ .target = w, .kind = if (action == c.GLFW_PRESS) .key_down else .key_up, .which = @intCast(u32, key) });
pushEvent(.{ .target = w, .kind = if (action == c.GLFW_PRESS) .key_down else .key_up, .which = @intCast(key) });
}

fn handleGlfwChar(w: ?*c.GLFWwindow, char: c_uint) callconv(.C) void {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ pub const Renderer = struct {

noinline fn renderNode(self: *Renderer, node: *Node) void {
switch (node.node_type) {
.element => self.renderElement(@ptrCast(*Element, node)),
.text => self.renderText(@ptrCast(*CharacterData, node)),
.element => self.renderElement(@ptrCast(node)),
.text => self.renderText(@ptrCast(node)),
else => return,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/uv_hook.zig
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn waitEvents(_: [*c]uv_prepare_t) callconv(.C) void {
switch (uv_backend_timeout(uv_loop)) {
0 => platform.pollEvents(),
-1 => platform.waitEvents(),
else => |t| platform.waitEventsTimeout(@floatFromInt(f64, t) / 1000),
else => |t| platform.waitEventsTimeout(@as(f64, @floatFromInt(t)) / 1000),
}

// prepare JS scope
Expand Down

0 comments on commit 2d94711

Please sign in to comment.