1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:17:34 +00:00

FontEditor: Include file basename in error messages when available

And write messages in the [action] failed: [error] format to match
common error message formatting elsewhere in Userland.
This commit is contained in:
thankyouverycool 2022-08-15 07:20:20 -04:00 committed by Andreas Kling
parent 9725fd162f
commit 92a75ccaed
3 changed files with 24 additions and 22 deletions

View file

@ -101,9 +101,9 @@ ErrorOr<void> MainWidget::create_actions()
new_font_wizard->hide(); new_font_wizard->hide();
auto maybe_font = new_font_wizard->create_font(); auto maybe_font = new_font_wizard->create_font();
if (maybe_font.is_error()) if (maybe_font.is_error())
return show_error("Failed to create new font"sv, maybe_font.error()); return show_error(maybe_font.error(), "Creating new font failed"sv);
if (auto result = initialize({}, move(maybe_font.value())); result.is_error()) if (auto result = initialize({}, move(maybe_font.value())); result.is_error())
show_error("Failed to initialize font"sv, result.error()); show_error(result.error(), "Initializing new font failed"sv);
}); });
m_new_action->set_status_tip("Create a new font"); m_new_action->set_status_tip("Create a new font");
@ -114,14 +114,14 @@ ErrorOr<void> MainWidget::create_actions()
if (!open_path.has_value()) if (!open_path.has_value())
return; return;
if (auto result = open_file(open_path.value()); result.is_error()) if (auto result = open_file(open_path.value()); result.is_error())
show_error("Failed to open font"sv, result.error()); show_error(result.error(), "Opening"sv, LexicalPath { open_path.value() }.basename());
}); });
m_save_action = GUI::CommonActions::make_save_action([&](auto&) { m_save_action = GUI::CommonActions::make_save_action([&](auto&) {
if (m_path.is_empty()) if (m_path.is_empty())
return m_save_as_action->activate(); return m_save_as_action->activate();
if (auto result = save_file(m_path); result.is_error()) if (auto result = save_file(m_path); result.is_error())
show_error("Failed to save font"sv, result.error()); show_error(result.error(), "Saving"sv, LexicalPath { m_path }.basename());
}); });
m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) { m_save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
@ -130,17 +130,17 @@ ErrorOr<void> MainWidget::create_actions()
if (!save_path.has_value()) if (!save_path.has_value())
return; return;
if (auto result = save_file(save_path.value()); result.is_error()) if (auto result = save_file(save_path.value()); result.is_error())
show_error("Failed to save font"sv, result.error()); show_error(result.error(), "Saving"sv, lexical_path.basename());
}); });
m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) { m_cut_action = GUI::CommonActions::make_cut_action([&](auto&) {
if (auto result = cut_selected_glyphs(); result.is_error()) if (auto result = cut_selected_glyphs(); result.is_error())
show_error("Failed to cut selection"sv, result.error()); show_error(result.error(), "Cutting selection failed"sv);
}); });
m_copy_action = GUI::CommonActions::make_copy_action([&](auto&) { m_copy_action = GUI::CommonActions::make_copy_action([&](auto&) {
if (auto result = copy_selected_glyphs(); result.is_error()) if (auto result = copy_selected_glyphs(); result.is_error())
show_error("Failed to copy selection"sv, result.error()); show_error(result.error(), "Copying selection failed"sv);
}); });
m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) { m_paste_action = GUI::CommonActions::make_paste_action([&](auto&) {
@ -177,7 +177,7 @@ ErrorOr<void> MainWidget::create_actions()
m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png"sv)), [&](auto&) { m_open_preview_action = GUI::Action::create("&Preview Font", { Mod_Ctrl, Key_P }, TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/find.png"sv)), [&](auto&) {
if (!m_font_preview_window) { if (!m_font_preview_window) {
if (auto maybe_window = create_preview_window(); maybe_window.is_error()) if (auto maybe_window = create_preview_window(); maybe_window.is_error())
show_error("Failed to create preview window"sv, maybe_window.error()); show_error(maybe_window.error(), "Creating preview window failed"sv);
else else
m_font_preview_window = maybe_window.release_value(); m_font_preview_window = maybe_window.release_value();
} }
@ -775,12 +775,12 @@ void MainWidget::push_undo()
{ {
auto maybe_state = m_undo_selection->save_state(); auto maybe_state = m_undo_selection->save_state();
if (maybe_state.is_error()) if (maybe_state.is_error())
return show_error("Failed to save undo state"sv, maybe_state.error()); return show_error(maybe_state.error(), "Saving undo state failed"sv);
auto maybe_command = try_make<SelectionUndoCommand>(*m_undo_selection, move(maybe_state.value())); auto maybe_command = try_make<SelectionUndoCommand>(*m_undo_selection, move(maybe_state.value()));
if (maybe_command.is_error()) if (maybe_command.is_error())
return show_error("Failed to make undo command"sv, maybe_command.error()); return show_error(maybe_command.error(), "Making undo command failed"sv);
if (auto maybe_push = m_undo_stack->try_push(move(maybe_command.value())); maybe_push.is_error()) if (auto maybe_push = m_undo_stack->try_push(move(maybe_command.value())); maybe_push.is_error())
show_error("Failed to push undo stack"sv, maybe_push.error()); show_error(maybe_push.error(), "Pushing undo stack failed"sv);
} }
void MainWidget::reset_selection_and_push_undo() void MainWidget::reset_selection_and_push_undo()
@ -950,7 +950,7 @@ void MainWidget::drop_event(GUI::DropEvent& event)
return; return;
if (auto result = open_file(urls.first().path()); result.is_error()) if (auto result = open_file(urls.first().path()); result.is_error())
show_error("Failed to load font"sv, result.error()); show_error(result.error(), "Opening"sv, LexicalPath { urls.first().path() }.basename());
} }
} }
@ -1056,9 +1056,13 @@ void MainWidget::delete_selected_glyphs()
update_statusbar(); update_statusbar();
} }
void MainWidget::show_error(StringView preface, Error error) void MainWidget::show_error(Error error, StringView preface, StringView basename)
{ {
auto formatted_error = String::formatted("{}: {}", preface, error); String formatted_error;
if (basename.is_empty())
formatted_error = String::formatted("{}: {}", preface, error);
else
formatted_error = String::formatted("{} \"{}\" failed: {}", preface, basename, error);
GUI::MessageBox::show_error(window(), formatted_error); GUI::MessageBox::show_error(window(), formatted_error);
warnln(formatted_error); warnln(formatted_error);
} }

View file

@ -84,7 +84,7 @@ private:
void push_undo(); void push_undo();
void reset_selection_and_push_undo(); void reset_selection_and_push_undo();
void show_error(StringView preface, Error); void show_error(Error, StringView action, StringView basename = {});
RefPtr<Gfx::BitmapFont> m_edited_font; RefPtr<Gfx::BitmapFont> m_edited_font;

View file

@ -82,12 +82,10 @@ public:
virtual void undo() override virtual void undo() override
{ {
if (!m_redo_state) { if (!m_redo_state) {
if (auto maybe_state = m_undo_state->save_state(); !maybe_state.is_error()) { if (auto maybe_state = m_undo_state->save_state(); !maybe_state.is_error())
auto state = maybe_state.release_value(); m_redo_state = move(maybe_state.value());
m_redo_state = move(state); else
} else { warnln("Saving redo state failed: {}", maybe_state.error());
warnln("Failed to save redo state: {}", maybe_state.error());
}
} }
m_undo_selection.restore_state(*m_undo_state); m_undo_selection.restore_state(*m_undo_state);
} }
@ -96,7 +94,7 @@ public:
if (m_redo_state) if (m_redo_state)
m_undo_selection.restore_state(*m_redo_state); m_undo_selection.restore_state(*m_redo_state);
else else
warnln("Failed to restore state"); warnln("Restoring state failed");
} }
private: private: