Register callbacks at message pump level, invoke them after refresh (#607)

* Register callbacks at message pump level, invoke them after refresh

* Fix a typo

* Code review feedback actions

* call_later callbacks invoked after refresh or on idle

* Fix space key in text input

* Make Widget.on_idle synchronous

* Fix call_later

* Rename PostScreenUpdate to InvokeCallbacks, and only fire if callbacks exist

* Update type hints for InvokeLater callbacks

* Update type signature of call_later callbacks, extract typevar
This commit is contained in:
darrenburns
2022-08-05 13:47:47 +01:00
committed by GitHub
parent 589bf6a1bc
commit a166d84eef
10 changed files with 66 additions and 19 deletions

View File

@@ -193,14 +193,20 @@ class MessagePump:
return timer
def call_later(self, callback: Callable, *args, **kwargs) -> None:
"""Run a callback after processing all messages and refreshing the screen.
"""Schedule a callback to run after all messages are processed and the screen
has been refreshed.
Args:
callback (Callable): A callable.
"""
self.post_message_no_wait(
events.Callback(self, partial(callback, *args, **kwargs))
)
# We send the InvokeLater message to ourselves first, to ensure we've cleared
# out anything already pending in our own queue.
message = messages.InvokeLater(self, partial(callback, *args, **kwargs))
self.post_message_no_wait(message)
def handle_invoke_later(self, message: messages.InvokeLater) -> None:
# Forward InvokeLater message to the Screen
self.app.screen.post_message_no_wait(message)
def close_messages_no_wait(self) -> None:
"""Request the message queue to exit."""
@@ -392,9 +398,6 @@ class MessagePump:
return False
return self.post_message_no_wait(message)
async def on_callback(self, event: events.Callback) -> None:
await invoke(event.callback)
def emit_no_wait(self, message: Message) -> bool:
if self._parent:
return self._parent.post_message_from_child_no_wait(message)