From f8a2858df8bd8359e23724dfd1a3dfbd4dd12d8f Mon Sep 17 00:00:00 2001 From: foxnne Date: Sun, 1 Dec 2024 23:02:07 -0600 Subject: [PATCH] darwin: Get window title working --- src/Core.zig | 8 ++++---- src/core/Darwin.zig | 35 +++++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/Core.zig b/src/Core.zig index c8ca5839..0826f7e0 100644 --- a/src/Core.zig +++ b/src/Core.zig @@ -40,7 +40,7 @@ windows: mach.Objects( /// Window title string // TODO: document how to set this using a format string // TODO: allocation/free strategy - title: []const u8 = "Mach Window", + title: [:0]const u8 = "Mach Window", /// Texture format of the framebuffer (read-only) framebuffer_format: gpu.Texture.Format = .bgra8_unorm, @@ -65,17 +65,17 @@ windows: mach.Objects( /// Outer border border: bool = true, - /// Width of the window in virtual pixels (read-only) + /// Width of the window in virtual pixels width: u32 = 1920 / 2, - /// Height of the window in virtual pixels (read-only) + /// Height of the window in virtual pixels height: u32 = 1080 / 2, /// Target frames per second refresh_rate: u32 = 0, // GPU - // When device is not null, the rest of the fields have been + // When `native` is not null, the rest of the fields have been // initialized. device: *gpu.Device = undefined, instance: *gpu.Instance = undefined, diff --git a/src/core/Darwin.zig b/src/core/Darwin.zig index 4690ce47..4be82a4d 100644 --- a/src/core/Darwin.zig +++ b/src/core/Darwin.zig @@ -21,7 +21,7 @@ const log = std.log.scoped(.mach); pub const Darwin = @This(); pub const Native = struct { - window: ?*objc.app_kit.Window = null, + window: *objc.app_kit.Window = undefined, }; pub const Context = struct { @@ -57,7 +57,6 @@ pub fn run(comptime on_each_update_fn: anytype, args_tuple: std.meta.ArgsTuple(@ ns_app.setDelegate(@ptrCast(delegate)); ns_app.run(); - //_ = objc.app_kit.applicationMain(0, undefined); unreachable; // TODO: support UIKit. @@ -66,17 +65,22 @@ pub fn run(comptime on_each_update_fn: anytype, args_tuple: std.meta.ArgsTuple(@ pub fn tick(core: *Core) !void { var windows = core.windows.slice(); while (windows.next()) |window_id| { - const native_opt: ?Native = core.windows.get(window_id, .native); + const core_window = windows.get(window_id); - if (native_opt) |native| { - if (native.window) |native_window| { - // Handle resizing the window when the user changes width or height - if (core.windows.updated(window_id, .width) or core.windows.updated(window_id, .height)) { - var frame = native_window.frame(); - frame.size.width = @floatFromInt(core.windows.get(window_id, .width)); - frame.size.height = @floatFromInt(core.windows.get(window_id, .height)); - native_window.setFrame_display_animate(frame, true, true); - } + if (core_window.native) |native| { + const native_window: *objc.app_kit.Window = native.window; + + if (core.windows.updated(window_id, .title)) { + const string = objc.foundation.String.allocInit(); + defer string.release(); + native.window.setTitle(string.initWithUTF8String(core_window.title)); + } + + if (core.windows.updated(window_id, .width) or core.windows.updated(window_id, .height)) { + var frame = native_window.frame(); + frame.size.width = @floatFromInt(core.windows.get(window_id, .width)); + frame.size.height = @floatFromInt(core.windows.get(window_id, .height)); + native_window.setFrame_display_animate(native_window.frameRectForContentRect(frame), true, true); } } else { try initWindow(core, window_id); @@ -156,10 +160,13 @@ fn initWindow( native_window.setIsVisible(true); native_window.makeKeyAndOrderFront(null); + const string = objc.foundation.String.allocInit(); + defer string.release(); + native_window.setTitle(string.initWithUTF8String(core_window.title)); + const delegate = objc.mach.WindowDelegate.allocInit(); defer native_window.setDelegate(@ptrCast(delegate)); - { // Set WindowDelegate blocks - + { var windowWillResize_toSize = objc.foundation.stackBlockLiteral( WindowDelegateCallbacks.windowWillResize_toSize, context,