|
| 1 | +/* SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | + * SPDX-FileCopyrightText: 2025 elementary, Inc. (https://elementary.io) |
| 3 | + */ |
| 4 | + |
| 5 | +public class Keyboard.AppChooser : Granite.Dialog { |
| 6 | + public signal void app_chosen (string path); |
| 7 | + public signal void custom_command_chosen (string command); |
| 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 | + |
| 18 | + list = new Gtk.ListBox () { |
| 19 | + hexpand = true, |
| 20 | + vexpand = true |
| 21 | + }; |
| 22 | + list.add_css_class (Granite.STYLE_CLASS_RICH_LIST); |
| 23 | + list.set_sort_func (sort_function); |
| 24 | + list.set_filter_func (filter_function); |
| 25 | + |
| 26 | + var scrolled = new Gtk.ScrolledWindow () { |
| 27 | + child = list |
| 28 | + }; |
| 29 | + |
| 30 | + var frame = new Gtk.Frame (null) { |
| 31 | + child = scrolled |
| 32 | + }; |
| 33 | + |
| 34 | + custom_entry = new Gtk.Entry () { |
| 35 | + placeholder_text = _("Type in a custom command"), |
| 36 | + primary_icon_activatable = false, |
| 37 | + primary_icon_name = "utilities-terminal-symbolic" |
| 38 | + }; |
| 39 | + |
| 40 | + var box = new Gtk.Box (VERTICAL, 6); |
| 41 | + box.append (search_entry); |
| 42 | + box.append (frame); |
| 43 | + box.append (custom_entry); |
| 44 | + |
| 45 | + modal = true; |
| 46 | + default_height = 500; |
| 47 | + default_width = 400; |
| 48 | + get_content_area ().append (box); |
| 49 | + add_button (_("Cancel"), Gtk.ResponseType.CANCEL); |
| 50 | + |
| 51 | + // TRANSLATORS: This string is used by screen reader |
| 52 | + update_property (Gtk.AccessibleProperty.LABEL, _("Select startup app"), -1); |
| 53 | + |
| 54 | + search_entry.grab_focus (); |
| 55 | + search_entry.search_changed.connect (() => { |
| 56 | + list.invalidate_filter (); |
| 57 | + }); |
| 58 | + |
| 59 | + response.connect (hide); |
| 60 | + |
| 61 | + list.row_activated.connect (on_app_selected); |
| 62 | + custom_entry.activate.connect (on_custom_command_entered); |
| 63 | + } |
| 64 | + |
| 65 | + public void init_list (GLib.List<AppInfo?> app_infos) { |
| 66 | + foreach (var app_info in app_infos) { |
| 67 | + var app_row = new AppChooserRow (app_info); |
| 68 | + list.prepend (app_row); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private int sort_function (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) { |
| 73 | + unowned AppChooserRow row_1 = (AppChooserRow) row1.get_child (); |
| 74 | + unowned AppChooserRow row_2 = (AppChooserRow) row2.get_child (); |
| 75 | + |
| 76 | + var name_1 = row_1.app_info.name; |
| 77 | + var name_2 = row_2.app_info.name; |
| 78 | + |
| 79 | + return name_1.collate (name_2); |
| 80 | + } |
| 81 | + |
| 82 | + private bool filter_function (Gtk.ListBoxRow list_box_row) { |
| 83 | + var app_row = (AppChooserRow) list_box_row.get_child (); |
| 84 | + return search_entry.text.down () in app_row.app_info.name.down () |
| 85 | + || search_entry.text.down () in app_row.app_info.comment.down (); |
| 86 | + } |
| 87 | + |
| 88 | + private void on_app_selected (Gtk.ListBoxRow list_box_row) { |
| 89 | + var app_row = (AppChooserRow) list_box_row.get_child (); |
| 90 | + app_chosen (app_row.app_info.path); |
| 91 | + hide (); |
| 92 | + } |
| 93 | + |
| 94 | + private void on_custom_command_entered () { |
| 95 | + custom_command_chosen (custom_entry.text); |
| 96 | + hide (); |
| 97 | + } |
| 98 | +} |
0 commit comments