Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ json_glib_dep = dependency('json-glib-1.0')
switchboard_dep = dependency('switchboard-3')
libhandy_dep = dependency('libhandy-1', version: '>= 0.83.0')
wingpanel_dep = dependency('wingpanel', version: '>=2.1.0')
math_dep = meson.get_compiler('c').find_library('m')
posix_dep = meson.get_compiler('vala').find_library('posix')

zeitgeist_dep = []
Expand Down
63 changes: 38 additions & 25 deletions src/Views/GridView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
public class Slingshot.Widgets.Grid : Gtk.Box {
private const int PAGE_ROWS = 3;
private const int PAGE_COLUMNS = 5;
private const int MAX_APPS_IN_PAGE = (PAGE_ROWS * PAGE_COLUMNS);

private Hdy.Carousel paginator;
private Gtk.EventControllerKey key_controller;
Expand Down Expand Up @@ -75,35 +76,54 @@ public class Slingshot.Widgets.Grid : Gtk.Box {
paginator.remove (child);
}

var grid = add_new_grid ();
// Where to insert new app button
var next_row_index = 0;
var next_col_index = 0;
var apps = app_system.get_apps_by_name ();
var num_apps = apps.length ();

foreach (Backend.App app in app_system.get_apps_by_name ()) {
var app_button = new Widgets.AppButton (app);
app_button.app_launched.connect (() => ((Gtk.Popover) get_ancestor (typeof (Gtk.Popover))).popdown ());
// e.g. Number of pages needed to show 33 apps is 3; 2 pages with fully apps and 1 additional page
var num_pages = (int) Math.ceil ((double) num_apps / MAX_APPS_IN_PAGE);

if (next_col_index == PAGE_COLUMNS) {
next_col_index = 0;
next_row_index++;
}
for (uint page_idx = 0; page_idx < num_pages; page_idx++) {
// Number of total apps populated after dealing with this page
// clamp is used to handle last page correctly; has no effect for other pages
var num_apps_populated = ((page_idx + 1) * MAX_APPS_IN_PAGE).clamp (1, num_apps);

if (next_row_index == PAGE_ROWS) {
grid = add_new_grid ();
next_row_index = 0;
next_col_index = 0;
}
// Number of apps in this page, in range of 1 to MAX_APPS_IN_PAGE
// Subtract 1 before calculating modulo and finally adds 1 to prevent the result becomes 0
var num_apps_in_page = ((num_apps_populated - 1) % MAX_APPS_IN_PAGE) + 1;

grid.attach (app_button, (int)next_col_index, (int)next_row_index);
next_col_index++;
populate_page (apps.nth (page_idx * MAX_APPS_IN_PAGE), num_apps_in_page);
}

show_all ();
// Show first page after populating the carousel
set_page (0);
}

private void populate_page (SList<Backend.App> apps, uint num_apps) {
var grid = add_new_grid ();

for (var row = 0; row < PAGE_ROWS; row++) {
for (var column = 0; column < PAGE_COLUMNS; column++) {
Gtk.Widget widget_to_attach;

var app_idx = (row * PAGE_COLUMNS) + column;
if (app_idx < num_apps) {
unowned var app = apps.nth_data (app_idx);

var app_button = new Widgets.AppButton (app);
app_button.app_launched.connect (() => ((Gtk.Popover) get_ancestor (typeof (Gtk.Popover))).popdown ());

widget_to_attach = app_button;
} else {
// Fake grid in case there are not enough apps to fill the page
widget_to_attach = new Gtk.Grid ();
}

grid.attach (widget_to_attach, column, row);
}
}
}

private Gtk.Grid add_new_grid () {
var grid = new Gtk.Grid () {
hexpand = true,
Expand All @@ -116,13 +136,6 @@ public class Slingshot.Widgets.Grid : Gtk.Box {
column_spacing = 0
};

// Fake grids in case there are not enough apps to fill the grid
for (var row = 0; row < PAGE_ROWS; row++) {
for (var column = 0; column < PAGE_COLUMNS; column++) {
grid.attach (new Gtk.Grid (), column, row, 1, 1);
}
}

paginator.add (grid);

return grid;
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ dependencies = [
zeitgeist_dep,
wingpanel_dep,
libhandy_dep,
math_dep,
posix_dep,
]

Expand Down