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

prevent messages from constructor #4392

Merged
merged 4 commits into from
Apr 6, 2024
Merged

prevent messages from constructor #4392

merged 4 commits into from
Apr 6, 2024

Conversation

willmcgugan
Copy link
Collaborator

@willmcgugan willmcgugan commented Apr 5, 2024

#4391

Allows use of self.prevent to prevent messages from being sent on_mount

Original issue may be fixed with the following change to compose:

from textual import on
from textual.app import App, ComposeResult
from textual.widgets import Header, Select

ITEMS = {"food": ["pizza", "burger", "salad"], "drink": ["water", "soda", "juice"]}


class SelectApp(App):
    CSS_PATH = "select.tcss"
    primary = "drink"
    secondary = "juice"

    def compose(self) -> ComposeResult:
        primary_items = [(item, item) for item in ITEMS]
        secondary_items = [(item, item) for item in ITEMS[self.primary]]
        yield Header()
        with self.prevent(Select.Changed): 
            yield Select(
                    primary_items,
                    value=self.primary,
                    prompt="Select Category",
                    id="primary",
                    classes="settings_option",
                    allow_blank=False,
                )
                yield Select(
                    secondary_items,
                    value=self.secondary,
                    prompt="Select Item",
                    id="secondary",
                    classes="settings_option",
                    allow_blank=False,
                )
        self.title = f"{self.primary=} {self.secondary=}"

    @on(Select.Changed)
    def select_changed(self, event: Select.Changed) -> None:
        if event.control.id == "primary":
            self.primary = event.value
            items = ITEMS[event.value]
            widget: Select = self.query_one("#secondary")
            widget.set_options((item, item) for item in items)
        if event.control.id == "secondary":
            self.secondary = event.value
        self.title = f"{self.primary=} {self.secondary=}"


if __name__ == "__main__":
    app = SelectApp()
    app.run()

@TomJGooding
Copy link
Contributor

TomJGooding commented Apr 5, 2024

Sorry just to check I understand this correctly, it is the responsibility of the developer to prevent the changed event?

@willmcgugan
Copy link
Collaborator Author

Sorry just to check I understand this correctly, it is the responsibility of the developer to prevent the changed event?

Yes, the majority of the time you do want to handle messages for any changes. The alternative would be for every widget to have some kind of send_change_events switch.

@willmcgugan willmcgugan merged commit e08c3f9 into main Apr 6, 2024
20 checks passed
@willmcgugan willmcgugan deleted the select-no-changed branch April 6, 2024 10:15
@TomJGooding
Copy link
Contributor

TomJGooding commented Apr 6, 2024

Thanks Will for confirming. I just wasn't sure based on the title of your issue if actually a Changed message shouldn't be posted on initialisation at all, but I think you could argue either way!

Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this pull request Apr 16, 2024
Was due to Textual changed behaviour

In 0.53.0 this change caused that:
BREAKING: for many widgets, messages are now sent when programmatic changes that mirror user input are made #4256

And with 0.56.0 this fix is available.
See also: Textualize/textual#4392
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this pull request Apr 17, 2024
Was due to Textual changed behaviour

In 0.53.0 this change caused that:
BREAKING: for many widgets, messages are now sent when programmatic changes that mirror user input are made #4256

And with 0.56.0 this fix is available.
See also: Textualize/textual#4392
Gandalf-the-Grey pushed a commit to openhive-network/clive that referenced this pull request Apr 24, 2024
Was due to Textual changed behaviour

In 0.53.0 this change caused that:
BREAKING: for many widgets, messages are now sent when programmatic changes that mirror user input are made #4256

And with 0.56.0 this fix is available.
See also: Textualize/textual#4392
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants