feat: open a tab in a new window from its context menu
This commit is contained in:
@@ -65,6 +65,29 @@ impl DocumentSession {
|
||||
true
|
||||
}
|
||||
|
||||
pub fn with_tab(tab: DocumentTab) -> Self {
|
||||
let id = tab.id;
|
||||
Self {
|
||||
tabs: vec![tab],
|
||||
active_id: Some(id),
|
||||
next_id: id,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn detach(&mut self, id: u64) -> Option<DocumentTab> {
|
||||
let index = self.tabs.iter().position(|tab| tab.id == id)?;
|
||||
let was_active = self.active_id == Some(id);
|
||||
let tab = self.tabs.remove(index);
|
||||
if was_active {
|
||||
self.active_id = self
|
||||
.tabs
|
||||
.get(index)
|
||||
.or_else(|| index.checked_sub(1).and_then(|i| self.tabs.get(i)))
|
||||
.map(|tab| tab.id);
|
||||
}
|
||||
Some(tab)
|
||||
}
|
||||
|
||||
pub fn active(&self) -> Option<&DocumentTab> {
|
||||
let id = self.active_id?;
|
||||
self.tabs.iter().find(|tab| tab.id == id)
|
||||
@@ -180,6 +203,25 @@ mod tests {
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detaching_a_tab_keeps_source_and_adopts_it_in_new_session() {
|
||||
let dir = temp_dir("detach");
|
||||
let mut session = DocumentSession::default();
|
||||
let first = session.open(dir.join("one.md"), false);
|
||||
let second = session.open(dir.join("two.md"), false);
|
||||
session.activate(first);
|
||||
|
||||
let moved = session.detach(second).unwrap();
|
||||
assert_eq!(session.active_id, Some(first));
|
||||
assert_eq!(session.tabs.len(), 1);
|
||||
|
||||
let adopted = DocumentSession::with_tab(moved);
|
||||
assert_eq!(adopted.active_id, Some(second));
|
||||
assert_eq!(adopted.tabs.len(), 1);
|
||||
assert_eq!(adopted.tabs[0].id, second);
|
||||
let _ = fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
#[test]
|
||||
fn opened_paths_drop_windows_verbatim_prefix() {
|
||||
|
||||
Reference in New Issue
Block a user