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
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros", "net", "
libc = "0.2"
uuid = { version = "1", features = ["v4"] }
chrono = { version = "0.4", default-features = false, features = ["clock"] }
rfd = "0.17"

# The profile that 'dist' will build with
[profile.dist]
Expand Down
13 changes: 13 additions & 0 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ pub enum Message {
FocusPreviousTab,
NextIdle,
PendingInput(PendingKey),
/// Open the native folder picker for the pending tab `tab_id`.
OpenProjectDialog(usize),
/// Folder picker resolved: spawn or focus the project tab for the
/// chosen folder. `None` means cancelled (no-op).
SpawnOrFocusProjectTab { tab_id: usize, path: Option<PathBuf> },
McpSpawnAgent(usize, Option<PathBuf>, Option<usize>, Option<String>, Option<String>, Option<String>, Option<String>),
McpCloseTab(usize, usize),
McpCheckpoint(usize),
Expand Down Expand Up @@ -232,6 +237,9 @@ pub struct App {
ckpt_store: crate::checkpoint_store::CheckpointStore,
toasts: Vec<Toast>,
next_toast_id: usize,
/// A native folder picker is in flight; further `OpenProjectDialog`
/// requests are ignored until its `SpawnOrFocusProjectTab` arrives.
project_dialog_open: bool,
}

impl App {
Expand Down Expand Up @@ -272,6 +280,7 @@ impl App {
ckpt_store,
toasts: Vec::new(),
next_toast_id: 0,
project_dialog_open: false,
};

(app, listen_task)
Expand Down Expand Up @@ -307,6 +316,10 @@ impl App {
Message::FocusPreviousTab => self.handle_focus_previous_tab(),
Message::NextIdle => self.handle_next_idle(),
Message::PendingInput(key) => self.handle_pending_input(key),
Message::OpenProjectDialog(tab_id) => self.handle_open_project_dialog(tab_id),
Message::SpawnOrFocusProjectTab { tab_id, path } => {
self.handle_spawn_or_focus_project_tab(tab_id, path)
}
Message::CloseTab(tab_id) => self.close_tab(tab_id),
Message::McpCloseTab(rtid, target) => self.handle_mcp_close_tab(rtid, target),
Message::SelectTab(tab_id) => self.handle_select_tab(tab_id),
Expand Down
148 changes: 114 additions & 34 deletions src/ui/handlers/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,44 +374,81 @@ impl App {
self.close_tab(tab_id)
}
PendingKey::Submit => {
let raw_path = input.clone();
let expanded = if raw_path.starts_with('~') {
let home = std::env::var("HOME").unwrap_or_default();
raw_path.replacen('~', &home, 1)
} else {
raw_path
};
let path = PathBuf::from(&expanded);
let canonical = std::fs::canonicalize(&path)
.unwrap_or(path);

if let Some(existing) = self.tabs.find_project_for_dir(&canonical) {
self.focus_tab(existing);
return self.close_tab(tab_id);
}

let parent_id = match self.tabs.get(tab_id) {
Some(t) => t.parent_id,
None => return Task::none(),
};
self.tabs.remove(tab_id);

let (id, task) = self.spawn_tab(
true,
AgentRank::Project,
Some(canonical),
parent_id,
None,
None,
None,
None,
);
self.focus_tab(id);
task
let home = std::env::var("HOME").unwrap_or_default();
let path = expand_tilde(input, &home);
self.open_project_from_path(tab_id, path)
}
}
}

/// Open a project from `path`, consuming the pending tab `tab_id`:
/// canonicalize, focus an already-open project if one matches,
/// otherwise replace the pending tab with a spawned project tab.
/// No-op when `tab_id` is gone — the folder dialog is non-modal, so
/// its result can arrive after the pending tab was closed or
/// submitted via a typed path.
fn open_project_from_path(&mut self, tab_id: usize, path: PathBuf) -> Task<Message> {
let parent_id = match self.tabs.get(tab_id) {
Some(t) => t.parent_id,
None => return Task::none(),
};

let canonical = std::fs::canonicalize(&path).unwrap_or(path);

if let Some(existing) = self.tabs.find_project_for_dir(&canonical) {
self.focus_tab(existing);
return self.close_tab(tab_id);
}

self.tabs.remove(tab_id);

let (id, task) = self.spawn_tab(
true,
AgentRank::Project,
Some(canonical),
parent_id,
None,
None,
None,
None,
);
self.focus_tab(id);
task
}

pub(in crate::ui) fn handle_open_project_dialog(&mut self, tab_id: usize) -> Task<Message> {
if self.project_dialog_open || !self.tabs.contains(tab_id) {
return Task::none();
}
self.project_dialog_open = true;

// The dialog future must be built here in update() — on macOS rfd
// dialogs have to be spawned on the main thread; only the await
// runs on the executor.
let home = std::env::var("HOME").unwrap_or_default();
let dialog = rfd::AsyncFileDialog::new()
.set_title("Open Project")
.set_directory(home)
.pick_folder();

Task::perform(dialog, move |handle| Message::SpawnOrFocusProjectTab {
tab_id,
path: handle.map(|h| h.path().to_path_buf()),
})
}

pub(in crate::ui) fn handle_spawn_or_focus_project_tab(
&mut self,
tab_id: usize,
path: Option<PathBuf>,
) -> Task<Message> {
self.project_dialog_open = false;
match path {
Some(path) => self.open_project_from_path(tab_id, path),
None => Task::none(),
}
}

pub(in crate::ui) fn handle_mcp_close_tab(
&mut self,
requesting_tab_id: usize,
Expand Down Expand Up @@ -583,6 +620,16 @@ impl App {
}
}

/// Expand a leading `~` to `home`. Only the first occurrence is
/// replaced, matching shell behavior for `~/...` paths.
fn expand_tilde(raw: &str, home: &str) -> PathBuf {
if raw.starts_with('~') {
PathBuf::from(raw.replacen('~', home, 1))
} else {
PathBuf::from(raw)
}
}

fn build_list_tabs_json(
tabs: &crate::tabs::Tabs,
requesting_tab_id: usize,
Expand Down Expand Up @@ -638,6 +685,39 @@ fn build_list_tabs_json(
.collect()
}

#[cfg(test)]
mod expand_tilde_tests {
use std::path::PathBuf;

use super::expand_tilde;

#[test]
fn expands_leading_tilde() {
assert_eq!(
expand_tilde("~/work", "/Users/me"),
PathBuf::from("/Users/me/work"),
);
}

#[test]
fn bare_tilde_is_home() {
assert_eq!(expand_tilde("~", "/Users/me"), PathBuf::from("/Users/me"));
}

#[test]
fn absolute_path_untouched() {
assert_eq!(expand_tilde("/tmp/x", "/Users/me"), PathBuf::from("/tmp/x"));
}

#[test]
fn interior_tilde_untouched() {
assert_eq!(
expand_tilde("/tmp/~x", "/Users/me"),
PathBuf::from("/tmp/~x"),
);
}
}

#[cfg(test)]
mod list_tabs_tests {
use super::build_list_tabs_json;
Expand Down
Loading