PDFViewer: Propagate errors from OutlineModel construction

This follows the same idea that Andreas was doing in this latest videos,
where construction-time TRY()s were not present but should have been.
Like Andreas did, moving the initialisation of such fields to the
factory function, which then returns ErrorOr solves the issue.
This commit is contained in:
Rodrigo Tobar 2022-12-17 12:18:29 +08:00 committed by Andreas Kling
parent cb2cf6de99
commit 2ea8d2547e
Notes: sideshowbarker 2024-07-17 03:03:41 +09:00
4 changed files with 11 additions and 9 deletions

View File

@ -7,16 +7,17 @@
#include "OutlineModel.h"
#include <LibGfx/Font/FontDatabase.h>
NonnullRefPtr<OutlineModel> OutlineModel::create(NonnullRefPtr<PDF::OutlineDict> const& outline)
ErrorOr<NonnullRefPtr<OutlineModel>> OutlineModel::create(NonnullRefPtr<PDF::OutlineDict> const& outline)
{
return adopt_ref(*new OutlineModel(outline));
auto outline_model = adopt_ref(*new OutlineModel(outline));
outline_model->m_closed_item_icon.set_bitmap_for_size(16, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book.png"sv)));
outline_model->m_open_item_icon.set_bitmap_for_size(16, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png"sv)));
return outline_model;
}
OutlineModel::OutlineModel(NonnullRefPtr<PDF::OutlineDict> const& outline)
: m_outline(outline)
{
m_closed_item_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book.png"sv).release_value_but_fixme_should_propagate_errors());
m_open_item_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/book-open.png"sv).release_value_but_fixme_should_propagate_errors());
}
void OutlineModel::set_index_open_state(const GUI::ModelIndex& index, bool is_open)

View File

@ -12,7 +12,7 @@
class OutlineModel final : public GUI::Model {
public:
static NonnullRefPtr<OutlineModel> create(NonnullRefPtr<PDF::OutlineDict> const& outline);
static ErrorOr<NonnullRefPtr<OutlineModel>> create(NonnullRefPtr<PDF::OutlineDict> const& outline);
void set_index_open_state(const GUI::ModelIndex& index, bool is_open);

View File

@ -376,11 +376,11 @@ PDF::PDFErrorOr<void> PDFViewerWidget::try_open_file(Core::File& file)
if (document->outline()) {
auto outline = document->outline();
m_sidebar->set_outline(outline.release_nonnull());
TRY(m_sidebar->set_outline(outline.release_nonnull()));
m_sidebar->set_visible(true);
m_sidebar_open = true;
} else {
m_sidebar->set_outline({});
TRY(m_sidebar->set_outline({}));
m_sidebar->set_visible(false);
m_sidebar_open = false;
}

View File

@ -16,15 +16,16 @@ class SidebarWidget final : public GUI::Widget {
public:
~SidebarWidget() override = default;
void set_outline(RefPtr<PDF::OutlineDict> outline)
ErrorOr<void> set_outline(RefPtr<PDF::OutlineDict> outline)
{
if (outline) {
m_model = OutlineModel::create(outline.release_nonnull());
m_model = TRY(OutlineModel::create(outline.release_nonnull()));
m_outline_tree_view->set_model(m_model);
} else {
m_model = RefPtr<OutlineModel> {};
m_outline_tree_view->set_model({});
}
return {};
}
private: