-
-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A very hacky undo redo implementation #531
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
public class Akira.Lib.Managers.UndoManager : Object { | ||
|
||
private GLib.Queue<string> undos; | ||
private GLib.Queue<string> redos; | ||
|
||
public UndoManager () { | ||
undos = new GLib.Queue<string>(); | ||
redos = new GLib.Queue<string>(); | ||
} | ||
|
||
|
||
public void add_undo(Akira.Lib.Canvas canvas) { | ||
redos.clear (); | ||
inner_add_undo(Akira.FileFormat.JsonContent.serialize_canvas(canvas)); | ||
debug("%d %d", (int)undos.get_length(), (int)redos.get_length()); | ||
} | ||
|
||
public void apply_undo(Akira.Lib.Canvas canvas) { | ||
if (undos.get_length () == 0) { | ||
debug("undo empty"); | ||
return; | ||
} | ||
|
||
var old_undo = undos.pop_head(); | ||
inner_add_redo(old_undo); | ||
|
||
try { | ||
var parser = new Json.Parser (); | ||
parser.load_from_data (old_undo); | ||
var obj = parser.get_root ().get_object (); | ||
|
||
clear_canvas(canvas); | ||
Akira.FileFormat.JsonLoader.inner_load_content(canvas, obj); | ||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this, it seems a bit extreme. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If clearing and reloading is expensive, we have bigger issues than undo/redo. There is no reason the view should be expensive to regenerate. I'm open to new ideas, but am cautious about an alternative that doesn't require cleaning the slate. They tend to be rather brittle and crash or artifact prone approaches. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, the current implementation of clearing and reloading is not efficient, but that's a different story. |
||
|
||
} catch (Error e) { | ||
debug("failed to read undo"); | ||
return; | ||
} | ||
|
||
debug("%d %d", (int)undos.get_length(), (int)redos.get_length()); | ||
} | ||
|
||
private void inner_add_undo(string undo_to_add) { | ||
undos.push_head(undo_to_add); | ||
} | ||
|
||
private void inner_add_redo(string redo_to_add) { | ||
redos.push_head(redo_to_add); | ||
} | ||
|
||
public void apply_redo(Akira.Lib.Canvas canvas) { | ||
if (redos.get_length () == 0) { | ||
debug("redo empty"); | ||
return; | ||
} | ||
|
||
var old_redo = redos.pop_head(); | ||
inner_add_undo(old_redo); | ||
|
||
|
||
try { | ||
var parser = new Json.Parser (); | ||
parser.load_from_data (old_redo); | ||
var obj = parser.get_root ().get_object (); | ||
|
||
clear_canvas(canvas); | ||
Akira.FileFormat.JsonLoader.inner_load_content(canvas, obj); | ||
|
||
} catch (Error e) { | ||
debug("failed to read redo"); | ||
return; | ||
} | ||
|
||
debug("%d %d", (int)undos.get_length(), (int)redos.get_length()); | ||
|
||
} | ||
|
||
private void clear_canvas(Akira.Lib.Canvas canvas) { | ||
var item_manager = canvas.window.items_manager; | ||
|
||
var to_delete = new GLib.Queue<Akira.Lib.Items.CanvasItem>(); | ||
|
||
foreach (var item in item_manager.free_items) { | ||
to_delete.push_head(item); | ||
} | ||
|
||
foreach (var item in item_manager.artboards) { | ||
to_delete.push_head(item); | ||
} | ||
|
||
foreach (var item in item_manager.images) { | ||
to_delete.push_head(item); | ||
} | ||
|
||
while (to_delete.get_length () != 0) { | ||
canvas.window.event_bus.request_delete_item (to_delete.pop_head()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to collect the images as the request_delete_item will remove those automatically. |
||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd call this HistoryManager since it will handle both undos and redos.