From fa8e49b4379f9c081b32c3c3d262c28935ffe060 Mon Sep 17 00:00:00 2001
From: sam <43527203+bytemunch@users.noreply.github.com>
Date: Tue, 24 Oct 2023 23:23:10 +0100
Subject: [PATCH] fix no focus causing failed rendering (#97)

---
 src/lib.rs    |  9 +++++++--
 src/render.rs | 15 +++++++++------
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 20c7fe4..63f126b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -507,12 +507,17 @@ fn cosmic_editor_builder(
     mut commands: Commands,
 ) {
     for (entity, metrics) in added_editors.iter_mut() {
-        let buffer = Buffer::new(
+        let mut buffer = Buffer::new(
             &mut font_system.0,
             Metrics::new(metrics.font_size, metrics.line_height).scale(metrics.scale_factor),
         );
         // buffer.set_wrap(&mut font_system.0, cosmic_text::Wrap::None);
-        let editor = Editor::new(buffer);
+        buffer.set_redraw(true);
+        let mut editor = Editor::new(buffer);
+
+        let mut cursor = editor.cursor();
+        cursor.color = Some(cosmic_text::Color::rgba(0, 0, 0, 0));
+        editor.set_cursor(cursor);
 
         commands.entity(entity).insert(CosmicEditor(editor));
         commands.entity(entity).insert(CosmicEditHistory::default());
diff --git a/src/render.rs b/src/render.rs
index e1ad1cd..90a92e3 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -429,14 +429,14 @@ pub(crate) fn hide_inactive_or_readonly_cursor(
         editor.0.buffer_mut().set_redraw(true);
     }
 
-    if active_editor.is_changed() || active_editor.0.is_none() {
-        return;
-    }
-
     for (e, mut editor, readonly_opt) in &mut cosmic_editor_q_placeholder.iter_mut() {
-        if e != active_editor.0.unwrap() || readonly_opt.is_some() {
+        // filthy short circuiting instead of correct unwrapping
+        if active_editor.is_none() || e != active_editor.0.unwrap() || readonly_opt.is_some() {
             let editor = &mut editor.0;
             let mut cursor = editor.0.cursor();
+            if cursor.color == Some(cosmic_text::Color::rgba(0, 0, 0, 0)) {
+                return;
+            }
             cursor.color = Some(cosmic_text::Color::rgba(0, 0, 0, 0));
             editor.0.set_cursor(cursor);
             editor.0.buffer_mut().set_redraw(true);
@@ -444,8 +444,11 @@ pub(crate) fn hide_inactive_or_readonly_cursor(
     }
 
     for (e, mut editor) in &mut cosmic_editor_q_editable.iter_mut() {
-        if e != active_editor.0.unwrap() {
+        if active_editor.is_none() || e != active_editor.0.unwrap() {
             let mut cursor = editor.0.cursor();
+            if cursor.color == Some(cosmic_text::Color::rgba(0, 0, 0, 0)) {
+                return;
+            }
             cursor.color = Some(cosmic_text::Color::rgba(0, 0, 0, 0));
             editor.0.set_cursor(cursor);
             editor.0.buffer_mut().set_redraw(true);
-- 
GitLab