| 1 |
/* |
| 2 |
* This file is part of budgie-desktop |
| 3 |
* |
| 4 |
* Copyright Budgie Desktop Developers |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation; either version 2 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Abstract class for different views for the application menu to |
| 14 |
* extend. |
| 15 |
*/ |
| 16 |
public abstract class ApplicationView : Gtk.Box { |
| 17 |
/** Mapped id to MenuButton */ |
| 18 |
public HashTable<string,MenuButton?> application_buttons { get; construct set; default = null; } |
| 19 |
public Gee.ArrayList<MenuButton> control_center_buttons { get; construct set; default = null; } |
| 20 |
public string search_term { get; public set; default = ""; } |
| 21 |
public int icon_size { get; protected set; default = 24; } |
| 22 |
|
| 23 |
protected Budgie.RelevancyService relevancy_service; |
| 24 |
private uint timeout_id = 0; |
| 25 |
|
| 26 |
/** |
| 27 |
* Emitted when an app is launched. |
| 28 |
*/ |
| 29 |
public signal void app_launched(); |
| 30 |
|
| 31 |
construct { |
| 32 |
this.application_buttons = new HashTable<string,MenuButton?>(str_hash, str_equal); |
| 33 |
this.control_center_buttons = new Gee.ArrayList<MenuButton>(); |
| 34 |
this.relevancy_service = new Budgie.RelevancyService(); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* This should be called when the user activates a search entry. |
| 39 |
*/ |
| 40 |
public abstract void on_search_entry_activated(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Performs any work that should be done to the view when the menu |
| 44 |
* is opened, e.g. resetting the current category or invalidating filters. |
| 45 |
*/ |
| 46 |
public abstract void on_show(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Refreshes the entire application view. |
| 50 |
*/ |
| 51 |
public abstract void refresh(Budgie.AppIndex app_tracker); |
| 52 |
|
| 53 |
/** |
| 54 |
* Invalidate aspects of the view, e.g. category headers. |
| 55 |
*/ |
| 56 |
public abstract void invalidate(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Queue a refresh of the application view. |
| 60 |
* |
| 61 |
* The time to wait before refreshing can be set by passing in `seconds`. |
| 62 |
* By default, the time is 1 second. |
| 63 |
*/ |
| 64 |
public void queue_refresh(Budgie.AppIndex app_tracker, int seconds = 1) { |
| 65 |
// Reset the refresh timer if an update is already queued |
| 66 |
if (this.timeout_id != 0) { |
| 67 |
Source.remove(this.timeout_id); |
| 68 |
this.timeout_id = 0; |
| 69 |
} |
| 70 |
|
| 71 |
// Update the view after the timeout |
| 72 |
this.timeout_id = Timeout.add_seconds(seconds, () => { |
| 73 |
this.refresh(app_tracker); |
| 74 |
this.timeout_id = 0; |
| 75 |
return Source.REMOVE; |
| 76 |
}); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* To be called when the search entry changes. |
| 81 |
*/ |
| 82 |
public void search_changed(string search_term) { |
| 83 |
this.search_term = search_term; |
| 84 |
|
| 85 |
// Update the relevancy of all apps when |
| 86 |
// the search term changes |
| 87 |
foreach (var child in this.application_buttons.get_values()) { |
| 88 |
this.relevancy_service.update_relevancy(child.app, search_term); |
| 89 |
} |
| 90 |
|
| 91 |
this.invalidate(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Checks if a `MenuButton` already exists in the view. |
| 96 |
*/ |
| 97 |
protected bool is_item_dupe(MenuButton? button) { |
| 98 |
MenuButton? compare_item = this.application_buttons.lookup(button.app.desktop_id); |
| 99 |
if (compare_item != null && compare_item != button) { |
| 100 |
return true; |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
} |