app: only create first window in exe mode

This commit is contained in:
Mitchell Hashimoto
2023-02-15 21:53:14 -08:00
parent 26182611c6
commit ba8f142770
3 changed files with 17 additions and 8 deletions

View File

@ -7,6 +7,7 @@ const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const assert = std.debug.assert; const assert = std.debug.assert;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const build_config = @import("build_config.zig");
const apprt = @import("apprt.zig"); const apprt = @import("apprt.zig");
const Window = @import("Window.zig"); const Window = @import("Window.zig");
const tracy = @import("tracy"); const tracy = @import("tracy");
@ -47,9 +48,11 @@ quit: bool,
/// Mac settings /// Mac settings
darwin: if (Darwin.enabled) Darwin else void, darwin: if (Darwin.enabled) Darwin else void,
/// Mac-specific settings /// Mac-specific settings. This is only enabled when the target is
/// Mac and the artifact is a standalone exe. We don't target libs because
/// the embedded API doesn't do windowing.
pub const Darwin = struct { pub const Darwin = struct {
pub const enabled = builtin.target.isDarwin(); pub const enabled = builtin.target.isDarwin() and build_config.artifact == .exe;
tabbing_id: *macos.foundation.String, tabbing_id: *macos.foundation.String,
@ -87,8 +90,9 @@ pub fn create(alloc: Allocator, config: *const Config) !*App {
}; };
errdefer app.windows.deinit(alloc); errdefer app.windows.deinit(alloc);
// On Mac, we enable window tabbing // On Mac, we enable window tabbing. We only do this if we're building
if (comptime builtin.target.isDarwin()) { // a standalone exe. In embedded mode the host app handles this for us.
if (Darwin.enabled) {
const NSWindow = objc.Class.getClass("NSWindow").?; const NSWindow = objc.Class.getClass("NSWindow").?;
NSWindow.msgSend(void, objc.sel("setAllowsAutomaticWindowTabbing:"), .{true}); NSWindow.msgSend(void, objc.sel("setAllowsAutomaticWindowTabbing:"), .{true});
@ -107,8 +111,12 @@ pub fn create(alloc: Allocator, config: *const Config) !*App {
} }
errdefer if (comptime builtin.target.isDarwin()) app.darwin.deinit(); errdefer if (comptime builtin.target.isDarwin()) app.darwin.deinit();
// Create the first window // Create the first window if we're an executable. If we're a lib we
_ = try app.newWindow(.{}); // do NOT create the first window because we expect the embedded API
// to do it via surfaces.
if (build_config.artifact == .exe) {
_ = try app.newWindow(.{});
}
return app; return app;
} }
@ -117,7 +125,7 @@ pub fn destroy(self: *App) void {
// Clean up all our windows // Clean up all our windows
for (self.windows.items) |window| window.destroy(); for (self.windows.items) |window| window.destroy();
self.windows.deinit(self.alloc); self.windows.deinit(self.alloc);
if (comptime builtin.target.isDarwin()) self.darwin.deinit(); if (Darwin.enabled) self.darwin.deinit();
self.mailbox.destroy(self.alloc); self.mailbox.destroy(self.alloc);
self.alloc.destroy(self); self.alloc.destroy(self);

View File

@ -30,6 +30,7 @@ pub const Artifact = enum {
return switch (builtin.output_mode) { return switch (builtin.output_mode) {
.Exe => .exe, .Exe => .exe,
.Obj => .lib, .Obj => .lib,
else => unreachable,
}; };
} }
}; };

View File

@ -91,7 +91,7 @@ pub fn main() !void {
// We want to log all our errors // We want to log all our errors
glfw.setErrorCallback(glfwErrorCallback); glfw.setErrorCallback(glfwErrorCallback);
// Run our app // Run our app with a single initial window to start.
var app = try App.create(alloc, &config); var app = try App.create(alloc, &config);
defer app.destroy(); defer app.destroy();
try app.run(); try app.run();