feat: add Home as the default tab
- Model Home as a tab without a file path (Option<PathBuf>) - + creates a Home tab; opening a file from Home reuses that tab - Add a New File button to the Home page (replaces + for file creation) - Closing the last file tab falls back to a Home tab - Hide the tab strip while the only tab is Home - Keep Home tabs out of detach, reveal, watcher, and duplicate checks
This commit is contained in:
210
src/session.rs
210
src/session.rs
@@ -1,15 +1,26 @@
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct DocumentTab {
|
||||
pub id: u64,
|
||||
pub path: PathBuf,
|
||||
/// `None` marks the built-in Home tab; Home has no file on disk.
|
||||
pub path: Option<PathBuf>,
|
||||
pub dirty: bool,
|
||||
pub missing: bool,
|
||||
pub edit_on_open: bool,
|
||||
}
|
||||
|
||||
impl DocumentTab {
|
||||
pub fn is_home(&self) -> bool {
|
||||
self.path.is_none()
|
||||
}
|
||||
|
||||
pub fn file_path(&self) -> Option<&Path> {
|
||||
self.path.as_deref()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DocumentSession {
|
||||
pub tabs: Vec<DocumentTab>,
|
||||
@@ -20,8 +31,12 @@ pub struct DocumentSession {
|
||||
impl DocumentSession {
|
||||
pub fn open(&mut self, path: PathBuf, edit_on_open: bool) -> u64 {
|
||||
let path = normalize_path(path);
|
||||
if let Some(tab) = self.tabs.iter_mut().find(|tab| tab.path == path) {
|
||||
tab.missing = !tab.path.exists();
|
||||
if let Some(tab) = self
|
||||
.tabs
|
||||
.iter_mut()
|
||||
.find(|tab| tab.path.as_deref() == Some(path.as_path()))
|
||||
{
|
||||
tab.missing = !path.exists();
|
||||
tab.edit_on_open |= edit_on_open;
|
||||
self.active_id = Some(tab.id);
|
||||
return tab.id;
|
||||
@@ -29,22 +44,82 @@ impl DocumentSession {
|
||||
|
||||
self.next_id += 1;
|
||||
let id = self.next_id;
|
||||
let missing = !path.exists();
|
||||
self.tabs.push(DocumentTab {
|
||||
id,
|
||||
missing: !path.exists(),
|
||||
path,
|
||||
path: Some(path),
|
||||
dirty: false,
|
||||
missing,
|
||||
edit_on_open,
|
||||
});
|
||||
self.active_id = Some(id);
|
||||
id
|
||||
}
|
||||
|
||||
/// Append and activate a Home tab (the in-app "new tab page").
|
||||
pub fn open_home(&mut self) -> u64 {
|
||||
self.next_id += 1;
|
||||
let id = self.next_id;
|
||||
self.tabs.push(DocumentTab {
|
||||
id,
|
||||
path: None,
|
||||
dirty: false,
|
||||
missing: false,
|
||||
edit_on_open: false,
|
||||
});
|
||||
self.active_id = Some(id);
|
||||
id
|
||||
}
|
||||
|
||||
/// Open `path`, turning the active Home tab into the document when possible
|
||||
/// (browser-style navigation). Falls back to appending a tab otherwise.
|
||||
pub fn open_reusing_home(&mut self, path: PathBuf, edit_on_open: bool) -> u64 {
|
||||
let Some(active_id) = self.active_id else {
|
||||
return self.open(path, edit_on_open);
|
||||
};
|
||||
if !self
|
||||
.tabs
|
||||
.iter()
|
||||
.any(|tab| tab.id == active_id && tab.is_home())
|
||||
{
|
||||
return self.open(path, edit_on_open);
|
||||
}
|
||||
|
||||
let path = normalize_path(path);
|
||||
if let Some(existing) = self
|
||||
.tabs
|
||||
.iter_mut()
|
||||
.find(|tab| tab.id != active_id && tab.path.as_deref() == Some(path.as_path()))
|
||||
{
|
||||
existing.missing = !path.exists();
|
||||
existing.edit_on_open |= edit_on_open;
|
||||
let existing_id = existing.id;
|
||||
// The blank tab did its job; drop it the way a browser would.
|
||||
self.tabs.retain(|tab| tab.id != active_id);
|
||||
self.active_id = Some(existing_id);
|
||||
return existing_id;
|
||||
}
|
||||
|
||||
let missing = !path.exists();
|
||||
let tab = self
|
||||
.tabs
|
||||
.iter_mut()
|
||||
.find(|tab| tab.id == active_id)
|
||||
.expect("active tab disappeared");
|
||||
tab.path = Some(path);
|
||||
tab.missing = missing;
|
||||
tab.dirty = false;
|
||||
tab.edit_on_open = edit_on_open;
|
||||
active_id
|
||||
}
|
||||
|
||||
pub fn activate(&mut self, id: u64) -> bool {
|
||||
let Some(tab) = self.tabs.iter_mut().find(|tab| tab.id == id) else {
|
||||
return false;
|
||||
};
|
||||
tab.missing = !tab.path.exists();
|
||||
if let Some(path) = tab.path.as_deref() {
|
||||
tab.missing = !path.exists();
|
||||
}
|
||||
self.active_id = Some(id);
|
||||
true
|
||||
}
|
||||
@@ -65,6 +140,17 @@ impl DocumentSession {
|
||||
true
|
||||
}
|
||||
|
||||
/// Close a tab and leave a fresh Home tab behind when it was the last one.
|
||||
pub fn close_with_home_fallback(&mut self, id: u64) -> bool {
|
||||
if !self.close(id) {
|
||||
return false;
|
||||
}
|
||||
if self.tabs.is_empty() {
|
||||
self.open_home();
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn with_tab(tab: DocumentTab) -> Self {
|
||||
let id = tab.id;
|
||||
Self {
|
||||
@@ -76,6 +162,9 @@ impl DocumentSession {
|
||||
|
||||
pub fn detach(&mut self, id: u64) -> Option<DocumentTab> {
|
||||
let index = self.tabs.iter().position(|tab| tab.id == id)?;
|
||||
if self.tabs[index].is_home() {
|
||||
return None;
|
||||
}
|
||||
let was_active = self.active_id == Some(id);
|
||||
let tab = self.tabs.remove(index);
|
||||
if was_active {
|
||||
@@ -98,20 +187,36 @@ impl DocumentSession {
|
||||
self.tabs.iter_mut().find(|tab| tab.id == id)
|
||||
}
|
||||
|
||||
pub fn active_path(&self) -> Option<&Path> {
|
||||
self.active().and_then(DocumentTab::file_path)
|
||||
}
|
||||
|
||||
pub fn active_is_home(&self) -> bool {
|
||||
self.active().is_some_and(DocumentTab::is_home)
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, id: u64) -> Option<&mut DocumentTab> {
|
||||
self.tabs.iter_mut().find(|tab| tab.id == id)
|
||||
}
|
||||
|
||||
pub fn relocate(&mut self, id: u64, path: PathBuf) -> bool {
|
||||
let path = normalize_path(path);
|
||||
if self.tabs.iter().any(|tab| tab.id != id && tab.path == path) {
|
||||
if self
|
||||
.tabs
|
||||
.iter()
|
||||
.any(|tab| tab.id != id && tab.path.as_deref() == Some(path.as_path()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
let missing = !path.exists();
|
||||
let Some(tab) = self.get_mut(id) else {
|
||||
return false;
|
||||
};
|
||||
tab.path = path;
|
||||
tab.missing = !tab.path.exists();
|
||||
if tab.is_home() {
|
||||
return false;
|
||||
}
|
||||
tab.path = Some(path);
|
||||
tab.missing = missing;
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -222,6 +327,83 @@ mod tests {
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn home_tab_is_a_tab_without_a_file_path() {
|
||||
let mut session = DocumentSession::default();
|
||||
|
||||
let home = session.open_home();
|
||||
|
||||
assert_eq!(session.active_id, Some(home));
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
assert!(session.tabs[0].is_home());
|
||||
assert_eq!(session.active_path(), None);
|
||||
assert!(session.active_is_home());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_a_file_from_home_reuses_the_home_tab() {
|
||||
let dir = temp_dir("home-reuse");
|
||||
let file = dir.join("note.md");
|
||||
fs::write(&file, "# Note").unwrap();
|
||||
let mut session = DocumentSession::default();
|
||||
let home = session.open_home();
|
||||
|
||||
let opened = session.open_reusing_home(file.clone(), true);
|
||||
|
||||
assert_eq!(opened, home);
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
assert!(!session.tabs[0].is_home());
|
||||
assert_eq!(session.tabs[0].file_path(), Some(file.as_path()));
|
||||
assert!(session.tabs[0].edit_on_open);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn opening_an_already_open_file_from_home_drops_the_blank_tab() {
|
||||
let dir = temp_dir("home-dedupe");
|
||||
let first = dir.join("one.md");
|
||||
fs::write(&first, "one").unwrap();
|
||||
let mut session = DocumentSession::default();
|
||||
let first_id = session.open(first.clone(), false);
|
||||
let home = session.open_home();
|
||||
|
||||
let opened = session.open_reusing_home(first, false);
|
||||
|
||||
assert_eq!(opened, first_id);
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
assert_eq!(session.active_id, Some(first_id));
|
||||
assert!(!session.tabs.iter().any(|tab| tab.id == home));
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn home_tab_cannot_be_detached_or_relocated() {
|
||||
let dir = temp_dir("home-guard");
|
||||
let mut session = DocumentSession::default();
|
||||
let home = session.open_home();
|
||||
|
||||
assert!(session.detach(home).is_none());
|
||||
assert!(!session.relocate(home, dir.join("note.md")));
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_the_last_file_leaves_a_home_tab() {
|
||||
let dir = temp_dir("close-home");
|
||||
let file = dir.join("note.md");
|
||||
fs::write(&file, "").unwrap();
|
||||
let mut session = DocumentSession::default();
|
||||
let file_id = session.open(file, false);
|
||||
|
||||
assert!(session.close_with_home_fallback(file_id));
|
||||
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
assert!(session.active().unwrap().is_home());
|
||||
assert_eq!(session.active_id, Some(session.tabs[0].id));
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn opened_paths_drop_windows_verbatim_prefix() {
|
||||
@@ -232,9 +414,13 @@ mod tests {
|
||||
|
||||
session.open(file.clone(), false);
|
||||
|
||||
let stored = session.tabs[0].path.to_string_lossy().to_string();
|
||||
let stored = session.tabs[0]
|
||||
.file_path()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
assert!(!stored.starts_with(r"\\?\"), "verbatim prefix leaked: {stored}");
|
||||
assert!(session.tabs[0].path.ends_with("note.md"));
|
||||
assert!(session.tabs[0].file_path().unwrap().ends_with("note.md"));
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user