fix: remember edit mode and scroll position per tab
Edit mode used to live on the window, so switching tabs carried the mode across documents, and the viewport stayed at whatever pixel the previous document had scrolled to. - DocumentTab now owns editing and scroll_ratio. - The page reports edit-mode and scroll changes by tab id; Rust restores them when the tab is activated. Reports carry the id so a late report cannot land on the tab that just became active. - Detaching a tab keeps its edit mode instead of forcing preview.
This commit is contained in:
@@ -1,14 +1,19 @@
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct DocumentTab {
|
||||
pub id: u64,
|
||||
/// `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,
|
||||
/// Edit mode belongs to the tab, so switching tabs restores the mode the
|
||||
/// tab was left in instead of carrying one global mode across documents.
|
||||
pub editing: bool,
|
||||
/// Viewport position as a 0.0 (top) ..= 1.0 (bottom) fraction. Stores a
|
||||
/// ratio rather than pixels so zoom and reflow do not move the reader.
|
||||
pub scroll_ratio: f32,
|
||||
}
|
||||
|
||||
impl DocumentTab {
|
||||
@@ -37,7 +42,7 @@ impl DocumentSession {
|
||||
.find(|tab| tab.path.as_deref() == Some(path.as_path()))
|
||||
{
|
||||
tab.missing = !path.exists();
|
||||
tab.edit_on_open |= edit_on_open;
|
||||
tab.editing |= edit_on_open;
|
||||
self.active_id = Some(tab.id);
|
||||
return tab.id;
|
||||
}
|
||||
@@ -50,7 +55,8 @@ impl DocumentSession {
|
||||
path: Some(path),
|
||||
dirty: false,
|
||||
missing,
|
||||
edit_on_open,
|
||||
editing: edit_on_open,
|
||||
scroll_ratio: 0.0,
|
||||
});
|
||||
self.active_id = Some(id);
|
||||
id
|
||||
@@ -65,7 +71,8 @@ impl DocumentSession {
|
||||
path: None,
|
||||
dirty: false,
|
||||
missing: false,
|
||||
edit_on_open: false,
|
||||
editing: false,
|
||||
scroll_ratio: 0.0,
|
||||
});
|
||||
self.active_id = Some(id);
|
||||
id
|
||||
@@ -92,7 +99,7 @@ impl DocumentSession {
|
||||
.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;
|
||||
existing.editing |= 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);
|
||||
@@ -109,7 +116,8 @@ impl DocumentSession {
|
||||
tab.path = Some(path);
|
||||
tab.missing = missing;
|
||||
tab.dirty = false;
|
||||
tab.edit_on_open = edit_on_open;
|
||||
tab.editing = edit_on_open;
|
||||
tab.scroll_ratio = 0.0;
|
||||
active_id
|
||||
}
|
||||
|
||||
@@ -195,6 +203,33 @@ impl DocumentSession {
|
||||
self.active().is_some_and(DocumentTab::is_home)
|
||||
}
|
||||
|
||||
/// Remember the viewport of the active tab (0.0 top .. 1.0 bottom).
|
||||
pub fn set_active_scroll_ratio(&mut self, ratio: f32) {
|
||||
if !ratio.is_finite() {
|
||||
return;
|
||||
}
|
||||
if let Some(tab) = self.active_mut() {
|
||||
tab.scroll_ratio = ratio.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Store the viewport by tab id; late scroll reports must not land on a tab
|
||||
/// that was activated in the meantime.
|
||||
pub fn set_tab_scroll_ratio(&mut self, id: u64, ratio: f32) {
|
||||
if !ratio.is_finite() {
|
||||
return;
|
||||
}
|
||||
if let Some(tab) = self.get_mut(id) {
|
||||
tab.scroll_ratio = ratio.clamp(0.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_tab_editing(&mut self, id: u64, editing: bool) {
|
||||
if let Some(tab) = self.get_mut(id) {
|
||||
tab.editing = editing;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self, id: u64) -> Option<&mut DocumentTab> {
|
||||
self.tabs.iter_mut().find(|tab| tab.id == id)
|
||||
}
|
||||
@@ -285,7 +320,7 @@ mod tests {
|
||||
assert_eq!(first, second);
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
assert_eq!(session.active_id, Some(first));
|
||||
assert!(session.tabs[0].edit_on_open);
|
||||
assert!(session.tabs[0].editing);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
@@ -354,7 +389,7 @@ mod tests {
|
||||
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);
|
||||
assert!(session.tabs[0].editing);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
@@ -404,6 +439,30 @@ mod tests {
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_mode_and_scroll_position_are_remembered_per_tab() {
|
||||
let dir = temp_dir("per-tab-view");
|
||||
let mut session = DocumentSession::default();
|
||||
let first = session.open(dir.join("one.md"), true);
|
||||
session.set_active_scroll_ratio(0.75);
|
||||
let second = session.open(dir.join("two.md"), false);
|
||||
|
||||
assert_ne!(first, second);
|
||||
assert!(!session.active().unwrap().editing);
|
||||
assert_eq!(session.active().unwrap().scroll_ratio, 0.0);
|
||||
|
||||
assert!(session.activate(first));
|
||||
assert!(session.active().unwrap().editing);
|
||||
assert_eq!(session.active().unwrap().scroll_ratio, 0.75);
|
||||
|
||||
// Late reports address the tab by id, not by whichever tab is active.
|
||||
session.set_tab_scroll_ratio(second, 0.4);
|
||||
assert!(session.activate(second));
|
||||
assert_eq!(session.active().unwrap().scroll_ratio, 0.4);
|
||||
assert!(!session.active().unwrap().editing);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn opened_paths_drop_windows_verbatim_prefix() {
|
||||
@@ -419,7 +478,10 @@ mod tests {
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.to_string();
|
||||
assert!(!stored.starts_with(r"\\?\"), "verbatim prefix leaked: {stored}");
|
||||
assert!(
|
||||
!stored.starts_with(r"\\?\"),
|
||||
"verbatim prefix leaked: {stored}"
|
||||
);
|
||||
assert!(session.tabs[0].file_path().unwrap().ends_with("note.md"));
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user