Skip to content

Commit

Permalink
Merge pull request #4 from LegitCamper/3-create-stack-of-previous-tab…
Browse files Browse the repository at this point in the history
…s-to-return-to-if-current-tab-is-closed

add tab history
  • Loading branch information
LegitCamper authored Aug 19, 2024
2 parents 34067e5 + 20e71e2 commit abd97d6
Showing 1 changed file with 11 additions and 16 deletions.
27 changes: 11 additions & 16 deletions src/engines/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl<Info: TabInfo> Tab<Info> {

pub struct Tabs<Info: TabInfo> {
tabs: Vec<Tab<Info>>,
current: u32,
history: Vec<u32>,
}

impl<Info: TabInfo> Default for Tabs<Info> {
Expand All @@ -110,7 +110,7 @@ impl<Info: TabInfo> Tabs<Info> {
pub fn new() -> Self {
Self {
tabs: Vec::new(),
current: 0,
history: Vec::new(),
}
}

Expand All @@ -131,11 +131,14 @@ impl<Info: TabInfo> Tabs<Info> {
}

pub fn get_current_id(&self) -> u32 {
self.current
self.history
.last()
.expect("No tab in history to get current from")
.to_owned()
}

pub fn set_current_id(&mut self, id: u32) {
self.current = id
self.history.push(id)
}

pub fn tabs(&self) -> &Vec<Tab<Info>> {
Expand All @@ -150,26 +153,18 @@ impl<Info: TabInfo> Tabs<Info> {

/// Returns the newly active tab
pub fn remove(&mut self, id: u32) -> u32 {
// TODO: have list of prevous tabs instead
if self.current == id {
for tab in self.tabs.iter().rev() {
if tab.id != id {
self.current = tab.id;
break;
}
}
}
self.history.retain(|tab_id| *tab_id != id);

self.tabs.retain(|tab| tab.id != id);
self.current
self.get_current_id()
}

pub fn get_current(&self) -> &Tab<Info> {
self.get(self.current)
self.get(self.get_current_id())
}

pub fn get_current_mut(&mut self) -> &mut Tab<Info> {
self.get_mut(self.current)
self.get_mut(self.get_current_id())
}

pub fn get(&self, id: u32) -> &Tab<Info> {
Expand Down

0 comments on commit abd97d6

Please sign in to comment.