Skip to content

Commit

Permalink
Merge pull request #4392 from Textualize/select-no-changed
Browse files Browse the repository at this point in the history
prevent messages from constructor
  • Loading branch information
willmcgugan authored Apr 6, 2024
2 parents 1154b56 + f5edc81 commit e08c3f9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/textual/message_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]]:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions src/textual/widgets/_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand All @@ -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."""
Expand Down

0 comments on commit e08c3f9

Please sign in to comment.