Help+LibManual: Open sibling page for subsections

Clicking on a subsection now displays the sibling page, which is
intended to be the main page for that section.
This commit is contained in:
kleines Filmröllchen 2022-12-14 17:53:41 +01:00 committed by Andrew Kaster
parent 3e67f14e58
commit 201c9d7c77
6 changed files with 26 additions and 2 deletions

View file

@ -58,9 +58,9 @@ Optional<String> ManualModel::page_path(const GUI::ModelIndex& index) const
if (!index.is_valid())
return {};
auto* node = static_cast<Manual::Node const*>(index.internal_data());
if (!node->is_page())
auto page = node->document();
if (!page)
return {};
auto* page = static_cast<Manual::PageNode const*>(node);
auto path = page->path();
if (path.is_error())
return {};

View file

@ -26,6 +26,7 @@ public:
virtual bool is_page() const { return false; }
virtual bool is_open() const { return false; }
virtual ErrorOr<String> path() const = 0;
virtual PageNode const* document() const = 0;
// Backend for the command-line argument format that Help and man accept. Handles:
// [/path/to/documentation.md] (no second argument)

View file

@ -27,6 +27,7 @@ public:
virtual Node const* parent() const override;
virtual ErrorOr<String> name() const override { return m_page; };
virtual bool is_page() const override { return true; }
virtual PageNode const* document() const override { return this; };
virtual ErrorOr<String> path() const override;

View file

@ -33,6 +33,7 @@ public:
virtual ErrorOr<String> name() const override;
String const& section_name() const { return m_section; }
virtual ErrorOr<String> path() const override;
virtual PageNode const* document() const override { return nullptr; }
virtual bool is_open() const override { return m_open; }
void set_open(bool open);

View file

@ -5,6 +5,8 @@
*/
#include "SubsectionNode.h"
#include "PageNode.h"
#include <AK/TypeCasts.h>
namespace Manual {
@ -16,6 +18,24 @@ SubsectionNode::SubsectionNode(NonnullRefPtr<SectionNode> parent, StringView nam
Node const* SubsectionNode::parent() const { return m_parent; }
PageNode const* SubsectionNode::document() const
{
auto maybe_siblings = parent()->children();
if (maybe_siblings.is_error())
return nullptr;
auto siblings = maybe_siblings.release_value();
for (auto const& sibling : siblings) {
if (&*sibling == this)
continue;
auto sibling_name = sibling->name();
if (sibling_name.is_error())
continue;
if (sibling_name.value() == m_name && is<PageNode>(*sibling))
return static_cast<PageNode*>(&*sibling);
}
return nullptr;
}
ErrorOr<String> SubsectionNode::name() const { return m_name; }
ErrorOr<String> SubsectionNode::path() const

View file

@ -19,6 +19,7 @@ public:
virtual Node const* parent() const override;
virtual ErrorOr<String> path() const override;
virtual ErrorOr<String> name() const override;
virtual PageNode const* document() const override;
protected:
NonnullRefPtr<SectionNode> m_parent;