Skip to content

Commit

Permalink
Merge pull request #67 from dvmason/master
Browse files Browse the repository at this point in the history
var->const and internal trait.zig to compile on latest zig
  • Loading branch information
zenith391 authored Dec 21, 2023
2 parents b927197 + 83aa9fb commit 71a578f
Show file tree
Hide file tree
Showing 41 changed files with 248 additions and 124 deletions.
2 changes: 1 addition & 1 deletion android/build/auto-detect.zig
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ pub fn findUserConfig(b: *Builder, versions: Sdk.ToolchainVersions) !UserConfig
};

// Get the android studio registry entry
var android_studio_key: HKEY = for ([_]HKEY{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }) |root_key| {
const android_studio_key: HKEY = for ([_]HKEY{ HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE }) |root_key| {
var software: HKEY = null;
if (reg.RegOpenKeyA(root_key, "software", &software) == ERROR_SUCCESS) {
defer _ = reg.RegCloseKey(software);
Expand Down
2 changes: 1 addition & 1 deletion android/examples/invocationhandler/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub const AndroidApp = struct {
pipe: [2]std.os.fd_t = undefined,
// This is used with futexes so that runOnUiThread waits until the callback is completed
// before returning.
uiThreadCondition: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
uiThreadCondition: std.atomic.Value(u32) = std.atomic.Value(u32).init(0),
uiThreadLooper: *android.ALooper = undefined,
uiThreadId: std.Thread.Id = undefined,

Expand Down
2 changes: 1 addition & 1 deletion android/examples/textview/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const AndroidApp = struct {
pipe: [2]std.os.fd_t = undefined,
// This is used with futexes so that runOnUiThread waits until the callback is completed
// before returning.
uiThreadCondition: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
uiThreadCondition: std.atomic.Value(u32) = std.atomic.Value(u32).init(0),
uiThreadLooper: *android.ALooper = undefined,
uiThreadId: std.Thread.Id = undefined,

Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

var examplesDir = try std.fs.cwd().openIterableDir("examples", .{});
var examplesDir = try if (@hasField(std.fs.Dir.OpenDirOptions,"iterate")) std.fs.cwd().openDir("examples",.{ .iterate = true }) else std.fs.cwd().openIterableDir("examples",.{}); // support zig 0.11 as well as current master
defer examplesDir.close();

const broken = switch (target.getOsTag()) {
Expand Down
2 changes: 1 addition & 1 deletion src/async.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub const ThreadPool = struct {
thread: std.Thread,
/// The last time a task was executed on this thread, in milliseconds.
last_used: i64,
busy: std.atomic.Atomic(bool) = false,
busy: std.atomic.Value(bool) = false,
};

pub fn init(allocator: std.mem.Allocator) ThreadPool {
Expand Down
15 changes: 8 additions & 7 deletions src/backends/android/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");
const shared = @import("../shared.zig");
const lib = @import("../../main.zig");
const android = @import("android");
const trait = @import("../../trait.zig");

const EventFunctions = shared.EventFunctions(@This());
const EventType = shared.BackendEventType;
Expand All @@ -10,7 +11,7 @@ const MouseButton = shared.MouseButton;

pub const PeerType = *anyopaque; // jobject but not optional

var activeWindows = std.atomic.Atomic(usize).init(0);
var activeWindows = std.atomic.Value(usize).init(0);
var hasInit: bool = false;
var theApp: *backendExport.AndroidApp = undefined;

Expand Down Expand Up @@ -129,7 +130,7 @@ pub fn Events(comptime T: type) type {

pub fn setupEvents(widget: PeerType) BackendError!void {
const jni = theApp.getJni();
var data = try lib.internal.lasting_allocator.create(EventUserData);
const data = try lib.internal.lasting_allocator.create(EventUserData);
data.* = EventUserData{ .peer = widget }; // ensure that it uses default values

// Wrap the memory address in a Long object
Expand All @@ -154,7 +155,7 @@ pub fn Events(comptime T: type) type {

pub inline fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down Expand Up @@ -732,7 +733,7 @@ pub const backendExport = struct {
pipe: [2]std.os.fd_t = undefined,
// This is used with futexes so that runOnUiThread waits until the callback is completed
// before returning.
uiThreadCondition: std.atomic.Atomic(u32) = std.atomic.Atomic(u32).init(0),
uiThreadCondition: std.atomic.Value(u32) = std.atomic.Value(u32).init(0),

// TODO: add an interface in capy for handling stored state
pub fn init(allocator: std.mem.Allocator, activity: *android.ANativeActivity, stored_state: ?[]const u8) !AndroidApp {
Expand All @@ -751,7 +752,7 @@ pub const backendExport = struct {
self.pipe = try std.os.pipe();
android.ALooper_acquire(self.uiThreadLooper);

var native_activity = android.NativeActivity.init(self.activity);
const native_activity = android.NativeActivity.init(self.activity);
self.uiJni = native_activity.jni;
const jni = native_activity.jni;

Expand Down Expand Up @@ -836,7 +837,7 @@ pub const backendExport = struct {
}

pub fn getJni(self: *AndroidApp) *android.JNI {
var native_activity = android.NativeActivity.get(self.activity);
const native_activity = android.NativeActivity.get(self.activity);
return native_activity.jni;
}

Expand Down Expand Up @@ -869,7 +870,7 @@ pub const backendExport = struct {
}

fn mainLoop(self: *AndroidApp) !void {
var native_activity = android.NativeActivity.init(self.activity);
const native_activity = android.NativeActivity.init(self.activity);
self.mainJni = native_activity.jni;

try self.runOnUiThread(setAppContentView, .{self});
Expand Down
3 changes: 2 additions & 1 deletion src/backends/gles/backend.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const shared = @import("../shared.zig");
const lib = @import("../../main.zig");
const trait = @import("../../trait.zig");
const c = @cImport({
@cDefine("GLFW_INCLUDE_ES3", {});
@cInclude("GLFW/glfw3.h");
Expand Down Expand Up @@ -161,7 +162,7 @@ pub fn Events(comptime T: type) type {

pub inline fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/backends/gtk/backend.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const shared = @import("../shared.zig");
const lib = @import("../../main.zig");
const trait = @import("../../trait.zig");
pub const c = @cImport({
@cInclude("gtk/gtk.h");
});
Expand All @@ -20,7 +21,7 @@ const GTK_VERSION = std.SemanticVersion.Range{

pub const Capabilities = .{ .useEventLoop = true };

var activeWindows = std.atomic.Atomic(usize).init(0);
var activeWindows = std.atomic.Value(usize).init(0);
var randomWindow: *c.GtkWidget = undefined;

var hasInit: bool = false;
Expand Down Expand Up @@ -281,7 +282,7 @@ pub fn Events(comptime T: type) type {
_ = c.g_signal_connect_data(event_controller_legacy, "event", @as(c.GCallback, @ptrCast(&gtkButtonPress)), null, null, c.G_CONNECT_AFTER);
c.gtk_widget_add_controller(widget, event_controller_legacy);

var data = try lib.internal.lasting_allocator.create(EventUserData);
const data = try lib.internal.lasting_allocator.create(EventUserData);
data.* = EventUserData{ .peer = widget }; // ensure that it uses default values
c.g_object_set_data(@as(*c.GObject, @ptrCast(widget)), "eventUserData", data);
_ = c.g_object_ref(@as(*c.GObject, @ptrCast(widget)));
Expand Down Expand Up @@ -441,7 +442,7 @@ pub fn Events(comptime T: type) type {

pub inline fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down Expand Up @@ -1242,7 +1243,7 @@ pub const NavigationSidebar = struct {

pub fn create() BackendError!NavigationSidebar {
const listBox = c.gtk_list_box_new();
var context: *c.GtkStyleContext = c.gtk_widget_get_style_context(listBox);
const context: *c.GtkStyleContext = c.gtk_widget_get_style_context(listBox);
c.gtk_style_context_add_class(context, "navigation-sidebar");

// A custom component is used to bypass GTK's minimum size mechanism
Expand All @@ -1268,7 +1269,7 @@ pub const NavigationSidebar = struct {
const label_gtk = c.gtk_label_new(label);
c.gtk_box_append(@ptrCast(box), label_gtk);

var context: *c.GtkStyleContext = c.gtk_widget_get_style_context(box);
const context: *c.GtkStyleContext = c.gtk_widget_get_style_context(box);
c.gtk_style_context_add_class(context, "activatable");
c.gtk_style_context_add_class(context, "row");
}
Expand Down
6 changes: 4 additions & 2 deletions src/backends/macos/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const shared = @import("../shared.zig");
const lib = @import("../../main.zig");
const objc = @import("objc.zig");
const AppKit = @import("AppKit.zig");
const trait = @import("../../trait.zig");

const EventFunctions = shared.EventFunctions(@This());
const EventType = shared.BackendEventType;
Expand All @@ -12,7 +13,8 @@ const MouseButton = shared.MouseButton;
// pub const PeerType = *opaque {};
pub const PeerType = objc.id;

var activeWindows = std.atomic.Atomic(usize).init(0);
const atomicValue = if (@hasDecl(std.atomic,"Value")) std.atomic.Value else std.atomic.Atomic; // support zig 0.11 as well as current master
var activeWindows = atomicValue(usize).init(0);
var hasInit: bool = false;

pub fn init() BackendError!void {
Expand Down Expand Up @@ -54,7 +56,7 @@ pub fn Events(comptime T: type) type {

pub fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/backends/macos/objc.zig
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Courtesy of https://github.com/hazeycode/zig-objcrt
const std = @import("std");
const c = @import("c.zig");
const trait = @import("../../trait.zig");

/// Sends a message to an id or Class and returns the return value of the called method
pub fn msgSend(comptime ReturnType: type, target: anytype, selector: SEL, args: anytype) ReturnType {
const target_type = @TypeOf(target);
if ((target_type == id or target_type == Class) == false) @compileError("msgSend target should be of type id or Class");

const args_meta = @typeInfo(@TypeOf(args)).Struct.fields;

if (comptime !std.meta.trait.isContainer(ReturnType)) {
if (comptime !trait.isContainer(ReturnType) or @import("builtin").cpu.arch == .aarch64) {
const FnType = blk: {
{
// TODO(hazeycode): replace this hack with the more generalised code above once it doens't crash the compiler
Expand All @@ -25,7 +25,7 @@ pub fn msgSend(comptime ReturnType: type, target: anytype, selector: SEL, args:
}
};
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
var func = @as(FnType, @ptrCast(&c.objc_msgSend));
const func = @as(FnType, @ptrCast(&c.objc_msgSend));
return @call(.auto, func, .{ target, selector } ++ args);
} else {
const FnType = blk: {
Expand All @@ -40,7 +40,7 @@ pub fn msgSend(comptime ReturnType: type, target: anytype, selector: SEL, args:
}
};
// NOTE: func is a var because making it const causes a compile error which I believe is a compiler bug
var func = @as(FnType, @ptrCast(&c.objc_msgSend_stret));
const func = @as(FnType, @ptrCast(&c.objc_msgSend_stret));
var stret: ReturnType = undefined;
_ = @call(.auto, func, .{ &stret, target, selector } ++ args);
return stret;
Expand Down
4 changes: 3 additions & 1 deletion src/backends/wasm/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const std = @import("std");
const shared = @import("../shared.zig");
const lib = @import("../../main.zig");
const js = @import("js.zig");
const trait = @import("../../trait.zig");

const lasting_allocator = lib.internal.lasting_allocator;

const EventType = shared.BackendEventType;
Expand Down Expand Up @@ -106,7 +108,7 @@ pub fn Events(comptime T: type) type {

pub inline fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/backends/win32/backend.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const std = @import("std");
const lib = @import("../../main.zig");
const shared = @import("../shared.zig");
const trait = @import("../../trait.zig");
const os = @import("builtin").target.os;
const log = std.log.scoped(.win32);

Expand Down Expand Up @@ -516,7 +517,7 @@ pub fn Events(comptime T: type) type {
_ = win32.GetClientRect(hwnd, &rc);

var render_target: ?*win32.ID2D1HwndRenderTarget = null;
var hresult = d2dFactory.ID2D1Factory_CreateHwndRenderTarget(
const hresult = d2dFactory.ID2D1Factory_CreateHwndRenderTarget(
&win32.D2D1_RENDER_TARGET_PROPERTIES{
.type = win32.D2D1_RENDER_TARGET_TYPE_DEFAULT,
.pixelFormat = .{
Expand Down Expand Up @@ -576,14 +577,14 @@ pub fn Events(comptime T: type) type {
}

pub fn setupEvents(peer: HWND) !void {
var data = try lib.internal.lasting_allocator.create(EventUserData);
const data = try lib.internal.lasting_allocator.create(EventUserData);
data.* = EventUserData{}; // ensure that it uses default values
_ = win32Backend.setWindowLongPtr(peer, win32.GWL_USERDATA, @intFromPtr(data));
}

pub inline fn setUserData(self: *T, data: anytype) void {
comptime {
if (!std.meta.trait.isSingleItemPtr(@TypeOf(data))) {
if (!trait.isSingleItemPtr(@TypeOf(data))) {
@compileError(std.fmt.comptimePrint("Expected single item pointer, got {s}", .{@typeName(@TypeOf(data))}));
}
}
Expand Down Expand Up @@ -1287,7 +1288,7 @@ pub const TabContainer = struct {
if (self.shownPeer) |previousPeer| {
_ = win32.ShowWindow(previousPeer, win32.SW_HIDE);
}
var peer = self.peerList.items[index];
const peer = self.peerList.items[index];
_ = win32.SetParent(peer, self.peer);
_ = win32.ShowWindow(peer, win32.SW_SHOWDEFAULT);
_ = win32.UpdateWindow(peer);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Alignment.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const Alignment = struct {
widget_data: Alignment.WidgetData = .{},

child: Widget,
relayouting: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
relayouting: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
x: Atom(f32) = Atom(f32).of(0.5),
y: Atom(f32) = Atom(f32).of(0.5),

Expand Down
2 changes: 1 addition & 1 deletion src/components/Navigation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub const Navigation = struct {
peer: ?backend.Container = null,
widget_data: Navigation.WidgetData = .{},

relayouting: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
relayouting: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
routeName: Atom([]const u8),
activeChild: *Widget,
routes: std.StringHashMap(Widget),
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextArea.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const TextArea = struct {
peer: ?backend.TextArea = null,
widget_data: TextArea.WidgetData = .{},
text: StringAtom = StringAtom.of(""),
_wrapperTextBlock: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
_wrapperTextBlock: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),

// TODO: replace with TextArea.setFont(.{ .family = "monospace" }) ?
/// Whether to let the system choose a monospace font for us and use it in this TextArea..
Expand Down
2 changes: 1 addition & 1 deletion src/components/TextField.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const TextField = struct {
widget_data: TextField.WidgetData = .{},
text: StringAtom = StringAtom.of(""),
readOnly: Atom(bool) = Atom(bool).of(false),
_wrapperTextBlock: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
_wrapperTextBlock: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),

pub fn init(config: TextField.Config) TextField {
var field = TextField.init_events(TextField{
Expand Down
3 changes: 2 additions & 1 deletion src/containers.zig
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,14 @@ pub const Container = struct {
widget_data: Container.WidgetData = .{},
childrens: std.ArrayList(Widget),
expand: bool,
relayouting: std.atomic.Atomic(bool) = std.atomic.Atomic(bool).init(false),
relayouting: atomicValue(bool) = atomicValue(bool).init(false),
layout: Layout,
layoutConfig: [16]u8,

/// The widget associated to this Container
widget: ?*Widget = null,

const atomicValue = if (@hasDecl(std.atomic,"Value")) std.atomic.Value else std.atomic.Atomic; // support zig 0.11 as well as current master
pub fn init(childrens: std.ArrayList(Widget), config: GridConfig, layout: Layout, layoutConfig: anytype) !Container {
const LayoutConfig = @TypeOf(layoutConfig);
comptime std.debug.assert(@sizeOf(LayoutConfig) <= 16);
Expand Down
Loading

0 comments on commit 71a578f

Please sign in to comment.