mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 15:57:35 +00: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:
parent
437d3ca0ea
commit
aa5e574872
5 changed files with 22 additions and 12 deletions
|
@ -109,8 +109,11 @@ GUI::ModelIndex ManualModel::index(int row, int column, const GUI::ModelIndex& p
|
||||||
if (!parent_index.is_valid())
|
if (!parent_index.is_valid())
|
||||||
return create_index(row, column, Manual::sections[row].ptr());
|
return create_index(row, column, Manual::sections[row].ptr());
|
||||||
auto* parent = static_cast<Manual::Node const*>(parent_index.internal_data());
|
auto* parent = static_cast<Manual::Node const*>(parent_index.internal_data());
|
||||||
auto* child = &parent->children()[row];
|
auto const children = parent->children();
|
||||||
return create_index(row, column, child);
|
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
|
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);
|
return create_index(row, 0, parent);
|
||||||
VERIFY_NOT_REACHED();
|
VERIFY_NOT_REACHED();
|
||||||
}
|
}
|
||||||
for (size_t row = 0; row < parent->parent()->children().size(); row++) {
|
auto maybe_children = parent->parent()->children();
|
||||||
Manual::Node* child_at_row = &parent->parent()->children()[row];
|
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)
|
if (child_at_row == parent)
|
||||||
return create_index(row, 0, parent);
|
return create_index(row, 0, parent);
|
||||||
}
|
}
|
||||||
|
@ -141,7 +148,10 @@ int ManualModel::row_count(const GUI::ModelIndex& index) const
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
return static_cast<int>(Manual::sections.size());
|
return static_cast<int>(Manual::sections.size());
|
||||||
auto* node = static_cast<Manual::Node const*>(index.internal_data());
|
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
|
int ManualModel::column_count(const GUI::ModelIndex&) const
|
||||||
|
|
|
@ -20,7 +20,7 @@ class Node : public RefCounted<Node> {
|
||||||
public:
|
public:
|
||||||
virtual ~Node() = default;
|
virtual ~Node() = default;
|
||||||
|
|
||||||
virtual NonnullRefPtrVector<Node>& children() const = 0;
|
virtual ErrorOr<Span<NonnullRefPtr<Node>>> children() const = 0;
|
||||||
virtual Node const* parent() const = 0;
|
virtual Node const* parent() const = 0;
|
||||||
virtual ErrorOr<String> name() const = 0;
|
virtual ErrorOr<String> name() const = 0;
|
||||||
virtual bool is_page() const { return false; }
|
virtual bool is_page() const { return false; }
|
||||||
|
|
|
@ -16,10 +16,10 @@ Node const* PageNode::parent() const
|
||||||
return m_section.ptr();
|
return m_section.ptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtrVector<Node>& PageNode::children() const
|
ErrorOr<Span<NonnullRefPtr<Node>>> PageNode::children() const
|
||||||
{
|
{
|
||||||
static NonnullRefPtrVector<Node> empty_vector;
|
static NonnullRefPtrVector<Node> empty_vector;
|
||||||
return empty_vector;
|
return empty_vector.span();
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<String> PageNode::path() const
|
ErrorOr<String> PageNode::path() const
|
||||||
|
|
|
@ -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 Node const* parent() const override;
|
||||||
virtual ErrorOr<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; }
|
||||||
|
|
|
@ -23,10 +23,10 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual NonnullRefPtrVector<Node>& children() const override
|
virtual ErrorOr<Span<NonnullRefPtr<Node>>> children() const override
|
||||||
{
|
{
|
||||||
MUST(reify_if_needed());
|
TRY(reify_if_needed());
|
||||||
return m_children;
|
return m_children.span();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual Node const* parent() const override { return nullptr; }
|
virtual Node const* parent() const override { return nullptr; }
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue