From 03b60ab2eff7df452e23320ebfd77d7ee39ef01d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 19 Nov 2024 16:11:54 -0800 Subject: [PATCH] apprt/gtk: support light/dark mode change on the fly --- src/apprt/gtk/App.zig | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index 8815ef39f..99148fd87 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -462,6 +462,7 @@ pub fn performAction( .equalize_splits => self.equalizeSplits(target), .goto_split => self.gotoSplit(target, value), .open_config => try configpkg.edit.open(self.core_app.alloc), + .config_change_conditional_state => self.configChangeConditionalState(target), .inspector => self.controlInspector(target, value), .desktop_notification => self.showDesktopNotification(target, value), .set_title => try self.setTitle(target, value), @@ -487,7 +488,6 @@ pub fn performAction( .render_inspector, .renderer_health, .color_change, - .config_change_conditional_state, => log.warn("unimplemented action={}", .{action}), } } @@ -817,6 +817,35 @@ fn showDesktopNotification( c.g_application_send_notification(g_app, n.body.ptr, notification); } +fn configChangeConditionalState( + self: *App, + 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.