core: move resources dir to main global state

This commit is contained in:
Mitchell Hashimoto
2023-11-22 21:12:01 -08:00
parent 0dc5516ac6
commit 45a4be6873
3 changed files with 14 additions and 12 deletions

View File

@ -41,10 +41,6 @@ mailbox: Mailbox.Queue,
/// Set to true once we're quitting. This never goes false again.
quit: bool,
/// The app resources directory, equivalent to zig-out/share when we build
/// from source. This is null if we can't detect it.
resources_dir: ?[]const u8 = null,
/// Font discovery mechanism. This is only safe to use from the main thread.
/// This is lazily initialized on the first call to fontDiscover so do
/// not access this directly.
@ -59,17 +55,11 @@ pub fn create(
var app = try alloc.create(App);
errdefer alloc.destroy(app);
// Find our resources directory once for the app so every launch
// hereafter can use this cached value.
const resources_dir = try internal_os.resourcesDir(alloc);
errdefer if (resources_dir) |dir| alloc.free(dir);
app.* = .{
.alloc = alloc,
.surfaces = .{},
.mailbox = .{},
.quit = false,
.resources_dir = resources_dir,
};
errdefer app.surfaces.deinit(alloc);
@ -81,7 +71,6 @@ pub fn destroy(self: *App) void {
for (self.surfaces.items) |surface| surface.deinit();
self.surfaces.deinit(self.alloc);
if (self.resources_dir) |dir| self.alloc.free(dir);
if (comptime font.Discover != void) {
if (self.font_discover) |*v| v.deinit();
}

View File

@ -21,6 +21,7 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const ArenaAllocator = std.heap.ArenaAllocator;
const ziglyph = @import("ziglyph");
const main = @import("main.zig");
const renderer = @import("renderer.zig");
const termio = @import("termio.zig");
const objc = @import("objc");
@ -445,7 +446,7 @@ pub fn init(
.padding = padding,
.full_config = config,
.config = try termio.Impl.DerivedConfig.init(alloc, config),
.resources_dir = app.resources_dir,
.resources_dir = main.state.resources_dir,
.renderer_state = &self.renderer_state,
.renderer_wakeup = render_thread.wakeup,
.renderer_mailbox = render_thread.mailbox,

View File

@ -175,6 +175,10 @@ pub const GlobalState = struct {
action: ?cli.Action,
logging: Logging,
/// The app resources directory, equivalent to zig-out/share when we build
/// from source. This is null if we can't detect it.
resources_dir: ?[]const u8,
/// Where logging should go
pub const Logging = union(enum) {
disabled: void,
@ -192,6 +196,7 @@ pub const GlobalState = struct {
.tracy = undefined,
.action = null,
.logging = .{ .stderr = {} },
.resources_dir = null,
};
errdefer self.deinit();
@ -271,11 +276,18 @@ pub const GlobalState = struct {
// Initialize glslang for shader compilation
try glslang.init();
// Find our resources directory once for the app so every launch
// hereafter can use this cached value.
self.resources_dir = try internal_os.resourcesDir(self.alloc);
errdefer if (self.resources_dir) |dir| self.alloc.free(dir);
}
/// Cleans up the global state. This doesn't _need_ to be called but
/// doing so in dev modes will check for memory leaks.
pub fn deinit(self: *GlobalState) void {
if (self.resources_dir) |dir| self.alloc.free(dir);
if (self.gpa) |*value| {
// We want to ensure that we deinit the GPA because this is
// the point at which it will output if there were safety violations.