Skip to content

Commit d4066fc

Browse files
authored
library: Add search bar inside library view (#950)
1 parent 5a29338 commit d4066fc

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

src/Library/Library.blp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ Adw.Window window {
1818

1919
content: Adw.PreferencesPage {
2020
Adw.PreferencesGroup {
21-
Picture picture_illustration {
22-
can-shrink: false;
23-
margin-bottom: 32;
21+
Box {
22+
halign: center;
23+
vexpand: false;
24+
25+
Picture picture_illustration {
26+
can-shrink: false;
27+
margin-bottom: 32;
28+
}
2429
}
2530

2631
Label {
@@ -30,6 +35,14 @@ Adw.Window window {
3035
"title-1"
3136
]
3237
}
38+
39+
SearchEntry search_entry {
40+
search-delay: 100;
41+
placeholder-text: _("Search demos");
42+
activates-default: true;
43+
width-request: 400;
44+
margin-top: 32;
45+
}
3346
}
3447

3548
Adw.PreferencesGroup library_uncategorized {}

src/Library/Library.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { build } from "../../troll/src/builder.js";
1919

2020
export default function Library({ application }) {
2121
const objects = build(resource);
22-
const { window, picture_illustration } = objects;
22+
const { window, picture_illustration, search_entry } = objects;
2323
window.application = application;
2424
picture_illustration.set_resource(illustration);
2525

@@ -32,6 +32,8 @@ export default function Library({ application }) {
3232
window.connect("close-request", quitOnLastWindowClose);
3333

3434
const demos = getDemos();
35+
const widgets_map = new Map();
36+
const category_map = new Map();
3537
demos.forEach((demo) => {
3638
const widget = new EntryRow({ demo: demo });
3739
if (demo.name === "Welcome") last_selected = widget;
@@ -45,10 +47,27 @@ export default function Library({ application }) {
4547
language,
4648
}).catch(console.error);
4749
});
48-
50+
if (!category_map.has(demo.category)) {
51+
category_map.set(demo.category, objects[`library_${demo.category}`]);
52+
}
4953
objects[`library_${demo.category}`].add(widget);
54+
widgets_map.set(demo.name, { widget, category: demo.category });
5055
});
5156

57+
search_entry.connect("search-changed", () => {
58+
const search_term = search_entry.get_text().toLowerCase();
59+
const visible_categories = new Set();
60+
61+
widgets_map.forEach(({ widget, category }, demo_name) => {
62+
const is_match = demo_name.toLowerCase().includes(search_term);
63+
widget.visible = is_match;
64+
if (is_match) visible_categories.add(category);
65+
});
66+
67+
category_map.forEach((category_widget, category_name) => {
68+
category_widget.visible = visible_categories.has(category_name);
69+
});
70+
});
5271
const action_library = new Gio.SimpleAction({
5372
name: "library",
5473
parameter_type: null,
@@ -86,9 +105,9 @@ async function openDemo({ application, demo_name, language }) {
86105
session.settings.set_int("code-language", language.index);
87106
global_settings.set_int("recent-code-language", language.index);
88107

89-
// If the user explictely requested to open the demo
108+
// If the user explicitly requested to open the demo
90109
// in a specific language then that's probably what they are interested in
91-
// therefor override the demo default and force show the code panel
110+
// therefore override the demo default and force show the code panel
92111
session.settings.set_boolean("show-code", true);
93112
}
94113

0 commit comments

Comments
 (0)