Skip to content

Commit

Permalink
Optionally restore last session
Browse files Browse the repository at this point in the history
Closes: pop-os#534
  • Loading branch information
joshuamegnauth54 committed Oct 3, 2024
1 parent 69a53d4 commit f3433f5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
20 changes: 20 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ pub enum Message {
Rename(Option<Entity>),
ReplaceResult(ReplaceResult),
RestoreFromTrash(Option<Entity>),
SaveSession,
SearchActivate,
SearchClear,
SearchInput(String),
Expand Down Expand Up @@ -2134,6 +2135,24 @@ impl Application for App {
self.operation(Operation::Restore { paths });
}
}
Message::SaveSession => {
if self.config.session.restore {
let session = crate::config::Session {
tabs: Some(
self.tab_model
.iter()
.filter_map(|entity| {
self.tab_model
.data::<Tab>(entity)
.map(|tab| tab.location.clone())
})
.collect(),
),
..self.config.session
};
config_set!(session, session);
}
}
Message::SearchActivate => {
self.search_active = true;
return widget::text_input::focus(self.search_id.clone());
Expand Down Expand Up @@ -2480,6 +2499,7 @@ impl Application for App {
if let Some(window_id) = self.window_id_opt.take() {
return Command::batch([
window::close(window_id),
Command::perform(async move { message::app(Message::Con) }, |x| x),
Command::perform(async move { message::app(Message::MaybeExit) }, |x| x),
]);
}
Expand Down
9 changes: 8 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};

use crate::{
app::App,
tab::{HeadingOptions, View},
tab::{HeadingOptions, Location, View},
};

pub const CONFIG_VERSION: u64 = 1;
Expand Down Expand Up @@ -97,6 +97,7 @@ pub struct Config {
pub app_theme: AppTheme,
pub favorites: Vec<Favorite>,
pub tab: TabConfig,
pub session: Session,
}

impl Config {
Expand Down Expand Up @@ -212,3 +213,9 @@ impl IconSizes {
percent!(self.grid, ICON_SIZE_GRID) as _
}
}

#[derive(Clone, Copy, Debug, Default, CosmicConfigEntry, Deserialize, Serialize)]
pub struct Session {
pub restore: bool,
pub tabs: Option<Vec<Location>>,
}
23 changes: 15 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,29 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {

localize::localize();

let (config_handler, config) = Config::load();
let (config_handler, mut config) = Config::load();

let mut locations = Vec::new();
for arg in env::args().skip(1) {
let location = if &arg == "--trash" {
Location::Trash
} else {
match fs::canonicalize(&arg) {
Ok(absolute) => Location::Path(absolute),
match &*arg {
"--trash" => locations.push(Location::Trash),
// Override session regardless of config
"--no-session" => {
_ = config.session.tabs.take();
}
path => {
match fs::canonicalize(path) {
Ok(absolute) => locations.push(Location::Path(absolute)),
Err(err) => {
log::warn!("failed to canonicalize {:?}: {}", arg, err);
continue;
}
}
};
locations.push(location);
}
}
}
if let Some(session) = config.session.restore.then(|| config.session.tabs.take()).flatten() {
locations.extend(session)
}

let mut settings = Settings::default();
Expand Down
2 changes: 1 addition & 1 deletion src/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ pub fn scan_network(uri: &str, mounters: Mounters, sizes: IconSizes) -> Vec<Item
Vec::new()
}

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum Location {
Path(PathBuf),
Search(PathBuf, String),
Expand Down

0 comments on commit f3433f5

Please sign in to comment.