| 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 |
* Widget to display applications in a list. |
| 14 |
* |
| 15 |
* This shows a list of categories on the left, and all applications |
| 16 |
* on the right. |
| 17 |
*/ |
| 18 |
public class ApplicationListView : ApplicationView { |
| 19 |
const int MIN_HEIGHT = 480; |
| 20 |
const int MAX_HEIGHT = 600; |
| 21 |
const int WIDTH = 300; |
| 22 |
const double HEIGHT_RATIO = 0.35; |
| 23 |
private int current_height = MIN_HEIGHT; |
| 24 |
private int current_width = WIDTH; |
| 25 |
|
| 26 |
private Gtk.Box categories; |
| 27 |
private Gtk.ListBox applications; |
| 28 |
private Gtk.ScrolledWindow categories_scroll; |
| 29 |
private Gtk.ScrolledWindow content_scroll; |
| 30 |
private CategoryButton all_categories; |
| 31 |
|
| 32 |
public Settings settings { get; construct; default = null; } |
| 33 |
|
| 34 |
// The current group |
| 35 |
private Budgie.Category? current_category = null; |
| 36 |
private bool compact_mode; |
| 37 |
private bool headers_visible; |
| 38 |
private bool show_control_center_panels; |
| 39 |
|
| 40 |
/* Whether we allow rollover category switch */ |
| 41 |
private bool rollover_menus = true; |
| 42 |
|
| 43 |
private bool reloading = false; |
| 44 |
|
| 45 |
public ApplicationListView(Settings settings) { |
| 46 |
Object( |
| 47 |
settings: settings, |
| 48 |
orientation: Gtk.Orientation.HORIZONTAL, |
| 49 |
spacing: 0 |
| 50 |
); |
| 51 |
|
| 52 |
} |
| 53 |
|
| 54 |
construct { |
| 55 |
this.realize.connect(() => { |
| 56 |
this.update_sizing(); |
| 57 |
}); |
| 58 |
|
| 59 |
this.set_size_request(current_width, current_height); |
| 60 |
this.icon_size = settings.get_int("menu-icons-size"); |
| 61 |
|
| 62 |
this.categories = new Gtk.Box(Gtk.Orientation.VERTICAL, 0) { |
| 63 |
margin_top = 3, |
| 64 |
margin_bottom = 3 |
| 65 |
}; |
| 66 |
|
| 67 |
this.categories_scroll = new Gtk.ScrolledWindow(null, null) { |
| 68 |
overlay_scrolling = false, |
| 69 |
shadow_type = Gtk.ShadowType.NONE, // Don't have an outline |
| 70 |
hscrollbar_policy = Gtk.PolicyType.NEVER, |
| 71 |
vscrollbar_policy = Gtk.PolicyType.AUTOMATIC, |
| 72 |
min_content_height = current_height, |
| 73 |
propagate_natural_height = true |
| 74 |
}; |
| 75 |
this.categories_scroll.get_style_context().add_class("categories"); |
| 76 |
this.categories_scroll.get_style_context().add_class("sidebar"); |
| 77 |
this.categories_scroll.add(categories); |
| 78 |
this.pack_start(categories_scroll, false, false, 0); |
| 79 |
|
| 80 |
// "All" button" |
| 81 |
this.all_categories = new CategoryButton(null); |
| 82 |
this.all_categories.enter_notify_event.connect(this.on_mouse_enter); |
| 83 |
this.all_categories.toggled.connect(()=> { |
| 84 |
this.update_category(all_categories); |
| 85 |
}); |
| 86 |
this.categories.pack_start(all_categories, false); |
| 87 |
|
| 88 |
var right_layout = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); |
| 89 |
this.pack_start(right_layout, true, true, 0); |
| 90 |
|
| 91 |
// holds all the applications |
| 92 |
this.applications = new Gtk.ListBox() { |
| 93 |
selection_mode = Gtk.SelectionMode.SINGLE, |
| 94 |
valign = Gtk.Align.START, |
| 95 |
// Make sure that the box at least covers the whole area. This helps more themes look better |
| 96 |
height_request = current_height |
| 97 |
}; |
| 98 |
this.applications.row_activated.connect(this.on_row_activate); |
| 99 |
|
| 100 |
this.content_scroll = new Gtk.ScrolledWindow(null, null) { |
| 101 |
overlay_scrolling = true, |
| 102 |
hscrollbar_policy = Gtk.PolicyType.NEVER, |
| 103 |
vscrollbar_policy = Gtk.PolicyType.AUTOMATIC, |
| 104 |
min_content_height = current_height |
| 105 |
}; |
| 106 |
this.content_scroll.set_overlay_scrolling(true); |
| 107 |
this.content_scroll.add(applications); |
| 108 |
right_layout.pack_start(content_scroll, true, true, 0); |
| 109 |
|
| 110 |
// placeholder in case of no results |
| 111 |
var placeholder = new Gtk.Label("<big>%s</big>".printf(_("Sorry, no items found"))) { |
| 112 |
use_markup = true, |
| 113 |
margin = 6, |
| 114 |
}; |
| 115 |
placeholder.get_style_context().add_class("dim-label"); |
| 116 |
placeholder.show(); |
| 117 |
this.applications.set_placeholder(placeholder); |
| 118 |
|
| 119 |
this.settings.changed.connect(on_settings_changed); |
| 120 |
this.on_settings_changed("menu-compact"); |
| 121 |
this.on_settings_changed("menu-headers"); |
| 122 |
this.on_settings_changed("menu-categories-hover"); |
| 123 |
this.on_settings_changed("menu-show-control-center-items"); |
| 124 |
|
| 125 |
// management of our listbox |
| 126 |
this.applications.set_filter_func(do_filter_list); |
| 127 |
this.applications.set_sort_func(do_sort_list); |
| 128 |
|
| 129 |
this.update_sizing(); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Computes the menu height from the monitor workarea. |
| 134 |
* Returns at least MIN_HEIGHT. |
| 135 |
*/ |
| 136 |
private int compute_base_height() { |
| 137 |
var toplevel = this.get_toplevel(); |
| 138 |
if (toplevel == null) { |
| 139 |
return MIN_HEIGHT; |
| 140 |
} |
| 141 |
|
| 142 |
var gdk_window = toplevel.get_window(); |
| 143 |
if (gdk_window == null) { |
| 144 |
return MIN_HEIGHT; |
| 145 |
} |
| 146 |
|
| 147 |
var display = toplevel.get_display(); |
| 148 |
var monitor = display.get_monitor_at_window(gdk_window); |
| 149 |
var workarea = monitor.get_workarea(); |
| 150 |
|
| 151 |
int computed = (int) (workarea.height * HEIGHT_RATIO); |
| 152 |
return computed.clamp(MIN_HEIGHT, MAX_HEIGHT); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Updates widget sizing based on the monitor workarea. |
| 157 |
*/ |
| 158 |
private void update_sizing() { |
| 159 |
current_height = compute_base_height(); |
| 160 |
this.set_size_request(current_width, current_height); |
| 161 |
|
| 162 |
this.categories_scroll.min_content_height = current_height; |
| 163 |
this.content_scroll.min_content_height = current_height; |
| 164 |
this.applications.height_request = current_height; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Refreshes the category and application lists. |
| 169 |
*/ |
| 170 |
public override void refresh(Budgie.AppIndex app_tracker) { |
| 171 |
lock (this.reloading) { |
| 172 |
if (this.reloading) { |
| 173 |
return; |
| 174 |
} |
| 175 |
this.reloading = true; |
| 176 |
} |
| 177 |
|
| 178 |
// Destroy all application items |
| 179 |
foreach (var child in this.applications.get_children()) { |
| 180 |
child.destroy(); |
| 181 |
} |
| 182 |
this.application_buttons.remove_all(); |
| 183 |
this.control_center_buttons.clear(); |
| 184 |
|
| 185 |
// Destroy all category items |
| 186 |
this.categories.get_children().foreach((child) => { |
| 187 |
child.destroy(); |
| 188 |
}); |
| 189 |
|
| 190 |
// Load all of the new content in the background |
| 191 |
Idle.add(() => { |
| 192 |
this.load_menus(app_tracker); |
| 193 |
this.invalidate(); |
| 194 |
return false; |
| 195 |
}); |
| 196 |
|
| 197 |
lock (this.reloading) { |
| 198 |
this.reloading = false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Build the category and application lists. |
| 204 |
*/ |
| 205 |
private void load_menus(Budgie.AppIndex app_tracker) { |
| 206 |
// "All" button" |
| 207 |
this.all_categories = new CategoryButton(null); |
| 208 |
this.all_categories.enter_notify_event.connect(this.on_mouse_enter); |
| 209 |
this.all_categories.toggled.connect(()=> { |
| 210 |
this.update_category(all_categories); |
| 211 |
}); |
| 212 |
all_categories.show_all(); |
| 213 |
this.categories.pack_start(all_categories, false); |
| 214 |
|
| 215 |
foreach (var category in app_tracker.get_categories()) { |
| 216 |
// Skip empty categories |
| 217 |
if (category.apps.is_empty) { |
| 218 |
continue; |
| 219 |
} |
| 220 |
|
| 221 |
// Create a new button for this category |
| 222 |
var btn = new CategoryButton(category); |
| 223 |
btn.join_group(all_categories); |
| 224 |
btn.enter_notify_event.connect(this.on_mouse_enter); |
| 225 |
btn.toggled.connect(() => { |
| 226 |
update_category(btn); |
| 227 |
}); |
| 228 |
|
| 229 |
btn.show_all(); |
| 230 |
this.categories.pack_start(btn, false); // Add the button |
| 231 |
|
| 232 |
// Create a button for each app in this category |
| 233 |
foreach (var app in category.apps) { |
| 234 |
var app_btn = new MenuButton(app, category, icon_size); |
| 235 |
|
| 236 |
app_btn.clicked.connect(() => { |
| 237 |
app.launch(); |
| 238 |
this.app_launched(); |
| 239 |
}); |
| 240 |
|
| 241 |
this.application_buttons.insert(app.desktop_id, app_btn); |
| 242 |
app_btn.show_all(); |
| 243 |
this.applications.add(app_btn); |
| 244 |
|
| 245 |
if (app_btn.is_control_center_panel()) { |
| 246 |
this.control_center_buttons.add(app_btn); |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Invalidate the application headers, filters, and sorting. |
| 254 |
*/ |
| 255 |
public override void invalidate() { |
| 256 |
this.applications.invalidate_headers(); |
| 257 |
this.applications.invalidate_filter(); |
| 258 |
this.applications.invalidate_sort(); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Launches the application selected by the current search result. |
| 263 |
*/ |
| 264 |
public override void on_search_entry_activated() { |
| 265 |
Gtk.ListBoxRow? selected = null; |
| 266 |
|
| 267 |
var rows = this.applications.get_selected_rows(); |
| 268 |
if (rows != null) { |
| 269 |
selected = rows.data; |
| 270 |
} else { |
| 271 |
foreach (var child in this.applications.get_children()) { |
| 272 |
if (child.get_visible() && child.get_child_visible()) { |
| 273 |
selected = child as Gtk.ListBoxRow; |
| 274 |
break; |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
if (selected == null) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
MenuButton btn = selected.get_child() as MenuButton; |
| 283 |
btn.app.launch(); |
| 284 |
this.app_launched(); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Permits "rolling" over categories. |
| 289 |
*/ |
| 290 |
private bool on_mouse_enter(Gtk.Widget source_widget, Gdk.EventCrossing e) { |
| 291 |
if (!this.rollover_menus) { |
| 292 |
return Gdk.EVENT_PROPAGATE; |
| 293 |
} |
| 294 |
|
| 295 |
// If it's not valid, don't use it. |
| 296 |
Gtk.ToggleButton? b = source_widget as Gtk.ToggleButton; |
| 297 |
if (!b.get_sensitive() || !b.get_visible()) { |
| 298 |
return Gdk.EVENT_PROPAGATE; |
| 299 |
} |
| 300 |
|
| 301 |
// Activate the source_widget category |
| 302 |
b.set_active(true); |
| 303 |
return Gdk.EVENT_PROPAGATE; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Handles changes to our applet settings. |
| 308 |
*/ |
| 309 |
private void on_settings_changed(string key) { |
| 310 |
switch (key) { |
| 311 |
case "menu-compact": |
| 312 |
var vis = settings.get_boolean(key); |
| 313 |
this.categories_scroll.no_show_all = vis; |
| 314 |
this.categories_scroll.set_visible(vis); |
| 315 |
this.compact_mode = vis; |
| 316 |
this.invalidate(); |
| 317 |
break; |
| 318 |
case "menu-headers": |
| 319 |
var hed = this.settings.get_boolean(key); |
| 320 |
this.headers_visible = hed; |
| 321 |
if (hed) { |
| 322 |
this.applications.set_header_func(this.do_list_header); |
| 323 |
} else { |
| 324 |
this.applications.set_header_func(null); |
| 325 |
} |
| 326 |
this.invalidate(); |
| 327 |
break; |
| 328 |
case "menu-categories-hover": |
| 329 |
// Category hover |
| 330 |
this.rollover_menus = this.settings.get_boolean(key); |
| 331 |
break; |
| 332 |
case "menu-show-control-center-items": |
| 333 |
this.show_control_center_panels = this.settings.get_boolean(key); |
| 334 |
this.invalidate(); |
| 335 |
break; |
| 336 |
default: |
| 337 |
// not interested |
| 338 |
break; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Launches the application in the given row. |
| 344 |
*/ |
| 345 |
private void on_row_activate(Gtk.ListBoxRow? row) { |
| 346 |
if (row == null) { |
| 347 |
return; |
| 348 |
} |
| 349 |
// Launch this item, i.e. keyboard access |
| 350 |
MenuButton btn = row.get_child() as MenuButton; |
| 351 |
btn.app.launch(); |
| 352 |
this.app_launched(); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Provide category headers in the "All" category |
| 357 |
*/ |
| 358 |
private void do_list_header(Gtk.ListBoxRow? before, Gtk.ListBoxRow? after) { |
| 359 |
MenuButton? child = null; |
| 360 |
string? prev = null; |
| 361 |
string? next = null; |
| 362 |
|
| 363 |
// In a category listing, kill headers |
| 364 |
if (this.current_category != null) { |
| 365 |
if (before != null) { |
| 366 |
before.set_header(null); |
| 367 |
} |
| 368 |
if (after != null) { |
| 369 |
after.set_header(null); |
| 370 |
} |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
// Just retrieve the category names |
| 375 |
if (before != null) { |
| 376 |
child = before.get_child() as MenuButton; |
| 377 |
prev = child.category.name; |
| 378 |
} |
| 379 |
|
| 380 |
if (after != null) { |
| 381 |
child = after.get_child() as MenuButton; |
| 382 |
next = child.category.name; |
| 383 |
} |
| 384 |
|
| 385 |
// Only add one if we need one! |
| 386 |
if (before == null || after == null || prev != next) { |
| 387 |
var label = new Gtk.Label(Markup.printf_escaped("<big>%s</big>", prev)); |
| 388 |
label.get_style_context().add_class("dim-label"); |
| 389 |
label.halign = Gtk.Align.START; |
| 390 |
label.use_markup = true; |
| 391 |
before.set_header(label); |
| 392 |
label.margin = 6; |
| 393 |
} else { |
| 394 |
before.set_header(null); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Filter out results in the list according to whatever the current filter is, |
| 400 |
* i.e. group based or search based |
| 401 |
*/ |
| 402 |
private bool do_filter_list(Gtk.ListBoxRow row) { |
| 403 |
MenuButton child = row.get_child() as MenuButton; |
| 404 |
|
| 405 |
// Check if there is a search going on |
| 406 |
string term = this.search_term.strip(); |
| 407 |
if (term.length > 0) { |
| 408 |
// "disable" categories while searching |
| 409 |
this.categories.sensitive = false; |
| 410 |
// Items must be unique across the search |
| 411 |
if (this.is_item_dupe(child)) { |
| 412 |
return false; |
| 413 |
} |
| 414 |
|
| 415 |
// Only show this item if its relevancy to the search term |
| 416 |
// is within an arbitrary threshold |
| 417 |
return this.relevancy_service.is_app_relevant(child.app); |
| 418 |
} |
| 419 |
|
| 420 |
// "enable" categories if not searching |
| 421 |
this.categories.sensitive = true; |
| 422 |
|
| 423 |
// We are currently in the "All" category, so show this item |
| 424 |
if (this.current_category == null) { |
| 425 |
// Don't show this item if it's a control center panel and |
| 426 |
// we're set to not show them |
| 427 |
if (child.is_control_center_panel()) { |
| 428 |
if (!this.show_control_center_panels) { |
| 429 |
return false; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
if (this.headers_visible) { |
| 434 |
// Show all items if headers are visible |
| 435 |
return true; |
| 436 |
} else { |
| 437 |
// Headers aren't being shown, so only show this item if |
| 438 |
// it's not a duplicate |
| 439 |
return !this.is_item_dupe(child); |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
// Hide this item if we're in a different category |
| 444 |
if (child.category != this.current_category) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
|
| 448 |
// Don't show this item if it's a control panel and we're not set to show them |
| 449 |
if (child.is_control_center_panel()) { |
| 450 |
if (!this.show_control_center_panels) { |
| 451 |
return false; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
// If we got here, then we are in a category that this item belongs to, |
| 456 |
// so show it |
| 457 |
return true; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Sorts two list items. |
| 462 |
* |
| 463 |
* If there is an active search, items will be sorted by how well they match the term. |
| 464 |
* Otherwise, they will be sorted alphebetically by their name. |
| 465 |
*/ |
| 466 |
private int do_sort_list(Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) { |
| 467 |
MenuButton child1 = row1.get_child() as MenuButton; |
| 468 |
MenuButton child2 = row2.get_child() as MenuButton; |
| 469 |
|
| 470 |
string term = this.search_term.strip(); |
| 471 |
|
| 472 |
// Check for an active search |
| 473 |
if (term.length > 0) { |
| 474 |
// Get the scores relative to the search term |
| 475 |
int sc1 = this.relevancy_service.get_score(child1.app); |
| 476 |
int sc2 = this.relevancy_service.get_score(child2.app); |
| 477 |
|
| 478 |
// The item with the lower score should be higher in the list |
| 479 |
if (sc1 < sc2) { |
| 480 |
return -1; |
| 481 |
} else if (sc1 > sc2) { |
| 482 |
return 1; |
| 483 |
} else { |
| 484 |
// Scores are equal, so sort by name |
| 485 |
return child1.app.name.collate(child2.app.name); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
// Only perform category grouping if headers are visible |
| 490 |
string parentA = Budgie.RelevancyService.searchable_string(child1.category.name); |
| 491 |
string parentB = Budgie.RelevancyService.searchable_string(child2.category.name); |
| 492 |
if (child1.category != child2.category && this.headers_visible) { |
| 493 |
return parentA.collate(parentB); |
| 494 |
} |
| 495 |
|
| 496 |
// Two application items, sort by name |
| 497 |
string nameA = Budgie.RelevancyService.searchable_string(child1.app.name); |
| 498 |
string nameB = Budgie.RelevancyService.searchable_string(child2.app.name); |
| 499 |
return nameA.collate(nameB); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Change the current group/category |
| 504 |
*/ |
| 505 |
private void update_category(CategoryButton btn) { |
| 506 |
if (btn.active) { |
| 507 |
this.current_category = btn.category; |
| 508 |
this.invalidate(); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* We need to make some changes to our display before we go showing ourselves |
| 514 |
* again! :) |
| 515 |
*/ |
| 516 |
public override void on_show() { |
| 517 |
this.all_categories.set_active(true); |
| 518 |
this.update_category(all_categories); |
| 519 |
|
| 520 |
this.applications.select_row(null); |
| 521 |
this.content_scroll.get_vadjustment().set_value(0); |
| 522 |
this.categories_scroll.get_vadjustment().set_value(0); |
| 523 |
this.categories.sensitive = true; |
| 524 |
|
| 525 |
if (!this.compact_mode) { |
| 526 |
this.categories_scroll.show_all(); |
| 527 |
} else { |
| 528 |
this.categories_scroll.hide(); |
| 529 |
} |
| 530 |
} |
| 531 |
} |