fix: cd on the initial creation of a tab should not be added to the backstack (#2842)

This commit is contained in:
三咲雅 misaki masa 2025-06-05 20:11:37 +08:00 committed by GitHub
parent 145349b0e6
commit d55e002d5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 43 additions and 32 deletions

View file

@ -3,7 +3,7 @@ use yazi_macro::render;
use yazi_proxy::AppProxy; use yazi_proxy::AppProxy;
use yazi_shared::{event::CmdCow, url::Url}; use yazi_shared::{event::CmdCow, url::Url};
use crate::{mgr::Tabs, tab::Tab}; use crate::{mgr::Tabs, tab::{Tab, commands::CdSource}};
const MAX_TABS: usize = 9; const MAX_TABS: usize = 9;
@ -35,15 +35,15 @@ impl Tabs {
let mut tab = Tab::default(); let mut tab = Tab::default();
if !opt.current { if !opt.current {
tab.cd(opt.url); tab.cd((opt.url, CdSource::Tab));
} else if let Some(h) = self.active().hovered() { } else if let Some(h) = self.active().hovered() {
tab.pref = self.active().pref.clone(); tab.pref = self.active().pref.clone();
tab.apply_files_attrs(); tab.apply_files_attrs();
tab.reveal(h.url.to_regular()); tab.reveal((h.url.to_regular(), CdSource::Tab));
} else { } else {
tab.pref = self.active().pref.clone(); tab.pref = self.active().pref.clone();
tab.apply_files_attrs(); tab.apply_files_attrs();
tab.cd(self.active().cwd().to_regular()); tab.cd((self.active().cwd().to_regular(), CdSource::Tab));
} }
self.items.insert(self.cursor + 1, tab); self.items.insert(self.cursor + 1, tab);

View file

@ -6,7 +6,7 @@ use yazi_fs::File;
use yazi_proxy::MgrProxy; use yazi_proxy::MgrProxy;
use yazi_shared::{Id, url::Url}; use yazi_shared::{Id, url::Url};
use crate::tab::{Folder, Tab}; use crate::tab::{Folder, Tab, commands::CdSource};
pub struct Tabs { pub struct Tabs {
pub cursor: usize, pub cursor: usize,
@ -21,9 +21,9 @@ impl Tabs {
for (i, tab) in tabs.iter_mut().enumerate() { for (i, tab) in tabs.iter_mut().enumerate() {
let file = &BOOT.files[i]; let file = &BOOT.files[i];
if file.is_empty() { if file.is_empty() {
tab.cd(Url::from(&BOOT.cwds[i])); tab.cd((Url::from(&BOOT.cwds[i]), CdSource::Tab));
} else { } else {
tab.reveal(Url::from(BOOT.cwds[i].join(file))); tab.reveal((Url::from(BOOT.cwds[i].join(file)), CdSource::Tab));
} }
} }
tabs tabs

View file

@ -1,14 +1,12 @@
use yazi_shared::event::CmdCow; use yazi_shared::event::CmdCow;
use super::cd::CdSource;
use crate::tab::Tab; use crate::tab::Tab;
impl Tab { impl Tab {
pub fn back(&mut self, _: CmdCow) { pub fn back(&mut self, _: CmdCow) {
if self.current.url.is_regular() {
self.backstack.push(&self.current.url);
}
if let Some(u) = self.backstack.shift_backward().cloned() { if let Some(u) = self.backstack.shift_backward().cloned() {
self.cd((u, super::cd::OptSource::Back)); self.cd((u, CdSource::Back));
} }
} }
} }

View file

@ -13,14 +13,14 @@ use crate::tab::Tab;
struct Opt { struct Opt {
target: Url, target: Url,
source: OptSource, source: CdSource,
interactive: bool, interactive: bool,
} }
impl From<CmdCow> for Opt { impl From<CmdCow> for Opt {
fn from(mut c: CmdCow) -> Self { fn from(mut c: CmdCow) -> Self {
Self { Self {
source: OptSource::Cd, source: CdSource::Cd,
interactive: c.bool("interactive"), interactive: c.bool("interactive"),
..Self::from(c.take_first_url().unwrap_or_default()) ..Self::from(c.take_first_url().unwrap_or_default())
} }
@ -28,11 +28,11 @@ impl From<CmdCow> for Opt {
} }
impl From<Url> for Opt { impl From<Url> for Opt {
fn from(target: Url) -> Self { Self::from((target, OptSource::Cd)) } fn from(target: Url) -> Self { Self::from((target, CdSource::Cd)) }
} }
impl From<(Url, OptSource)> for Opt { impl From<(Url, CdSource)> for Opt {
fn from((mut target, source): (Url, OptSource)) -> Self { fn from((mut target, source): (Url, CdSource)) -> Self {
if target.is_regular() { if target.is_regular() {
target = Url::from(expand_path(&target)); target = Url::from(expand_path(&target));
} }
@ -60,6 +60,16 @@ impl Tab {
self.history.insert(rep.url.to_owned(), rep); self.history.insert(rep.url.to_owned(), rep);
} }
// Backstack
if opt.source.big_jump() {
if self.current.url.is_regular() {
self.backstack.push(&self.current.url);
}
if opt.target.is_regular() {
self.backstack.push(&opt.target);
}
}
// Current // Current
let rep = self.history.remove_or(&opt.target); let rep = self.history.remove_or(&opt.target);
let rep = mem::replace(&mut self.current, rep); let rep = mem::replace(&mut self.current, rep);
@ -72,11 +82,6 @@ impl Tab {
self.parent = Some(self.history.remove_or(&parent)); self.parent = Some(self.history.remove_or(&parent));
} }
// Backstack
if opt.source.big_jump() && opt.target.is_regular() {
self.backstack.push(&opt.target);
}
Pubsub::pub_from_cd(self.id, self.cwd()); Pubsub::pub_from_cd(self.id, self.cwd());
self.hover(None); self.hover(None);
@ -118,7 +123,8 @@ impl Tab {
// --- OptSource // --- OptSource
#[derive(Clone, Copy, PartialEq, Eq)] #[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum OptSource { pub enum CdSource {
Tab,
Cd, Cd,
Reveal, Reveal,
Enter, Enter,
@ -127,7 +133,7 @@ pub(super) enum OptSource {
Back, Back,
} }
impl OptSource { impl CdSource {
#[inline] #[inline]
fn big_jump(self) -> bool { self == Self::Cd || self == Self::Reveal } fn big_jump(self) -> bool { self == Self::Cd || self == Self::Reveal }
} }

View file

@ -1,5 +1,6 @@
use yazi_shared::event::CmdCow; use yazi_shared::event::CmdCow;
use super::cd::CdSource;
use crate::tab::Tab; use crate::tab::Tab;
impl Tab { impl Tab {
@ -8,6 +9,6 @@ impl Tab {
.hovered() .hovered()
.filter(|h| h.is_dir()) .filter(|h| h.is_dir())
.map(|h| h.url.to_regular()) .map(|h| h.url.to_regular())
.map(|u| self.cd((u, super::cd::OptSource::Enter))); .map(|u| self.cd((u, CdSource::Enter)));
} }
} }

View file

@ -1,14 +1,12 @@
use yazi_shared::event::CmdCow; use yazi_shared::event::CmdCow;
use super::cd::CdSource;
use crate::tab::Tab; use crate::tab::Tab;
impl Tab { impl Tab {
pub fn forward(&mut self, _: CmdCow) { pub fn forward(&mut self, _: CmdCow) {
if self.current.url.is_regular() {
self.backstack.push(&self.current.url);
}
if let Some(u) = self.backstack.shift_forward().cloned() { if let Some(u) = self.backstack.shift_forward().cloned() {
self.cd((u, super::cd::OptSource::Forward)); self.cd((u, CdSource::Forward));
} }
} }
} }

View file

@ -1,5 +1,6 @@
use yazi_shared::event::CmdCow; use yazi_shared::event::CmdCow;
use super::cd::CdSource;
use crate::tab::Tab; use crate::tab::Tab;
struct Opt; struct Opt;
@ -19,6 +20,6 @@ impl Tab {
.and_then(|h| h.url.parent_url()) .and_then(|h| h.url.parent_url())
.filter(|u| u != self.cwd()) .filter(|u| u != self.cwd())
.or_else(|| self.cwd().parent_url()) .or_else(|| self.cwd().parent_url())
.map(|u| self.cd((u.into_regular(), super::cd::OptSource::Leave))); .map(|u| self.cd((u.into_regular(), CdSource::Leave)));
} }
} }

View file

@ -2,10 +2,12 @@ use yazi_fs::{File, FilesOp, expand_path};
use yazi_proxy::MgrProxy; use yazi_proxy::MgrProxy;
use yazi_shared::{event::CmdCow, url::Url}; use yazi_shared::{event::CmdCow, url::Url};
use super::cd::CdSource;
use crate::tab::Tab; use crate::tab::Tab;
struct Opt { struct Opt {
target: Url, target: Url,
source: CdSource,
no_dummy: bool, no_dummy: bool,
} }
@ -16,11 +18,16 @@ impl From<CmdCow> for Opt {
target = Url::from(expand_path(&target)); target = Url::from(expand_path(&target));
} }
Self { target, no_dummy: c.bool("no-dummy") } Self { target, source: CdSource::Reveal, no_dummy: c.bool("no-dummy") }
} }
} }
impl From<Url> for Opt { impl From<Url> for Opt {
fn from(target: Url) -> Self { Self { target, no_dummy: false } } fn from(target: Url) -> Self { Self { target, source: CdSource::Reveal, no_dummy: false } }
}
impl From<(Url, CdSource)> for Opt {
fn from((target, source): (Url, CdSource)) -> Self { Self { target, source, no_dummy: false } }
} }
impl Tab { impl Tab {
@ -30,7 +37,7 @@ impl Tab {
return; return;
}; };
self.cd((parent.clone(), super::cd::OptSource::Reveal)); self.cd((parent.clone(), opt.source));
self.current.hover(child.as_urn()); self.current.hover(child.as_urn());
if !opt.no_dummy && self.hovered().is_none_or(|f| &child != f.urn()) { if !opt.no_dummy && self.hovered().is_none_or(|f| &child != f.urn()) {