Working on undo redo

This commit is contained in:
Kodjo Sossouvi
2025-07-25 17:22:18 +02:00
parent fb82365980
commit 72f5f30da6
6 changed files with 86 additions and 34 deletions

View File

@@ -13,9 +13,10 @@ logger = logging.getLogger("UndoRedoApp")
class CommandHistory(ABC):
def __init__(self, name, desc):
def __init__(self, name, desc, owner):
self.name = name
self.desc = desc
self.owner = owner
@abstractmethod
def undo(self):
@@ -31,38 +32,42 @@ class UndoRedo(BaseComponentSingleton):
def __init__(self, session, _id, settings_manager=None, tabs_manager=None):
super().__init__(session, _id, settings_manager, tabs_manager)
self.index = 0
self.index = -1
self.history = []
self._commands = UndoRedoCommandManager(self)
def push(self, command: CommandHistory):
self.history = self.history[:self.index + 1]
self.history.append(command)
self.index += 1
def undo(self):
logger.debug(f"Undo command")
if self.index == 0:
if self.index < 0 :
logger.debug(f" No command to undo.")
return self
self.index -= 1
command = self.history[self.index]
logger.debug(f" Undoing command {command.name} ({command.desc})")
res = command.undo()
self.index -= 1
return self, res
def redo(self):
logger.debug("Redo command")
if self.index >= len(self.history):
if self.index == len(self.history) - 1:
logger.debug(f" No command to redo.")
return self
self.index += 1
command = self.history[self.index]
logger.debug(f" Redoing command {command.name} ({command.desc})")
res = command.redo()
self.index += 1
return self, res
def refresh(self):
return self.__ft__(oob=True)
def __ft__(self, oob=False):
return Div(
self._mk_undo(),
@@ -74,33 +79,34 @@ class UndoRedo(BaseComponentSingleton):
def _mk_undo(self):
if self._can_undo():
command = self.history[self.index - 1]
command = self.history[self.index]
return mk_tooltip(mk_icon(icon_undo,
size=24,
**self._commands.undo()),
"Undo")
f"Undo '{command.name}'.")
else:
return mk_tooltip(mk_icon(icon_undo,
size=24,
can_select=False,
cls="mmt-btn-disabled"),
"Nothing to undo")
"Nothing to undo.")
def _mk_redo(self):
if self._can_redo():
command = self.history[self.index]
return mk_tooltip(mk_icon(icon_redo,
size=24,
**self._commands.redo()),
"Redo")
f"Redo '{command.name}'.")
else:
return mk_tooltip(mk_icon(icon_redo,
size=24,
can_select=False,
cls="mmt-btn-disabled"),
"Nothing to redo")
"Nothing to redo.")
def _can_undo(self):
return self.index > 0
return self.index >= 0
def _can_redo(self):
return self.index < len(self.history)
return self.index < len(self.history) - 1