diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2022-12-15 15:12:51 +0100 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-01-02 06:15:13 -0700 |
commit | 2d08e53493102c1d81f1bbd0b8bfd1f59607cea5 (patch) | |
tree | b2b13b6473a39da69cc74d7c05d4c40405cba20c /Userland/Applications/Help | |
parent | 3cecd612bae6d6a5823786e08a783c7814f760cf (diff) | |
download | serenity-2d08e53493102c1d81f1bbd0b8bfd1f59607cea5.zip |
Help: Detect clicked page correctly
index_from_path is the only remaining model index handling function that
wasn't aware of subsections. After this change, clicked pages are
resolved to a model index correctly, eliminating the weird
subsection-expansion bugs from before.
Diffstat (limited to 'Userland/Applications/Help')
-rw-r--r-- | Userland/Applications/Help/ManualModel.cpp | 41 |
1 files changed, 27 insertions, 14 deletions
diff --git a/Userland/Applications/Help/ManualModel.cpp b/Userland/Applications/Help/ManualModel.cpp index 9f22293539..49c80f2ced 100644 --- a/Userland/Applications/Help/ManualModel.cpp +++ b/Userland/Applications/Help/ManualModel.cpp @@ -9,6 +9,7 @@ #include <AK/Utf8View.h> #include <LibManual/Node.h> #include <LibManual/PageNode.h> +#include <LibManual/Path.h> #include <LibManual/SectionNode.h> ManualModel::ManualModel() @@ -20,22 +21,34 @@ ManualModel::ManualModel() Optional<GUI::ModelIndex> ManualModel::index_from_path(StringView path) const { - for (int section = 0; section < row_count(); ++section) { - auto parent_index = index(section, 0); - for (int row = 0; row < row_count(parent_index); ++row) { - auto child_index = index(row, 0, parent_index); - auto* node = static_cast<Manual::Node const*>(child_index.internal_data()); - if (!node->is_page()) - continue; - auto* page = static_cast<Manual::PageNode const*>(node); - auto const maybe_path = page->path(); - if (maybe_path.is_error()) - return {}; - if (maybe_path.value().bytes_as_string_view() != path) - continue; - return child_index; + // The first slice removes the man pages base path plus the `/man` from the main section subdirectory. + // The second slice removes the trailing `.md`. + auto path_without_base = path.substring_view(Manual::manual_base_path.string().length() + 4); + auto url = URL::create_with_help_scheme(path_without_base.substring_view(0, path_without_base.length() - 3), {}, "man"); + + auto maybe_page = Manual::Node::try_find_from_help_url(url); + if (maybe_page.is_error()) + return {}; + + auto page = maybe_page.release_value(); + // Main section + if (page->parent() == nullptr) { + for (size_t section = 0; section < Manual::number_of_sections; ++section) { + auto main_section_index = index(static_cast<int>(section), 0); + if (main_section_index.internal_data() == page.ptr()) + return main_section_index; } + return {}; + } + auto maybe_siblings = page->parent()->children(); + if (maybe_siblings.is_error()) + return {}; + auto siblings = maybe_siblings.release_value(); + for (size_t row = 0; row < siblings.size(); ++row) { + if (siblings[row] == page) + return create_index(static_cast<int>(row), 0, page.ptr()); } + return {}; } |