Bläddra i källkod

Fix icons on launched apps

Johannes Zellner 6 månader sedan
förälder
incheckning
fe2f3e53e9
2 ändrade filer med 76 tillägg och 21 borttagningar
  1. 42 19
      src/desktop_install.rs
  2. 34 2
      src/main.rs

+ 42 - 19
src/desktop_install.rs

@@ -9,6 +9,20 @@ use url::Url;
 
 const ID_HEX_LEN: usize = 12;
 
+/// Prefix for desktop file names and GApplication IDs.
+const APP_ID_PREFIX: &str = "dev.appiphany";
+
+/// Build a valid GApplication ID for the given launcher hash.
+/// Result looks like `dev.appiphany.e8009ee032e7`.
+pub fn app_id_for_launcher(id: &str) -> String {
+    format!("{APP_ID_PREFIX}.{id}")
+}
+
+/// Desktop file stem for the given launcher hash (also used as `StartupWMClass`).
+fn desktop_stem(id: &str) -> String {
+    app_id_for_launcher(id)
+}
+
 #[derive(Clone, Debug)]
 pub enum LauncherIcon {
     /// Load via `gtk::Image::from_file`
@@ -37,7 +51,7 @@ pub fn run_install(
     let data_home = xdg_data_home();
     let apps_dir = data_home.join("applications");
     let icon_dir = appiphany_icon_dir(&data_home);
-    let desktop_path = apps_dir.join(format!("appiphany-{id}.desktop"));
+    let desktop_path = apps_dir.join(format!("{}.desktop", desktop_stem(&id)));
 
     if desktop_path.exists() && !force {
         return Err(format!(
@@ -83,7 +97,7 @@ pub fn run_install(
         "applications-internet".to_string()
     };
 
-    let desktop_body = build_desktop_file(&name, exe_str, &url, &icon_value);
+    let desktop_body = build_desktop_file(&name, exe_str, &url, &icon_value, &id);
     std::fs::write(&desktop_path, desktop_body).map_err(|e| e.to_string())?;
 
     eprintln!("Wrote {}", desktop_path.display());
@@ -105,17 +119,19 @@ pub fn run_uninstall(
     };
 
     let data_home = xdg_data_home();
-    let desktop_path = data_home
-        .join("applications")
-        .join(format!("appiphany-{id}.desktop"));
+    let apps_dir = data_home.join("applications");
+    let desktop_path = apps_dir.join(format!("{}.desktop", desktop_stem(&id)));
+    let legacy_desktop_path = apps_dir.join(format!("appiphany-{id}.desktop"));
     let legacy_icon_path = legacy_hicolor_icon_path(&data_home, &id);
     let icon_dir = appiphany_icon_dir(&data_home);
 
     let mut removed = false;
-    if desktop_path.exists() {
-        std::fs::remove_file(&desktop_path).map_err(|e| e.to_string())?;
-        eprintln!("Removed {}", desktop_path.display());
-        removed = true;
+    for path in [&desktop_path, &legacy_desktop_path] {
+        if path.exists() {
+            std::fs::remove_file(path).map_err(|e| e.to_string())?;
+            eprintln!("Removed {}", path.display());
+            removed = true;
+        }
     }
     if legacy_icon_path.exists() {
         std::fs::remove_file(&legacy_icon_path).map_err(|e| e.to_string())?;
@@ -162,15 +178,19 @@ pub fn list_launchers() -> Result<Vec<DesktopLauncher>, String> {
         let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
             continue;
         };
-        if !(name.starts_with("appiphany-") && name.ends_with(".desktop")) {
-            continue;
-        }
 
-        let id = name
-            .strip_prefix("appiphany-")
-            .and_then(|s| s.strip_suffix(".desktop"))
-            .unwrap_or_default()
-            .to_string();
+        let new_prefix = format!("{APP_ID_PREFIX}.");
+        let id = if let Some(rest) = name.strip_prefix(&new_prefix) {
+            rest.strip_suffix(".desktop")
+                .unwrap_or_default()
+                .to_string()
+        } else if let Some(rest) = name.strip_prefix("appiphany-") {
+            rest.strip_suffix(".desktop")
+                .unwrap_or_default()
+                .to_string()
+        } else {
+            continue;
+        };
         if id.len() != ID_HEX_LEN || !id.chars().all(|c| c.is_ascii_hexdigit()) {
             continue;
         }
@@ -259,10 +279,12 @@ fn escape_desktop_exec_component(s: &str) -> String {
     out
 }
 
-fn build_desktop_file(name: &str, exe: &str, page_url: &str, icon: &str) -> String {
+fn build_desktop_file(name: &str, exe: &str, page_url: &str, icon: &str, id: &str) -> String {
+    let app_id = app_id_for_launcher(id);
     let exec_line = format!(
-        "{} {}",
+        "{} --app-id {} {}",
         escape_desktop_exec_component(exe),
+        escape_desktop_exec_component(&app_id),
         escape_desktop_exec_component(page_url)
     );
 
@@ -278,6 +300,7 @@ fn build_desktop_file(name: &str, exe: &str, page_url: &str, icon: &str) -> Stri
         ),
         &format!("Exec={exec_line}"),
         &format!("Icon={}", escape_desktop_value(icon)),
+        &format!("StartupWMClass={app_id}"),
     ]
     .join("\n")
         + "\n"

+ 34 - 2
src/main.rs

@@ -617,6 +617,10 @@ struct Args {
 
     /// URL to open (when no subcommand is used)
     url: Option<String>,
+
+    /// GApplication ID override (used by desktop launchers for icon matching)
+    #[arg(long, hide = true, global = true)]
+    app_id: Option<String>,
 }
 
 #[derive(Subcommand)]
@@ -692,18 +696,26 @@ fn main() -> glib::ExitCode {
         }
     }
 
+    let effective_app_id = parsed
+        .app_id
+        .as_deref()
+        .unwrap_or("dev.appiphany.app");
+
+    // set_prgname before GTK init so both Wayland and X11 see the right identity.
+    glib::set_prgname(Some(effective_app_id));
+
     if let Err(e) = adw::init() {
         eprintln!("appiphany: failed to initialize libadwaita: {e}");
         return glib::ExitCode::from(1);
     }
 
     let app = Application::builder()
-        .application_id("dev.appiphany.app")
+        .application_id(effective_app_id)
         .flags(ApplicationFlags::HANDLES_COMMAND_LINE)
         .build();
 
     app.connect_command_line(|app, cmdline| {
-        let args: Vec<String> = match cmdline
+        let raw_args: Vec<String> = match cmdline
             .arguments()
             .into_iter()
             .map(|s| s.into_string())
@@ -716,6 +728,26 @@ fn main() -> glib::ExitCode {
             }
         };
 
+        let args: Vec<String> = {
+            let mut filtered = Vec::new();
+            let mut skip_next = false;
+            for arg in raw_args {
+                if skip_next {
+                    skip_next = false;
+                    continue;
+                }
+                if arg == "--app-id" {
+                    skip_next = true;
+                    continue;
+                }
+                if arg.starts_with("--app-id=") {
+                    continue;
+                }
+                filtered.push(arg);
+            }
+            filtered
+        };
+
         if args.len() == 1 {
             if app.active_window().is_none() {
                 build_manager_window(app);