|
| 1 | +/* SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) |
| 3 | + */ |
| 4 | + |
| 5 | +public class Keyboard.Shortcuts.AppChooser : Granite.Dialog { |
| 6 | + public signal void app_chosen (string filename, GLib.HashTable<string, Variant> parameters); |
| 7 | + public signal void custom_command_chosen (string command, GLib.HashTable<string, Variant> parameters); |
| 8 | + |
| 9 | + private Gtk.ListBox list; |
| 10 | + private Gtk.SearchEntry search_entry; |
| 11 | + private Gtk.Entry custom_entry; |
| 12 | + |
| 13 | + construct { |
| 14 | + search_entry = new Gtk.SearchEntry () { |
| 15 | + placeholder_text = _("Search Applications") |
| 16 | + }; |
| 17 | + search_entry.set_key_capture_widget (this); |
| 18 | + |
| 19 | + list = new Gtk.ListBox () { |
| 20 | + hexpand = true, |
| 21 | + vexpand = true |
| 22 | + }; |
| 23 | + list.add_css_class (Granite.STYLE_CLASS_RICH_LIST); |
| 24 | + list.set_sort_func (sort_function); |
| 25 | + list.set_filter_func (filter_function); |
| 26 | + |
| 27 | + var scrolled = new Gtk.ScrolledWindow () { |
| 28 | + child = list, |
| 29 | + has_frame = true |
| 30 | + }; |
| 31 | + |
| 32 | + custom_entry = new Gtk.Entry () { |
| 33 | + placeholder_text = _("Type in a custom command"), |
| 34 | + primary_icon_activatable = false, |
| 35 | + primary_icon_name = "utilities-terminal-symbolic" |
| 36 | + }; |
| 37 | + |
| 38 | + var box = new Gtk.Box (VERTICAL, 6); |
| 39 | + box.append (search_entry); |
| 40 | + box.append (scrolled); |
| 41 | + box.append (custom_entry); |
| 42 | + |
| 43 | + modal = true; |
| 44 | + default_height = 500; |
| 45 | + default_width = 400; |
| 46 | + get_content_area ().append (box); |
| 47 | + add_button (_("Cancel"), Gtk.ResponseType.CANCEL); |
| 48 | + |
| 49 | + // TRANSLATORS: This string is used by screen reader |
| 50 | + update_property (Gtk.AccessibleProperty.LABEL, _("Select an app"), -1); |
| 51 | + |
| 52 | + search_entry.grab_focus (); |
| 53 | + search_entry.search_changed.connect (() => { |
| 54 | + list.invalidate_filter (); |
| 55 | + }); |
| 56 | + |
| 57 | + response.connect (hide); |
| 58 | + |
| 59 | + list.row_activated.connect (on_app_selected); |
| 60 | + custom_entry.activate.connect (on_custom_command_entered); |
| 61 | + } |
| 62 | + |
| 63 | + public void init_list (GLib.List<GLib.DesktopAppInfo> app_infos) { |
| 64 | + foreach (var app_info in app_infos) { |
| 65 | + var icon = app_info.get_icon () ?? new ThemedIcon ("application-default-icon"); |
| 66 | + var name = app_info.get_name (); |
| 67 | + var description = app_info.get_description (); |
| 68 | + var filename = File.new_for_path (app_info.get_filename ()).get_basename (); |
| 69 | + |
| 70 | + list.prepend (new AppChooserRow ({icon, null, name, description, null, filename})); |
| 71 | + |
| 72 | + unowned var icon_theme = Gtk.IconTheme.get_for_display (Gdk.Display.get_default ()); |
| 73 | + var actions = app_info.list_actions (); |
| 74 | + for (var i = 0; i < actions.length; i++) { |
| 75 | + var action = actions[i]; |
| 76 | + var action_icon = Utils.get_action_icon (app_info, action); |
| 77 | + var action_name = "%s → %s".printf (name, app_info.get_action_name (action)); |
| 78 | + list.prepend (new AppChooserRow ({icon, action_icon, action_name, description, action, filename})); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private int sort_function (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) { |
| 84 | + unowned AppChooserRow row_1 = (AppChooserRow) row1.get_child (); |
| 85 | + unowned AppChooserRow row_2 = (AppChooserRow) row2.get_child (); |
| 86 | + |
| 87 | + var name_1 = row_1.info.name; |
| 88 | + var name_2 = row_2.info.name; |
| 89 | + |
| 90 | + return name_1.collate (name_2); |
| 91 | + } |
| 92 | + |
| 93 | + private bool filter_function (Gtk.ListBoxRow list_box_row) { |
| 94 | + var app_row = (AppChooserRow) list_box_row.get_child (); |
| 95 | + return search_entry.text.down () in app_row.info.name.down () |
| 96 | + || search_entry.text.down () in app_row.info.name.down (); |
| 97 | + } |
| 98 | + |
| 99 | + private void on_app_selected (Gtk.ListBoxRow list_box_row) { |
| 100 | + var app_row = (AppChooserRow) list_box_row.get_child (); |
| 101 | + |
| 102 | + var parameters = new GLib.HashTable<string, Variant> (null, null); |
| 103 | + if (app_row.info.action != null) { |
| 104 | + parameters["action"] = app_row.info.action; |
| 105 | + } |
| 106 | + |
| 107 | + app_chosen (app_row.info.filename, parameters); |
| 108 | + hide (); |
| 109 | + } |
| 110 | + |
| 111 | + private void on_custom_command_entered () { |
| 112 | + custom_command_chosen ( |
| 113 | + custom_entry.text, |
| 114 | + new GLib.HashTable<string, Variant> (null, null) |
| 115 | + ); |
| 116 | + hide (); |
| 117 | + } |
| 118 | +} |
0 commit comments