Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
Hide read-only toggle on tabs that cannot be toggled (#66650)
Kirill Bulatovb5cd310b3b54
diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rsindex 060cf0665bd..c583333003f 100644--- a/crates/editor/src/items.rs+++ b/crates/editor/src/items.rs@@ -919,23 +919,8 @@ impl Item for Editor {self.capability(cx)}- // Note: this mirrors the logic in `Editor::toggle_read_only`, but is reachable- // without relying on focus-based action dispatch.- fn toggle_read_only(&mut self, window: &mut Window, cx: &mut Context<Self>) {- if let Some(buffer) = self.buffer.read(cx).as_singleton() {- buffer.update(cx, |buffer, cx| {- buffer.set_capability(- match buffer.capability() {- Capability::ReadWrite => Capability::Read,- Capability::Read => Capability::ReadWrite,- Capability::ReadOnly => Capability::ReadOnly,- },- cx,- );- });- }- cx.notify();- window.refresh();+ fn read_only_toggle_buffer(&self, cx: &App) -> Option<Entity<Buffer>> {+ self.buffer.read(cx).as_singleton()}fn has_deleted_file(&self, cx: &App) -> bool {@@ -2612,8 +2597,39 @@ mod tests {use serde_json::json;use std::path::{Path, PathBuf};use util::{path, paths::PathWithPosition, rel_path::RelPath};+ use workspace::item::ItemHandle as _;use workspace::path_link::{OpenTarget, OpenTargetFoundBy};+ #[gpui::test]+ fn test_read_only_toggle_requires_singleton_buffer(cx: &mut gpui::TestAppContext) {+ init_test(cx, |_| {});++ let buffer = cx.new(|cx| Buffer::local("text", cx));+ let (singleton_editor, cx) = cx.add_window_view({+ let buffer = buffer.clone();+ |window, cx| Editor::for_buffer(buffer, None, window, cx)+ });+ let multibuffer = cx.new(|_| MultiBuffer::new(Capability::ReadWrite));+ let multibuffer_editor = cx.new_window_entity(|window, cx| {+ Editor::for_multibuffer(multibuffer.clone(), None, window, cx)+ });++ cx.update(|window, cx| {+ assert_eq!(+ singleton_editor.read_only_toggle_buffer(cx).as_ref(),+ Some(&buffer)+ );+ singleton_editor.toggle_read_only(window, cx);+ assert_eq!(buffer.read(cx).capability(), Capability::Read);+ singleton_editor.toggle_read_only(window, cx);+ assert_eq!(buffer.read(cx).capability(), Capability::ReadWrite);++ assert_eq!(multibuffer_editor.read_only_toggle_buffer(cx), None);+ multibuffer_editor.toggle_read_only(window, cx);+ assert_eq!(multibuffer.read(cx).capability(), Capability::ReadWrite);+ });+ }+#[gpui::test]fn test_path_for_file(cx: &mut App) {let file: Arc<dyn language::File> = Arc::new(TestFile {diff --git a/crates/markdown_preview/src/markdown_preview_view.rs b/crates/markdown_preview/src/markdown_preview_view.rsindex 867abac25b4..5e9f0de1637 100644--- a/crates/markdown_preview/src/markdown_preview_view.rs+++ b/crates/markdown_preview/src/markdown_preview_view.rs@@ -3183,6 +3183,49 @@ mod tests {);}+ #[gpui::test]+ async fn preview_does_not_offer_read_only_toggle(cx: &mut TestAppContext) {+ let app_state = init_test(cx);+ app_state+ .fs+ .as_fake()+ .insert_tree(path!("/dir"), json!({ "notes.md": "# Notes\n" }))+ .await;++ cx.update(|cx| {+ open_paths(+ &[PathBuf::from(path!("/dir/notes.md"))],+ app_state.clone(),+ workspace::OpenOptions::default(),+ cx,+ )+ })+ .await+ .unwrap();++ let multi_workspace = cx.update(|cx| cx.windows()[0].downcast::<MultiWorkspace>().unwrap());+ multi_workspace+ .update(cx, |multi_workspace, window, cx| {+ let workspace = multi_workspace.workspace().clone();+ let editor = workspace+ .read(cx)+ .active_item(cx)+ .and_then(|item| item.act_as::<Editor>(cx))+ .unwrap();+ let preview = workspace.update(cx, |workspace, cx| {+ MarkdownPreviewView::create_markdown_view(workspace, editor.clone(), window, cx)+ });++ assert_eq!(preview.read_only_toggle_buffer(cx), None);+ preview.toggle_read_only(window, cx);+ assert_eq!(+ editor.read(cx).capability(cx),+ language::Capability::ReadWrite+ );+ })+ .unwrap();+ }+#[gpui::test]async fn toggles_task_checkbox_and_saves_when_preview_is_active(cx: &mut TestAppContext) {let app_state = init_test(cx);diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rsindex ab4195f6a4e..a1785f01106 100644--- a/crates/workspace/src/item.rs+++ b/crates/workspace/src/item.rs@@ -15,8 +15,8 @@ use gpui::{EventEmitter, FocusHandle, Focusable, Font, Pixels, Point, Render, SharedString, Task, TaskExt,WeakEntity, Window,};-use language::Capability;pub use language::HighlightedText;+use language::{Buffer, Capability};use project::{Project, ProjectEntryId, ProjectPath};pub use settings::{ActivateOnClose, ClosePosition, RegisterSetting, Settings, SettingsLocation, ShowCloseButton,@@ -283,7 +283,9 @@ pub trait Item: Focusable + EventEmitter<Self::Event> + Render + Sized {Capability::ReadWrite}- fn toggle_read_only(&mut self, _window: &mut Window, _cx: &mut Context<Self>) {}+ fn read_only_toggle_buffer(&self, _: &App) -> Option<Entity<Buffer>> {+ None+ }fn has_deleted_file(&self, _: &App) -> bool {false@@ -527,6 +529,7 @@ pub trait ItemHandle: 'static + Send {fn to_any_view(&self) -> AnyView;fn is_dirty(&self, cx: &App) -> bool;fn capability(&self, cx: &App) -> Capability;+ fn read_only_toggle_buffer(&self, cx: &App) -> Option<Entity<Buffer>>;fn toggle_read_only(&self, window: &mut Window, cx: &mut App);fn has_deleted_file(&self, cx: &App) -> bool;fn has_conflict(&self, cx: &App) -> bool;@@ -1043,10 +1046,25 @@ impl<T: Item> ItemHandle for Entity<T> {self.read(cx).capability(cx)}+ fn read_only_toggle_buffer(&self, cx: &App) -> Option<Entity<Buffer>> {+ self.read(cx).read_only_toggle_buffer(cx)+ }+fn toggle_read_only(&self, window: &mut Window, cx: &mut App) {- self.update(cx, |this, cx| {- this.toggle_read_only(window, cx);- })+ let Some(buffer) = self.read_only_toggle_buffer(cx) else {+ return;+ };+ buffer.update(cx, |buffer, cx| {+ buffer.set_capability(+ match buffer.capability() {+ Capability::ReadWrite => Capability::Read,+ Capability::Read => Capability::ReadWrite,+ Capability::ReadOnly => Capability::ReadOnly,+ },+ cx,+ );+ });+ window.refresh();}fn has_deleted_file(&self, cx: &App) -> bool {diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rsindex 509a8e8fd36..faf7cbeb468 100644--- a/crates/workspace/src/pane.rs+++ b/crates/workspace/src/pane.rs@@ -2939,6 +2939,7 @@ impl Pane {let has_file_icon = icon.is_some();let capability = item.capability(cx);+ let can_toggle_read_only = item.read_only_toggle_buffer(cx).is_some();let tab = Tab::new(ix).position(if is_first_item {TabPosition::First@@ -3090,7 +3091,7 @@ impl Pane {.gap_1().children(if let Some(icon) = icon {Some(icon)- } else if !capability.editable() {+ } else if can_toggle_read_only && !capability.editable() {Some(read_only_toggle(capability == Capability::Read).into_any_element())} else {None@@ -3112,9 +3113,10 @@ impl Pane {}None => this,})- .when(capability == Capability::Read && has_file_icon, |this| {- this.child(read_only_toggle(true))- }),+ .when(+ can_toggle_read_only && capability == Capability::Read && has_file_icon,+ |this| this.child(read_only_toggle(true)),+ ),);let single_entry_to_resolve = (self.items[ix].buffer_kind(cx) == ItemBufferKind::Singleton)@@ -3283,7 +3285,7 @@ impl Pane {})};- if capability != Capability::ReadOnly {+ if can_toggle_read_only && capability != Capability::ReadOnly {let read_only_label = if capability.editable() {"Make Tab Read-Only"} else {