Bläddra i källkod

Restore window size for installed webapps

Johannes Zellner 4 månader sedan
förälder
incheckning
74be6b7bfe
2 ändrade filer med 78 tillägg och 5 borttagningar
  1. 59 3
      src/desktop_install.rs
  2. 19 2
      src/main.rs

+ 59 - 3
src/desktop_install.rs

@@ -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`).
-fn desktop_stem(id: &str) -> String {
+pub(crate) fn desktop_stem(id: &str) -> String {
     app_id_for_launcher(id)
 }
 
@@ -209,14 +209,70 @@ pub fn list_launchers() -> Result<Vec<DesktopLauncher>, String> {
     Ok(out)
 }
 
-fn stable_id(url: &str) -> String {
+pub(crate) fn stable_id(url: &str) -> String {
     let mut h = Sha256::new();
     h.update(url.as_bytes());
     let full = format!("{:x}", h.finalize());
     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")
         .filter(|s| !s.is_empty())
         .map(PathBuf::from)

+ 19 - 2
src/main.rs

@@ -606,18 +606,35 @@ fn build_persistent_network_session() -> NetworkSession {
 }
 
 fn open_web_window(app: &Application, url: &str) {
+    let (default_w, default_h, maximized) = desktop_install::load_window_state(url)
+        .unwrap_or((1024, 768, false));
+
     let window = ApplicationWindow::builder()
         .application(app)
         .title("Appiphany")
-        .default_width(1024)
-        .default_height(768)
+        .default_width(default_w)
+        .default_height(default_h)
         .build();
 
+    if maximized {
+        window.maximize();
+    }
+
     let network_session = network_session_for_app(app);
     let webview = build_appiphany_webview(&window, &network_session, None, url.to_string());
     let shell = build_overlay_with_webview(&webview);
     window.set_content(Some(&shell));
     webview.load_uri(url);
+
+    let url_owned = url.to_string();
+    window.connect_close_request(move |win| {
+        let maximized = win.is_maximized();
+        let width = win.default_width();
+        let height = win.default_height();
+        desktop_install::save_window_state(&url_owned, width, height, maximized);
+        glib::Propagation::Proceed
+    });
+
     window.present();
 }