From 201c9d7c775da90763f562e0ed3d414a7d3eaf9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Wed, 14 Dec 2022 17:53:41 +0100 Subject: [PATCH] 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. --- Userland/Applications/Help/ManualModel.cpp | 4 ++-- Userland/Libraries/LibManual/Node.h | 1 + Userland/Libraries/LibManual/PageNode.h | 1 + Userland/Libraries/LibManual/SectionNode.h | 1 + .../Libraries/LibManual/SubsectionNode.cpp | 20 +++++++++++++++++++ Userland/Libraries/LibManual/SubsectionNode.h | 1 + 6 files changed, 26 insertions(+), 2 deletions(-) diff --git a/Userland/Applications/Help/ManualModel.cpp b/Userland/Applications/Help/ManualModel.cpp index 7809a0dd12..9f22293539 100644 --- a/Userland/Applications/Help/ManualModel.cpp +++ b/Userland/Applications/Help/ManualModel.cpp @@ -58,9 +58,9 @@ Optional ManualModel::page_path(const GUI::ModelIndex& index) const if (!index.is_valid()) return {}; auto* node = static_cast(index.internal_data()); - if (!node->is_page()) + auto page = node->document(); + if (!page) return {}; - auto* page = static_cast(node); auto path = page->path(); if (path.is_error()) return {}; diff --git a/Userland/Libraries/LibManual/Node.h b/Userland/Libraries/LibManual/Node.h index aceca11084..9e1c2aeda1 100644 --- a/Userland/Libraries/LibManual/Node.h +++ b/Userland/Libraries/LibManual/Node.h @@ -26,6 +26,7 @@ public: virtual bool is_page() const { return false; } virtual bool is_open() const { return false; } virtual ErrorOr 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) diff --git a/Userland/Libraries/LibManual/PageNode.h b/Userland/Libraries/LibManual/PageNode.h index 3dc33d9a67..7c48a7dc8d 100644 --- a/Userland/Libraries/LibManual/PageNode.h +++ b/Userland/Libraries/LibManual/PageNode.h @@ -27,6 +27,7 @@ public: virtual Node const* parent() const override; virtual ErrorOr name() const override { return m_page; }; virtual bool is_page() const override { return true; } + virtual PageNode const* document() const override { return this; }; virtual ErrorOr path() const override; diff --git a/Userland/Libraries/LibManual/SectionNode.h b/Userland/Libraries/LibManual/SectionNode.h index d0d4476eaa..4e228799f9 100644 --- a/Userland/Libraries/LibManual/SectionNode.h +++ b/Userland/Libraries/LibManual/SectionNode.h @@ -33,6 +33,7 @@ public: virtual ErrorOr name() const override; String const& section_name() const { return m_section; } virtual ErrorOr 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); diff --git a/Userland/Libraries/LibManual/SubsectionNode.cpp b/Userland/Libraries/LibManual/SubsectionNode.cpp index 1d166b9282..18f81d54fe 100644 --- a/Userland/Libraries/LibManual/SubsectionNode.cpp +++ b/Userland/Libraries/LibManual/SubsectionNode.cpp @@ -5,6 +5,8 @@ */ #include "SubsectionNode.h" +#include "PageNode.h" +#include namespace Manual { @@ -16,6 +18,24 @@ SubsectionNode::SubsectionNode(NonnullRefPtr 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(*sibling)) + return static_cast(&*sibling); + } + return nullptr; +} + ErrorOr SubsectionNode::name() const { return m_name; } ErrorOr SubsectionNode::path() const diff --git a/Userland/Libraries/LibManual/SubsectionNode.h b/Userland/Libraries/LibManual/SubsectionNode.h index c2f8461bd8..fec53604dc 100644 --- a/Userland/Libraries/LibManual/SubsectionNode.h +++ b/Userland/Libraries/LibManual/SubsectionNode.h @@ -19,6 +19,7 @@ public: virtual Node const* parent() const override; virtual ErrorOr path() const override; virtual ErrorOr name() const override; + virtual PageNode const* document() const override; protected: NonnullRefPtr m_parent;