Multiple collections, tab close-all and rename, tab scroll bar fix

- Keep several collections open at once. Each has its own environments,
  selected environment, cookie jar and file watcher, and they are reopened
  on start. The environment picker acts on the current collection (the
  active tab's, or the one last chosen). Overlapping folders are refused.
- Close all / close others for tabs (Ctrl+Shift+W, tab menu, File menu),
  asking once about tabs with unsaved changes.
- Rename a request from its tab (double-click, tab menu, or the … menu).
- Give the tab strip's scroll bar its own row so it no longer covers the
  tab names.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Torbjörn MolinandClaude Opus 5.5 committed 2026-10-19 10:55:20 +02:00
1 parent 4b729c5d32
commit d5e7b484fc
13 files changed
+997 -237

No files matched your search

+223 -94
View File
@@ -26,6 +26,15 @@ pub enum Action {
CloseTab(TabId),
/// Close even with unsaved changes.
DiscardAndCloseTab(TabId),
/// Close several tabs; asks once about the ones with unsaved changes.
CloseTabs(Vec<TabId>),
DiscardAndCloseTabs(Vec<TabId>),
/// Rename the tab's request: in its file right away if the tab has no
/// other unsaved edits, else as an edit that Save writes.
RenameTab {
tab: TabId,
title: String,
},
SelectTab(TabId),
OpenRequest {
file: PathBuf,
@@ -73,14 +82,20 @@ pub enum Action {
// Collections.
PickCollection,
OpenCollection(PathBuf),
CloseCollection(PathBuf),
/// Makes a collection the current one (environment picker, untitled tabs).
SetActiveCollection(PathBuf),
CollectionLoaded(Result<Collection, String>),
/// The environments, and whether the private file exists without being git-ignored.
EnvironmentsLoaded(Result<(Environments, bool), String>),
EnvironmentsLoaded {
root: PathBuf,
result: Result<(Environments, bool), String>,
},
FileChangedOnDisk {
path: PathBuf,
text: Option<String>,
},
Rescan,
Rescan(PathBuf),
Save(TabId),
/// `existing` is the current content of `path`, if it exists.
SaveAs {
@@ -130,8 +145,8 @@ pub enum Action {
EnvEditorChanged(EnvEditor),
EnvEditorSave,
EnvEditorClose,
AddPrivateFileToGitignore,
DismissGitignoreOffer,
AddPrivateFileToGitignore(PathBuf),
DismissGitignoreOffer(PathBuf),
// Import / export.
CopyAsCurl(TabId),
@@ -187,9 +202,55 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
state.dialog = None;
close_tab(state, id);
}
Action::CloseTabs(ids) => {
let mut dirty = Vec::new();
for id in ids {
if state.tab(id).is_some_and(Tab::is_dirty) {
dirty.push(id);
} else {
close_tab(state, id);
}
}
if !dirty.is_empty() {
state.dialog = Some(Dialog::ConfirmCloseTabs(dirty));
}
}
Action::DiscardAndCloseTabs(ids) => {
state.dialog = None;
for id in ids {
close_tab(state, id);
}
}
Action::RenameTab { tab, title } => {
state.dialog = None;
let Some(t) = state.tab(tab) else { return fx };
let in_file = t
.origin
.as_ref()
.filter(|_| !t.is_dirty())
.and_then(|o| Some((o.file.clone(), file_of(state, &o.file)?.find(&o.key)?)));
if let Some((file, index)) = in_file {
return update(state, Action::RenameRequest { file, index, title });
}
let title = title.trim().to_string();
edit(state, tab, |r| {
r.title = (!title.is_empty()).then_some(title);
if r.name.is_some() {
r.name = r.title.as_deref().map(slug_name);
}
});
}
Action::SelectTab(id) => {
if state.tab(id).is_some() {
if let Some(t) = state.tab(id) {
let root = t
.origin
.as_ref()
.and_then(|o| state.collection_of(&o.file))
.map(|c| c.root.clone());
state.active = Some(id);
if root.is_some() {
state.active_collection = root;
}
}
}
Action::OpenRequest { file, index } => open_request(state, &file, index),
@@ -357,23 +418,78 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
// ---------------------------------------------------- collections
Action::PickCollection => fx.push(Effect::PickCollectionFolder),
Action::OpenCollection(root) => {
if state.collection(&root).is_some() {
state.active_collection = Some(root);
return fx;
}
if let Some(other) = state
.collections
.iter()
.find(|c| root.starts_with(&c.root) || c.root.starts_with(&root))
{
state.set_error(format!(
"{} overlaps the open collection {}; close that one first",
root.display(),
other.root.display()
));
return fx;
}
state.settings.add_recent(root.clone());
if !state.settings.open_collections.contains(&root) {
state.settings.open_collections.push(root.clone());
}
fx.push(Effect::SaveSettings(state.settings.clone()));
fx.push(Effect::LoadCollection(root));
}
Action::CloseCollection(root) => {
let Some(pos) = state.collections.iter().position(|c| c.root == root) else {
return fx;
};
state.collections.remove(pos);
fx.push(Effect::Unwatch(root.clone()));
state.settings.open_collections.retain(|r| r != &root);
fx.push(Effect::SaveSettings(state.settings.clone()));
state.cookie_jars.remove(&Some(root.clone()));
if state.env_editor.as_ref().is_some_and(|e| e.root == root) {
state.env_editor = None;
}
if state.active_collection.as_ref() == Some(&root) {
state.active_collection = state.collections.first().map(|c| c.root.clone());
}
// Clean tabs from it close; tabs with edits stay, detached from their file.
let ids: Vec<(TabId, bool)> = state
.tabs
.iter()
.filter(|t| t.origin.as_ref().is_some_and(|o| o.file.starts_with(&root)))
.map(|t| (t.id, t.is_dirty()))
.collect();
for (id, dirty) in ids {
if dirty {
if let Some(t) = state.tab_mut(id) {
t.origin = None;
t.orphaned = true;
}
} else {
close_tab(state, id);
}
}
}
Action::SetActiveCollection(root) => {
if state.collection(&root).is_some() {
state.active_collection = Some(root);
}
}
Action::CollectionLoaded(Err(e)) => state.set_error(format!("Could not open the collection: {e}")),
Action::CollectionLoaded(Ok(c)) => collection_loaded(state, c, &mut fx),
Action::EnvironmentsLoaded(res) => {
if let Some(c) = &mut state.collection {
match res {
Action::EnvironmentsLoaded { root, result } => {
if let Some(c) = state.collection_mut(&root) {
match result {
Ok((envs, private_not_ignored)) => {
c.envs = envs;
c.env_error = None;
c.private_not_ignored = private_not_ignored;
if let Some(sel) = &state.selected_env {
if !c.envs.names().contains(sel) {
state.selected_env = None;
}
if c.selected_env.as_ref().is_some_and(|sel| !c.envs.names().contains(sel)) {
c.selected_env = None;
}
}
Err(e) => c.env_error = Some(e),
@@ -381,9 +497,9 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
}
}
Action::FileChangedOnDisk { path, text } => file_changed(state, path, text, &mut fx),
Action::Rescan => {
if let Some(c) = &state.collection {
fx.push(Effect::LoadCollection(c.root.clone()));
Action::Rescan(root) => {
if state.collection(&root).is_some() {
fx.push(Effect::LoadCollection(root));
}
}
Action::Save(id) => save(state, id, &mut fx),
@@ -410,7 +526,7 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
format!("{name}.http")
};
let path = dir.join(name);
if state.collection.as_ref().is_some_and(|c| c.files.contains_key(&path)) {
if state.collection_of(&path).is_some_and(|c| c.files.contains_key(&path)) {
state.set_error(format!("{} already exists", path.display()));
return fx;
}
@@ -568,7 +684,7 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
}
}
}
if let Some(c) = &mut state.collection {
if let Some(c) = state.collection_of_mut(&path) {
if let Some(f) = c.files.remove(&path) {
c.files.insert(to, f);
}
@@ -586,7 +702,7 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
.map(|e| e.to_string_lossy().into_owned())
.unwrap_or("http".into());
let dir = path.parent().map(Path::to_path_buf).unwrap_or_default();
let taken = |p: &PathBuf| state.collection.as_ref().is_some_and(|c| c.files.contains_key(p));
let taken = |p: &PathBuf| state.collection_of(p).is_some_and(|c| c.files.contains_key(p));
let copy = std::iter::once(dir.join(format!("{stem} copy.{ext}")))
.chain((2..).map(|n| dir.join(format!("{stem} copy {n}.{ext}"))))
.find(|p| !taken(p))
@@ -598,21 +714,30 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
state.dialog = None;
fx.push(file_ops(state, vec![FileOp::Delete(path.clone())], true));
orphan_tabs(state, &path, None);
if let Some(c) = &mut state.collection {
if let Some(c) = state.collection_of_mut(&path) {
c.files.remove(&path);
}
}
// ---------------------------------------------------- environments
Action::SelectEnv(e) => state.selected_env = e,
Action::SelectEnv(e) => {
let Some(root) = state.current_collection().map(|c| c.root.clone()) else {
return fx;
};
if let Some(c) = state.collection_mut(&root) {
c.selected_env = e;
}
state.active_collection = Some(root);
}
Action::OpenEnvEditor => {
if let Some(c) = &state.collection {
let selected = state
if let Some(c) = state.current_collection() {
let selected = c
.selected_env
.clone()
.or_else(|| c.envs.names().first().cloned())
.unwrap_or_else(|| "dev".into());
state.env_editor = Some(EnvEditor {
root: c.root.clone(),
envs: c.envs.clone(),
selected,
revealed: Vec::new(),
@@ -625,7 +750,8 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
Action::EnvEditorChanged(e) => state.env_editor = Some(e),
Action::EnvEditorClose => state.env_editor = None,
Action::EnvEditorSave => {
let (Some(ed), Some(c)) = (&mut state.env_editor, &mut state.collection) else {
let Some(ed) = &mut state.env_editor else { return fx };
let Some(c) = state.collections.iter_mut().find(|c| c.root == ed.root) else {
return fx;
};
let mut ops = Vec::new();
@@ -643,14 +769,14 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
fx.push(Effect::LoadEnvironments(c.root.clone()));
}
}
Action::AddPrivateFileToGitignore => {
if let Some(c) = &mut state.collection {
fx.push(Effect::AppendGitignore(c.root.clone()));
Action::AddPrivateFileToGitignore(root) => {
if let Some(c) = state.collection_mut(&root) {
fx.push(Effect::AppendGitignore(root));
c.private_not_ignored = false;
}
}
Action::DismissGitignoreOffer => {
if let Some(c) = &mut state.collection {
Action::DismissGitignoreOffer(root) => {
if let Some(c) = state.collection_mut(&root) {
c.gitignore_offer_dismissed = true;
}
}
@@ -697,13 +823,14 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
Action::RestoreHistory(i) => {
if let Some(e) = state.history.get(i) {
let text = e.http.clone();
if e.env.is_some()
&& state
.collection
.as_ref()
.is_some_and(|c| c.envs.names().contains(e.env.as_ref().unwrap()))
{
state.selected_env = e.env.clone();
let env = e.env.clone();
let root = state.current_collection().map(|c| c.root.clone());
if let (Some(env), Some(root)) = (env, root) {
if let Some(c) = state.collection_mut(&root) {
if c.envs.names().contains(&env) {
c.selected_env = Some(env);
}
}
}
return update(state, Action::NewTabFromHttp(text));
}
@@ -713,11 +840,11 @@ pub fn update(state: &mut AppState, action: Action) -> Vec<Effect> {
fx.push(Effect::RewriteHistory(Vec::new()));
}
Action::ClearCookies => {
let key = state.collection.as_ref().map(|c| c.root.clone());
let key = state.current_collection().map(|c| c.root.clone());
state.cookie_jars.entry(key).or_default().clear();
}
Action::DeleteCookie(i) => {
let key = state.collection.as_ref().map(|c| c.root.clone());
let key = state.current_collection().map(|c| c.root.clone());
state.cookie_jars.entry(key).or_default().remove(i);
}
Action::SettingsLoaded(s) => {
@@ -795,23 +922,27 @@ fn close_tab(state: &mut AppState, id: TabId) {
}
fn file_of<'a>(state: &'a AppState, path: &Path) -> Option<&'a HttpFile> {
state.collection.as_ref()?.files.get(path)
state.collection_of(path)?.files.get(path)
}
fn insert_file(state: &mut AppState, path: &Path, text: String) {
if let Some(c) = &mut state.collection {
if let Some(c) = state.collection_of_mut(path) {
c.files.insert(path.to_path_buf(), HttpFile::parse(text));
}
}
/// File operations, followed by a rescan of the collection of the first
/// operation's file when `rescan` is set.
fn file_ops(state: &AppState, ops: Vec<FileOp>, rescan: bool) -> Effect {
let first = ops.first().map(|op| match op {
FileOp::Write(p, _) | FileOp::Delete(p) | FileOp::Rename(p, _) => p.clone(),
});
Effect::FileOps {
rescan: first
.filter(|_| rescan)
.and_then(|p| state.collection_of(&p))
.map(|c| c.root.clone()),
ops,
rescan: if rescan {
state.collection.as_ref().map(|c| c.root.clone())
} else {
None
},
}
}
@@ -830,6 +961,7 @@ fn open_request(state: &mut AppState, file: &Path, index: usize) {
return;
};
let (req, src) = (req.clone(), src.to_string());
state.active_collection = state.collection_of(file).map(|c| c.root.clone());
new_tab(
state,
req,
@@ -894,7 +1026,9 @@ fn send(state: &mut AppState, id: TabId, fx: &mut Vec<Effect>) {
}
};
let jar_key = (!t.request.directives.no_cookie_jar).then(|| state.collection.as_ref().map(|c| c.root.clone()));
let collection = state.collection_for_tab(t);
let jar_key = (!t.request.directives.no_cookie_jar).then(|| collection.map(|c| c.root.clone()));
let env = collection.and_then(|c| c.selected_env.clone());
if let Some(key) = &jar_key {
let has_cookie = resolved.headers.iter().any(|(n, _)| n.eq_ignore_ascii_case("cookie"));
if let Some(c) = state
@@ -917,7 +1051,7 @@ fn send(state: &mut AppState, id: TabId, fx: &mut Vec<Effect>) {
url: resolved.url.clone(),
status: None,
duration_ms: 0,
env: state.selected_env.clone(),
env,
http: if t.source.trim().is_empty() {
render_request(&t.request, "\n")
} else {
@@ -1068,54 +1202,55 @@ fn recompute_search(t: &mut Tab) {
}
fn collection_loaded(state: &mut AppState, c: Collection, fx: &mut Vec<Effect>) {
let same_root = state.collection.as_ref().is_some_and(|old| old.root == c.root);
let (envs, env_error, dismissed) = match state.collection.take() {
Some(old) if same_root => (old.envs, old.env_error, old.gitignore_offer_dismissed),
_ => (Environments::default(), None, false),
};
let root = c.root.clone();
state.collection = Some(CollectionState {
root: c.root,
tree: c.tree,
files: c.files,
envs,
env_error,
private_not_ignored: false,
gitignore_offer_dismissed: dismissed,
});
if !same_root {
// Tabs from another collection keep their content but lose their file.
for t in state.tabs.iter_mut() {
if t.origin.as_ref().is_some_and(|o| !o.file.starts_with(&root)) {
t.origin = None;
t.orphaned = true;
}
}
state.selected_env = None;
fx.push(Effect::Watch(Some(root.clone())));
} else {
// Re-sync open tabs with what's on disk now.
let ids: Vec<(TabId, PathBuf)> = state
.tabs
.iter()
.filter_map(|t| t.origin.as_ref().map(|o| (t.id, o.file.clone())))
.collect();
for (id, file) in ids {
if file_of(state, &file).is_none() {
if let Some(t) = state.tab_mut(id) {
t.orphaned = true;
match state.collection_mut(&root) {
Some(existing) => {
// A rescan: keep environments and choices, take the new tree.
existing.tree = c.tree;
existing.files = c.files;
let ids: Vec<(TabId, PathBuf)> = state
.tabs
.iter()
.filter_map(|t| t.origin.as_ref().map(|o| (t.id, o.file.clone())))
.filter(|(_, f)| f.starts_with(&root))
.collect();
for (id, file) in ids {
if file_of(state, &file).is_none() {
if let Some(t) = state.tab_mut(id) {
t.orphaned = true;
}
}
}
}
None => {
state.collections.push(CollectionState {
root: c.root,
tree: c.tree,
files: c.files,
envs: Environments::default(),
selected_env: None,
env_error: None,
private_not_ignored: false,
gitignore_offer_dismissed: false,
});
state.active_collection = Some(root.clone());
fx.push(Effect::Watch(root.clone()));
}
}
fx.push(Effect::LoadEnvironments(root));
}
fn file_changed(state: &mut AppState, path: PathBuf, text: Option<String>, fx: &mut Vec<Effect>) {
let Some(c) = &mut state.collection else { return };
let Some(c) = state.collection_of_mut(&path) else {
return;
};
let root = c.root.clone();
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
if name == env::PUBLIC_FILE || name == env::PRIVATE_FILE {
fx.push(Effect::LoadEnvironments(c.root.clone()));
// Only the env files at the collection root count.
if path.parent() == Some(root.as_path()) {
fx.push(Effect::LoadEnvironments(root));
}
return;
}
if !collection::is_http_file(&path) {
@@ -1125,7 +1260,7 @@ fn file_changed(state: &mut AppState, path: PathBuf, text: Option<String>, fx: &
None => {
if c.files.remove(&path).is_some() {
orphan_tabs(state, &path, None);
fx.push(Effect::LoadCollection(state.collection.as_ref().unwrap().root.clone()));
fx.push(Effect::LoadCollection(root));
}
}
Some(text) => {
@@ -1135,7 +1270,7 @@ fn file_changed(state: &mut AppState, path: PathBuf, text: Option<String>, fx: &
let is_new = !c.files.contains_key(&path);
c.files.insert(path.clone(), HttpFile::parse(text));
if is_new {
fx.push(Effect::LoadCollection(c.root.clone()));
fx.push(Effect::LoadCollection(root));
}
let ids: Vec<TabId> = state
.tabs
@@ -1295,11 +1430,7 @@ fn save_as(state: &mut AppState, id: TabId, path: PathBuf, existing: Option<Stri
let text = file.insert_request_text(None, &block);
fx.push(Effect::FileOps {
ops: vec![FileOp::Write(path.clone(), text.clone())],
rescan: state
.collection
.as_ref()
.filter(|c| path.starts_with(&c.root))
.map(|c| c.root.clone()),
rescan: state.collection_of(&path).map(|c| c.root.clone()),
});
let parsed = HttpFile::parse(text);
let fresh = (|| {
@@ -1309,10 +1440,8 @@ fn save_as(state: &mut AppState, id: TabId, path: PathBuf, existing: Option<Stri
parsed.key_of(index)?,
))
})();
if let Some(c) = &mut state.collection {
if path.starts_with(&c.root) {
c.files.insert(path.clone(), parsed);
}
if let Some(c) = state.collection_of_mut(&path) {
c.files.insert(path.clone(), parsed);
}
let t = state.tab_mut(id).unwrap();
if let Some((req, src, key)) = fresh {