Skip to content

Commit

Permalink
Leaflet → NavigationView (#292)
Browse files Browse the repository at this point in the history
* Leaflet → NavigationView

* some casts

* Improve legibility

* Remove unnecessary casts

* Add mininum adwaita version

* Connect hidden signals, no more next page

---------

Co-authored-by: Jeremy Wootten <jeremywootten@gmail.com>
Co-authored-by: Ryan Kornheisl <ryan@skarva.tech>
  • Loading branch information
3 people authored Feb 29, 2024
1 parent 6525d15 commit 8a991fa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You'll need the following dependencies:
* libglib2.0-dev
* libgranite-7-dev
* libgtk-4-dev
* libadwaita-1-dev
* libadwaita-1-dev (>= 1.4)
* meson
* valac

Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ gmodule_dep = dependency('gmodule-2.0')
gtk_dep = dependency('gtk4', version: '>=3.10')
gee_dep = dependency('gee-0.8')
granite_dep = dependency('granite-7', version: '>=7.0.0')
adwaita_dep = dependency('libadwaita-1')
adwaita_dep = dependency('libadwaita-1', version: '>=1.4')
m_dep = meson.get_compiler('c').find_library('m', required : false)

subdir('data')
Expand Down
94 changes: 27 additions & 67 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Switchboard {

private GLib.HashTable <Gtk.Widget, Switchboard.Plug> plug_widgets;
private Gtk.Button navigation_button;
private Adw.Leaflet leaflet;
private Adw.NavigationView navigation_view;
private Gtk.HeaderBar headerbar;
private Gtk.Window main_window;
private Switchboard.CategoryView category_view;
Expand Down Expand Up @@ -153,16 +153,12 @@ namespace Switchboard {

category_view = new Switchboard.CategoryView (plug_to_open);

leaflet = new Adw.Leaflet () {
can_navigate_back = true,
can_navigate_forward = true,
can_unfold = false
};
leaflet.append (category_view);
navigation_view = new Adw.NavigationView ();
navigation_view.add (category_view);

main_window = new Gtk.Window () {
application = this,
child = leaflet,
child = navigation_view,
icon_name = application_id,
title = _("System Settings"),
titlebar = headerbar
Expand Down Expand Up @@ -190,70 +186,34 @@ namespace Switchboard {
main_window.bind_property ("title", title_label, "label");

shutdown.connect (() => {
if (plug_widgets[leaflet.visible_child] != null && plug_widgets[leaflet.visible_child] is Switchboard.Plug) {
plug_widgets[leaflet.visible_child].hidden ();
}
});

leaflet.notify["visible-child"].connect (() => {
update_navigation ();
navigation_view.visible_page.hidden ();
});

leaflet.notify["child-transition-running"].connect (() => {
update_navigation ();
});
navigation_view.popped.connect (update_navigation);
navigation_view.pushed.connect (update_navigation);
}

private void update_navigation () {
if (!leaflet.child_transition_running) {
if (plug_widgets[leaflet.get_adjacent_child (Adw.NavigationDirection.FORWARD)] != null) {
plug_widgets[leaflet.get_adjacent_child (Adw.NavigationDirection.FORWARD)].hidden ();
}

var previous_child = plug_widgets[leaflet.get_adjacent_child (Adw.NavigationDirection.BACK)];
if (previous_child != null && previous_child is Switchboard.Plug) {
previous_child.hidden ();
}

var visible_widget = leaflet.visible_child;
if (visible_widget is Switchboard.CategoryView) {
main_window.title = _("System Settings");

navigation_button.hide ();
} else {
var plug = plug_widgets[visible_widget];
if (plug != null) {
plug.shown ();
main_window.title = plug.display_name;
} else {
critical ("Visible child is not CategoryView nor is associated with a Plug.");
}


if (previous_child != null && previous_child is Switchboard.Plug) {
navigation_button.label = previous_child.display_name;
} else {
navigation_button.label = _(all_settings_label);
}

navigation_button.show ();
}
if (navigation_view.visible_page is Switchboard.CategoryView) {
navigation_button.hide ();
} else {
var previous_page = navigation_view.get_previous_page (navigation_view.visible_page);
navigation_button.label = previous_page.title;
navigation_button.show ();
}

main_window.title = navigation_view.visible_page.title;
}

public void load_plug (Switchboard.Plug plug) {
if (leaflet.child_transition_running) {
return;
}

Idle.add (() => {
while (leaflet.get_adjacent_child (Adw.NavigationDirection.FORWARD) != null) {
leaflet.remove (leaflet.get_adjacent_child (Adw.NavigationDirection.FORWARD));
}

var plug_widget = plug.get_widget ();
if (plug_widget.parent == null) {
leaflet.append (plug_widget);
var navigation_page = new Adw.NavigationPage (plug_widget, plug.display_name);
navigation_page.hidden.connect (plug.hidden);
navigation_page.shown.connect (plug.shown);

navigation_view.add (navigation_page);
}

if (plug_widgets[plug_widget] == null) {
Expand Down Expand Up @@ -281,26 +241,26 @@ namespace Switchboard {
}

if (opened_directly) {
leaflet.mode_transition_duration = 0;
navigation_view.animate_transitions = false;
opened_directly = false;
} else if (leaflet.mode_transition_duration == 0) {
leaflet.mode_transition_duration = 200;
} else if (navigation_view.animate_transitions == false) {
navigation_view.animate_transitions = true;
}

leaflet.visible_child = plug.get_widget ();
navigation_view.push ((Adw.NavigationPage) plug.get_widget ().parent);

return false;
}, GLib.Priority.DEFAULT_IDLE);
}

// Handles clicking the navigation button
private void action_navigate_back () {
if (leaflet.get_adjacent_child (Adw.NavigationDirection.BACK) == category_view) {
if (navigation_view.get_previous_page (navigation_view.visible_page) == category_view) {
opened_directly = false;
leaflet.mode_transition_duration = 200;
navigation_view.animate_transitions = true;
}

leaflet.navigate (Adw.NavigationDirection.BACK);
navigation_view.pop ();
}

// Try to find a supported plug, fallback paths like "foo/bar" to "foo"
Expand Down
11 changes: 7 additions & 4 deletions src/CategoryView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Authored by: Avi Romanoff <aviromanoff@gmail.com>
*/

public class Switchboard.CategoryView : Gtk.Box {
public class Switchboard.CategoryView : Adw.NavigationPage {
public Gee.ArrayList<SearchEntry?> plug_search_result { get; private set; }
public string? plug_to_open { get; construct set; default = null; }

Expand Down Expand Up @@ -77,9 +77,12 @@ public class Switchboard.CategoryView : Gtk.Box {
hscrollbar_policy = NEVER
};

orientation = VERTICAL;
append (search_clamp);
append (scrolled);
var box = new Gtk.Box (VERTICAL, 0);
box.append (search_clamp);
box.append (scrolled);

child = box;
title = _("All Settings");

load_default_plugs.begin ();

Expand Down

0 comments on commit 8a991fa

Please sign in to comment.