diff options
author | thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> | 2022-07-05 05:28:39 -0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-07-06 14:25:30 +0200 |
commit | 7376c6865290c2153ac766acdb84bde83deb259f (patch) | |
tree | a83d7c289b6f25ad9c184e8ba091e55a691fb2bf /Userland/Applications/FontEditor | |
parent | 510551bb4f7c7fd325b753b5654174d4baaa5464 (diff) | |
download | serenity-7376c6865290c2153ac766acdb84bde83deb259f.zip |
FontEditor: Propagate errors when pushing undo commands
Diffstat (limited to 'Userland/Applications/FontEditor')
-rw-r--r-- | Userland/Applications/FontEditor/FontEditor.cpp | 24 | ||||
-rw-r--r-- | Userland/Applications/FontEditor/FontEditor.h | 1 | ||||
-rw-r--r-- | Userland/Applications/FontEditor/UndoSelection.h | 27 |
3 files changed, 40 insertions, 12 deletions
diff --git a/Userland/Applications/FontEditor/FontEditor.cpp b/Userland/Applications/FontEditor/FontEditor.cpp index c5833aa7a2..383ec3905f 100644 --- a/Userland/Applications/FontEditor/FontEditor.cpp +++ b/Userland/Applications/FontEditor/FontEditor.cpp @@ -739,6 +739,24 @@ bool FontEditorWidget::open_file(String const& path) return true; } +void FontEditorWidget::push_undo() +{ + auto maybe_state = m_undo_selection->save_state(); + if (maybe_state.is_error()) { + warnln("Failed to save undo state: {}", maybe_state.error()); + return; + } + auto state = maybe_state.release_value(); + auto maybe_command = try_make<SelectionUndoCommand>(*m_undo_selection, move(state)); + if (maybe_command.is_error()) { + warnln("Failed to make undo command: {}", maybe_command.error()); + return; + } + auto command = maybe_command.release_value(); + if (auto maybe_push = m_undo_stack->try_push(move(command)); maybe_push.is_error()) + warnln("Failed to push undo stack: {}", maybe_push.error()); +} + void FontEditorWidget::reset_selection_and_push_undo() { auto selection = m_glyph_map_widget->selection().normalized(); @@ -749,7 +767,7 @@ void FontEditorWidget::reset_selection_and_push_undo() m_glyph_map_widget->set_selection(start, 1); m_glyph_map_widget->update(); } - m_undo_stack->push(make<SelectionUndoCommand>(*m_undo_selection)); + push_undo(); } void FontEditorWidget::undo() @@ -963,7 +981,7 @@ void FontEditorWidget::paste_glyphs() auto selection = m_glyph_map_widget->selection().normalized(); auto range_bound_glyph_count = min(glyph_count, 1 + m_range.last - selection.start()); m_undo_selection->set_size(range_bound_glyph_count); - m_undo_stack->push(make<SelectionUndoCommand>(*m_undo_selection)); + push_undo(); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * edited_font().glyph_height(); size_t bytes_per_copied_glyph = Gfx::GlyphBitmap::bytes_per_row() * height; @@ -993,7 +1011,7 @@ void FontEditorWidget::paste_glyphs() void FontEditorWidget::delete_selected_glyphs() { - m_undo_stack->push(make<SelectionUndoCommand>(*m_undo_selection)); + push_undo(); auto selection = m_glyph_map_widget->selection().normalized(); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * m_edited_font->glyph_height(); diff --git a/Userland/Applications/FontEditor/FontEditor.h b/Userland/Applications/FontEditor/FontEditor.h index a23eb844c7..b049331c70 100644 --- a/Userland/Applications/FontEditor/FontEditor.h +++ b/Userland/Applications/FontEditor/FontEditor.h @@ -75,6 +75,7 @@ private: void paste_glyphs(); void delete_selected_glyphs(); + void push_undo(); void reset_selection_and_push_undo(); RefPtr<Gfx::BitmapFont> m_edited_font; diff --git a/Userland/Applications/FontEditor/UndoSelection.h b/Userland/Applications/FontEditor/UndoSelection.h index f6fa963ad6..6965c013d7 100644 --- a/Userland/Applications/FontEditor/UndoSelection.h +++ b/Userland/Applications/FontEditor/UndoSelection.h @@ -19,14 +19,14 @@ public: , m_font(font) { } - NonnullRefPtr<UndoSelection> save_state() + ErrorOr<NonnullRefPtr<UndoSelection>> save_state() { - auto state = adopt_ref(*new UndoSelection(m_start, m_size, m_active_glyph, *m_font)); + auto state = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) UndoSelection(m_start, m_size, m_active_glyph, *m_font))); size_t bytes_per_glyph = Gfx::GlyphBitmap::bytes_per_row() * font().glyph_height(); auto* rows = font().rows() + m_start * bytes_per_glyph; auto* widths = font().widths() + m_start; - state->m_data.append(&rows[0], bytes_per_glyph * m_size); - state->m_data.append(&widths[0], m_size); + TRY(state->m_data.try_append(&rows[0], bytes_per_glyph * m_size)); + TRY(state->m_data.try_append(&widths[0], m_size)); return state; } void restore_state(UndoSelection const& state) @@ -61,20 +61,29 @@ private: class SelectionUndoCommand : public GUI::Command { public: - SelectionUndoCommand(UndoSelection& selection) - : m_undo_state(selection.save_state()) + SelectionUndoCommand(UndoSelection& selection, NonnullRefPtr<UndoSelection> undo_state) + : m_undo_state(undo_state) , m_undo_selection(selection) { } virtual void undo() override { - if (!m_redo_state) - m_redo_state = m_undo_state->save_state(); + if (!m_redo_state) { + if (auto maybe_state = m_undo_state->save_state(); !maybe_state.is_error()) { + auto state = maybe_state.release_value(); + m_redo_state = move(state); + } else { + warnln("Failed to save redo state: {}", maybe_state.error()); + } + } m_undo_selection.restore_state(*m_undo_state); } virtual void redo() override { - m_undo_selection.restore_state(*m_redo_state); + if (m_redo_state) + m_undo_selection.restore_state(*m_redo_state); + else + warnln("Failed to restore state"); } private: |