From e6b65310cd10b50cb36773e591900213f5cb7938 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 5 Apr 2024 15:37:26 +0100 Subject: [PATCH 1/4] prevent messages from constructor --- src/textual/widgets/_select.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/textual/widgets/_select.py b/src/textual/widgets/_select.py index 240cffe9ef..2402ad7174 100644 --- a/src/textual/widgets/_select.py +++ b/src/textual/widgets/_select.py @@ -3,6 +3,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Generic, Iterable, TypeVar, Union +import rich.repr from rich.console import RenderableType from rich.text import Text @@ -256,6 +257,7 @@ class Select(Generic[SelectType], Vertical, can_focus=True): exception. """ + @rich.repr.auto class Changed(Message): """Posted when the select value was changed. @@ -274,6 +276,10 @@ def __init__( self.value = value """The value of the Select when it changed.""" + def __rich_repr__(self) -> rich.repr.Result: + yield self.select + yield self.value + @property def control(self) -> Select[SelectType]: """The Select that sent the message.""" @@ -407,7 +413,8 @@ def _init_selected_option(self, hint: SelectType | NoSelection = BLANK) -> None: """Initialises the selected option for the `Select`.""" if hint == self.BLANK and not self._allow_blank: hint = self._options[0][1] - self.value = hint + with self.prevent(Select.Changed): + self.value = hint def set_options(self, options: Iterable[tuple[RenderableType, SelectType]]) -> None: """Set the options for the Select. From 19437ffc90fc9721ac42c3a8db3a24a29adaceb2 Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 5 Apr 2024 17:24:26 +0100 Subject: [PATCH 2/4] prevent on mount --- src/textual/message_pump.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/textual/message_pump.py b/src/textual/message_pump.py index 6e5a784c6a..ea2127ae65 100644 --- a/src/textual/message_pump.py +++ b/src/textual/message_pump.py @@ -144,6 +144,7 @@ def __init__(self, parent: MessagePump | None = None) -> None: """ self._next_callbacks: list[events.Callback] = [] self._thread_id: int = threading.get_ident() + self._prevented_messages_on_mount = self._prevent_message_types_stack[-1] @property def _prevent_message_types_stack(self) -> list[set[type[Message]]]: @@ -524,7 +525,11 @@ async def _pre_process(self) -> bool: try: await self._dispatch_message(events.Compose()) - await self._dispatch_message(events.Mount()) + if self._prevented_messages_on_mount: + with self.prevent(*self._prevented_messages_on_mount): + await self._dispatch_message(events.Mount()) + else: + await self._dispatch_message(events.Mount()) self.check_idle() self._post_mount() except Exception as error: From 01f32c79f66488f7bfccc4c8450f9a96e61ab06e Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 5 Apr 2024 17:27:27 +0100 Subject: [PATCH 3/4] no explicit prevent --- src/textual/widgets/_select.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/textual/widgets/_select.py b/src/textual/widgets/_select.py index 2402ad7174..43875e8fe8 100644 --- a/src/textual/widgets/_select.py +++ b/src/textual/widgets/_select.py @@ -413,8 +413,7 @@ def _init_selected_option(self, hint: SelectType | NoSelection = BLANK) -> None: """Initialises the selected option for the `Select`.""" if hint == self.BLANK and not self._allow_blank: hint = self._options[0][1] - with self.prevent(Select.Changed): - self.value = hint + self.value = hint def set_options(self, options: Iterable[tuple[RenderableType, SelectType]]) -> None: """Set the options for the Select. From f5edc8152778ad2d167903bc48463313ce0a1c7f Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 5 Apr 2024 17:28:36 +0100 Subject: [PATCH 4/4] changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b8e0f5be7..cc2a5a53ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.56.0] - Unreleased + +### Changed + +- self.prevent can be used in a widget constructor to prevent messages on mount https://github.com/Textualize/textual/pull/4392 + ## [0.55.1] - 2024-04-2 ### Fixed