diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index ec01cf31696f23d4bacc6b368292228c49c03303..b6837652b5071ca227d78a9f790951c570819a53 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -1,6 +1,6 @@ use crate::{ - Anchor, Autoscroll, BufferSerialization, Editor, EditorEvent, EditorSettings, ExcerptId, - ExcerptRange, FormatTarget, MultiBuffer, MultiBufferSnapshot, NavigationData, + Anchor, Autoscroll, BufferSerialization, Capability, Editor, EditorEvent, EditorSettings, + ExcerptId, ExcerptRange, FormatTarget, MultiBuffer, MultiBufferSnapshot, NavigationData, ReportEditorEvent, SearchWithinRange, SelectionEffects, ToPoint as _, display_map::HighlightKey, editor_settings::SeedQuerySetting, @@ -809,6 +809,25 @@ impl Item for Editor { self.read_only(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) { + 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 has_deleted_file(&self, cx: &App) -> bool { self.buffer().read(cx).read(cx).has_deleted_file() } diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index d07a361c05d23a83a96708891f42f7bce99dde5f..367cec0da617de612d76f559fe8bb589733b50e0 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -258,6 +258,9 @@ pub trait Item: Focusable + EventEmitter + Render + Sized { fn is_read_only(&self, _: &App) -> bool { false } + + fn toggle_read_only(&mut self, _window: &mut Window, _cx: &mut Context) {} + fn has_deleted_file(&self, _: &App) -> bool { false } @@ -480,6 +483,7 @@ pub trait ItemHandle: 'static + Send { fn to_any_view(&self) -> AnyView; fn is_dirty(&self, cx: &App) -> bool; fn is_read_only(&self, cx: &App) -> bool; + 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; fn can_save(&self, cx: &App) -> bool; @@ -957,6 +961,12 @@ impl ItemHandle for Entity { self.read(cx).is_read_only(cx) } + fn toggle_read_only(&self, window: &mut Window, cx: &mut App) { + self.update(cx, |this, cx| { + this.toggle_read_only(window, cx); + }) + } + fn has_deleted_file(&self, cx: &App) -> bool { self.read(cx).has_deleted_file(cx) }