implement reload_config app message

This commit is contained in:
Mitchell Hashimoto
2023-03-13 21:52:42 -07:00
parent 3e1f975551
commit a9928cfb90
4 changed files with 47 additions and 16 deletions

View File

@ -18,7 +18,6 @@ const renderer = @import("renderer.zig");
const font = @import("font/main.zig"); const font = @import("font/main.zig");
const macos = @import("macos"); const macos = @import("macos");
const objc = @import("objc"); const objc = @import("objc");
const DevMode = @import("DevMode.zig");
const log = std.log.scoped(.app); const log = std.log.scoped(.app);
@ -43,9 +42,6 @@ quit: bool,
pub fn create( pub fn create(
alloc: Allocator, alloc: Allocator,
) !*App { ) !*App {
// If we have DevMode on, store the config so we can show it
//if (DevMode.enabled) DevMode.instance.config = config;
var app = try alloc.create(App); var app = try alloc.create(App);
errdefer alloc.destroy(app); errdefer alloc.destroy(app);
app.* = .{ app.* = .{
@ -93,17 +89,12 @@ pub fn tick(self: *App, rt_app: *apprt.App) !bool {
} }
/// Update the configuration associated with the app. This can only be /// Update the configuration associated with the app. This can only be
/// called from the main thread. /// called from the main thread. The caller owns the config memory. The
/// /// memory can be freed immediately when this returns.
/// The caller owns the config memory. The prior config must not be freed
/// until this function returns successfully.
pub fn updateConfig(self: *App, config: *const Config) !void { pub fn updateConfig(self: *App, config: *const Config) !void {
// Update our config
self.config = config;
// Go through and update all of the surface configurations. // Go through and update all of the surface configurations.
for (self.surfaces.items) |surface| { for (self.surfaces.items) |surface| {
try surface.handleMessage(.{ .change_config = config }); try surface.core_surface.handleMessage(.{ .change_config = config });
} }
} }
@ -144,9 +135,9 @@ fn drainMailbox(self: *App, rt_app: *apprt.App) !void {
} }
fn reloadConfig(self: *App, rt_app: *apprt.App) !void { fn reloadConfig(self: *App, rt_app: *apprt.App) !void {
_ = rt_app; if (try rt_app.reloadConfig()) |new| {
_ = self; try self.updateConfig(new);
//try rt_app.reloadConfig(); }
} }
fn closeSurface(self: *App, rt_app: *apprt.App, surface: *Surface) !void { fn closeSurface(self: *App, rt_app: *apprt.App, surface: *Surface) !void {

View File

@ -27,7 +27,7 @@ pub var instance: DevMode = .{};
visible: bool = false, visible: bool = false,
/// Our app config /// Our app config
config: ?*const Config = null, config: ?Config = null,
/// The surface we're tracking. /// The surface we're tracking.
surface: ?*Surface = null, surface: ?*Surface = null,

View File

@ -20,6 +20,7 @@ const apprt = @import("../apprt.zig");
const CoreApp = @import("../App.zig"); const CoreApp = @import("../App.zig");
const CoreSurface = @import("../Surface.zig"); const CoreSurface = @import("../Surface.zig");
const Config = @import("../config.zig").Config; const Config = @import("../config.zig").Config;
const DevMode = @import("../DevMode.zig");
// Get native API access on certain platforms so we can do more customization. // Get native API access on certain platforms so we can do more customization.
const glfwNative = glfw.Native(.{ const glfwNative = glfw.Native(.{
@ -59,6 +60,11 @@ pub const App = struct {
var config = try Config.load(core_app.alloc); var config = try Config.load(core_app.alloc);
errdefer config.deinit(); errdefer config.deinit();
// If we have DevMode on, store the config so we can show it. This
// is messy because we're copying a thing here. We should clean this
// up when we take a pass at cleaning up the dev mode.
if (DevMode.enabled) DevMode.instance.config = config;
return .{ return .{
.app = core_app, .app = core_app,
.config = config, .config = config,
@ -97,6 +103,23 @@ pub const App = struct {
glfw.postEmptyEvent(); glfw.postEmptyEvent();
} }
/// Reload the configuration. This should return the new configuration.
/// The old value can be freed immediately at this point assuming a
/// successful return.
///
/// The returned pointer value is only valid for a stable self pointer.
pub fn reloadConfig(self: *App) !?*const Config {
// Load our configuration
var config = try Config.load(self.app.alloc);
errdefer config.deinit();
// Update the existing config, be sure to clean up the old one.
self.config.deinit();
self.config = config;
return &self.config;
}
/// Create a new window for the app. /// Create a new window for the app.
pub fn newWindow(self: *App, parent_: ?*CoreSurface) !void { pub fn newWindow(self: *App, parent_: ?*CoreSurface) !void {
_ = try self.newSurface(parent_); _ = try self.newSurface(parent_);

View File

@ -137,6 +137,23 @@ pub const App = struct {
glfw.terminate(); glfw.terminate();
} }
/// Reload the configuration. This should return the new configuration.
/// The old value can be freed immediately at this point assuming a
/// successful return.
///
/// The returned pointer value is only valid for a stable self pointer.
pub fn reloadConfig(self: *App) !?*const Config {
// Load our configuration
var config = try Config.load(self.core_app.alloc);
errdefer config.deinit();
// Update the existing config, be sure to clean up the old one.
self.config.deinit();
self.config = config;
return &self.config;
}
pub fn wakeup(self: App) void { pub fn wakeup(self: App) void {
_ = self; _ = self;
c.g_main_context_wakeup(null); c.g_main_context_wakeup(null);