1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 05:57:44 +00:00

FontEditor: Propagate errors when pushing undo commands

This commit is contained in:
thankyouverycool 2022-07-05 05:28:39 -04:00 committed by Andreas Kling
parent 510551bb4f
commit 7376c68652
3 changed files with 40 additions and 12 deletions

View file

@ -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();

View file

@ -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;

View file

@ -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: