Skip to content

Commit 4f537ea

Browse files
Optionally restore last session on start
Closes: #534 This patch adds a config option to restore closed sessions on start. So, if a user had three tabs opened, all three tabs would be restored when Files is launched again.
1 parent e997bbb commit 4f537ea

File tree

5 files changed

+97
-36
lines changed

5 files changed

+97
-36
lines changed

i18n/en/cosmic_files.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,10 @@ match-desktop = Match desktop
197197
dark = Dark
198198
light = Light
199199
200+
### Session
201+
session = Session
202+
restore-session = Restore previous session on start
203+
200204
# Context menu
201205
add-to-sidebar = Add to sidebar
202206
compress = Compress

src/app.rs

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use wayland_client::{protocol::wl_output::WlOutput, Proxy};
5858

5959
use crate::{
6060
clipboard::{ClipboardCopy, ClipboardKind, ClipboardPaste},
61-
config::{AppTheme, Config, DesktopConfig, Favorite, IconSizes, TabConfig},
61+
config::{AppTheme, Config, DesktopConfig, Favorite, IconSizes, SessionConfig, TabConfig},
6262
desktop_dir, fl, home_dir,
6363
key_bind::key_binds,
6464
localize::LANGUAGE_SORTER,
@@ -71,7 +71,7 @@ use crate::{
7171
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
7272
};
7373

74-
#[derive(Clone, Debug)]
74+
#[derive(Clone, Copy, Debug, PartialEq)]
7575
pub enum Mode {
7676
App,
7777
Desktop,
@@ -305,10 +305,12 @@ pub enum Message {
305305
Rename(Option<Entity>),
306306
ReplaceResult(ReplaceResult),
307307
RestoreFromTrash(Option<Entity>),
308+
SaveSession,
308309
SearchActivate,
309310
SearchClear,
310311
SearchInput(String),
311312
SearchSubmit,
313+
SessionConfig(SessionConfig),
312314
SystemThemeModeChange(cosmic_theme::ThemeMode),
313315
TabActivate(Entity),
314316
TabNext,
@@ -1140,27 +1142,43 @@ impl App {
11401142

11411143
fn settings(&self) -> Element<Message> {
11421144
// TODO: Should dialog be updated here too?
1143-
widget::settings::view_column(vec![widget::settings::section()
1144-
.title(fl!("appearance"))
1145-
.add({
1146-
let app_theme_selected = match self.config.app_theme {
1147-
AppTheme::Dark => 1,
1148-
AppTheme::Light => 2,
1149-
AppTheme::System => 0,
1150-
};
1151-
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
1152-
&self.app_themes,
1153-
Some(app_theme_selected),
1154-
move |index| {
1155-
Message::AppTheme(match index {
1156-
1 => AppTheme::Dark,
1157-
2 => AppTheme::Light,
1158-
_ => AppTheme::System,
1159-
})
1160-
},
1161-
))
1162-
})
1163-
.into()])
1145+
widget::settings::view_column(vec![
1146+
widget::settings::section()
1147+
.title(fl!("appearance"))
1148+
.add({
1149+
let app_theme_selected = match self.config.app_theme {
1150+
AppTheme::Dark => 1,
1151+
AppTheme::Light => 2,
1152+
AppTheme::System => 0,
1153+
};
1154+
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
1155+
&self.app_themes,
1156+
Some(app_theme_selected),
1157+
move |index| {
1158+
Message::AppTheme(match index {
1159+
1 => AppTheme::Dark,
1160+
2 => AppTheme::Light,
1161+
_ => AppTheme::System,
1162+
})
1163+
},
1164+
))
1165+
})
1166+
.into(),
1167+
widget::settings::section()
1168+
.title(fl!("session"))
1169+
.add(
1170+
widget::settings::item::builder(fl!("restore-session")).toggler(
1171+
self.config.session.restore,
1172+
move |restore| {
1173+
Message::SessionConfig(SessionConfig {
1174+
restore,
1175+
..Default::default()
1176+
})
1177+
},
1178+
),
1179+
)
1180+
.into(),
1181+
])
11641182
.into()
11651183
}
11661184
}
@@ -2326,6 +2344,24 @@ impl Application for App {
23262344
self.operation(Operation::Restore { paths });
23272345
}
23282346
}
2347+
Message::SaveSession => {
2348+
if self.config.session.restore && self.mode == Mode::App {
2349+
let session = SessionConfig {
2350+
tabs: Some(
2351+
self.tab_model
2352+
.iter()
2353+
.filter_map(|entity| {
2354+
self.tab_model
2355+
.data::<Tab>(entity)
2356+
.map(|tab| tab.location.clone())
2357+
})
2358+
.collect(),
2359+
),
2360+
..self.config.session
2361+
};
2362+
config_set!(session, session);
2363+
}
2364+
}
23292365
Message::SearchActivate => {
23302366
self.search_active = true;
23312367
return widget::text_input::focus(self.search_id.clone());
@@ -2355,6 +2391,9 @@ impl Application for App {
23552391
return self.search();
23562392
}
23572393
}
2394+
Message::SessionConfig(session) => {
2395+
config_set!(session, session);
2396+
}
23582397
Message::SystemThemeModeChange(_theme_mode) => {
23592398
return self.update_config();
23602399
}
@@ -2429,9 +2468,12 @@ impl Application for App {
24292468
// Remove item
24302469
self.tab_model.remove(entity);
24312470

2432-
// If that was the last tab, close window
2471+
// If that was the last tab, close window and serialize empty session if necessary
24332472
if self.tab_model.iter().next().is_none() {
2434-
return window::close(window::Id::MAIN);
2473+
return Command::batch([
2474+
self.update(Message::SaveSession),
2475+
window::close(window::Id::MAIN),
2476+
]);
24352477
}
24362478

24372479
return Command::batch([self.update_title(), self.update_watcher()]);
@@ -2695,6 +2737,7 @@ impl Application for App {
26952737
if let Some(window_id) = self.window_id_opt.take() {
26962738
return Command::batch([
26972739
window::close(window_id),
2740+
self.update(Message::SaveSession),
26982741
Command::perform(async move { message::app(Message::MaybeExit) }, |x| x),
26992742
]);
27002743
}

src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use cosmic::{
99
};
1010
use serde::{Deserialize, Serialize};
1111

12-
use crate::{app::App, tab::View};
12+
use crate::{
13+
app::App,
14+
tab::{Location, View},
15+
};
1316

1417
pub const CONFIG_VERSION: u64 = 1;
1518

@@ -97,6 +100,7 @@ pub struct Config {
97100
pub favorites: Vec<Favorite>,
98101
pub show_details: bool,
99102
pub tab: TabConfig,
103+
pub session: SessionConfig,
100104
}
101105

102106
impl Config {
@@ -144,6 +148,7 @@ impl Default for Config {
144148
],
145149
show_details: false,
146150
tab: TabConfig::default(),
151+
session: SessionConfig::default(),
147152
}
148153
}
149154
}
@@ -229,3 +234,9 @@ impl IconSizes {
229234
percent!(self.grid, ICON_SIZE_GRID) as _
230235
}
231236
}
237+
238+
#[derive(Clone, Debug, Default, PartialEq, Eq, CosmicConfigEntry, Deserialize, Serialize)]
239+
pub struct SessionConfig {
240+
pub restore: bool,
241+
pub tabs: Option<Vec<Location>>,
242+
}

src/lib.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,25 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
9696

9797
localize::localize();
9898

99-
let (config_handler, config) = Config::load();
99+
let (config_handler, mut config) = Config::load();
100100

101101
let mut locations = Vec::new();
102102
for arg in env::args().skip(1) {
103-
let location = if &arg == "--trash" {
104-
Location::Trash
105-
} else {
106-
match fs::canonicalize(&arg) {
107-
Ok(absolute) => Location::Path(absolute),
103+
match &*arg {
104+
"--trash" => locations.push(Location::Trash),
105+
// Override session regardless of config
106+
"--no-session" => _ = config.session.tabs.take(),
107+
path => match fs::canonicalize(path) {
108+
Ok(absolute) => locations.push(Location::Path(absolute)),
108109
Err(err) => {
109110
log::warn!("failed to canonicalize {:?}: {}", arg, err);
110111
continue;
111112
}
112-
}
113-
};
114-
locations.push(location);
113+
}
114+
}
115+
}
116+
if let Some(session) = config.session.restore.then(|| config.session.tabs.take()).flatten() {
117+
locations.extend(session);
115118
}
116119

117120
let mut settings = Settings::default();

src/tab.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ pub fn scan_desktop(
885885
items
886886
}
887887

888-
#[derive(Clone, Debug, Eq, PartialEq)]
888+
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
889889
pub enum Location {
890890
Desktop(PathBuf, String),
891891
Network(String, String),

0 commit comments

Comments
 (0)