|
@@ -19,7 +19,7 @@ pub fn app_id_for_launcher(id: &str) -> String {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Desktop file stem for the given launcher hash (also used as `StartupWMClass`).
|
|
/// Desktop file stem for the given launcher hash (also used as `StartupWMClass`).
|
|
|
-fn desktop_stem(id: &str) -> String {
|
|
|
|
|
|
|
+pub(crate) fn desktop_stem(id: &str) -> String {
|
|
|
app_id_for_launcher(id)
|
|
app_id_for_launcher(id)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -209,14 +209,70 @@ pub fn list_launchers() -> Result<Vec<DesktopLauncher>, String> {
|
|
|
Ok(out)
|
|
Ok(out)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn stable_id(url: &str) -> String {
|
|
|
|
|
|
|
+pub(crate) fn stable_id(url: &str) -> String {
|
|
|
let mut h = Sha256::new();
|
|
let mut h = Sha256::new();
|
|
|
h.update(url.as_bytes());
|
|
h.update(url.as_bytes());
|
|
|
let full = format!("{:x}", h.finalize());
|
|
let full = format!("{:x}", h.finalize());
|
|
|
full[..ID_HEX_LEN].to_string()
|
|
full[..ID_HEX_LEN].to_string()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn xdg_data_home() -> PathBuf {
|
|
|
|
|
|
|
+pub(crate) fn load_window_state(url: &str) -> Option<(i32, i32, bool)> {
|
|
|
|
|
+ let id = stable_id(url);
|
|
|
|
|
+ let data_home = xdg_data_home();
|
|
|
|
|
+ let desktop_path = data_home
|
|
|
|
|
+ .join("applications")
|
|
|
|
|
+ .join(format!("{}.desktop", desktop_stem(&id)));
|
|
|
|
|
+ if !desktop_path.exists() {
|
|
|
|
|
+ return None;
|
|
|
|
|
+ }
|
|
|
|
|
+ let body = std::fs::read_to_string(&desktop_path).ok()?;
|
|
|
|
|
+ let mut width = None;
|
|
|
|
|
+ let mut height = None;
|
|
|
|
|
+ let mut maximized = None;
|
|
|
|
|
+ for line in body.lines() {
|
|
|
|
|
+ if let Some(rest) = line.strip_prefix("X-Appiphany-WindowWidth=") {
|
|
|
|
|
+ width = rest.parse().ok();
|
|
|
|
|
+ } else if let Some(rest) = line.strip_prefix("X-Appiphany-WindowHeight=") {
|
|
|
|
|
+ height = rest.parse().ok();
|
|
|
|
|
+ } else if let Some(rest) = line.strip_prefix("X-Appiphany-Maximized=") {
|
|
|
|
|
+ maximized = Some(rest.eq_ignore_ascii_case("true"));
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ match (width, height, maximized) {
|
|
|
|
|
+ (Some(w), Some(h), Some(m)) => Some((w, h, m)),
|
|
|
|
|
+ _ => None,
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+pub(crate) fn save_window_state(url: &str, width: i32, height: i32, maximized: bool) {
|
|
|
|
|
+ let id = stable_id(url);
|
|
|
|
|
+ let data_home = xdg_data_home();
|
|
|
|
|
+ let desktop_path = data_home
|
|
|
|
|
+ .join("applications")
|
|
|
|
|
+ .join(format!("{}.desktop", desktop_stem(&id)));
|
|
|
|
|
+ if !desktop_path.exists() {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ let Ok(body) = std::fs::read_to_string(&desktop_path) else {
|
|
|
|
|
+ return;
|
|
|
|
|
+ };
|
|
|
|
|
+ let mut lines: Vec<String> = body
|
|
|
|
|
+ .lines()
|
|
|
|
|
+ .filter(|l| {
|
|
|
|
|
+ !l.starts_with("X-Appiphany-WindowWidth=")
|
|
|
|
|
+ && !l.starts_with("X-Appiphany-WindowHeight=")
|
|
|
|
|
+ && !l.starts_with("X-Appiphany-Maximized=")
|
|
|
|
|
+ })
|
|
|
|
|
+ .map(String::from)
|
|
|
|
|
+ .collect();
|
|
|
|
|
+ lines.push(format!("X-Appiphany-WindowWidth={width}"));
|
|
|
|
|
+ lines.push(format!("X-Appiphany-WindowHeight={height}"));
|
|
|
|
|
+ lines.push(format!("X-Appiphany-Maximized={maximized}"));
|
|
|
|
|
+ let new_body = lines.join("\n") + "\n";
|
|
|
|
|
+ let _ = std::fs::write(&desktop_path, new_body);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+pub(crate) fn xdg_data_home() -> PathBuf {
|
|
|
std::env::var_os("XDG_DATA_HOME")
|
|
std::env::var_os("XDG_DATA_HOME")
|
|
|
.filter(|s| !s.is_empty())
|
|
.filter(|s| !s.is_empty())
|
|
|
.map(PathBuf::from)
|
|
.map(PathBuf::from)
|