mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
Hide read-only toggle on tabs that cannot be toggled (#66650)
Closes https://github.com/zed-industries/zed/issues/65010 https://github.com/user-attachments/assets/9c41f46c-7328-4c54-b1a5-fd0560395523 IMO, `Item` interface should be more generic as maybe in the future we want to have non-singleton-buffers read-only also, but current impl clearly does not deal with anything else, so make it more obvious and fix the issue. Release Notes: - Fixed read-only toggle icon and action shown on item tabs that do not support it
This commit is contained in:
+33
-17
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user