Skip to content

Commit

Permalink
allow select
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 4, 2025
1 parent 8fdb3ee commit 63ef97d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `Widget.scrollable_container` property
- Added `Widget.select_all`
- Added `Region.bottom_right_inclusive`
- Added double click to select, triple click to select all in container
- Added arbitrary text selection
- Added Widget.ALLOW_SELECT classvar for a per-widget switch to disable text selection
- Added Widget.allow_select method for programmatic control of text selection
- Added App.ALLOW_SELECT for a global switch to disable text selection

### Fixed

Expand Down
6 changes: 6 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ class MyApp(App[None]):
Setting to `None` or `""` disables auto focus.
"""

ALLOW_SELECT: ClassVar[bool] = True
"""A switch to toggle arbitrary text selection for the app.
Note that this doesn't apply to Input and TextArea which have builtin support for selection.
"""

_BASE_PATH: str | None = None
CSS_PATH: ClassVar[CSSPathType | None] = None
"""File paths to load CSS from."""
Expand Down
12 changes: 11 additions & 1 deletion src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,11 @@ def is_active(self) -> bool:
except Exception:
return False

@property
def allow_select(self) -> bool:
"""Check if this widget permits text selection."""
return self.ALLOW_SELECT

def render(self) -> RenderableType:
"""Render method inherited from widget, used to render the screen's background.
Expand Down Expand Up @@ -1520,7 +1525,12 @@ def _forward_event(self, event: events.Event) -> None:
select_widget, select_offset = self.get_widget_and_offset_at(
event.screen_x, event.screen_y
)
if select_widget is not None and select_widget.allow_select:
if (
select_widget is not None
and select_widget.allow_select
and self.screen.allow_select
and self.app.ALLOW_SELECT
):
self._selecting = True
if select_widget is not None and select_offset is not None:
self._select_start = (
Expand Down

0 comments on commit 63ef97d

Please sign in to comment.