From e865a89ede19ada0c7cfc962caa970c91e6e5db8 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 17 Dec 2024 21:22:49 +0100 Subject: [PATCH] feat: render secondary/unfocused cursors with secondary cursor theme style --- build.zig.zon | 4 +-- src/tui/editor.zig | 65 ++++++++++++++++++++++++---------------------- src/tui/tui.zig | 4 +-- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 3b98af6..2522a8b 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -22,8 +22,8 @@ .hash = "1220f6fdc977fff899aaf624afc8cf01e29a0e100dbb52860902a3bc256f4ddd687b", }, .themes = .{ - .url = "https://github.com/neurocyte/flow-themes/releases/download/master-5f1ca2fd3c784d430306a5cd1df237681a196333/flow-themes.tar.gz", - .hash = "122095b1a6110b920571c7e49e61c124cd9a164fe9b1b0faa1bd11d04d89822d3304", + .url = "https://github.com/neurocyte/flow-themes/releases/download/master-0c2a187c604c63031225847a966b6ca279b2be91/flow-themes.tar.gz", + .hash = "1220b2109e0aadf85e4ac5e1cd084e321fe50f1e59cea690c022a7a8f7eb6021eadb", }, .fuzzig = .{ .url = "https://github.com/fjebaker/fuzzig/archive/0fd156d5097365151e85a85eef9d8cf0eebe7b00.tar.gz", diff --git a/src/tui/editor.zig b/src/tui/editor.zig index dd4b919..e6738b9 100644 --- a/src/tui/editor.zig +++ b/src/tui/editor.zig @@ -842,40 +842,50 @@ pub const Editor = struct { self.render_cursors(theme) catch {}; } - fn render_terminal_cursor(self: *const Self, cursor_: *const Cursor) !void { - const tui_ = tui.current(); - if (tui_.is_mini_or_overlay_enabled()) - return; - if (self.screen_cursor(cursor_)) |cursor| { - const y, const x = self.plane.rel_yx_to_abs(@intCast(cursor.row), @intCast(cursor.col)); - const shape = if (tui_.input_mode) |mode| - mode.cursor_shape - else - .block; - tui_.rdr.cursor_enable(y, x, tui.translate_cursor_shape(shape)) catch {}; - } else { - tui_.rdr.cursor_disable(); - } - } - fn render_cursors(self: *Self, theme: *const Widget.Theme) !void { const frame = tracy.initZone(@src(), .{ .name = "editor render cursors" }); defer frame.deinit(); - if (self.cursels.items.len == 1 and self.enable_terminal_cursor) - return self.render_terminal_cursor(&self.get_primary().cursor); - for (self.cursels.items) |*cursel_| if (cursel_.*) |*cursel| - try self.render_cursor(&cursel.cursor, theme); - if (self.enable_terminal_cursor) - try self.render_terminal_cursor(&self.get_primary().cursor); + for (self.cursels.items[0 .. self.cursels.items.len - 1]) |*cursel_| if (cursel_.*) |*cursel| + try self.render_cursor_secondary(&cursel.cursor, theme); + try self.render_cursor_primary(&self.get_primary().cursor, theme); } - fn render_cursor(self: *Self, cursor: *const Cursor, theme: *const Widget.Theme) !void { + fn render_cursor_primary(self: *Self, cursor: *const Cursor, theme: *const Widget.Theme) !void { + const tui_ = tui.current(); + if (!tui_.is_mainview_focused() or !self.enable_terminal_cursor) { + if (self.screen_cursor(cursor)) |pos| { + self.plane.cursor_move_yx(@intCast(pos.row), @intCast(pos.col)) catch return; + const style = if (tui_.is_mainview_focused()) theme.editor_cursor else theme.editor_cursor_secondary; + self.render_cursor_cell(style); + } + } else { + if (self.screen_cursor(cursor)) |pos| { + const y, const x = self.plane.rel_yx_to_abs(@intCast(pos.row), @intCast(pos.col)); + const shape = if (tui_.input_mode) |mode| + mode.cursor_shape + else + .block; + tui_.rdr.cursor_enable(y, x, tui.translate_cursor_shape(shape)) catch {}; + } else { + tui_.rdr.cursor_disable(); + } + } + } + + fn render_cursor_secondary(self: *Self, cursor: *const Cursor, theme: *const Widget.Theme) !void { if (self.screen_cursor(cursor)) |pos| { self.plane.cursor_move_yx(@intCast(pos.row), @intCast(pos.col)) catch return; - self.render_cursor_cell(theme); + self.render_cursor_cell(theme.editor_cursor_secondary); } } + inline fn render_cursor_cell(self: *Self, style: Widget.Theme.Style) void { + var cell = self.plane.cell_init(); + _ = self.plane.at_cursor_cell(&cell) catch return; + cell.set_style(style); + _ = self.plane.putc(&cell) catch {}; + } + fn render_line_highlight(self: *Self, cursor: *const Cursor, theme: *const Widget.Theme) !void { const row_min = self.view.row; const row_max = row_min + self.view.rows; @@ -982,13 +992,6 @@ pub const Editor = struct { _ = self.plane.putc(&cell) catch {}; } - inline fn render_cursor_cell(self: *Self, theme: *const Widget.Theme) void { - var cell = self.plane.cell_init(); - _ = self.plane.at_cursor_cell(&cell) catch return; - cell.set_style(theme.editor_cursor); - _ = self.plane.putc(&cell) catch {}; - } - inline fn render_selection_cell(_: *const Self, theme: *const Widget.Theme, cell: *Cell) void { cell.set_style_bg_opaque(theme.editor); cell.set_style_bg(theme.editor_selection); diff --git a/src/tui/tui.zig b/src/tui/tui.zig index eea4951..8d79ad4 100644 --- a/src/tui/tui.zig +++ b/src/tui/tui.zig @@ -585,8 +585,8 @@ pub fn save_config(self: *const Self) !void { try root.write_config(self.config, self.allocator); } -pub fn is_mini_or_overlay_enabled(self: *const Self) bool { - return !(self.mini_mode == null or self.input_mode_outer == null); +pub fn is_mainview_focused(self: *const Self) bool { + return self.mini_mode == null and self.input_mode_outer == null; } fn enter_overlay_mode(self: *Self, mode: type) command.Result {