From 258d51395c331d3999c7e4aac7e2ad00eeb47e92 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 2 Feb 2024 13:02:16 -0800 Subject: [PATCH] apprt/embedded: add API for reporting color scheme --- include/ghostty.h | 6 ++++++ src/Surface.zig | 2 +- src/apprt/embedded.zig | 20 ++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/include/ghostty.h b/include/ghostty.h index 2e5a01eef..db62fbc31 100644 --- a/include/ghostty.h +++ b/include/ghostty.h @@ -144,6 +144,11 @@ typedef enum { GHOSTTY_TAB_NEXT = -2, } ghostty_tab_e; +typedef enum { + GHOSTTY_COLOR_SCHEME_LIGHT = 0, + GHOSTTY_COLOR_SCHEME_DARK = 1, +} ghostty_color_scheme_e; + // This is a packed struct (see src/input/mouse.zig) but the C standard // afaik doesn't let us reliably define packed structs so we build it up // from scratch. @@ -475,6 +480,7 @@ void ghostty_surface_refresh(ghostty_surface_t); void ghostty_surface_set_content_scale(ghostty_surface_t, double, double); void ghostty_surface_set_focus(ghostty_surface_t, bool); void ghostty_surface_set_size(ghostty_surface_t, uint32_t, uint32_t); +void ghostty_surface_set_color_scheme(ghostty_surface_t, ghostty_color_scheme_e); ghostty_input_mods_e ghostty_surface_key_translation_mods(ghostty_surface_t, ghostty_input_mods_e); void ghostty_surface_key(ghostty_surface_t, ghostty_input_key_s); void ghostty_surface_text(ghostty_surface_t, const char *, uintptr_t); diff --git a/src/Surface.zig b/src/Surface.zig index 1707289d0..23dd0064a 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -2803,7 +2803,7 @@ fn dragLeftClickBefore( /// Call to notify Ghostty that the color scheme for the terminal has /// changed. -pub fn colorSchemeCallback(self: *Surface, scheme: apprt.ColorScheme) void { +pub fn colorSchemeCallback(self: *Surface, scheme: apprt.ColorScheme) !void { self.color_scheme = scheme; } diff --git a/src/apprt/embedded.zig b/src/apprt/embedded.zig index 66681df58..26473646f 100644 --- a/src/apprt/embedded.zig +++ b/src/apprt/embedded.zig @@ -689,6 +689,13 @@ pub const Surface = struct { }; } + pub fn colorSchemeCallback(self: *Surface, scheme: apprt.ColorScheme) void { + self.core_surface.colorSchemeCallback(scheme) catch |err| { + log.err("error setting color scheme err={}", .{err}); + return; + }; + } + pub fn mouseButtonCallback( self: *Surface, action: input.MouseButtonState, @@ -1516,6 +1523,19 @@ pub const CAPI = struct { surface.updateSize(w, h); } + /// Update the color scheme of the surface. + export fn ghostty_surface_set_color_scheme(surface: *Surface, scheme_raw: c_int) void { + const scheme = std.meta.intToEnum(apprt.ColorScheme, scheme_raw) catch { + log.warn( + "invalid color scheme to ghostty_surface_set_color_scheme value={}", + .{scheme_raw}, + ); + return; + }; + + surface.colorSchemeCallback(scheme); + } + /// Update the content scale of the surface. export fn ghostty_surface_set_content_scale(surface: *Surface, x: f64, y: f64) void { surface.updateContentScale(x, y);