1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

LibManual: Refactor SectionNode in preparation for subsections

- Calculate the full name on demand
- Make section and name protected
- Reorder some members logically
- Change the name getter to be fallible, as some implementors need to
  allocate
This commit is contained in:
kleines Filmröllchen 2022-06-18 21:53:44 +02:00 committed by Linus Groh
parent f502e24bd7
commit 4625f7aab5
6 changed files with 23 additions and 10 deletions

View file

@ -307,6 +307,7 @@ void MainWidget::open_page(Optional<String> const& path)
m_web_view->load_empty_document(); m_web_view->load_empty_document();
return; return;
} }
dbgln("open page: {}", path.value());
open_url(URL::create_with_url_or_path(path.value().to_deprecated_string())); open_url(URL::create_with_url_or_path(path.value().to_deprecated_string()));
} }

View file

@ -47,7 +47,10 @@ Optional<String> ManualModel::page_name(const GUI::ModelIndex& index) const
if (!node->is_page()) if (!node->is_page())
return {}; return {};
auto* page = static_cast<Manual::PageNode const*>(node); auto* page = static_cast<Manual::PageNode const*>(node);
return page->name(); auto path = page->name();
if (path.is_error())
return {};
return path.release_value();
} }
Optional<String> ManualModel::page_path(const GUI::ModelIndex& index) const Optional<String> ManualModel::page_path(const GUI::ModelIndex& index) const
@ -159,7 +162,9 @@ GUI::Variant ManualModel::data(const GUI::ModelIndex& index, GUI::ModelRole role
return DeprecatedString(page.release_value()); return DeprecatedString(page.release_value());
return {}; return {};
case GUI::ModelRole::Display: case GUI::ModelRole::Display:
return node->name(); if (auto name = node->name(); !name.is_error())
return name.release_value();
return {};
case GUI::ModelRole::Icon: case GUI::ModelRole::Icon:
if (node->is_page()) if (node->is_page())
return m_page_icon; return m_page_icon;

View file

@ -22,7 +22,7 @@ public:
virtual NonnullRefPtrVector<Node>& children() const = 0; virtual NonnullRefPtrVector<Node>& children() const = 0;
virtual Node const* parent() const = 0; virtual Node const* parent() const = 0;
virtual String name() const = 0; virtual ErrorOr<String> name() const = 0;
virtual bool is_page() const { return false; } virtual bool is_page() const { return false; }
virtual bool is_open() const { return false; } virtual bool is_open() const { return false; }
}; };

View file

@ -25,7 +25,7 @@ public:
virtual NonnullRefPtrVector<Node>& children() const override; virtual NonnullRefPtrVector<Node>& children() const override;
virtual Node const* parent() const override; virtual Node const* parent() const override;
virtual String name() const override { return m_page; }; virtual ErrorOr<String> name() const override { return m_page; };
virtual bool is_page() const override { return true; } virtual bool is_page() const override { return true; }
ErrorOr<String> path() const; ErrorOr<String> path() const;

View file

@ -17,6 +17,11 @@ ErrorOr<String> SectionNode::path() const
return String::formatted("/usr/share/man/man{}", m_section); return String::formatted("/usr/share/man/man{}", m_section);
} }
ErrorOr<String> SectionNode::name() const
{
return String::formatted("{}. {}", m_section, m_name);
}
ErrorOr<void> SectionNode::reify_if_needed() const ErrorOr<void> SectionNode::reify_if_needed() const
{ {
if (m_reified) if (m_reified)

View file

@ -19,7 +19,7 @@ public:
SectionNode(StringView section, StringView name) SectionNode(StringView section, StringView name)
: m_section(MUST(String::from_utf8(section))) : m_section(MUST(String::from_utf8(section)))
, m_full_name(MUST(String::formatted("{}. {}", section, name))) , m_name(MUST(String::from_utf8(name)))
{ {
} }
@ -30,18 +30,20 @@ public:
} }
virtual Node const* parent() const override { return nullptr; } virtual Node const* parent() const override { return nullptr; }
virtual String name() const override { return m_full_name; } virtual ErrorOr<String> name() const override;
String const& section_name() const { return m_section; }
ErrorOr<String> path() const;
virtual bool is_open() const override { return m_open; } virtual bool is_open() const override { return m_open; }
void set_open(bool open); void set_open(bool open);
String const& section_name() const { return m_section; } protected:
ErrorOr<String> path() const; String m_section;
String m_name;
private: private:
ErrorOr<void> reify_if_needed() const; ErrorOr<void> reify_if_needed() const;
String m_section;
String m_full_name;
mutable NonnullRefPtrVector<Node> m_children; mutable NonnullRefPtrVector<Node> m_children;
mutable bool m_reified { false }; mutable bool m_reified { false };
bool m_open { false }; bool m_open { false };