From 8924b3fb5b20774b3ec9d2b14036fb8bf8c698a1 Mon Sep 17 00:00:00 2001 From: thataboy Date: Tue, 15 Oct 2024 10:10:01 -0700 Subject: [PATCH] Fix block cursor obscuring placeholder text and editor text in some cases (#18114) --- crates/editor/src/element.rs | 39 ++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 89fa23ab1caacb2e63d81ed57a1fcf6ecc0604f7..8a2ec5cd0d527286d1fdd1065a988bb82ca769bb 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -64,7 +64,7 @@ use std::{ sync::Arc, }; use sum_tree::Bias; -use theme::{ActiveTheme, PlayerColor}; +use theme::{ActiveTheme, Appearance, PlayerColor}; use ui::prelude::*; use ui::{h_flex, ButtonLike, ButtonStyle, ContextMenu, Tooltip}; use util::RangeExt; @@ -1015,8 +1015,20 @@ impl EditorElement { block_width = em_width; } let block_text = if let CursorShape::Block = selection.cursor_shape { - snapshot.display_chars_at(cursor_position).next().and_then( - |(character, _)| { + snapshot + .display_chars_at(cursor_position) + .next() + .or_else(|| { + if cursor_column == 0 { + snapshot + .placeholder_text() + .and_then(|s| s.chars().next()) + .map(|c| (c, cursor_position)) + } else { + None + } + }) + .and_then(|(character, _)| { let text = if character == '\n' { SharedString::from(" ") } else { @@ -1031,6 +1043,22 @@ impl EditorElement { }) .unwrap_or(self.style.text.font()); + // Invert the text color for the block cursor. Ensure that the text + // color is opaque enough to be visible against the background color. + // + // 0.75 is an arbitrary threshold to determine if the background color is + // opaque enough to use as a text color. + // + // TODO: In the future we should ensure themes have a `text_inverse` color. + let color = if cx.theme().colors().editor_background.a < 0.75 { + match cx.theme().appearance { + Appearance::Dark => Hsla::black(), + Appearance::Light => Hsla::white(), + } + } else { + cx.theme().colors().editor_background + }; + cx.text_system() .shape_line( text, @@ -1038,15 +1066,14 @@ impl EditorElement { &[TextRun { len, font, - color: self.style.background, + color, background_color: None, strikethrough: None, underline: None, }], ) .log_err() - }, - ) + }) } else { None };