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 6, 2024
1 parent 69a53d4 commit 9650bf4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
2 changes: 2 additions & 0 deletions i18n/en/cosmic_files.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ show-details = Show details
settings = Settings
settings-tab = Tab
settings-show-hidden = Show hidden files
settings-general = General
settings-restore-session = Restore session on start
default-view = Default view
icon-size-list = Icon size (list)
icon-size-grid = Icon size (grid)
Expand Down
39 changes: 38 additions & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use crate::{
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
};

#[derive(Clone, Debug)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Mode {
App,
Desktop,
Expand Down Expand Up @@ -297,10 +297,12 @@ pub enum Message {
Rename(Option<Entity>),
ReplaceResult(ReplaceResult),
RestoreFromTrash(Option<Entity>),
SaveSession,
SearchActivate,
SearchClear,
SearchInput(String),
SearchSubmit,
SessionConfig(crate::config::Session),
SystemThemeModeChange(cosmic_theme::ThemeMode),
TabActivate(Entity),
TabNext,
Expand Down Expand Up @@ -1127,6 +1129,19 @@ impl App {
)
})
.into(),
widget::settings::view_section(fl!("settings-general"))
.add(
widget::settings::item::builder(fl!("settings-restore-session")).toggler(
self.config.session.restore,
move |restore| {
Message::SessionConfig(crate::config::Session {
restore,
..Default::default()
})
},
),
)
.into(),
])
.into()
}
Expand Down Expand Up @@ -2134,6 +2149,24 @@ impl Application for App {
self.operation(Operation::Restore { paths });
}
}
Message::SaveSession => {
if self.config.session.restore && self.mode == Mode::App {
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 @@ -2163,6 +2196,9 @@ impl Application for App {
return self.search();
}
}
Message::SessionConfig(session) => {
config_set!(session, session);
}
Message::SystemThemeModeChange(_theme_mode) => {
return self.update_config();
}
Expand Down Expand Up @@ -2480,6 +2516,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::SaveSession) }, |x| x),
Command::perform(async move { message::app(Message::MaybeExit) }, |x| x),
]);
}
Expand Down
10 changes: 9 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 @@ -142,6 +143,7 @@ impl Default for Config {
Favorite::Videos,
],
tab: TabConfig::default(),
session: Session::default(),
}
}
}
Expand Down Expand Up @@ -212,3 +214,9 @@ impl IconSizes {
percent!(self.grid, ICON_SIZE_GRID) as _
}
}

#[derive(Clone, Debug, Default, PartialEq, Eq, 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 9650bf4

Please sign in to comment.