1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

Help+LibManual: Make the children accessor fallible

This is convenient for the section node which might compute children
on the fly.
This commit is contained in:
kleines Filmröllchen 2022-12-14 13:33:28 +01:00 committed by Andrew Kaster
parent 437d3ca0ea
commit aa5e574872
Notes: sideshowbarker 2024-07-17 02:37:08 +09:00
5 changed files with 22 additions and 12 deletions

View file

@ -109,8 +109,11 @@ GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& p
if (!parent_index.is_valid())
return create_index(row, column, Manual::sections[row].ptr());
auto* parent = static_cast<Manual::Node const*>(parent_index.internal_data());
auto* child = &parent->children()[row];
return create_index(row, column, child);
auto const children = parent->children();
if (children.is_error())
return {};
auto child = children.value()[row];
return create_index(row, column, child.ptr());
}
GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
@ -128,8 +131,12 @@ GUI::ModelIndex ManualModel::parent_index(const GUI::ModelIndex& index) const
return create_index(row, 0, parent);
VERIFY_NOT_REACHED();
}
for (size_t row = 0; row < parent->parent()->children().size(); row++) {
Manual::Node* child_at_row = &parent->parent()->children()[row];
auto maybe_children = parent->parent()->children();
if (maybe_children.is_error())
return {};
auto children = maybe_children.release_value();
for (size_t row = 0; row < children.size(); row++) {
Manual::Node* child_at_row = children[row];
if (child_at_row == parent)
return create_index(row, 0, parent);
}
@ -141,7 +148,10 @@ int ManualModel::row_count(const GUI::ModelIndex& index) const
if (!index.is_valid())
return static_cast<int>(Manual::sections.size());
auto* node = static_cast<Manual::Node const*>(index.internal_data());
return node->children().size();
auto maybe_children = node->children();
if (maybe_children.is_error())
return 0;
return static_cast<int>(maybe_children.value().size());
}
int ManualModel::column_count(const GUI::ModelIndex&) const

View file

@ -20,7 +20,7 @@ class Node : public RefCounted<Node> {
public:
virtual ~Node() = default;
virtual NonnullRefPtrVector<Node>& children() const = 0;
virtual ErrorOr<Span<NonnullRefPtr<Node>>> children() const = 0;
virtual Node const* parent() const = 0;
virtual ErrorOr<String> name() const = 0;
virtual bool is_page() const { return false; }

View file

@ -16,10 +16,10 @@ Node const* PageNode::parent() const
return m_section.ptr();
}
NonnullRefPtrVector<Node>& PageNode::children() const
ErrorOr<Span<NonnullRefPtr<Node>>> PageNode::children() const
{
static NonnullRefPtrVector<Node> empty_vector;
return empty_vector;
return empty_vector.span();
}
ErrorOr<String> PageNode::path() const

View file

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

View file

@ -23,10 +23,10 @@ public:
{
}
virtual NonnullRefPtrVector<Node>& children() const override
virtual ErrorOr<Span<NonnullRefPtr<Node>>> children() const override
{
MUST(reify_if_needed());
return m_children;
TRY(reify_if_needed());
return m_children.span();
}
virtual Node const* parent() const override { return nullptr; }