Skip to content

Commit

Permalink
Undo and redo now return the command
Browse files Browse the repository at this point in the history
  • Loading branch information
Aesonus committed Dec 15, 2020
1 parent 3430216 commit f08f4a9
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions tklife/behaviors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Contains behaviors for ui functionality"""
import functools
from queue import Empty, Queue
from typing import Any

Expand All @@ -24,24 +23,28 @@ def add_history(self, command):
def undo(self):
"""Calls reverse on the previous command"""
if self.cursor is None:
return
self.history[self.cursor].reverse()
return None
command = self.history[self.cursor]
command.reverse()
new_cursor = self.cursor - 1
if new_cursor < 0:
self.cursor = None
return
return None
self.cursor = new_cursor
return command

def redo(self):
"""Calls execute on the next command"""
if self.cursor is None and len(self.history) == 0:
return
return None
if self.cursor is None and len(self.history) > 0:
self.cursor = -1
if self.cursor == self.history.index(self.history[-1]):
return
return None
self.cursor += 1
self.history[self.cursor].execute()
command = self.history[self.cursor]
command.execute()
return command

def undo_all(self, until=None):
"""Calls undo on all of the history"""
Expand Down Expand Up @@ -129,6 +132,16 @@ def register_listener(self, event, listener):
except KeyError:
self.__listeners[event] = [listener]

def remove_listener(self, event, listener):
"""
Removes the listener given for the given event.
Does nothing if there is no event listeners registered
"""
try:
self.__listeners[event].remove(listener)
except KeyError:
pass

def poll(self, call_after=None):
"""
Gets the next event from the queue and calls all of the
Expand Down

0 comments on commit f08f4a9

Please sign in to comment.