From 47e689a6043544e0dc942c3e06fc579a0a7f4dd4 Mon Sep 17 00:00:00 2001
From: sam edelsten <samedelsten1@gmail.com>
Date: Mon, 11 Sep 2023 09:19:22 +0100
Subject: [PATCH] show cursor only on active editor

---
 src/lib.rs | 62 ++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 48 insertions(+), 14 deletions(-)

diff --git a/src/lib.rs b/src/lib.rs
index 2d836b8..9c6e8c6 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -390,6 +390,7 @@ impl Plugin for CosmicEditPlugin {
                         .before(on_scale_factor_change),
                     cosmic_edit_redraw_buffer.before(on_scale_factor_change),
                     blink_cursor,
+                    hide_inactive_cursor,
                 ),
             )
             .init_resource::<ActiveEditor>()
@@ -1182,24 +1183,57 @@ fn blink_cursor(
     mut visible: Local<bool>,
     mut timer: Local<Option<Timer>>,
     time: Res<Time>,
+    active_editor: ResMut<ActiveEditor>,
     mut cosmic_editor_q: Query<(&mut CosmicEditor, &BackgroundColor), Without<ReadOnly>>,
 ) {
-    let timer = timer.get_or_insert_with(|| Timer::from_seconds(0.53, TimerMode::Repeating));
-    timer.tick(time.delta());
-    if !timer.just_finished() {
+    if let Some(e) = active_editor.entity {
+        match cosmic_editor_q.get_mut(e) {
+            Ok((mut editor, bg_color)) => {
+                let timer =
+                    timer.get_or_insert_with(|| Timer::from_seconds(0.53, TimerMode::Repeating));
+
+                timer.tick(time.delta());
+                if !timer.just_finished() && !active_editor.is_changed() {
+                    return;
+                }
+                *visible = !*visible;
+
+                // always start cursor visible on focus
+                if active_editor.is_changed() {
+                    *visible = true;
+                    timer.set_elapsed(Duration::from_secs(0));
+                }
+
+                let mut cursor = editor.0.cursor();
+                let new_color = if *visible {
+                    None
+                } else {
+                    Some(bevy_color_to_cosmic(bg_color.0))
+                };
+                cursor.color = new_color;
+                editor.0.set_cursor(cursor);
+                editor.0.buffer_mut().set_redraw(true);
+            }
+            Err(_) => {}
+        }
+    }
+}
+
+fn hide_inactive_cursor(
+    mut cosmic_editor_q: Query<(Entity, &mut CosmicEditor, &BackgroundColor)>,
+    active_editor: Res<ActiveEditor>,
+) {
+    if !active_editor.is_changed() || active_editor.entity.is_none() {
         return;
     }
-    *visible = !*visible;
-    for (mut editor, bg_color) in &mut cosmic_editor_q.iter_mut() {
-        let mut cursor = editor.0.cursor();
-        let new_color = if *visible {
-            None
-        } else {
-            Some(bevy_color_to_cosmic(bg_color.0))
-        };
-        cursor.color = new_color;
-        editor.0.set_cursor(cursor);
-        editor.0.buffer_mut().set_redraw(true);
+
+    for (e, mut editor, bg_color) in &mut cosmic_editor_q.iter_mut() {
+        if e != active_editor.entity.unwrap() {
+            let mut cursor = editor.0.cursor();
+            cursor.color = Some(bevy_color_to_cosmic(bg_color.0));
+            editor.0.set_cursor(cursor);
+            editor.0.buffer_mut().set_redraw(true);
+        }
     }
 }
 
-- 
GitLab