Add settings to hide/show navigation history buttons (#10240)

Mike Sun created

This is another variant for this [original
PR](https://github.com/zed-industries/zed/pull/10091) to add settings to
show/hide navigation history buttons that puts the settings under a new
section called `tab_bar`:

```
  "tab_bar": {
    // Whether or not to show the navigation history buttons.
    "show_nav_history_buttons": true
  }
```

<img width="314" alt="Screenshot 2024-04-02 at 3 00 53 PM"
src="https://github.com/zed-industries/zed/assets/1253505/23c4fa19-5a63-4160-b3b7-1b5e976c36bf">
<img width="329" alt="Screenshot 2024-04-02 at 3 01 03 PM"
src="https://github.com/zed-industries/zed/assets/1253505/64c2ebd2-9311-4589-a4e8-bd149c6c4ece">

Change summary

assets/settings/default.json               |  5 ++++
crates/workspace/src/pane.rs               | 12 ++++++++--
crates/workspace/src/workspace.rs          |  3 +
crates/workspace/src/workspace_settings.rs | 27 ++++++++++++++++++++++++
4 files changed, 43 insertions(+), 4 deletions(-)

Detailed changes

assets/settings/default.json 🔗

@@ -284,6 +284,11 @@
   // 4. Save when idle for a certain amount of time:
   //     "autosave": { "after_delay": {"milliseconds": 500} },
   "autosave": "off",
+  // Settings related to the editor's tab bar.
+  "tab_bar": {
+    // Whether or not to show the navigation history buttons.
+    "show_nav_history_buttons": true
+  },
   // Settings related to the editor's tabs
   "tabs": {
     // Show git status colors in the editor tabs.

crates/workspace/src/pane.rs 🔗

@@ -1,7 +1,7 @@
 use crate::{
     item::{ClosePosition, Item, ItemHandle, ItemSettings, WeakItemHandle},
     toolbar::Toolbar,
-    workspace_settings::{AutosaveSetting, WorkspaceSettings},
+    workspace_settings::{AutosaveSetting, TabBarSettings, WorkspaceSettings},
     NewCenterTerminal, NewFile, NewSearch, OpenVisible, SplitDirection, ToggleZoom, Workspace,
 };
 use anyhow::Result;
@@ -17,7 +17,7 @@ use gpui::{
 use parking_lot::Mutex;
 use project::{Project, ProjectEntryId, ProjectPath};
 use serde::Deserialize;
-use settings::Settings;
+use settings::{Settings, SettingsStore};
 use std::{
     any::Any,
     cmp, fmt, mem,
@@ -256,6 +256,7 @@ impl Pane {
             cx.on_focus(&focus_handle, Pane::focus_in),
             cx.on_focus_in(&focus_handle, Pane::focus_in),
             cx.on_focus_out(&focus_handle, Pane::focus_out),
+            cx.observe_global::<SettingsStore>(Self::settings_changed),
         ];
 
         let handle = cx.view().downgrade();
@@ -350,7 +351,7 @@ impl Pane {
                     })
                     .into_any_element()
             }),
-            display_nav_history_buttons: true,
+            display_nav_history_buttons: TabBarSettings::get_global(cx).show_nav_history_buttons,
             _subscriptions: subscriptions,
             double_click_dispatch_action,
         }
@@ -418,6 +419,11 @@ impl Pane {
         cx.notify();
     }
 
+    fn settings_changed(&mut self, cx: &mut ViewContext<Self>) {
+        self.display_nav_history_buttons = TabBarSettings::get_global(cx).show_nav_history_buttons;
+        cx.notify();
+    }
+
     pub fn active_item_index(&self) -> usize {
         self.active_item_index
     }

crates/workspace/src/workspace.rs 🔗

@@ -81,7 +81,7 @@ use ui::{
 };
 use util::ResultExt;
 use uuid::Uuid;
-pub use workspace_settings::{AutosaveSetting, WorkspaceSettings};
+pub use workspace_settings::{AutosaveSetting, TabBarSettings, WorkspaceSettings};
 
 use crate::persistence::{
     model::{DockData, DockStructure, SerializedItem, SerializedPane, SerializedPaneGroup},
@@ -260,6 +260,7 @@ impl Column for WorkspaceId {
 pub fn init_settings(cx: &mut AppContext) {
     WorkspaceSettings::register(cx);
     ItemSettings::register(cx);
+    TabBarSettings::register(cx);
 }
 
 pub fn init(app_state: Arc<AppState>, cx: &mut AppContext) {

crates/workspace/src/workspace_settings.rs 🔗

@@ -32,6 +32,19 @@ pub struct WorkspaceSettingsContent {
     pub autosave: Option<AutosaveSetting>,
 }
 
+#[derive(Deserialize)]
+pub struct TabBarSettings {
+    pub show_nav_history_buttons: bool,
+}
+
+#[derive(Clone, Default, Serialize, Deserialize, JsonSchema)]
+pub struct TabBarSettingsContent {
+    /// Whether or not to show the navigation history buttons in the tab bar.
+    ///
+    /// Default: true
+    pub show_nav_history_buttons: Option<bool>,
+}
+
 #[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema)]
 #[serde(rename_all = "snake_case")]
 pub enum AutosaveSetting {
@@ -58,3 +71,17 @@ impl Settings for WorkspaceSettings {
         Self::load_via_json_merge(default_value, user_values)
     }
 }
+
+impl Settings for TabBarSettings {
+    const KEY: Option<&'static str> = Some("tab_bar");
+
+    type FileContent = TabBarSettingsContent;
+
+    fn load(
+        default_value: &Self::FileContent,
+        user_values: &[&Self::FileContent],
+        _: &mut gpui::AppContext,
+    ) -> anyhow::Result<Self> {
+        Self::load_via_json_merge(default_value, user_values)
+    }
+}