Skip to content

Commit 42175a9

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. Updates ------- * Searches and mounts aren't serialized anymore. I'm not sure if it makes sense to do so, but likely a user would raise an issue if they want it.
1 parent 28b4dda commit 42175a9

File tree

5 files changed

+111
-36
lines changed

5 files changed

+111
-36
lines changed

i18n/en/cosmic_files.ftl

+4
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

+79-25
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
fl, home_dir,
6363
key_bind::key_binds,
6464
localize::LANGUAGE_SORTER,
@@ -69,7 +69,7 @@ use crate::{
6969
tab::{self, HeadingOptions, ItemMetadata, Location, Tab, HOVER_DURATION},
7070
};
7171

72-
#[derive(Clone, Debug)]
72+
#[derive(Clone, Copy, Debug, PartialEq)]
7373
pub enum Mode {
7474
App,
7575
Desktop,
@@ -303,9 +303,11 @@ pub enum Message {
303303
Rename(Option<Entity>),
304304
ReplaceResult(ReplaceResult),
305305
RestoreFromTrash(Option<Entity>),
306+
SaveSession,
306307
SearchActivate,
307308
SearchClear,
308309
SearchInput(String),
310+
SessionConfig(SessionConfig),
309311
SystemThemeModeChange(cosmic_theme::ThemeMode),
310312
TabActivate(Entity),
311313
TabNext,
@@ -1163,27 +1165,43 @@ impl App {
11631165

11641166
fn settings(&self) -> Element<Message> {
11651167
// TODO: Should dialog be updated here too?
1166-
widget::settings::view_column(vec![widget::settings::section()
1167-
.title(fl!("appearance"))
1168-
.add({
1169-
let app_theme_selected = match self.config.app_theme {
1170-
AppTheme::Dark => 1,
1171-
AppTheme::Light => 2,
1172-
AppTheme::System => 0,
1173-
};
1174-
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
1175-
&self.app_themes,
1176-
Some(app_theme_selected),
1177-
move |index| {
1178-
Message::AppTheme(match index {
1179-
1 => AppTheme::Dark,
1180-
2 => AppTheme::Light,
1181-
_ => AppTheme::System,
1182-
})
1183-
},
1184-
))
1185-
})
1186-
.into()])
1168+
widget::settings::view_column(vec![
1169+
widget::settings::section()
1170+
.title(fl!("appearance"))
1171+
.add({
1172+
let app_theme_selected = match self.config.app_theme {
1173+
AppTheme::Dark => 1,
1174+
AppTheme::Light => 2,
1175+
AppTheme::System => 0,
1176+
};
1177+
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
1178+
&self.app_themes,
1179+
Some(app_theme_selected),
1180+
move |index| {
1181+
Message::AppTheme(match index {
1182+
1 => AppTheme::Dark,
1183+
2 => AppTheme::Light,
1184+
_ => AppTheme::System,
1185+
})
1186+
},
1187+
))
1188+
})
1189+
.into(),
1190+
widget::settings::section()
1191+
.title(fl!("session"))
1192+
.add(
1193+
widget::settings::item::builder(fl!("restore-session")).toggler(
1194+
self.config.session.restore,
1195+
move |restore| {
1196+
Message::SessionConfig(SessionConfig {
1197+
restore,
1198+
..Default::default()
1199+
})
1200+
},
1201+
),
1202+
)
1203+
.into(),
1204+
])
11871205
.into()
11881206
}
11891207
}
@@ -2339,6 +2357,35 @@ impl Application for App {
23392357
self.operation(Operation::Restore { paths });
23402358
}
23412359
}
2360+
Message::SaveSession => {
2361+
if self.config.session.restore && self.mode == Mode::App {
2362+
let session = SessionConfig {
2363+
tabs: Some(
2364+
self.tab_model
2365+
.iter()
2366+
.filter_map(|entity| {
2367+
match self
2368+
.tab_model
2369+
.data::<Tab>(entity)
2370+
.map(|tab| &tab.location)
2371+
{
2372+
// Location's serialization implementation skips variants
2373+
// such as mounts
2374+
// However, we'd still clone all Locations here only for
2375+
// some to be skipped, so it's best to avoid the clone
2376+
loc @ Location::Path(_)
2377+
| Location::Recents
2378+
| Location::Trash => Some(loc.clone()),
2379+
_ => None,
2380+
}
2381+
})
2382+
.collect(),
2383+
),
2384+
..self.config.session
2385+
};
2386+
config_set!(session, session);
2387+
}
2388+
}
23422389
Message::SearchActivate => {
23432390
return if self.search_get().is_none() {
23442391
self.search_set(Some(String::new()))
@@ -2352,6 +2399,9 @@ impl Application for App {
23522399
Message::SearchInput(input) => {
23532400
return self.search_set(Some(input));
23542401
}
2402+
Message::SessionConfig(session) => {
2403+
config_set!(session, session);
2404+
}
23552405
Message::SystemThemeModeChange(_theme_mode) => {
23562406
return self.update_config();
23572407
}
@@ -2419,9 +2469,12 @@ impl Application for App {
24192469
// Remove item
24202470
self.tab_model.remove(entity);
24212471

2422-
// If that was the last tab, close window
2472+
// If that was the last tab, close window and serialize empty session if necessary
24232473
if self.tab_model.iter().next().is_none() {
2424-
return window::close(window::Id::MAIN);
2474+
return Command::batch([
2475+
self.update(Message::SaveSession),
2476+
window::close(window::Id::MAIN),
2477+
]);
24252478
}
24262479

24272480
return Command::batch([self.update_title(), self.update_watcher()]);
@@ -2681,6 +2734,7 @@ impl Application for App {
26812734
if let Some(window_id) = self.window_id_opt.take() {
26822735
return Command::batch([
26832736
window::close(window_id),
2737+
self.update(Message::SaveSession),
26842738
Command::perform(async move { message::app(Message::MaybeExit) }, |x| x),
26852739
]);
26862740
}

src/config.rs

+12-1
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

+12-9
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

+4-1
Original file line numberDiff line numberDiff line change
@@ -860,12 +860,15 @@ pub fn scan_desktop(
860860
items
861861
}
862862

863-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
863+
#[derive(Clone, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
864864
pub enum Location {
865+
#[serde(skip)]
865866
Desktop(PathBuf, String, DesktopConfig),
867+
#[serde(skip)]
866868
Network(String, String),
867869
Path(PathBuf),
868870
Recents,
871+
#[serde(skip)]
869872
Search(PathBuf, String, bool, Instant),
870873
Trash,
871874
}

0 commit comments

Comments
 (0)