Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions apps/native/crates/app/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub struct AppState {
pub(crate) ssh_prompt_sequence: u64,
pub(crate) connections: t3_client::environments::Environments,
_pump: Option<Task<()>>,
#[cfg(feature = "snapshot")]
snapshot_frozen: bool,
pub(crate) notification_route: Option<t3_core::sidebar::tag_filter::ThreadKey>,
_ssh_quit: Option<Subscription>,
}
Expand Down Expand Up @@ -185,6 +187,8 @@ impl AppState {
ssh_prompt_sequence: 0,
connections,
_pump: Some(pump),
#[cfg(feature = "snapshot")]
snapshot_frozen: false,
notification_route: None,
_ssh_quit: None,
};
Expand Down Expand Up @@ -216,6 +220,12 @@ impl AppState {
self._pump = None;
}

/// Turn-item fixtures keep asset RPCs alive while holding their projected UI state.
#[cfg(feature = "snapshot")]
pub(crate) fn freeze_snapshot(&mut self) {
self.snapshot_frozen = true;
}

/// Drops the client and everything that belonged to its environment.
pub(crate) fn clear_connection_state(&mut self, cx: &mut Context<Self>) {
self.reset_environment_announcements();
Expand Down Expand Up @@ -252,6 +262,8 @@ impl AppState {
}

fn apply_environment_event(&mut self, event: t3_client::environments::EnvironmentEvent, cx: &mut Context<Self>) {
#[cfg(feature = "snapshot")]
if self.snapshot_frozen { return; }
use t3_client::environments::EnvironmentEvent;
if !self.connections.is_current(&event) { return; }
match event {
Expand Down
5 changes: 3 additions & 2 deletions apps/native/crates/app/src/files/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ impl From<Result<Decoded, String>> for ImageState {
}

/// Mints the URL and downloads the bytes on the network runtime.
pub fn load(state: &AppState, root: &Root, path: &str, kind: ImageKind) -> Option<tokio::task::JoinHandle<Result<Vec<u8>, String>>> {
pub fn load(state: &AppState, root: &Root, path: &str, kind: ImageKind, mutation: Option<&str>) -> Option<tokio::task::JoinHandle<Result<Vec<u8>, String>>> {
let client = state.client_handle()?;
let request = image_asset_request(&root.workspace.thread_id, &root.workspace.cwd, &absolute_path(path, &root.workspace.cwd));
let mutation = mutation.map(str::to_owned);
Some(state.runtime().spawn(async move {
if kind == ImageKind::Unsupported {
return Err(UNSUPPORTED.to_owned());
}
let value = client.request(CREATE_ASSET_URL, request).await.map_err(|_| UNSUPPORTED.to_owned())?;
let url = decode_asset_url(&value)?;
client.asset_bytes(&url, MAX_IMAGE_BYTES).await
client.asset_bytes(&format!("{url}{}", t3_core::media::workspace_image_suffix(&url, mutation.as_deref())), MAX_IMAGE_BYTES).await
}))
}

Expand Down
9 changes: 7 additions & 2 deletions apps/native/crates/app/src/files/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod explorer;
mod explorer_rows;
mod image;
mod preview;
#[cfg(feature = "snapshot")]
pub(crate) use preview::MarkdownFixture;

use std::cell::Cell;
use std::rc::Rc;
Expand Down Expand Up @@ -203,7 +205,7 @@ impl FilesPanel {
if root_changed {
self.root = root.clone();
self.stale = false;
} else if mutation != self.mutation && self.mutation.is_some() {
} else if t3_core::media::workspace_mutation_changed(root_changed, self.mutation.as_deref(), mutation.as_deref()) {
self.stale = true;
}
self.mutation = mutation;
Expand All @@ -220,7 +222,10 @@ impl FilesPanel {
reveal_request_id: shown.reveal_request_id,
})
});
self.preview.update(cx, |preview, cx| preview.set_target(target, cx));
self.preview.update(cx, |preview, cx| {
preview.set_mutation(self.mutation.clone(), cx);
preview.set_target(target, cx);
});
if std::mem::take(&mut self.stale) {
self.explorer.update(cx, |explorer, cx| explorer.refresh(cx));
self.preview.update(cx, |preview, cx| preview.refetch(cx));
Expand Down
63 changes: 58 additions & 5 deletions apps/native/crates/app/src/files/preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,20 @@ pub struct Preview {
render_markdown: bool,
markdown: Option<(Key, Entity<TextViewState>)>,
markdown_scroll: ScrollHandle,
mutation: Option<String>,
media_scope: Option<std::sync::Arc<std::sync::Mutex<crate::media::MediaScope>>>,
media_document: Option<crate::transcript::MediaDocument>,
media_extensions: Option<gpui_component::text::MarkdownExtensions>,
viewer: Entity<crate::media::MediaViewer>,
handled_reveal: Option<(String, u64)>,
/// A reveal shows the source; the rendered view returns once the user asks for it.
rendered_after_reveal: Option<(String, u64)>,
}

impl Preview {
pub fn new(state: Entity<AppState>, _cx: &mut Context<Self>) -> Self {
pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {
crate::media::cache(&state, cx);
let viewer = cx.new(crate::media::MediaViewer::new);
Preview {
state,
target: None,
Expand All @@ -109,6 +116,11 @@ impl Preview {
render_markdown: false,
markdown: None,
markdown_scroll: ScrollHandle::new(),
mutation: None,
media_scope: None,
media_document: None,
media_extensions: None,
viewer,
handled_reveal: None,
rendered_after_reveal: None,
}
Expand Down Expand Up @@ -186,6 +198,14 @@ impl Preview {
fn fetch(&mut self, cx: &mut Context<Self>) {
let Some(target) = self.target.clone() else { return };
let key = Key::of(&target);
#[cfg(feature = "snapshot")]
if let Some(fixture) = cx.try_global::<MarkdownFixture>().filter(|fixture| fixture.path == key.path).cloned() {
let file = FileContents { relative_path: key.path.clone(), contents: fixture.text, byte_length: 0, truncated: false };
self.render_markdown = true;
self.replace_content(Content::Text(Rc::new(build_text_file(key, file))), cx);
cx.notify();
return;
}
self.generation += 1;
let generation = self.generation;
match image_kind(&key.path) {
Expand All @@ -194,7 +214,7 @@ impl Preview {
self.replace_content(Content::Image(ImageState::Loading), cx);
}
let state = self.state.read(cx);
let task = image::load(state, &target.root, &key.path, kind);
let task = image::load(state, &target.root, &key.path, kind, self.mutation.as_deref());
self._fetch = Some(cx.spawn(async move |this, cx| {
let result = match task {
Some(task) => task.await.unwrap_or_else(|error| Err(error.to_string())),
Expand Down Expand Up @@ -315,16 +335,43 @@ impl Preview {
file.truncated.then(|| format!("Preview limited to the first 1 MB of a {} byte file.", group_thousands(file.byte_length)))
}

pub fn set_mutation(&mut self, mutation: Option<String>, cx: &mut Context<Self>) {
if self.mutation == mutation { return; }
self.mutation = mutation;
if let Some(scope) = &self.media_scope { scope.lock().unwrap().workspace_mutation_id = self.mutation.clone(); }
cx.notify();
}

fn render_markdown(&mut self, file: &Rc<TextFile>, cx: &mut Context<Self>) -> AnyElement {
let state = match &self.markdown {
Some((key, state)) if *key == file.key => state.clone(),
_ => {
let text = mask_frontmatter(&file.contents).into_owned();
let state = cx.new(|cx| TextViewState::markdown(&text, cx).selectable(true));
if let Some(document) = &self.media_document { *document.write().unwrap() = std::sync::Arc::new(t3_core::media::MarkdownMediaDocument::new(std::sync::Arc::from(text.as_str()))); }
else { self.media_document = Some(crate::transcript::media_document(std::sync::Arc::from(text.as_str()))); }
let state = cx.new(|cx| {
TextViewState::markdown(&text, cx).selectable(true)
});
#[cfg(feature = "snapshot")]
if !text.contains("![") { crate::snapshot::record_media_prose(&state, &text, cx); }
self.markdown = Some((file.key.clone(), state.clone()));
state
}
};
let target = self.target.as_ref().unwrap();
let scope = crate::media::MediaScope {
owner: cx.entity_id(),
thread_id: Some(target.root.workspace.thread_id.clone()),
workspace_root: Some(t3_core::media::markdown_image_base_dir(&target.root.workspace.cwd, &target.path)),
workspace_mutation_id: self.mutation.clone(),
viewer: self.viewer.downgrade(),
};
if let Some(current) = &self.media_scope { *current.lock().unwrap() = scope; }
else {
let scope = std::sync::Arc::new(std::sync::Mutex::new(scope));
self.media_extensions = Some(crate::transcript::markdown_extensions(self.media_document.clone().unwrap(), Some(scope.clone())));
self.media_scope = Some(scope);
}
let p = palette(cx);
div()
.id("file-markdown")
Expand All @@ -342,7 +389,7 @@ impl Preview {
.text_size(px(14.))
.line_height(px(22.75))
.text_color(hsla(p.foreground).opacity(0.9))
.child(crate::transcript::markdown::markdown_view(&state, cx)),
.child(crate::transcript::markdown::markdown_view(&state, cx).markdown_extensions(self.media_extensions.clone().unwrap())),
),
)
.vertical_scrollbar(&self.markdown_scroll)
Expand Down Expand Up @@ -433,6 +480,12 @@ impl Render for Preview {
if self.renders_markdown() { self.render_markdown(&file, cx) } else { self.render_code(window, cx) }
}
};
v_flex().size_full().min_h_0().child(body)
v_flex().relative().size_full().min_h_0().child(body).child(self.viewer.clone())
}
}

#[cfg(feature = "snapshot")]
#[derive(Clone, serde::Deserialize)]
pub(crate) struct MarkdownFixture { pub path: String, pub text: String }
#[cfg(feature = "snapshot")]
impl gpui::Global for MarkdownFixture {}
Loading