Skip to content
Open
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
128 changes: 105 additions & 23 deletions src-tauri/src/commands/remote_workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Deserialize;
#[cfg(feature = "tauri-runtime")]
use std::time::Duration;
#[cfg(feature = "tauri-runtime")]
use tauri::{AppHandle, Manager, WebviewUrl, WebviewWindowBuilder};
use tauri::{AppHandle, Emitter, EventTarget, Manager, WebviewUrl, WebviewWindowBuilder};

#[cfg(feature = "tauri-runtime")]
use crate::app_error::AppCommandError;
Expand Down Expand Up @@ -171,35 +171,43 @@ pub async fn reorder_remote_workspace_connections(
remote_workspace_connection_service::reorder(&db.conn, ids).await
}

/// Tauri event used to hand an already-open remote workspace window a folder
/// to open. MUST match `REMOTE_OPEN_FOLDER_EVENT` in
/// `src/lib/remote-workspace.ts`.
#[cfg(feature = "tauri-runtime")]
#[cfg_attr(feature = "tauri-runtime", tauri::command)]
pub async fn open_remote_workspace(
app: AppHandle,
db: tauri::State<'_, AppDatabase>,
id: i32,
) -> Result<(), AppCommandError> {
let connection = remote_workspace_connection_service::get(&db.conn, id)
.await
.map_err(AppCommandError::db)?
.ok_or_else(|| AppCommandError::not_found(format!("Remote connection {id} not found")))?;
const REMOTE_OPEN_FOLDER_EVENT: &str = "remote-open-folder";

let label = format!("remote-workspace-{id}");
if let Some(existing) = app.get_webview_window(&label) {
let _ = existing.unminimize();
existing.set_focus().map_err(|e| {
AppCommandError::window("Failed to focus remote workspace", e.to_string())
})?;
return Ok(());
}
/// Query param carrying the same request to a window that is being spawned
/// (an event can't reach a webview that doesn't exist yet). MUST match the
/// param read by `RemoteWorkspaceOpenFolderListener`.
#[cfg(feature = "tauri-runtime")]
const OPEN_FOLDER_PATH_PARAM: &str = "openFolderPath";

validate_remote_health(&connection.base_url, &connection.token, &connection.headers).await?;
#[cfg(feature = "tauri-runtime")]
#[derive(Clone, serde::Serialize)]
struct RemoteOpenFolderPayload {
path: String,
}

/// Spawn (and register) the window bound to a remote connection. `extra_query`
/// is appended verbatim to the workspace URL and must already be URL-encoded.
#[cfg(feature = "tauri-runtime")]
fn build_remote_workspace_window(
app: &AppHandle,
id: i32,
name: &str,
extra_query: &str,
) -> Result<tauri::WebviewWindow, AppCommandError> {
let label = format!("remote-workspace-{id}");
let window_instance_id = new_remote_window_instance_id();
let url = WebviewUrl::App(
format!("workspace?remoteConnectionId={id}&remoteWindowId={window_instance_id}").into(),
format!(
"workspace?remoteConnectionId={id}&remoteWindowId={window_instance_id}{extra_query}"
)
.into(),
);
let builder = WebviewWindowBuilder::new(&app, &label, url)
.title(format!("Codeg - {}", connection.name))
let builder = WebviewWindowBuilder::new(app, &label, url)
.title(format!("Codeg - {name}"))
.inner_size(1260.0, 860.0)
.min_inner_size(400.0, 600.0)
.center();
Expand All @@ -221,5 +229,79 @@ pub async fn open_remote_workspace(
.register_window_instance_cleanup(&window, window_instance_id);
}
crate::commands::windows::post_window_setup(&window);
Ok(window)
}

#[cfg(feature = "tauri-runtime")]
#[cfg_attr(feature = "tauri-runtime", tauri::command)]
pub async fn open_remote_workspace(
app: AppHandle,
db: tauri::State<'_, AppDatabase>,
id: i32,
) -> Result<(), AppCommandError> {
let connection = remote_workspace_connection_service::get(&db.conn, id)
.await
.map_err(AppCommandError::db)?
.ok_or_else(|| AppCommandError::not_found(format!("Remote connection {id} not found")))?;

let label = format!("remote-workspace-{id}");
if let Some(existing) = app.get_webview_window(&label) {
let _ = existing.unminimize();
existing.set_focus().map_err(|e| {
AppCommandError::window("Failed to focus remote workspace", e.to_string())
})?;
return Ok(());
}

validate_remote_health(&connection.base_url, &connection.token, &connection.headers).await?;

build_remote_workspace_window(&app, id, &connection.name, "")?;
Ok(())
}

/// Open a folder that lives on a remote workspace host.
///
/// Folders belong to the backend that owns their paths, so this never opens the
/// folder here — it raises (or spawns) the window bound to `connection_id` and
/// hands it the path. That window opens the folder through its own transport,
/// which keeps the folder in the workspace that owns it and lets any failure
/// surface where the user is looking.
///
/// A live window gets a Tauri event; a window we have to spawn gets the path as
/// a URL param, since an event can't reach a webview that doesn't exist yet.
#[cfg(feature = "tauri-runtime")]
#[cfg_attr(feature = "tauri-runtime", tauri::command)]
pub async fn open_remote_workspace_folder(
app: AppHandle,
db: tauri::State<'_, AppDatabase>,
id: i32,
path: String,
) -> Result<(), AppCommandError> {
let connection = remote_workspace_connection_service::get(&db.conn, id)
.await
.map_err(AppCommandError::db)?
.ok_or_else(|| AppCommandError::not_found(format!("Remote connection {id} not found")))?;

let label = format!("remote-workspace-{id}");
if let Some(existing) = app.get_webview_window(&label) {
let _ = existing.unminimize();
existing.set_focus().map_err(|e| {
AppCommandError::window("Failed to focus remote workspace", e.to_string())
})?;
app.emit_to(
EventTarget::webview(&label),
REMOTE_OPEN_FOLDER_EVENT,
RemoteOpenFolderPayload { path },
)
.map_err(|e| {
AppCommandError::window("Failed to open the folder in the remote workspace", e.to_string())
})?;
return Ok(());
}

validate_remote_health(&connection.base_url, &connection.token, &connection.headers).await?;

let query = format!("&{}={}", OPEN_FOLDER_PATH_PARAM, urlencoding::encode(&path));
build_remote_workspace_window(&app, id, &connection.name, &query)?;
Ok(())
}
1 change: 1 addition & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,7 @@ mod tauri_app {
remote_workspace_commands::get_remote_workspace_connection,
remote_workspace_commands::reorder_remote_workspace_connections,
remote_workspace_commands::open_remote_workspace,
remote_workspace_commands::open_remote_workspace_folder,
remote_proxy_commands::remote_http_call,
remote_proxy_commands::remote_upload_attachment,
remote_proxy_commands::remote_upload_workspace_paths,
Expand Down
2 changes: 2 additions & 0 deletions src/app/workspace/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
PetFocusBridge,
} from "@/components/workspace/deep-link-bootstrap"
import { WorkspaceOpenFolderListener } from "@/components/workspace/workspace-open-folder-listener"
import { RemoteWorkspaceOpenFolderListener } from "@/components/workspace/remote-workspace-open-folder-listener"
import { HeavyPluginsWarmup } from "@/components/ai-elements/heavy-plugins-warmup"
import {
ResizableHandle,
Expand Down Expand Up @@ -1297,6 +1298,7 @@ function WorkspaceLayoutInner({ children }: { children: React.ReactNode }) {
listener calls openConversations() to
surface a launcher-opened folder. */}
<WorkspaceOpenFolderListener />
<RemoteWorkspaceOpenFolderListener />
<FolderLayoutShell>
{children}
</FolderLayoutShell>
Expand Down
Loading
Loading