mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:07:45 +00:00
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.
This commit is contained in:
parent
3cecd612ba
commit
2d08e53493
1 changed files with 27 additions and 14 deletions
|
@ -9,6 +9,7 @@
|
||||||
#include <AK/Utf8View.h>
|
#include <AK/Utf8View.h>
|
||||||
#include <LibManual/Node.h>
|
#include <LibManual/Node.h>
|
||||||
#include <LibManual/PageNode.h>
|
#include <LibManual/PageNode.h>
|
||||||
|
#include <LibManual/Path.h>
|
||||||
#include <LibManual/SectionNode.h>
|
#include <LibManual/SectionNode.h>
|
||||||
|
|
||||||
ManualModel::ManualModel()
|
ManualModel::ManualModel()
|
||||||
|
@ -20,22 +21,34 @@ ManualModel::ManualModel()
|
||||||
|
|
||||||
Optional<GUI::ModelIndex> ManualModel::index_from_path(StringView path) const
|
Optional<GUI::ModelIndex> ManualModel::index_from_path(StringView path) const
|
||||||
{
|
{
|
||||||
for (int section = 0; section < row_count(); ++section) {
|
// The first slice removes the man pages base path plus the `/man` from the main section subdirectory.
|
||||||
auto parent_index = index(section, 0);
|
// The second slice removes the trailing `.md`.
|
||||||
for (int row = 0; row < row_count(parent_index); ++row) {
|
auto path_without_base = path.substring_view(Manual::manual_base_path.string().length() + 4);
|
||||||
auto child_index = index(row, 0, parent_index);
|
auto url = URL::create_with_help_scheme(path_without_base.substring_view(0, path_without_base.length() - 3), {}, "man");
|
||||||
auto* node = static_cast<Manual::Node const*>(child_index.internal_data());
|
|
||||||
if (!node->is_page())
|
auto maybe_page = Manual::Node::try_find_from_help_url(url);
|
||||||
continue;
|
if (maybe_page.is_error())
|
||||||
auto* page = static_cast<Manual::PageNode const*>(node);
|
|
||||||
auto const maybe_path = page->path();
|
|
||||||
if (maybe_path.is_error())
|
|
||||||
return {};
|
return {};
|
||||||
if (maybe_path.value().bytes_as_string_view() != path)
|
|
||||||
continue;
|
auto page = maybe_page.release_value();
|
||||||
return child_index;
|
// 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 {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue