feat: Make the encoding indicator appear only when an editor is open.

R Aadarsh created

feat: Enable the user to choose whether or not the encoding indicator should be displayed by enabling or disabling `encoding_indicator` in `settings.json`

Change summary

Cargo.lock                   |  1 +
assets/settings/default.json |  4 +++-
crates/encodings/Cargo.toml  | 13 +++++++------
crates/encodings/src/lib.rs  | 13 ++++++++++++-
docs/src/configuring-zed.md  |  3 ++-
5 files changed, 25 insertions(+), 9 deletions(-)

Detailed changes

Cargo.lock 🔗

@@ -5598,6 +5598,7 @@ dependencies = [
  "gpui",
  "language",
  "picker",
+ "settings",
  "ui",
  "util",
  "workspace",

assets/settings/default.json 🔗

@@ -1352,7 +1352,9 @@
     // Whether to show the cursor position button in the status bar.
     "cursor_position_button": true,
     // Whether to show active line endings button in the status bar.
-    "line_endings_button": false
+    "line_endings_button": false,
+    // Whether to show the encoding indicator in the status bar.
+    "encoding_indicator": true
   },
   // Settings specific to the terminal
   "terminal": {

crates/encodings/Cargo.toml 🔗

@@ -6,15 +6,16 @@ edition.workspace = true
 
 [dependencies]
 anyhow.workspace = true
-ui.workspace = true
-workspace.workspace = true
-gpui.workspace = true
-picker.workspace = true
-util.workspace = true
-fuzzy.workspace = true
 editor.workspace = true
 encoding.workspace = true
+fuzzy.workspace = true
+gpui.workspace = true
 language.workspace = true
+picker.workspace = true
+settings.workspace = true
+ui.workspace = true
+util.workspace = true
+workspace.workspace = true
 
 [lints]
 workspace = true

crates/encodings/src/lib.rs 🔗

@@ -1,5 +1,5 @@
 ///! A crate for handling file encodings in the text editor.
-use editor::Editor;
+use editor::{Editor, EditorSettings};
 use encoding::Encoding;
 use encoding::all::{
     BIG5_2003, EUC_JP, GB18030, GBK, HZ, IBM866, ISO_2022_JP, ISO_8859_1, ISO_8859_2, ISO_8859_3,
@@ -9,6 +9,7 @@ use encoding::all::{
     WINDOWS_1253, WINDOWS_1254, WINDOWS_1255, WINDOWS_1256, WINDOWS_1257, WINDOWS_1258,
 };
 use gpui::{ClickEvent, Entity, Subscription, WeakEntity};
+use settings::Settings;
 use ui::{Button, ButtonCommon, Context, LabelSize, Render, Tooltip, Window, div};
 use ui::{Clickable, ParentElement};
 use workspace::{ItemHandle, StatusItemView, Workspace};
@@ -20,6 +21,7 @@ pub struct EncodingIndicator {
     pub encoding: Option<&'static dyn Encoding>,
     pub workspace: WeakEntity<Workspace>,
     observe: Option<Subscription>, // Subscription to observe changes in the active editor
+    show: bool,
 }
 
 pub mod selectors;
@@ -28,6 +30,12 @@ impl Render for EncodingIndicator {
     fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl ui::IntoElement {
         let status_element = div();
 
+        if (EditorSettings::get_global(cx).status_bar.encoding_indicator == false)
+            || (self.show == false)
+        {
+            return status_element;
+        }
+
         status_element.child(
             Button::new("encoding", encoding_name(self.encoding.unwrap_or(UTF_8)))
                 .label_size(LabelSize::Small)
@@ -54,6 +62,7 @@ impl EncodingIndicator {
             encoding,
             workspace,
             observe,
+            show: true,
         }
     }
 
@@ -84,10 +93,12 @@ impl StatusItemView for EncodingIndicator {
             Some(editor) => {
                 self.observe = Some(cx.observe_in(&editor, window, Self::update));
                 self.update(editor, window, cx);
+                self.show = true;
             }
             None => {
                 self.encoding = None;
                 self.observe = None;
+                self.show = false;
             }
         }
     }

docs/src/configuring-zed.md 🔗

@@ -1541,7 +1541,8 @@ Positive `integer` value between 1 and 32. Values outside of this range will be
 "status_bar": {
   "active_language_button": true,
   "cursor_position_button": true,
-  "line_endings_button": false
+  "line_endings_button": false,
+  "encoding_indicator": true,
 },
 ```