Skip to content

Commit 0ec01e2

Browse files
committed
macos: Fix compile errors
1 parent 679eeeb commit 0ec01e2

File tree

6 files changed

+128
-34
lines changed

6 files changed

+128
-34
lines changed

.gitmodules

Lines changed: 0 additions & 4 deletions
This file was deleted.

examples/test-backend.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ pub fn main() !void {
99
try window.set(
1010
capy.row(.{}, .{}),
1111
);
12-
var row = capy.label(.{ .text = "test" });
13-
_ = row;
1412

1513
// window.resize(800, 450);
1614
window.show();

src/backends/macos/backend.zig

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const EventFunctions = shared.EventFunctions(@This());
88
const EventType = shared.BackendEventType;
99
const BackendError = shared.BackendError;
1010
const MouseButton = shared.MouseButton;
11-
//pub const PeerType = *c.GtkWidget;
12-
pub const PeerType = *opaque {};
11+
12+
// pub const PeerType = *opaque {};
13+
pub const PeerType = objc.id;
1314

1415
var activeWindows = std.atomic.Atomic(usize).init(0);
1516
var hasInit: bool = false;
@@ -40,14 +41,62 @@ pub const EventUserData = struct {
4041
focusOnClick: bool = false,
4142
};
4243

44+
var test_data = EventUserData{ .peer = undefined };
4345
pub inline fn getEventUserData(peer: PeerType) *EventUserData {
4446
_ = peer;
47+
return &test_data;
4548
//return @ptrCast(*EventUserData, @alignCast(@alignOf(EventUserData), c.g_object_get_data(@ptrCast(*c.GObject, peer), "eventUserData").?));
4649
}
4750

4851
pub fn Events(comptime T: type) type {
49-
_ = T;
50-
return struct {};
52+
return struct {
53+
const Self = @This();
54+
55+
pub fn setUserData(self: *T, data: anytype) void {
56+
comptime {
57+
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
58+
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
59+
}
60+
}
61+
62+
getEventUserData(self.peer).userdata = @intFromPtr(data);
63+
}
64+
65+
pub inline fn setCallback(self: *T, comptime eType: EventType, cb: anytype) !void {
66+
const data = &getEventUserData(self.peer).user;
67+
switch (eType) {
68+
.Click => data.clickHandler = cb,
69+
.Draw => data.drawHandler = cb,
70+
.MouseButton => data.mouseButtonHandler = cb,
71+
.MouseMotion => data.mouseMotionHandler = cb,
72+
.Scroll => data.scrollHandler = cb,
73+
.TextChanged => data.changedTextHandler = cb,
74+
.Resize => data.resizeHandler = cb,
75+
.KeyType => data.keyTypeHandler = cb,
76+
.KeyPress => data.keyPressHandler = cb,
77+
.PropertyChange => data.propertyChangeHandler = cb,
78+
}
79+
}
80+
81+
pub fn setOpacity(self: *const T, opacity: f32) void {
82+
_ = opacity;
83+
_ = self;
84+
}
85+
86+
pub fn getWidth(self: *const T) u32 {
87+
_ = self;
88+
return 100;
89+
}
90+
91+
pub fn getHeight(self: *const T) u32 {
92+
_ = self;
93+
return 100;
94+
}
95+
96+
pub fn deinit(self: *const T) void {
97+
_ = self;
98+
}
99+
};
51100
}
52101

53102
pub const Window = struct {
@@ -59,7 +108,7 @@ pub const Window = struct {
59108

60109
pub fn create() BackendError!Window {
61110
const NSWindow = objc.getClass("NSWindow") catch return BackendError.InitializationError;
62-
const rect = objc.NSRectMake(100, 100, 200, 200);
111+
const rect = objc.NSRect.make(100, 100, 200, 200);
63112
const style = AppKit.NSWindowStyleMask.Titled | AppKit.NSWindowStyleMask.Closable | AppKit.NSWindowStyleMask.Miniaturizable | AppKit.NSWindowStyleMask.Resizable;
64113

65114
std.log.info("make new rect", .{});
@@ -82,7 +131,12 @@ pub const Window = struct {
82131
}
83132

84133
pub fn resize(self: *Window, width: c_int, height: c_int) void {
85-
const frame = objc.NSRectMake(100, 100, @as(objc.CGFloat, @floatFromInt(width)), @as(objc.CGFloat, @floatFromInt(height)));
134+
const frame = objc.NSRect.make(
135+
100,
136+
100,
137+
@as(objc.CGFloat, @floatFromInt(width)),
138+
@as(objc.CGFloat, @floatFromInt(height)),
139+
);
86140
// TODO: resize animation can be handled using a DataWrapper on the user-facing API
87141
_ = objc.msgSendByName(void, self.peer, "setFrame:display:", .{ frame, true }) catch unreachable;
88142
}
@@ -117,6 +171,51 @@ pub const Window = struct {
117171
}
118172
};
119173

174+
pub const Container = struct {
175+
peer: objc.id,
176+
177+
pub usingnamespace Events(Container);
178+
179+
pub fn create() BackendError!Container {
180+
return Container{ .peer = undefined };
181+
}
182+
183+
pub fn add(self: *const Container, peer: PeerType) void {
184+
_ = peer;
185+
_ = self;
186+
}
187+
188+
pub fn remove(self: *const Container, peer: PeerType) void {
189+
_ = peer;
190+
_ = self;
191+
}
192+
193+
pub fn move(self: *const Container, peer: PeerType, x: u32, y: u32) void {
194+
_ = y;
195+
_ = x;
196+
_ = peer;
197+
_ = self;
198+
}
199+
200+
pub fn resize(self: *const Container, peer: PeerType, w: u32, h: u32) void {
201+
_ = h;
202+
_ = w;
203+
_ = peer;
204+
_ = self;
205+
}
206+
207+
pub fn setTabOrder(self: *const Container, peers: []const PeerType) void {
208+
_ = peers;
209+
_ = self;
210+
}
211+
};
212+
213+
pub const Canvas = struct {
214+
pub usingnamespace Events(Canvas);
215+
216+
pub const DrawContext = struct {};
217+
};
218+
120219
pub fn postEmptyEvent() void {
121220
@panic("TODO: postEmptyEvent");
122221
}

src/backends/macos/objc.zig

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ pub fn msgSend(comptime ReturnType: type, target: anytype, selector: SEL, args:
1313
const FnType = blk: {
1414
{
1515
// TODO(hazeycode): replace this hack with the more generalised code above once it doens't crash the compiler
16-
break :blk switch (args_meta.len) {
16+
break :blk *const switch (args_meta.len) {
1717
0 => fn (@TypeOf(target), SEL) callconv(.C) ReturnType,
18-
1 => fn (@TypeOf(target), SEL, args_meta[0].field_type) callconv(.C) ReturnType,
19-
2 => fn (@TypeOf(target), SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) ReturnType,
20-
3 => fn (@TypeOf(target), SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type) callconv(.C) ReturnType,
21-
4 => fn (@TypeOf(target), SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type) callconv(.C) ReturnType,
22-
5 => fn (@TypeOf(target), SEL, args_meta[0].field_type, args_meta[1].field_type, args_meta[2].field_type, args_meta[3].field_type, args_meta[4].field_type) callconv(.C) ReturnType,
18+
1 => fn (@TypeOf(target), SEL, args_meta[0].type) callconv(.C) ReturnType,
19+
2 => fn (@TypeOf(target), SEL, args_meta[0].type, args_meta[1].type) callconv(.C) ReturnType,
20+
3 => fn (@TypeOf(target), SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type) callconv(.C) ReturnType,
21+
4 => fn (@TypeOf(target), SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type, args_meta[3].type) callconv(.C) ReturnType,
22+
5 => fn (@TypeOf(target), SEL, args_meta[0].type, args_meta[1].type, args_meta[2].type, args_meta[3].type, args_meta[4].type) callconv(.C) ReturnType,
2323
else => @compileError("Unsupported number of args: add more variants in zig-objcrt/src/message.zig"),
2424
};
2525
}
2626
};
2727
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
28-
var func = @as(FnType, @ptrCast(c.objc_msgSend));
29-
return @call(.{}, func, .{ target, selector } ++ args);
28+
var func = @as(FnType, @ptrCast(&c.objc_msgSend));
29+
return @call(.auto, func, .{ target, selector } ++ args);
3030
} else {
3131
const FnType = blk: {
3232
{
3333
// TODO(hazeycode): replace this hack with the more generalised code above once it doens't crash the compiler
34-
break :blk switch (args_meta.len) {
34+
break :blk *const switch (args_meta.len) {
3535
0 => fn (*ReturnType, @TypeOf(target), SEL) callconv(.C) void,
36-
1 => fn (*ReturnType, @TypeOf(target), SEL, args_meta[0].field_type) callconv(.C) void,
37-
2 => fn (*ReturnType, @TypeOf(target), SEL, args_meta[0].field_type, args_meta[1].field_type) callconv(.C) void,
36+
1 => fn (*ReturnType, @TypeOf(target), SEL, args_meta[0].type) callconv(.C) void,
37+
2 => fn (*ReturnType, @TypeOf(target), SEL, args_meta[0].type, args_meta[1].type) callconv(.C) void,
3838
else => @compileError("Unsupported number of args: add more variants in zig-objcrt/src/message.zig"),
3939
};
4040
}
4141
};
4242
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
43-
var func = @as(FnType, @ptrCast(c.objc_msgSend_stret));
43+
var func = @as(FnType, @ptrCast(&c.objc_msgSend_stret));
4444
var stret: ReturnType = undefined;
45-
_ = @call(.{}, func, .{ &stret, target, selector } ++ args);
45+
_ = @call(.auto, func, .{ &stret, target, selector } ++ args);
4646
return stret;
4747
}
4848
}
@@ -143,20 +143,13 @@ pub fn getClass(class_name: [:0]const u8) Error!Class {
143143
return c.objc_getClass(class_name) orelse Error.ClassNotRegisteredWithRuntime;
144144
}
145145

146-
pub const CGFloat = switch (@import("builtin").cpu.arch.ptrBitWidth()) {
146+
pub const CGFloat = switch (@import("builtin").target.ptrBitWidth()) {
147147
64 => f64,
148148
32 => f32,
149149
else => unreachable,
150150
};
151151

152152
pub const NSRect = CGRect;
153-
pub const NSRectMake = CGRectMake;
154-
pub fn CGRectMake(x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat) CGRect {
155-
return CGRect{
156-
.origin = .{ .x = x, .y = y },
157-
.size = .{ .width = width, .height = height },
158-
};
159-
}
160153

161154
pub const CGPoint = extern struct {
162155
x: CGFloat,
@@ -171,4 +164,11 @@ pub const CGSize = extern struct {
171164
pub const CGRect = extern struct {
172165
origin: CGPoint,
173166
size: CGSize,
167+
168+
pub fn make(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat) CGRect {
169+
return .{
170+
.origin = .{ .x = x, .y = y },
171+
.size = .{ .width = w, .height = h },
172+
};
173+
}
174174
};

src/data.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,15 @@ pub fn Atom(comptime T: type) type {
126126
onChange: ChangeListenerList = .{},
127127
/// List of all Atoms this one is bound to.
128128
bindings: BindingList = .{},
129+
129130
/// This boolean is used to protect from recursive relations between wrappers
130131
/// For example if there are two two-way binded data wrappers A and B:
131132
/// When A is set, B is set too. Since B is set, it will set A too. A is set, it will set B too, and so on..
132133
/// To prevent that, the bindLock is set to true when setting the value of the other.
133134
/// If the lock is equal to true, set() returns without calling the other. For example:
134135
/// When A is set, it sets the lock to true and sets B. Since B is set, it will set A too.
135136
/// A notices that bindLock is already set to true, and thus returns.
137+
/// TODO: make the bind lock more general and just use it for any change, and explicit how this favors completeness instead of consistency (in case an onChange triggers set method manually)
136138
bindLock: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
137139

138140
/// If dependOn has been called, this is a pointer to the callback function

src/internal.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,6 @@ pub fn Events(comptime T: type) type {
578578
/// When the value is changed in the opacity data wrapper
579579
fn opacityChanged(newValue: f32, userdata: usize) void {
580580
const widget = @as(*T, @ptrFromInt(userdata));
581-
std.log.info("opaccity changed to {d}", .{newValue});
582581
if (widget.peer) |*peer| {
583582
peer.setOpacity(newValue);
584583
}

0 commit comments

Comments
 (0)