From 3e4a8644b5e43edc63be23d90e7741f29c874ff9 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 22 Jul 2024 16:22:53 -0500 Subject: [PATCH] renderer/opengl: implement cursor-invert-fg-bg --- src/config/Config.zig | 2 -- src/renderer/OpenGL.zig | 60 ++++++++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 1f2a2543f..3d13e9ead 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -291,8 +291,6 @@ palette: Palette = .{}, /// Swap the foreground and background colors of the cell under the cursor. This /// option overrides the `cursor-color` and `cursor-text` options. -/// -/// This is currently only supported on macOS. @"cursor-invert-fg-bg": bool = false, /// The opacity level (opposite of transparency) of the cursor. A value of 1 diff --git a/src/renderer/OpenGL.zig b/src/renderer/OpenGL.zig index 46acff1a9..e413928e9 100644 --- a/src/renderer/OpenGL.zig +++ b/src/renderer/OpenGL.zig @@ -95,6 +95,11 @@ background_color: terminal.color.RGB, /// by a terminal application cursor_color: ?terminal.color.RGB, +/// When `cursor_color` is null, swap the foreground and background colors of +/// the cell under the cursor for the cursor color. Otherwise, use the default +/// foreground color as the cursor color. +cursor_invert: bool, + /// Padding options padding: renderer.Options.Padding, @@ -235,6 +240,7 @@ pub const DerivedConfig = struct { font_features: std.ArrayListUnmanaged([:0]const u8), font_styles: font.CodepointResolver.StyleStatus, cursor_color: ?terminal.color.RGB, + cursor_invert: bool, cursor_text: ?terminal.color.RGB, cursor_opacity: f64, background: terminal.color.RGB, @@ -274,17 +280,21 @@ pub const DerivedConfig = struct { config.link.links.items, ); + const cursor_invert = config.@"cursor-invert-fg-bg"; + return .{ .background_opacity = @max(0, @min(1, config.@"background-opacity")), .font_thicken = config.@"font-thicken", .font_features = font_features, .font_styles = font_styles, - .cursor_color = if (config.@"cursor-color") |col| - col.toTerminalRGB() + .cursor_color = if (!cursor_invert and config.@"cursor-color" != null) + config.@"cursor-color".?.toTerminalRGB() else null, + .cursor_invert = cursor_invert, + .cursor_text = if (config.@"cursor-text") |txt| txt.toTerminalRGB() else @@ -354,6 +364,7 @@ pub fn init(alloc: Allocator, options: renderer.Options) !OpenGL { .foreground_color = options.config.foreground, .background_color = options.config.background, .cursor_color = options.config.cursor_color, + .cursor_invert = options.config.cursor_invert, .padding = options.padding, .surface_mailbox = options.surface_mailbox, .deferred_font_size = .{ .metrics = grid.metrics }, @@ -1117,20 +1128,30 @@ pub fn rebuildCells( break :cursor_style; } - _ = try self.addCursor(screen, cursor_style); + const cursor_color = self.cursor_color orelse color: { + if (self.cursor_invert) { + const sty = screen.cursor.page_pin.style(screen.cursor.page_cell); + break :color sty.fg(color_palette, self.config.bold_is_bright) orelse self.foreground_color; + } else { + break :color self.foreground_color; + } + }; + + _ = try self.addCursor(screen, cursor_style, cursor_color); if (cursor_cell) |*cell| { if (cell.mode == .fg or cell.mode == .fg_constrained) { - if (self.config.cursor_text) |txt| { - cell.r = txt.r; - cell.g = txt.g; - cell.b = txt.b; - cell.a = 255; - } else { - cell.r = self.background_color.r; - cell.g = self.background_color.g; - cell.b = self.background_color.b; - cell.a = 255; - } + const cell_color = if (self.cursor_invert) blk: { + const sty = screen.cursor.page_pin.style(screen.cursor.page_cell); + break :blk sty.bg(screen.cursor.page_cell, color_palette) orelse self.background_color; + } else if (self.config.cursor_text) |txt| + txt + else + self.background_color; + + cell.r = cell_color.r; + cell.g = cell_color.g; + cell.b = cell_color.b; + cell.a = 255; } try self.cells.append(self.alloc, cell.*); } @@ -1218,6 +1239,7 @@ fn addCursor( self: *OpenGL, screen: *terminal.Screen, cursor_style: renderer.CursorStyle, + cursor_color: terminal.color.RGB, ) !?*const CellProgram.Cell { // Add the cursor. We render the cursor over the wide character if // we're on the wide characer tail. @@ -1233,7 +1255,6 @@ fn addCursor( break :cell .{ prev_cell.wide == .wide, screen.cursor.x - 1 }; }; - const color = self.cursor_color orelse self.foreground_color; const alpha: u8 = if (!self.focused) 255 else alpha: { const alpha = 255 * self.config.cursor_opacity; break :alpha @intFromFloat(@ceil(alpha)); @@ -1264,9 +1285,9 @@ fn addCursor( .grid_col = @intCast(x), .grid_row = @intCast(screen.cursor.y), .grid_width = if (wide) 2 else 1, - .r = color.r, - .g = color.g, - .b = color.b, + .r = cursor_color.r, + .g = cursor_color.g, + .b = cursor_color.b, .a = alpha, .bg_r = 0, .bg_g = 0, @@ -1583,7 +1604,8 @@ pub fn changeConfig(self: *OpenGL, config: *DerivedConfig) !void { // Set our new colors self.background_color = config.background; self.foreground_color = config.foreground; - self.cursor_color = config.cursor_color; + self.cursor_invert = config.cursor_invert; + self.cursor_color = if (!config.cursor_invert) config.cursor_color else null; // Update our uniforms self.deferred_config = .{};