Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SettingsSidebar: bind stack model to list #302

Merged
merged 4 commits into from
Mar 21, 2024
Merged
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
32 changes: 13 additions & 19 deletions lib/SettingsSidebar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public class Switchboard.SettingsSidebar : Gtk.Widget {
activate_on_single_click = true,
selection_mode = Gtk.SelectionMode.SINGLE
};
listbox.bind_model (stack.pages, create_widget_func);

var scrolled = new Gtk.ScrolledWindow () {
hscrollbar_policy = Gtk.PolicyType.NEVER,
Expand All @@ -95,16 +96,20 @@ public class Switchboard.SettingsSidebar : Gtk.Widget {

add_css_class (Granite.STYLE_CLASS_SIDEBAR);

on_sidebar_changed ();
stack.pages.items_changed.connect (on_sidebar_changed);

listbox.row_selected.connect ((row) => {
stack.visible_child = ((SettingsSidebarRow) row).page;
});

listbox.set_header_func ((row, before) => {
var header = ((SettingsSidebarRow) row).header;
if (header != null) {
if (before != null) {
var before_header = ((SettingsSidebarRow) before).header;
if (before_header != null && before_header == header) {
return;
}
}

var label = new Gtk.Label (header) {
halign = Gtk.Align.START,
xalign = 0
Expand All @@ -126,22 +131,11 @@ public class Switchboard.SettingsSidebar : Gtk.Widget {
get_first_child ().unparent ();
}

private void on_sidebar_changed () {
weak Gtk.Widget listbox_child = listbox.get_first_child ();
while (listbox_child != null) {
weak Gtk.Widget next_child = listbox_child.get_next_sibling ();
listbox.remove (listbox_child);
listbox_child = next_child;
}
private Gtk.Widget create_widget_func (Object object) {
unowned var stack_page = (Gtk.StackPage) object;
unowned var page = (SettingsPage) stack_page.child;
var row = new SettingsSidebarRow (page);

weak Gtk.Widget child = stack.get_first_child ();
while (child != null) {
if (child is SettingsPage) {
var row = new SettingsSidebarRow ((SettingsPage) child);
listbox.append (row);
}

child = child.get_next_sibling ();
}
return row;
}
}
Loading