Nacre/src/store/session.rs

113 lines
3.4 KiB
Rust

//! The open windows and tabs, saved so they can be restored after a restart or a crash, and
//! the list of recently closed tabs.
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use crate::util;
/// How many closed tabs "Reopen closed tab" remembers.
const MAX_CLOSED: usize = 25;
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct SavedTab {
pub url: String,
#[serde(default)]
pub title: String,
#[serde(default)]
pub pinned: bool,
#[serde(default)]
pub muted: bool,
/// Back/forward list of the tab and where it stood in it. Restoring loads only the current
/// entry (a web view cannot be given a history), the rest is kept for "recently closed".
#[serde(default)]
pub history: Vec<String>,
#[serde(default)]
pub history_index: usize,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct SavedWindow {
pub tabs: Vec<SavedTab>,
#[serde(default)]
pub active: usize,
#[serde(default)]
pub width: u32,
#[serde(default)]
pub height: u32,
#[serde(default)]
pub maximized: bool,
}
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
pub struct SavedSession {
#[serde(default)]
pub windows: Vec<SavedWindow>,
#[serde(default)]
pub closed: Vec<SavedTab>,
#[serde(default)]
pub saved_at: u64,
}
pub struct Session {
path: PathBuf,
/// What was on disk at start: the previous run's windows.
pub previous: SavedSession,
pub closed: Vec<SavedTab>,
last_written: Option<SavedSession>,
disabled: bool,
}
impl Session {
pub fn load(path: PathBuf, disabled: bool) -> Session {
let previous: SavedSession = if disabled { SavedSession::default() } else { util::load_json(&path) };
let closed = previous.closed.clone();
Session { path, previous, closed, last_written: None, disabled }
}
pub fn has_previous(&self) -> bool {
self.previous.windows.iter().any(|w| !w.tabs.is_empty())
}
pub fn push_closed(&mut self, tab: SavedTab) {
if tab.url.is_empty() || tab.url.starts_with("nacre://newtab") {
return;
}
self.closed.insert(0, tab);
self.closed.truncate(MAX_CLOSED);
}
pub fn pop_closed(&mut self) -> Option<SavedTab> {
if self.closed.is_empty() { None } else { Some(self.closed.remove(0)) }
}
pub fn take_closed(&mut self, index: usize) -> Option<SavedTab> {
if index < self.closed.len() { Some(self.closed.remove(index)) } else { None }
}
/// Save the current windows. Writes only when something changed.
pub fn save(&mut self, windows: Vec<SavedWindow>) {
if self.disabled {
return;
}
let mut session = SavedSession { windows, closed: self.closed.clone(), saved_at: 0 };
if self.last_written.as_ref().is_some_and(|last| last.windows == session.windows && last.closed == session.closed) {
return;
}
session.saved_at = util::now();
if let Err(error) = util::save_json(&self.path, &session) {
log::error!("could not save the session: {error}");
}
self.last_written = Some(session);
}
pub fn clear(&mut self) {
self.closed.clear();
self.previous = SavedSession::default();
self.last_written = None;
if !self.disabled {
let _ = std::fs::remove_file(&self.path);
}
}
}