summaryrefslogtreecommitdiff
path: root/src/App.zig
diff options
context:
space:
mode:
authorMitchell Hashimoto <m@mitchellh.com>2024-11-22 13:42:48 -0800
committerMitchell Hashimoto <m@mitchellh.com>2024-11-22 14:08:35 -0800
commitcd49015243b794cf51ca30e8d32f09d4d362925f (patch)
tree610a0ec55cfa6ff8afae452c68cf72c817e14300 /src/App.zig
parent958316d85172c12c00c8012f90c5406861b7a11e (diff)
App applies conditional state, supports theme setting
The prior light/dark mode awareness work works on surface-level APIs. As a result, configurations used at the app-level (such as split divider colors, inactive split opacity, etc.) are not aware of the current theme configurations and default to the "light" theme. This commit adds APIs to specify app-level color scheme changes. This changes the configuration for the app and sets the default conditional state to use that new theme. This latter point makes it so that future surfaces use the correct theme on load rather than requiring some apprt event loop ticks. Some users have already reported a short "flicker" to load the correct theme, so this should help alleviate that.
Diffstat (limited to 'src/App.zig')
-rw-r--r--src/App.zig48
1 files changed, 47 insertions, 1 deletions
diff --git a/src/App.zig b/src/App.zig
index 57b30ada0..279c4e497 100644
--- a/src/App.zig
+++ b/src/App.zig
@@ -67,6 +67,11 @@ font_grid_set: font.SharedGridSet,
last_notification_time: ?std.time.Instant = null,
last_notification_digest: u64 = 0,
+/// The conditional state of the configuration. See the equivalent field
+/// in the Surface struct for more information. In this case, this applies
+/// to the app-level config and as a default for new surfaces.
+config_conditional_state: configpkg.ConditionalState,
+
/// Set to false once we've created at least one surface. This
/// never goes true again. This can be used by surfaces to determine
/// if they are the first surface.
@@ -95,6 +100,7 @@ pub fn create(
.mailbox = .{},
.quit = false,
.font_grid_set = font_grid_set,
+ .config_conditional_state = .{},
};
errdefer app.surfaces.deinit(alloc);
@@ -154,11 +160,24 @@ pub fn updateConfig(self: *App, rt_app: *apprt.App, config: *const Config) !void
try surface.core_surface.handleMessage(.{ .change_config = config });
}
+ // Apply our conditional state. If we fail to apply the conditional state
+ // then we log and attempt to move forward with the old config.
+ // We only apply this to the app-level config because the surface
+ // config applies its own conditional state.
+ var applied_: ?configpkg.Config = config.changeConditionalState(
+ self.config_conditional_state,
+ ) catch |err| err: {
+ log.warn("failed to apply conditional state to config err={}", .{err});
+ break :err null;
+ };
+ defer if (applied_) |*c| c.deinit();
+ const applied: *const configpkg.Config = if (applied_) |*c| c else config;
+
// Notify the apprt that the app has changed configuration.
try rt_app.performAction(
.app,
.config_change,
- .{ .config = config },
+ .{ .config = applied },
);
}
@@ -380,6 +399,33 @@ pub fn keyEvent(
return true;
}
+/// Call to notify Ghostty that the color scheme for the app has changed.
+/// "Color scheme" in this case refers to system themes such as "light/dark".
+pub fn colorSchemeEvent(
+ self: *App,
+ rt_app: *apprt.App,
+ scheme: apprt.ColorScheme,
+) !void {
+ const new_scheme: configpkg.ConditionalState.Theme = switch (scheme) {
+ .light => .light,
+ .dark => .dark,
+ };
+
+ // If our scheme didn't change, then we don't do anything.
+ if (self.config_conditional_state.theme == new_scheme) return;
+
+ // Setup our conditional state which has the current color theme.
+ self.config_conditional_state.theme = new_scheme;
+
+ // Request our configuration be reloaded because the new scheme may
+ // impact the colors of the app.
+ try rt_app.performAction(
+ .app,
+ .reload_config,
+ .{ .soft = true },
+ );
+}
+
/// Perform a binding action. This only accepts actions that are scoped
/// to the app. Callers can use performAllAction to perform any action
/// and any non-app-scoped actions will be performed on all surfaces.