Skip to content

Commit

Permalink
Move sidebar to its own class
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit committed Apr 1, 2024
1 parent 0cc9efc commit e30cae8
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 188 deletions.
2 changes: 1 addition & 1 deletion po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
src/Plug.vala
src/Permissions/PermissionsPlug.vala
src/Sidebar.vala
src/Permissions/Widgets/AppSettingsView.vala
src/Permissions/Widgets/SidebarRow.vala
src/Startup/Utils.vala
Expand Down
194 changes: 8 additions & 186 deletions src/Plug.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
* Marius Meisenzahl <mariusmeisenzahl@gmail.com>
*/

public class ApplicationsPlug : Switchboard.Plug {
private const string DEFAULTS = "preferences-system";
private const string STARTUP = "preferences-desktop-startup";
private const string PERMISSIONS = "permissions";
public class Applications.Plug : Switchboard.Plug {
public const string DEFAULTS = "preferences-system";
public const string STARTUP = "preferences-desktop-startup";
public const string PERMISSIONS = "permissions";

private Gtk.Grid grid;
private Gtk.SearchEntry search_entry;
private Gtk.Stack stack;

public ApplicationsPlug () {
public Plug () {
GLib.Intl.bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
GLib.Intl.bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");

Expand All @@ -53,86 +52,14 @@ public class ApplicationsPlug : Switchboard.Plug {
if (grid == null) {
var app_settings_view = new Permissions.Widgets.AppSettingsView ();

stack = new Gtk.Stack () {
hexpand = true,
vexpand = true
};
stack = new Gtk.Stack ();
stack.add_named (new Defaults.Plug (), DEFAULTS);
stack.add_named (new Startup.Plug (), STARTUP);
stack.add_named (app_settings_view, PERMISSIONS);

var defaults_row = new SimpleSidebarRow (
_("Defaults"), DEFAULTS
);

var startup_row = new SimpleSidebarRow (
_("Startup"), STARTUP
);

search_entry = new Gtk.SearchEntry () {
placeholder_text = _("Search Apps"),
margin_top = 6,
margin_bottom = 6,
margin_start = 6,
margin_end = 6,
hexpand = true
};

var search_revealer = new Gtk.Revealer () {
child = search_entry
};

var search_toggle = new Gtk.ToggleButton () {
icon_name = "edit-find-symbolic",
tooltip_text = _("Search Apps")
};

var headerbar = new Adw.HeaderBar () {
show_end_title_buttons = false,
show_title = false
};
headerbar.pack_end (search_toggle);

var listbox = new Gtk.ListBox () {
vexpand = true,
selection_mode = BROWSE
};
listbox.add_css_class (Granite.STYLE_CLASS_SIDEBAR);
listbox.set_sort_func ((Gtk.ListBoxSortFunc) sort_func);
listbox.set_header_func ((row, before) => {
if (row is SimpleSidebarRow && !(before is SimpleSidebarRow)) {
row.set_header (new Granite.HeaderLabel (_("System")));
return;
}

if (row is Permissions.SidebarRow && before is SimpleSidebarRow) {
row.set_header (new Granite.HeaderLabel (_("Apps")));
return;
}

row.set_header (null);
});
listbox.set_filter_func (filter_function);
listbox.append (defaults_row);
listbox.append (startup_row);

var scrolled_window = new Gtk.ScrolledWindow () {
child = listbox,
hscrollbar_policy = NEVER
};

var toolbarview = new Adw.ToolbarView () {
content = scrolled_window,
top_bar_style = FLAT,
};
toolbarview.add_top_bar (headerbar);
toolbarview.add_top_bar (search_revealer);

var sidebar = new Sidebar ();
sidebar.append (toolbarview);
var sidebar = new Sidebar (stack);

var paned = new Gtk.Paned (HORIZONTAL) {
position = 200,
start_child = sidebar,
end_child = stack,
shrink_start_child = false,
Expand All @@ -142,43 +69,6 @@ public class ApplicationsPlug : Switchboard.Plug {

grid = new Gtk.Grid ();
grid.attach (paned, 0, 0);

Permissions.Backend.AppManager.get_default ().apps.foreach ((id, app) => {
var app_entry = new Permissions.SidebarRow (app);
listbox.append (app_entry);
});

listbox.row_selected.connect ((row) => {
if (row == null) {
return;
}

if (row is Permissions.SidebarRow) {
stack.visible_child = app_settings_view;
app_settings_view.selected_app = ((Permissions.SidebarRow)row).app;
} else if (row is SimpleSidebarRow) {
if (((SimpleSidebarRow) row).icon_name == DEFAULTS) {
stack.visible_child_name = DEFAULTS;
} else if (((SimpleSidebarRow) row).icon_name == STARTUP) {
stack.visible_child_name = STARTUP;
}

}
});

search_entry.search_changed.connect (() => {
listbox.invalidate_filter ();
});

search_toggle.bind_property ("active", search_revealer, "reveal-child");

search_revealer.notify["child-revealed"].connect (() => {
if (search_revealer.child_revealed) {
search_entry.grab_focus ();
} else {
search_entry.text = "";
}
});
}

return grid;
Expand Down Expand Up @@ -223,76 +113,8 @@ public class ApplicationsPlug : Switchboard.Plug {
search_results.set ("%s%s%s".printf (display_name, _("Default"), _("File Browser")), DEFAULTS);
return search_results;
}

private bool filter_function (Gtk.ListBoxRow row) {
if (search_entry.text != "") {
if (row is SimpleSidebarRow) {
return false;
}

var search_term = search_entry.text.down ();
var row_name = ((Permissions.SidebarRow) row).app.name.down ();

return search_term in row_name;
}

return true;
}

[CCode (instance_pos = -1)]
private int sort_func (Gtk.ListBoxRow row1, Gtk.ListBoxRow row2) {
if (row1 is Permissions.SidebarRow && row2 is Permissions.SidebarRow) {
return ((Permissions.SidebarRow) row1).app.name.collate (((Permissions.SidebarRow) row2).app.name);
}

return 0;
}

private class SimpleSidebarRow : Gtk.ListBoxRow {
public string label { get; construct; }
public string icon_name { get; construct; }

public SimpleSidebarRow (string label, string icon_name) {
Object (
label: label,
icon_name: icon_name
);
}

construct {
var image = new Gtk.Image.from_icon_name (icon_name) {
icon_size = LARGE
};

var title_label = new Gtk.Label (label) {
ellipsize = END,
xalign = 0
};
title_label.add_css_class (Granite.STYLE_CLASS_H3_LABEL);

var grid = new Gtk.Grid () {
column_spacing = 6
};
grid.attach (image, 0, 0);
grid.attach (title_label, 1, 0);

hexpand = true;
child = grid;
}
}

// Workaround to set styles
private class Sidebar : Gtk.Box {
class construct {
set_css_name ("settingssidebar");
}

construct {
add_css_class (Granite.STYLE_CLASS_SIDEBAR);
}
}
}

public Switchboard.Plug get_plug (Module module) {
return new ApplicationsPlug ();
return new Applications.Plug ();
}
Loading

0 comments on commit e30cae8

Please sign in to comment.