svg_preview: Ensure preview properly updates in follow mode (#38081)

This fixes an issue where we would not update neither the path nor the
editor that was listened to during follow mode, which in turn would
cause the preview to become stale.

Fix here is to update the subscription whenever the active item changes
and also update the associated path accordingly.


Release Notes:

- Fixed an issue where the SVG preview would not update when following
the active editor.
This commit is contained in:
Finn Evers
2025-09-12 18:08:21 +00:00
committed by GitHub
parent 2b3ca360c3
commit 2143c59fba
3 changed files with 56 additions and 118 deletions
Generated
+1 -2
View File
@@ -15922,11 +15922,10 @@ dependencies = [
"editor", "editor",
"file_icons", "file_icons",
"gpui", "gpui",
"project", "multi_buffer",
"ui", "ui",
"workspace", "workspace",
"workspace-hack", "workspace-hack",
"worktree",
] ]
[[package]] [[package]]
+1 -2
View File
@@ -15,8 +15,7 @@ path = "src/svg_preview.rs"
editor.workspace = true editor.workspace = true
file_icons.workspace = true file_icons.workspace = true
gpui.workspace = true gpui.workspace = true
project.workspace = true multi_buffer.workspace = true
ui.workspace = true ui.workspace = true
workspace.workspace = true workspace.workspace = true
worktree.workspace = true
workspace-hack.workspace = true workspace-hack.workspace = true
+54 -114
View File
@@ -1,29 +1,25 @@
use std::path::PathBuf; use std::path::PathBuf;
use editor::{Editor, EditorEvent}; use editor::Editor;
use file_icons::FileIcons; use file_icons::FileIcons;
use gpui::{ use gpui::{
App, Context, Entity, EventEmitter, FocusHandle, Focusable, ImageSource, IntoElement, App, Context, Entity, EventEmitter, FocusHandle, Focusable, ImageSource, IntoElement,
ParentElement, Render, Resource, RetainAllImageCache, Styled, Subscription, WeakEntity, Window, ParentElement, Render, Resource, RetainAllImageCache, Styled, Subscription, WeakEntity, Window,
div, img, div, img,
}; };
use project::ProjectPath; use multi_buffer::{Event as MultiBufferEvent, MultiBuffer};
use ui::prelude::*; use ui::prelude::*;
use workspace::item::Item; use workspace::item::Item;
use workspace::{Pane, Workspace}; use workspace::{Pane, Workspace};
use worktree::Event as WorktreeEvent;
use crate::{OpenFollowingPreview, OpenPreview, OpenPreviewToTheSide}; use crate::{OpenFollowingPreview, OpenPreview, OpenPreviewToTheSide};
pub struct SvgPreviewView { pub struct SvgPreviewView {
focus_handle: FocusHandle, focus_handle: FocusHandle,
svg_path: Option<PathBuf>, svg_path: Option<PathBuf>,
project_path: Option<ProjectPath>,
image_cache: Entity<RetainAllImageCache>, image_cache: Entity<RetainAllImageCache>,
workspace_handle: WeakEntity<Workspace>, _buffer_subscription: Subscription,
_editor_subscription: Subscription,
_workspace_subscription: Option<Subscription>, _workspace_subscription: Option<Subscription>,
_project_subscription: Option<Subscription>,
} }
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
@@ -114,7 +110,7 @@ impl SvgPreviewView {
editor: &Entity<Editor>, editor: &Entity<Editor>,
cx: &App, cx: &App,
) -> Option<usize> { ) -> Option<usize> {
let editor_path = Self::get_svg_path(editor, cx); let editor_path = Self::get_svg_path(editor.read(cx).buffer(), cx);
pane.items_of_type::<SvgPreviewView>() pane.items_of_type::<SvgPreviewView>()
.find(|view| { .find(|view| {
let view_read = view.read(cx); let view_read = view.read(cx);
@@ -155,26 +151,10 @@ impl SvgPreviewView {
cx: &mut Context<Workspace>, cx: &mut Context<Workspace>,
) -> Entity<Self> { ) -> Entity<Self> {
cx.new(|cx| { cx.new(|cx| {
let svg_path = Self::get_svg_path(&active_editor, cx);
let project_path = Self::get_project_path(&active_editor, cx);
let image_cache = RetainAllImageCache::new(cx); let image_cache = RetainAllImageCache::new(cx);
let buffer = active_editor.read(cx).buffer();
let subscription = cx.subscribe_in( let svg_path = Self::get_svg_path(buffer, cx);
&active_editor, let subscription = Self::create_buffer_subscription(&buffer.clone(), window, cx);
window,
|this: &mut SvgPreviewView, _editor, event: &EditorEvent, window, cx| {
if event == &EditorEvent::Saved {
// Remove cached image to force reload
if let Some(svg_path) = &this.svg_path {
let resource = Resource::Path(svg_path.clone().into());
this.image_cache.update(cx, |cache, cx| {
cache.remove(&resource, window, cx);
});
}
cx.notify();
}
},
);
// Subscribe to workspace active item changes to follow SVG files // Subscribe to workspace active item changes to follow SVG files
let workspace_subscription = if mode == SvgPreviewMode::Follow { let workspace_subscription = if mode == SvgPreviewMode::Follow {
@@ -185,17 +165,24 @@ impl SvgPreviewView {
|this: &mut SvgPreviewView, |this: &mut SvgPreviewView,
workspace, workspace,
event: &workspace::Event, event: &workspace::Event,
_window, window,
cx| { cx| {
if let workspace::Event::ActiveItemChanged = event { if let workspace::Event::ActiveItemChanged = event {
let workspace_read = workspace.read(cx); let workspace_read = workspace.read(cx);
if let Some(active_item) = workspace_read.active_item(cx) if let Some(active_item) = workspace_read.active_item(cx)
&& let Some(editor_entity) = active_item.downcast::<Editor>() && let Some(editor) = active_item.downcast::<Editor>()
&& Self::is_svg_file(&editor_entity, cx) && Self::is_svg_file(&editor, cx)
{ {
let new_path = Self::get_svg_path(&editor_entity, cx); let buffer = editor.read(cx).buffer();
let new_path = Self::get_svg_path(&buffer, cx);
if this.svg_path != new_path { if this.svg_path != new_path {
this.svg_path = new_path; this.svg_path = new_path;
this._buffer_subscription =
Self::create_buffer_subscription(
&buffer.clone(),
window,
cx,
);
cx.notify(); cx.notify();
} }
} }
@@ -207,74 +194,48 @@ impl SvgPreviewView {
None None
}; };
// We'll set up the project subscription after the entity is created Self {
let project_subscription = None;
let view = Self {
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
svg_path, svg_path,
project_path,
image_cache, image_cache,
workspace_handle, _buffer_subscription: subscription,
_editor_subscription: subscription,
_workspace_subscription: workspace_subscription, _workspace_subscription: workspace_subscription,
_project_subscription: project_subscription, }
};
view
}) })
} }
fn setup_project_subscription(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn create_buffer_subscription(
if let (Some(workspace), Some(project_path)) = active_buffer: &Entity<MultiBuffer>,
(self.workspace_handle.upgrade(), &self.project_path) window: &mut Window,
{ cx: &mut Context<Self>,
let project = workspace.read(cx).project().clone(); ) -> Subscription {
let worktree_id = project_path.worktree_id; cx.subscribe_in(
let worktree = { active_buffer,
let project_read = project.read(cx); window,
project_read |this: &mut SvgPreviewView, buffer, event: &MultiBufferEvent, window, cx| {
.worktrees(cx) let potential_path_change = event == &MultiBufferEvent::FileHandleChanged;
.find(|worktree| worktree.read(cx).id() == worktree_id) if event == &MultiBufferEvent::Saved || potential_path_change {
}; // Remove cached image to force reload
if let Some(svg_path) = &this.svg_path {
let resource = Resource::Path(svg_path.clone().into());
this.image_cache.update(cx, |cache, cx| {
cache.remove(&resource, window, cx);
});
}
if let Some(worktree) = worktree { if potential_path_change {
self._project_subscription = Some(cx.subscribe_in( this.svg_path = Self::get_svg_path(buffer, cx);
&worktree, }
window, cx.notify();
|this: &mut SvgPreviewView, _worktree, event: &WorktreeEvent, window, cx| { }
if let WorktreeEvent::UpdatedEntries(changes) = event { },
if let Some(project_path) = &this.project_path { )
// Check if our SVG file was modified
for (path, _entry_id, _change) in changes.iter() {
if path.as_ref() == project_path.path.as_ref() {
// File was modified externally, clear cache and refresh
if let Some(svg_path) = &this.svg_path {
let resource = Resource::Path(svg_path.clone().into());
this.image_cache.update(cx, |cache, cx| {
cache.remove(&resource, window, cx);
});
}
cx.notify();
break;
}
}
}
}
},
));
}
}
} }
pub fn is_svg_file<C>(editor: &Entity<Editor>, cx: &C) -> bool pub fn is_svg_file(editor: &Entity<Editor>, cx: &App) -> bool {
where let buffer = editor.read(cx).buffer().read(cx);
C: std::borrow::Borrow<App>,
{
let app = cx.borrow();
let buffer = editor.read(app).buffer().read(app);
if let Some(buffer) = buffer.as_singleton() if let Some(buffer) = buffer.as_singleton()
&& let Some(file) = buffer.read(app).file() && let Some(file) = buffer.read(cx).file()
{ {
return file return file
.path() .path()
@@ -286,37 +247,16 @@ impl SvgPreviewView {
false false
} }
fn get_svg_path<C>(editor: &Entity<Editor>, cx: &C) -> Option<PathBuf> fn get_svg_path(buffer: &Entity<MultiBuffer>, cx: &App) -> Option<PathBuf> {
where let buffer = buffer.read(cx).as_singleton()?;
C: std::borrow::Borrow<App>, let file = buffer.read(cx).file()?;
{
let app = cx.borrow();
let buffer = editor.read(app).buffer().read(app).as_singleton()?;
let file = buffer.read(app).file()?;
let local_file = file.as_local()?; let local_file = file.as_local()?;
Some(local_file.abs_path(app)) Some(local_file.abs_path(cx))
}
fn get_project_path<C>(editor: &Entity<Editor>, cx: &C) -> Option<ProjectPath>
where
C: std::borrow::Borrow<App>,
{
let app = cx.borrow();
let buffer = editor.read(app).buffer().read(app).as_singleton()?;
let file = buffer.read(app).file()?;
Some(ProjectPath {
worktree_id: file.worktree_id(app),
path: file.path().clone(),
})
} }
} }
impl Render for SvgPreviewView { impl Render for SvgPreviewView {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
// Set up project subscription on first render if not already done
if self._project_subscription.is_none() {
self.setup_project_subscription(window, cx);
}
v_flex() v_flex()
.id("SvgPreview") .id("SvgPreview")
.key_context("SvgPreview") .key_context("SvgPreview")