Skip to content

Commit

Permalink
CategoryView: absorb search bar (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Jan 23, 2024
1 parent c0c3399 commit be73a08
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 89 deletions.
81 changes: 2 additions & 79 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace Switchboard {
public class SwitchboardApp : Gtk.Application {
public Gtk.SearchEntry search_box { get; private set; }

private string all_settings_label = N_("All Settings");

private GLib.HashTable <Gtk.Widget, Switchboard.Plug> plug_widgets;
Expand All @@ -32,7 +30,6 @@ namespace Switchboard {
private Gtk.Window main_window;
private Switchboard.CategoryView category_view;
private Gtk.Label title_label;
private Gtk.Stack title_stack;

private static bool opened_directly = false;
private static string? link = null;
Expand Down Expand Up @@ -145,31 +142,12 @@ namespace Switchboard {
);
navigation_button.get_style_context ().add_class ("back-button");

var search_box_eventcontrollerkey = new Gtk.EventControllerKey ();

search_box = new Gtk.SearchEntry () {
placeholder_text = _("Search Settings"),
sensitive = false
};
search_box.add_controller (search_box_eventcontrollerkey);

title_label = new Gtk.Label ("");
title_label.add_css_class (Granite.STYLE_CLASS_TITLE_LABEL);

title_stack = new Gtk.Stack () {
hexpand = true
};
title_stack.add_child (search_box);
title_stack.add_child (title_label);

var title_clamp = new Adw.Clamp () {
child = title_stack,
maximum_size = 800
};

headerbar = new Gtk.HeaderBar () {
show_title_buttons = true,
title_widget = title_clamp
title_widget = title_label
};
headerbar.pack_start (navigation_button);

Expand All @@ -183,19 +161,9 @@ namespace Switchboard {
};
leaflet.append (category_view);

var searchview = new SearchView ();

var search_stack = new Gtk.Stack () {
transition_type = Gtk.StackTransitionType.OVER_DOWN_UP
};
search_stack.add_child (leaflet);
search_stack.add_child (searchview);

var window_eventcontrollerkey = new Gtk.EventControllerKey ();

main_window = new Gtk.Window () {
application = this,
child = search_stack,
child = leaflet,
icon_name = application_id,
title = _("System Settings"),
titlebar = headerbar
Expand All @@ -222,56 +190,19 @@ namespace Switchboard {

main_window.bind_property ("title", title_label, "label");

search_box.search_changed.connect (() => {
if (search_box.text.length > 0) {
search_stack.visible_child = searchview;
} else {
search_stack.visible_child = leaflet;
}
});

search_box.activate.connect (() => {
searchview.activate_first_item ();
});

search_box_eventcontrollerkey.key_released.connect ((keyval, keycode, state) => {
switch (keyval) {
case Gdk.Key.Down:
search_box.move_focus (Gtk.DirectionType.TAB_FORWARD);
break;
case Gdk.Key.Escape:
search_box.text = "";
break;
default:
break;
}
});

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

((Gtk.Widget) main_window).add_controller (window_eventcontrollerkey);
window_eventcontrollerkey.key_pressed.connect ((keyval, keycode, modifiers) => {
window_eventcontrollerkey.forward (search_box.get_delegate ());
return Gdk.EVENT_PROPAGATE;
});

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

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

if (Switchboard.PlugsManager.get_default ().has_plugs () == false) {
category_view.show_alert (_("No Settings Found"), _("Install some and re-launch Switchboard."), "dialog-warning");
} else {
search_box.sensitive = true;
}
}

private void update_navigation () {
Expand All @@ -288,17 +219,13 @@ namespace Switchboard {
var visible_widget = leaflet.visible_child;
if (visible_widget is Switchboard.CategoryView) {
main_window.title = _("System Settings");
title_stack.visible_child = search_box;

navigation_button.hide ();

search_box.sensitive = Switchboard.PlugsManager.get_default ().has_plugs ();
} else {
var plug = plug_widgets[visible_widget];
if (plug != null) {
plug.shown ();
main_window.title = plug.display_name;
title_stack.visible_child = title_label;
} else {
critical ("Visible child is not CategoryView nor is associated with a Plug.");
}
Expand All @@ -311,11 +238,7 @@ namespace Switchboard {
}

navigation_button.show ();

search_box.sensitive = false;
}

search_box.text = "";
}
}

Expand Down
73 changes: 71 additions & 2 deletions src/CategoryView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace Switchboard {

public class CategoryView : Gtk.Box {
private Gtk.SearchEntry search_box;

public PlugsSearch plug_search { get; construct; }
public Gee.ArrayList<SearchEntry?> plug_search_result { get; construct; }
public Switchboard.Category personal_category { get; construct; }
Expand All @@ -35,6 +37,25 @@ namespace Switchboard {
private Granite.Placeholder alert_view;

construct {
var search_box_eventcontrollerkey = new Gtk.EventControllerKey ();

search_box = new Gtk.SearchEntry () {
margin_top = 12,
margin_end = 12,
margin_bottom = 12,
margin_start = 12,
placeholder_text = _("Search Settings")
};
search_box.add_controller (search_box_eventcontrollerkey);

var search_clamp = new Adw.Clamp () {
child = search_box,
maximum_size = 800,
tightening_threshold = 800
};

var searchview = new SearchView (search_box);

alert_view = new Granite.Placeholder ("");

personal_category = new Switchboard.Category (Switchboard.Plug.Category.PERSONAL);
Expand All @@ -49,7 +70,8 @@ namespace Switchboard {
margin_top = 12,
margin_end = 12,
margin_bottom = 12,
margin_start = 12
margin_start = 12,
vexpand = true
};
category_box.append (personal_category);
category_box.append (hardware_category);
Expand All @@ -71,7 +93,54 @@ namespace Switchboard {
tightening_threshold = 800
};

append (clamp);
var search_stack = new Gtk.Stack () {
transition_type = CROSSFADE
};
search_stack.add_child (clamp);
search_stack.add_child (searchview);

orientation = VERTICAL;
append (search_clamp);
append (search_stack);

if (Switchboard.PlugsManager.get_default ().has_plugs () == false) {
show_alert (_("No Settings Found"), _("Install some and re-launch Switchboard."), "dialog-warning");
} else {
search_box.sensitive = true;
}

search_box.search_changed.connect (() => {
if (search_box.text.length > 0) {
search_stack.visible_child = searchview;
} else {
search_stack.visible_child = clamp;
}
});

search_box.activate.connect (() => {
searchview.activate_first_item ();
});

search_box_eventcontrollerkey.key_released.connect ((keyval, keycode, state) => {
switch (keyval) {
case Gdk.Key.Down:
search_box.move_focus (Gtk.DirectionType.TAB_FORWARD);
break;
case Gdk.Key.Escape:
search_box.text = "";
break;
default:
break;
}
});

var eventcontrollerkey = new Gtk.EventControllerKey ();
eventcontrollerkey.key_pressed.connect (() => {
eventcontrollerkey.forward (search_box.get_delegate ());
return Gdk.EVENT_PROPAGATE;
});

add_controller (eventcontrollerkey);
}

public CategoryView (string? plug = null) {
Expand Down
20 changes: 12 additions & 8 deletions src/SearchView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
*/

public class Switchboard.SearchView : Gtk.Box {
private Gtk.SearchEntry search_entry;
public Gtk.SearchEntry search_entry { get; construct; }
private Gtk.ListBox listbox;

public SearchView (Gtk.SearchEntry search_entry) {
Object (search_entry: search_entry);
}

construct {
var alert_view = new Granite.Placeholder ("") {
description = _("Try changing search terms."),
Expand All @@ -29,24 +33,24 @@ public class Switchboard.SearchView : Gtk.Box {

unowned SwitchboardApp app = (SwitchboardApp) GLib.Application.get_default ();

search_entry = app.search_box;

listbox = new Gtk.ListBox ();
listbox.add_css_class ("rich-list");
listbox.selection_mode = Gtk.SelectionMode.BROWSE;
listbox = new Gtk.ListBox () {
selection_mode = BROWSE
};
listbox.add_css_class (Granite.STYLE_CLASS_BACKGROUND);
listbox.add_css_class (Granite.STYLE_CLASS_RICH_LIST);
listbox.set_filter_func (filter_func);
listbox.set_placeholder (alert_view);

var clamp = new Adw.Clamp () {
child = listbox,
maximum_size = 800
maximum_size = 800,
tightening_threshold = 800
};

var scrolled = new Gtk.ScrolledWindow () {
child = clamp
};

add_css_class (Granite.STYLE_CLASS_VIEW);
append (scrolled);

load_plugs.begin ();
Expand Down

0 comments on commit be73a08

Please sign in to comment.