darwin: Get window title working

This commit is contained in:
foxnne 2024-12-01 23:02:07 -06:00 committed by Emi Gutekanst
parent ad5700cf48
commit f8a2858df8
2 changed files with 25 additions and 18 deletions

View file

@ -40,7 +40,7 @@ windows: mach.Objects(
/// Window title string /// Window title string
// TODO: document how to set this using a format string // TODO: document how to set this using a format string
// TODO: allocation/free strategy // TODO: allocation/free strategy
title: []const u8 = "Mach Window", title: [:0]const u8 = "Mach Window",
/// Texture format of the framebuffer (read-only) /// Texture format of the framebuffer (read-only)
framebuffer_format: gpu.Texture.Format = .bgra8_unorm, framebuffer_format: gpu.Texture.Format = .bgra8_unorm,
@ -65,17 +65,17 @@ windows: mach.Objects(
/// Outer border /// Outer border
border: bool = true, border: bool = true,
/// Width of the window in virtual pixels (read-only) /// Width of the window in virtual pixels
width: u32 = 1920 / 2, width: u32 = 1920 / 2,
/// Height of the window in virtual pixels (read-only) /// Height of the window in virtual pixels
height: u32 = 1080 / 2, height: u32 = 1080 / 2,
/// Target frames per second /// Target frames per second
refresh_rate: u32 = 0, refresh_rate: u32 = 0,
// GPU // 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. // initialized.
device: *gpu.Device = undefined, device: *gpu.Device = undefined,
instance: *gpu.Instance = undefined, instance: *gpu.Instance = undefined,

View file

@ -21,7 +21,7 @@ const log = std.log.scoped(.mach);
pub const Darwin = @This(); pub const Darwin = @This();
pub const Native = struct { pub const Native = struct {
window: ?*objc.app_kit.Window = null, window: *objc.app_kit.Window = undefined,
}; };
pub const Context = struct { 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.setDelegate(@ptrCast(delegate));
ns_app.run(); ns_app.run();
//_ = objc.app_kit.applicationMain(0, undefined);
unreachable; unreachable;
// TODO: support UIKit. // 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 { pub fn tick(core: *Core) !void {
var windows = core.windows.slice(); var windows = core.windows.slice();
while (windows.next()) |window_id| { 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 (core_window.native) |native| {
if (native.window) |native_window| { const native_window: *objc.app_kit.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)) { if (core.windows.updated(window_id, .title)) {
var frame = native_window.frame(); const string = objc.foundation.String.allocInit();
frame.size.width = @floatFromInt(core.windows.get(window_id, .width)); defer string.release();
frame.size.height = @floatFromInt(core.windows.get(window_id, .height)); native.window.setTitle(string.initWithUTF8String(core_window.title));
native_window.setFrame_display_animate(frame, true, true); }
}
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 { } else {
try initWindow(core, window_id); try initWindow(core, window_id);
@ -156,10 +160,13 @@ fn initWindow(
native_window.setIsVisible(true); native_window.setIsVisible(true);
native_window.makeKeyAndOrderFront(null); 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(); const delegate = objc.mach.WindowDelegate.allocInit();
defer native_window.setDelegate(@ptrCast(delegate)); defer native_window.setDelegate(@ptrCast(delegate));
{ // Set WindowDelegate blocks {
var windowWillResize_toSize = objc.foundation.stackBlockLiteral( var windowWillResize_toSize = objc.foundation.stackBlockLiteral(
WindowDelegateCallbacks.windowWillResize_toSize, WindowDelegateCallbacks.windowWillResize_toSize,
context, context,