Skip to content

Commit a2184ce

Browse files
committed
💄 Only make Undo available if there are things to undo
1 parent b1fcb4c commit a2184ce

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/complexitty/providers/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def commands(self) -> CommandHits:
8585
yield SetColourToShadesOfBlue()
8686
yield SetColourToShadesOfGreen()
8787
yield SetColourToShadesOfRed()
88-
yield Undo()
88+
yield from self.maybe(Undo)
8989
yield ZeroZero()
9090
yield ZoomIn()
9191
yield ZoomInFaster()

src/complexitty/screens/main.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,27 @@ def on_mount(self) -> None:
167167
else get_colour_map(self._arguments.colour_map),
168168
)
169169

170+
def check_action(self, action: str, parameters: tuple[object, ...]) -> bool | None:
171+
"""Check if an action is possible to perform right now.
172+
173+
Args:
174+
action: The action to perform.
175+
parameters: The parameters of the action.
176+
177+
Returns:
178+
`True` if it can perform, `False` or `None` if not.
179+
"""
180+
if not self.is_mounted:
181+
# Surprisingly it seems that Textual's "dynamic bindings" can
182+
# cause this method to be called before the DOM is up and
183+
# running. This breaks the rule of least astonishment, I'd say,
184+
# but okay let's be defensive... (when I can come up with a nice
185+
# little MRE I'll report it).
186+
return True
187+
if action == Undo.action_name():
188+
return bool(self._history) or None
189+
return True
190+
170191
@on(Mandelbrot.Plotted)
171192
def _update_situation(self, message: Mandelbrot.Plotted) -> None:
172193
"""Update the current situation after the latest plot.
@@ -206,6 +227,7 @@ def _remember(self) -> None:
206227
plot.multibrot,
207228
)
208229
)
230+
self.refresh_bindings()
209231

210232
def action_zoom(self, change: float) -> None:
211233
"""Change the zoom value.
@@ -321,6 +343,7 @@ def action_undo_command(self) -> None:
321343
situation = self._history.pop()
322344
except IndexError:
323345
return
346+
self.refresh_bindings()
324347
self.query_one(Mandelbrot).set(
325348
x_position=situation.x_position,
326349
y_position=situation.y_position,

0 commit comments

Comments
 (0)