From eef2066a12ceaa34af44d1f79f30d277f2d52267 Mon Sep 17 00:00:00 2001 From: lenemter Date: Thu, 5 Dec 2024 19:38:49 +0300 Subject: [PATCH 1/2] Create fake app info for unknown windows --- src/App.vala | 4 +- src/AppInfo.vala | 104 +++++++++++++++++++++++++++++++++++++++ src/Launcher.vala | 4 +- src/LauncherManager.vala | 30 ++++++----- src/meson.build | 1 + 5 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 src/AppInfo.vala diff --git a/src/App.vala b/src/App.vala index 27cb4da2d..cdd5c5802 100644 --- a/src/App.vala +++ b/src/App.vala @@ -22,7 +22,7 @@ public class Dock.App : Object { } public bool pinned { get; construct set; } - public GLib.DesktopAppInfo app_info { get; construct; } + public Dock.AppInfo app_info { get; construct; } public bool count_visible { get; private set; default = false; } public int64 current_count { get; private set; default = 0; } @@ -52,7 +52,7 @@ public class Dock.App : Object { private SimpleAction pinned_action; - public App (GLib.DesktopAppInfo app_info, bool pinned) { + public App (Dock.AppInfo app_info, bool pinned) { Object (app_info: app_info, pinned: pinned); } diff --git a/src/AppInfo.vala b/src/AppInfo.vala new file mode 100644 index 000000000..94c4ee43b --- /dev/null +++ b/src/AppInfo.vala @@ -0,0 +1,104 @@ +// TODO: Copyright + +public class Dock.AppInfo : GLib.Object { + private const string[] NULL_ACTIONS = {}; + private GLib.Icon FALLBACK_ICON = new GLib.ThemedIcon ("application-default-icon"); + + public GLib.DesktopAppInfo? desktop_app_info { get; construct; } + + private string _fake_id = "Unknown"; + public string? fake_id { + private get { + return _fake_id; + } + set { + _fake_id = value ?? "Unknown"; + } + } + + private string _fake_name = "Unknown"; + public string? fake_name { + private get { + return _fake_name; + } + set { + _fake_name = value ?? "Unknown"; + } + } + + public AppInfo (GLib.DesktopAppInfo? desktop_app_info) { + Object (desktop_app_info: desktop_app_info); + } + + public unowned string get_id () { + if (desktop_app_info == null) { + return fake_id; + } + + return desktop_app_info.get_id (); + } + + public unowned string get_display_name () { + if (desktop_app_info == null) { + return fake_name; + } + + return desktop_app_info.get_display_name (); + } + + public unowned GLib.Icon get_icon () { + if (desktop_app_info == null) { + return FALLBACK_ICON; + } + + return desktop_app_info.get_icon () ?? FALLBACK_ICON; + } + + public bool get_boolean (string key) { + if (desktop_app_info == null) { + return false; + } + + return desktop_app_info.get_boolean (key); + } + + public string get_string (string key) { + if (desktop_app_info == null) { + return ""; + } + + return desktop_app_info.get_string (key); + } + + public unowned string[] list_actions () { + if (desktop_app_info == null) { + return NULL_ACTIONS; + } + + return desktop_app_info.list_actions (); + } + + public string get_action_name (string action_name) { + if (desktop_app_info == null) { + return "Something went wrong if you see this"; + } + + return desktop_app_info.get_action_name (action_name); + } + + public void launch_action (string action_name, GLib.AppLaunchContext launch_context) { + if (desktop_app_info == null) { + return; + } + + desktop_app_info.launch_action (action_name, launch_context); + } + + public bool launch (GLib.List? files, GLib.AppLaunchContext? context) throws GLib.Error { + if (desktop_app_info == null) { + return false; + } + + return desktop_app_info.launch (files, context); + } +} diff --git a/src/Launcher.vala b/src/Launcher.vala index c27989483..174091507 100644 --- a/src/Launcher.vala +++ b/src/Launcher.vala @@ -331,7 +331,9 @@ public class Dock.Launcher : Gtk.Box { } break; case Gdk.BUTTON_SECONDARY: - popover.popup (); + if (app.app_info.desktop_app_info != null) { + popover.popup (); + } break; } } diff --git a/src/LauncherManager.vala b/src/LauncherManager.vala index 48303243c..56b9a78e8 100644 --- a/src/LauncherManager.vala +++ b/src/LauncherManager.vala @@ -69,7 +69,7 @@ } var file = (File) drop_target_file.get_value ().get_object (); - var app_info = new DesktopAppInfo.from_filename (file.get_path ()); + var app_info = new GLib.DesktopAppInfo.from_filename (file.get_path ()); if (app_info == null) { return; @@ -82,7 +82,8 @@ } var position = (int) Math.round (drop_x / get_launcher_size ()); - added_launcher = add_launcher (new DesktopAppInfo.from_filename (file.get_path ()), true, true, position); + var desktop_app_info = new GLib.DesktopAppInfo.from_filename (file.get_path ()); + added_launcher = add_launcher (new Dock.AppInfo (desktop_app_info), true, true, position); added_launcher.moving = true; }); @@ -99,7 +100,8 @@ Idle.add (() => { foreach (string app_id in settings.get_strv ("launchers")) { - var app_info = new GLib.DesktopAppInfo (app_id); + var desktop_app_info = new GLib.DesktopAppInfo (app_id); + var app_info = new Dock.AppInfo (desktop_app_info); add_launcher (app_info, true, false); } reposition_launchers (); @@ -145,7 +147,7 @@ return settings.get_int ("icon-size") + Launcher.PADDING * 2; } - private Launcher add_launcher (GLib.DesktopAppInfo app_info, bool pinned = false, bool reposition = true, int index = -1) { + private Launcher add_launcher (Dock.AppInfo app_info, bool pinned = false, bool reposition = true, int index = -1) { var app = new App (app_info, pinned); var launcher = new Launcher (app); @@ -247,9 +249,13 @@ unowned var app_id = window.properties["app-id"].get_string (); App? app = id_to_app[app_id]; if (app == null) { - var app_info = new GLib.DesktopAppInfo (app_id); - if (app_info == null) { - continue; + var desktop_app_info = new GLib.DesktopAppInfo (app_id); + var app_info = new Dock.AppInfo (desktop_app_info); + + if (desktop_app_info == null) { + var title = window.properties.get ("title"); + app_info.fake_name = title != null ? title.get_string () : app_id; + app_info.fake_id = app_id; } app = add_launcher (app_info).app; @@ -308,7 +314,7 @@ Launcher[] launchers_to_remove = {}; foreach (var launcher in launchers) { - if (launcher.app.pinned) { + if (launcher.app.pinned && launcher.app.app_info.desktop_app_info != null) { new_pinned_ids += launcher.app.app_info.get_id (); } else if (!launcher.app.pinned && launcher.app.windows.is_empty) { launchers_to_remove += launcher; @@ -338,14 +344,14 @@ return; } - var app_info = new DesktopAppInfo (app_id); + var desktop_app_info = new GLib.DesktopAppInfo (app_id); - if (app_info == null) { - warning ("App not found: %s", app_id); + if (desktop_app_info == null) { + warning ("LauncherManager.add_launcher_for_id: Desktop app info not found: %s", app_id); return; } - add_launcher (app_info).app.pinned = true; + add_launcher (new Dock.AppInfo (desktop_app_info)).app.pinned = true; } public void remove_launcher_by_id (string app_id) { diff --git a/src/meson.build b/src/meson.build index 30e3413d8..3d31c080f 100644 --- a/src/meson.build +++ b/src/meson.build @@ -1,5 +1,6 @@ sources = [ 'App.vala', + 'AppInfo.vala', 'Application.vala', 'AppWindow.vala', 'DesktopIntegration.vala', From 1d608b6ee6b1d23751804861904f427267b7deb0 Mon Sep 17 00:00:00 2001 From: lenemter Date: Thu, 5 Dec 2024 19:47:50 +0300 Subject: [PATCH 2/2] Fix lint --- src/AppInfo.vala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/AppInfo.vala b/src/AppInfo.vala index 94c4ee43b..18caac8c3 100644 --- a/src/AppInfo.vala +++ b/src/AppInfo.vala @@ -2,7 +2,7 @@ public class Dock.AppInfo : GLib.Object { private const string[] NULL_ACTIONS = {}; - private GLib.Icon FALLBACK_ICON = new GLib.ThemedIcon ("application-default-icon"); + private GLib.Icon fallback_icon = new GLib.ThemedIcon ("application-default-icon"); public GLib.DesktopAppInfo? desktop_app_info { get; construct; } @@ -48,10 +48,10 @@ public class Dock.AppInfo : GLib.Object { public unowned GLib.Icon get_icon () { if (desktop_app_info == null) { - return FALLBACK_ICON; + return fallback_icon; } - return desktop_app_info.get_icon () ?? FALLBACK_ICON; + return desktop_app_info.get_icon () ?? fallback_icon; } public bool get_boolean (string key) {