Skip to content

Commit d65dfe0

Browse files
Manuals: Sync sidebar without collapsing rows (#691)
1 parent 8ee17a7 commit d65dfe0

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/Manuals/DocumentationViewer.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ export default function DocumentationViewer({ application }) {
158158
adj.connect("value-changed", () => {
159159
if (scrolled_to) {
160160
const index = browse_selection_model.selected;
161-
const adj = browse_page.get_vscrollbar().adjustment;
162161
const bottom_edge = (index + 1) * 38 - adj.value;
163162
const top_edge = bottom_edge - 38;
164163
// If row is not visible after scroll_to, adjust
@@ -260,12 +259,8 @@ function collapseAllRows(model) {
260259

261260
function selectSidebarItem(browse_list_view, path) {
262261
const selection_model = browse_list_view.model;
263-
collapseAllRows(selection_model.model);
264-
for (const index of path.slice(0, -1)) {
265-
const row = selection_model.model.get_row(index);
266-
row.expanded = true;
267-
}
268-
const index = path[path.length - 1];
262+
const tree_model = selection_model.model;
263+
const index = getItemIndex(tree_model, path);
269264
// If possible, overshoot scrolling by one row to ensure selected row is visible
270265
index + 1 === selection_model.n_items
271266
? browse_list_view.scroll_to(index, Gtk.ListScrollFlags.NONE, null)
@@ -274,6 +269,31 @@ function selectSidebarItem(browse_list_view, path) {
274269
scrolled_to = true;
275270
}
276271

272+
function getItemIndex(tree_model, path) {
273+
let relative_index = 0; // Relative index of the item under its parent
274+
let absolute_index = 0; // Index of the item in the entire model
275+
let skip = 0; // Number of items to skip due to expanded rows
276+
277+
for (let i = 0; i < path.length; i++) {
278+
while (relative_index < path[i]) {
279+
const row = tree_model.get_row(absolute_index);
280+
if (row.expanded) {
281+
skip += row.children.get_n_items();
282+
}
283+
if (!skip) relative_index++; // Go to next sibling
284+
else skip--;
285+
absolute_index++;
286+
}
287+
// Check to ensure the last item is not expanded
288+
if (i < path.length - 1) {
289+
tree_model.get_row(absolute_index).expanded = true;
290+
absolute_index++;
291+
relative_index = 1;
292+
}
293+
}
294+
return absolute_index;
295+
}
296+
277297
async function loadLibrary(model, directory) {
278298
try {
279299
const json_file = directory.get_child("index.json");
@@ -340,10 +360,7 @@ function flattenModel(
340360
for (const item of list_store) {
341361
if (item.search_name) flattened_model.append(item);
342362
if (item.children) {
343-
flattenModel(item.children, flattened_model, [
344-
...path,
345-
path[path.length - 1] + 1,
346-
]);
363+
flattenModel(item.children, flattened_model, [...path, 1]);
347364
}
348365
URI_TO_SIDEBAR_PATH[item.uri] = path.slice();
349366
path[path.length - 1]++;

0 commit comments

Comments
 (0)