apprt/gtk: update for new reload config API

This commit is contained in:
Mitchell Hashimoto
2024-11-22 11:32:19 -08:00
parent 0a794addf1
commit 266e4781fb
2 changed files with 38 additions and 47 deletions

View File

@ -462,7 +462,8 @@ pub fn performAction(
.equalize_splits => self.equalizeSplits(target), .equalize_splits => self.equalizeSplits(target),
.goto_split => self.gotoSplit(target, value), .goto_split => self.gotoSplit(target, value),
.open_config => try configpkg.edit.open(self.core_app.alloc), .open_config => try configpkg.edit.open(self.core_app.alloc),
.config_change_conditional_state => self.configChangeConditionalState(target), .config_change => self.configChange(value.config),
.reload_config => try self.reloadConfig(target, value),
.inspector => self.controlInspector(target, value), .inspector => self.controlInspector(target, value),
.desktop_notification => self.showDesktopNotification(target, value), .desktop_notification => self.showDesktopNotification(target, value),
.set_title => try self.setTitle(target, value), .set_title => try self.setTitle(target, value),
@ -489,6 +490,7 @@ pub fn performAction(
.renderer_health, .renderer_health,
.color_change, .color_change,
.config_change, .config_change,
.config_change_conditional_state,
=> log.warn("unimplemented action={}", .{action}), => log.warn("unimplemented action={}", .{action}),
} }
} }
@ -818,48 +820,9 @@ fn showDesktopNotification(
c.g_application_send_notification(g_app, n.body.ptr, notification); c.g_application_send_notification(g_app, n.body.ptr, notification);
} }
fn configChangeConditionalState( fn configChange(self: *App, new_config: *const Config) void {
self: *App, _ = new_config;
target: apprt.Target,
) void {
const surface: *CoreSurface = switch (target) {
.app => return,
.surface => |v| v,
};
// Build our new configuration. We can free the memory
// immediately after because the surface will derive any
// values it needs to.
var new_config = self.config.changeConditionalState(
surface.config_conditional_state,
) catch |err| {
// Not a big deal if we error... we just don't update
// the config. We log the error and move on.
log.warn("error changing config conditional state err={}", .{err});
return;
};
defer new_config.deinit();
// Update our surface.
surface.updateConfig(&new_config) catch |err| {
log.warn("error updating surface config for state change err={}", .{err});
return;
};
}
/// 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;
self.syncConfigChanges() catch |err| { self.syncConfigChanges() catch |err| {
log.warn("error handling configuration changes err={}", .{err}); log.warn("error handling configuration changes err={}", .{err});
}; };
@ -870,8 +833,36 @@ pub fn reloadConfig(self: *App) !?*const Config {
if (surface.container.window()) |window| window.onConfigReloaded(); if (surface.container.window()) |window| window.onConfigReloaded();
} }
} }
}
return &self.config; fn reloadConfig(
self: *App,
target: apprt.action.Target,
opts: apprt.action.ReloadConfig,
) !void {
if (opts.soft) {
switch (target) {
.app => try self.core_app.updateConfig(self, &self.config),
.surface => |core_surface| try core_surface.updateConfig(
&self.config,
),
}
return;
}
// Load our configuration
var config = try Config.load(self.core_app.alloc);
errdefer config.deinit();
// Call into our app to update
switch (target) {
.app => try self.core_app.updateConfig(self, &config),
.surface => |core_surface| try core_surface.updateConfig(&config),
}
// Update the existing config, be sure to clean up the old one.
self.config.deinit();
self.config = config;
} }
/// Call this anytime the configuration changes. /// Call this anytime the configuration changes.
@ -1439,9 +1430,9 @@ fn gtkActionReloadConfig(
ud: ?*anyopaque, ud: ?*anyopaque,
) callconv(.C) void { ) callconv(.C) void {
const self: *App = @ptrCast(@alignCast(ud orelse return)); const self: *App = @ptrCast(@alignCast(ud orelse return));
_ = self.core_app.mailbox.push(.{ self.reloadConfig(.app, .{}) catch |err| {
.reload_config = {}, log.err("error reloading configuration: {s}", .{err});
}, .{ .forever = {} }); };
} }
fn gtkActionQuit( fn gtkActionQuit(

View File

@ -203,7 +203,7 @@ const ButtonsView = struct {
fn gtkReloadClick(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void { fn gtkReloadClick(_: *c.GtkWidget, ud: ?*anyopaque) callconv(.C) void {
const self: *ConfigErrors = @ptrCast(@alignCast(ud)); const self: *ConfigErrors = @ptrCast(@alignCast(ud));
_ = self.app.reloadConfig() catch |err| { _ = self.app.reloadConfig(.app, .{}) catch |err| {
log.warn("error reloading config error={}", .{err}); log.warn("error reloading config error={}", .{err});
return; return;
}; };