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

Decorator approach to event handling #2398

Closed
willmcgugan opened this issue Apr 27, 2023 · 3 comments · Fixed by #2453
Closed

Decorator approach to event handling #2398

willmcgugan opened this issue Apr 27, 2023 · 3 comments · Fixed by #2453
Labels
enhancement New feature or request Task

Comments

@willmcgugan
Copy link
Collaborator

willmcgugan commented Apr 27, 2023

Add a decorator approach to handling messages, in addition to the naming convention.

Implement a function called on, importable from the root. This would take the message type, and an optional selector.

The message system would check for decorated handlers before the naming convention.

Note: there could be more than one handler associated with a method.

from textual import on

class MyApp:

    @on(Button.Pressed, "#quit")
    def quit_app(self, event:Button.Pressed):
        ...

I think Textual should look for the decorated handler first, then the naming convention.

Note that we have a sender attribute, which for the most part could be used to match against the selector. Consequently I think we need a method on the message itself, so that any given message hand override the matching behaviour.

@davep davep added enhancement New feature or request Task labels Apr 27, 2023
@willmcgugan
Copy link
Collaborator Author

Updated to use @on which compliments the current naming convention, and reads better.

@willmcgugan
Copy link
Collaborator Author

#2453

@github-actions
Copy link

github-actions bot commented May 2, 2023

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
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants