FontEditor: Don't crash on missing icons

Instead of crashing on failed icon loading with TRY(), let's report
missing but non-critical resources in detail and gracefully move on.
This commit is contained in:
thankyouverycool 2023-05-10 17:00:50 -04:00 committed by Andreas Kling
parent ea8bcb2f30
commit 6212e5b1f0
4 changed files with 72 additions and 11 deletions

View file

@ -46,6 +46,8 @@
namespace FontEditor {
Resources g_resources;
static constexpr Array pangrams = {
"quick fox jumps nightly above wizard"sv,
"five quacking zephyrs jolt my wax bed"sv,
@ -110,7 +112,7 @@ ErrorOr<RefPtr<GUI::Window>> MainWidget::create_preview_window()
ErrorOr<void> MainWidget::create_actions()
{
m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-font.png"sv)), [this](auto&) {
m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, g_resources.new_font, [this](auto&) {
if (!request_close())
return;
auto new_font_wizard = NewFontDialog::construct(window());
@ -202,7 +204,7 @@ ErrorOr<void> MainWidget::create_actions()
update_statusbar();
});
m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/find.png"sv)), [this](auto&) {
m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, g_resources.preview_font, [this](auto&) {
if (!m_font_preview_window) {
if (auto maybe_window = create_preview_window(); maybe_window.is_error())
show_error(maybe_window.release_error(), "Creating preview window failed"sv);
@ -273,7 +275,7 @@ ErrorOr<void> MainWidget::create_actions()
m_show_system_emoji_action->set_checked(show_system_emoji);
m_show_system_emoji_action->set_status_tip("Show or hide system emoji");
m_go_to_glyph_action = GUI::Action::create("&Go to Glyph...", { Mod_Ctrl, Key_G }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv)), [this](auto&) {
m_go_to_glyph_action = GUI::Action::create("&Go to Glyph...", { Mod_Ctrl, Key_G }, g_resources.go_to_glyph, [this](auto&) {
String input;
if (GUI::InputBox::show(window(), input, {}, "Go to glyph"sv, GUI::InputType::NonemptyText, "Hexadecimal"sv) == GUI::InputBox::ExecResult::OK) {
auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input);
@ -288,12 +290,12 @@ ErrorOr<void> MainWidget::create_actions()
});
m_go_to_glyph_action->set_status_tip("Go to the specified code point");
m_previous_glyph_action = GUI::Action::create("Pre&vious Glyph", { Mod_Alt, Key_Left }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-back.png"sv)), [this](auto&) {
m_previous_glyph_action = GUI::Action::create("Pre&vious Glyph", { Mod_Alt, Key_Left }, g_resources.previous_glyph, [this](auto&) {
m_glyph_map_widget->select_previous_existing_glyph();
});
m_previous_glyph_action->set_status_tip("Seek the previous visible glyph");
m_next_glyph_action = GUI::Action::create("&Next Glyph", { Mod_Alt, Key_Right }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"sv)), [this](auto&) {
m_next_glyph_action = GUI::Action::create("&Next Glyph", { Mod_Alt, Key_Right }, g_resources.next_glyph, [this](auto&) {
m_glyph_map_widget->select_next_existing_glyph();
});
m_next_glyph_action->set_status_tip("Seek the next visible glyph");
@ -321,12 +323,12 @@ ErrorOr<void> MainWidget::create_actions()
m_glyph_editor_scale_actions.add_action(*m_scale_fifteen_action);
m_glyph_editor_scale_actions.set_exclusive(true);
m_paint_glyph_action = GUI::Action::create_checkable("Paint Glyph", { Mod_Ctrl, KeyCode::Key_J }, TRY(Gfx::Bitmap::load_from_file("/res/icons/pixelpaint/pen.png"sv)), [this](auto&) {
m_paint_glyph_action = GUI::Action::create_checkable("Paint Glyph", { Mod_Ctrl, KeyCode::Key_J }, g_resources.paint_glyph, [this](auto&) {
m_glyph_editor_widget->set_mode(GlyphEditorWidget::Paint);
});
m_paint_glyph_action->set_checked(true);
m_move_glyph_action = GUI::Action::create_checkable("Move Glyph", { Mod_Ctrl, KeyCode::Key_K }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/selection-move.png"sv)), [this](auto&) {
m_move_glyph_action = GUI::Action::create_checkable("Move Glyph", { Mod_Ctrl, KeyCode::Key_K }, g_resources.move_glyph, [this](auto&) {
m_glyph_editor_widget->set_mode(GlyphEditorWidget::Move);
});
@ -342,15 +344,15 @@ ErrorOr<void> MainWidget::create_actions()
m_glyph_editor_widget->rotate_90(Gfx::RotationDirection::Clockwise);
});
m_flip_horizontal_action = GUI::Action::create("Flip Horizontally", { Mod_Ctrl | Mod_Shift, Key_Q }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-flip-horizontal.png"sv)), [this](auto&) {
m_flip_horizontal_action = GUI::Action::create("Flip Horizontally", { Mod_Ctrl | Mod_Shift, Key_Q }, g_resources.flip_horizontally, [this](auto&) {
m_glyph_editor_widget->flip(Gfx::Orientation::Horizontal);
});
m_flip_vertical_action = GUI::Action::create("Flip Vertically", { Mod_Ctrl | Mod_Shift, Key_W }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-flip-vertical.png"sv)), [this](auto&) {
m_flip_vertical_action = GUI::Action::create("Flip Vertically", { Mod_Ctrl | Mod_Shift, Key_W }, g_resources.flip_vertically, [this](auto&) {
m_glyph_editor_widget->flip(Gfx::Orientation::Vertical);
});
m_copy_text_action = GUI::Action::create("Copy as Te&xt", { Mod_Ctrl, Key_T }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/edit-copy.png"sv)), [this](auto&) {
m_copy_text_action = GUI::Action::create("Copy as Te&xt", { Mod_Ctrl, Key_T }, g_resources.copy_as_text, [this](auto&) {
StringBuilder builder;
auto selection = m_glyph_map_widget->selection().normalized();
for (auto code_point = selection.start(); code_point < selection.start() + selection.size(); ++code_point) {
@ -740,7 +742,7 @@ ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
TRY(view_menu->try_add_action(*m_show_system_emoji_action));
TRY(view_menu->try_add_separator());
auto scale_menu = TRY(view_menu->try_add_submenu("&Scale"_short_string));
scale_menu->set_icon(TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/scale.png"sv)));
scale_menu->set_icon(g_resources.scale_editor);
TRY(scale_menu->try_add_action(*m_scale_five_action));
TRY(scale_menu->try_add_action(*m_scale_ten_action));
TRY(scale_menu->try_add_action(*m_scale_fifteen_action));

View file

@ -7,6 +7,7 @@
#pragma once
#include "Resources.h"
#include "UndoSelection.h"
#include <LibGUI/ActionGroup.h>
#include <LibGUI/FilteringProxyModel.h>
@ -17,6 +18,8 @@
namespace FontEditor {
extern Resources g_resources;
class GlyphEditorWidget;
class MainWidget final : public GUI::Widget {

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2023, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Bitmap.h>
namespace FontEditor {
struct Resources final {
static Resources create()
{
Resources resources;
auto load_bitmap = [](StringView path) -> RefPtr<Gfx::Bitmap> {
auto bitmap = Gfx::Bitmap::load_from_file(path);
if (!bitmap.is_error())
return bitmap.release_value();
warnln("Loading Gfx::Bitmap \"{}\" failed: {}", path, bitmap.release_error());
return nullptr;
};
resources.copy_as_text = load_bitmap("/res/icons/16x16/edit-copy.png"sv);
resources.flip_horizontally = load_bitmap("/res/icons/16x16/edit-flip-horizontal.png"sv);
resources.flip_vertically = load_bitmap("/res/icons/16x16/edit-flip-vertical.png"sv);
resources.go_to_glyph = load_bitmap("/res/icons/16x16/go-to.png"sv);
resources.move_glyph = load_bitmap("/res/icons/16x16/selection-move.png"sv);
resources.new_font = load_bitmap("/res/icons/16x16/filetype-font.png"sv);
resources.next_glyph = load_bitmap("/res/icons/16x16/go-forward.png"sv);
resources.paint_glyph = load_bitmap("/res/icons/pixelpaint/pen.png"sv);
resources.preview_font = load_bitmap("/res/icons/16x16/find.png"sv);
resources.previous_glyph = load_bitmap("/res/icons/16x16/go-back.png"sv);
resources.scale_editor = load_bitmap("/res/icons/16x16/scale.png"sv);
return resources;
}
RefPtr<Gfx::Bitmap> copy_as_text;
RefPtr<Gfx::Bitmap> flip_horizontally;
RefPtr<Gfx::Bitmap> flip_vertically;
RefPtr<Gfx::Bitmap> go_to_glyph;
RefPtr<Gfx::Bitmap> move_glyph;
RefPtr<Gfx::Bitmap> new_font;
RefPtr<Gfx::Bitmap> next_glyph;
RefPtr<Gfx::Bitmap> paint_glyph;
RefPtr<Gfx::Bitmap> preview_font;
RefPtr<Gfx::Bitmap> previous_glyph;
RefPtr<Gfx::Bitmap> scale_editor;
};
}

View file

@ -23,6 +23,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app = TRY(GUI::Application::create(arguments));
FontEditor::g_resources = FontEditor::Resources::create();
TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_scheme("/usr/share/man/man1/Applications/FontEditor.md") }));
TRY(Desktop::Launcher::seal_allowlist());