Skip to content
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

Message '.sender' set to last composed widget #2070

Closed
rodrigogiraoserrao opened this issue Mar 15, 2023 · 2 comments · Fixed by #2071
Closed

Message '.sender' set to last composed widget #2070

rodrigogiraoserrao opened this issue Mar 15, 2023 · 2 comments · Fixed by #2071
Labels
bug Something isn't working Task

Comments

@rodrigogiraoserrao
Copy link
Contributor

The attribute Message.sender is incorrectly set to the last widget that was composed instead of pointing to the widget that actually posts the message.

Run the app below and click the screen or the text as you please.
The text log will log events coming from a scrollbar.

For a MMWE get rid of the custom widget altogether and preserve only the on_click handler, which will still show clicks coming from the scrollbar...

from textual.app import App, ComposeResult
from textual.message import Message
from textual.widgets import Header, Footer, Static, TextLog


class CustomWidget(Static):
    class CustomMessage(Message):
        pass

    def trigger(self) -> None:
        self.post_message(self.CustomMessage())


class MyApp(App[None]):
    def compose(self) -> ComposeResult:
        yield Header()
        yield CustomWidget("Big text\nso you can target it.", id="custom")
        yield Footer()
        yield TextLog(id="tl")

    def on_click(self, event) -> None:
        self.query_one(TextLog).write(event.sender)
        self.query_one(CustomWidget).trigger()

    def on_custom_widget_custom_message(
        self, event: CustomWidget.CustomMessage
    ) -> None:
        self.query_one(TextLog).write(event.sender)


app = MyApp()

if __name__ == "__main__":
    app.run()
@willmcgugan
Copy link
Collaborator

    def trigger(self) -> None:
        self.post_message(self.CustomMessage())

You might be thinking that the sender is always self in the call to post_message. A reasonable assumption, but consider self to be the destination of the message.

Consider this line:

self.query_one(CustomWidget).post_message(MyEvent())

The sender is not CustomWidget. CustomWidget is the destination of the message. The sender is the active message pump at the time.

There is a bug though. Basically it wasn't storing the app as the sender.

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants