Skip to content

Commit 2ddcb38

Browse files
authored
Merge pull request #4625 from Textualize/tooltip-input
tooltip params
2 parents 14f5a9d + 5510ac3 commit 2ddcb38

File tree

11 files changed

+52
-2
lines changed

11 files changed

+52
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
- `get_content_height` will now return 0 if the renderable is Falsey https://github.com/Textualize/textual/pull/4617
1515
- Buttons may not be pressed within their "active_effect_duration" to prevent inadvertent activations https://github.com/Textualize/textual/pull/4621
1616
- `Screen.dismiss` is now a noop if the screen isn't active. Previously it would raise a `ScreenStackError`, now it returns `False`. https://github.com/Textualize/textual/pull/4621
17-
- Increased window for escape processing to 100ms
17+
- Increased window for escape processing to 100ms https://github.com/Textualize/textual/pull/4625
18+
- Tooltips are now hidden when any key is pressed https://github.com/Textualize/textual/pull/4625
1819

1920
### Added
2021

2122
- Added `Screen.is_active`
23+
- Added `tooltip` parameter to input widgets https://github.com/Textualize/textual/pull/4625
2224

2325
## [0.65.2] - 2023-06-06
2426

src/textual/app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,11 @@ async def on_event(self, event: events.Event) -> None:
30353035
pass
30363036

30373037
elif isinstance(event, events.Key):
3038+
if self.focused:
3039+
try:
3040+
self.screen._clear_tooltip()
3041+
except NoScreen:
3042+
pass
30383043
if not await self.check_bindings(event.key, priority=True):
30393044
forward_target = self.focused or self.screen
30403045
forward_target._forward_event(event)

src/textual/screen.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,7 @@ def _forward_event(self, event: events.Event) -> None:
11891189
if event.is_forwarded:
11901190
return
11911191
event._set_forwarded()
1192+
11921193
if isinstance(event, (events.Enter, events.Leave)):
11931194
self.post_message(event)
11941195

src/textual/widgets/_button.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ def __init__(
184184
id: str | None = None,
185185
classes: str | None = None,
186186
disabled: bool = False,
187+
tooltip: RenderableType | None = None,
187188
):
188189
"""Create a Button widget.
189190
@@ -194,6 +195,7 @@ def __init__(
194195
id: The ID of the button in the DOM.
195196
classes: The CSS classes of the button.
196197
disabled: Whether the button is disabled or not.
198+
tooltip: Optional tooltip.
197199
"""
198200
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
199201

@@ -202,8 +204,10 @@ def __init__(
202204

203205
self.label = label
204206
self.variant = variant
205-
self.active_effect_duration = 0.3
207+
self.active_effect_duration = 0.2
206208
"""Amount of time in seconds the button 'press' animation lasts."""
209+
if tooltip is not None:
210+
self.tooltip = tooltip
207211

208212
def get_content_width(self, container: Size, viewport: Size) -> int:
209213
try:

src/textual/widgets/_input.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ def __init__(
257257
id: str | None = None,
258258
classes: str | None = None,
259259
disabled: bool = False,
260+
tooltip: RenderableType | None = None,
260261
) -> None:
261262
"""Initialise the `Input` widget.
262263
@@ -279,6 +280,7 @@ def __init__(
279280
id: Optional ID for the widget.
280281
classes: Optional initial classes for the widget.
281282
disabled: Whether the input is disabled or not.
283+
tooltip: Optional tooltip.
282284
"""
283285
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
284286

@@ -335,6 +337,8 @@ def __init__(
335337

336338
if value is not None:
337339
self.value = value
340+
if tooltip is not None:
341+
self.tooltip = tooltip
338342

339343
def _position_to_cell(self, position: int) -> int:
340344
"""Convert an index within the value to cell position."""

src/textual/widgets/_option_list.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ def __init__(
285285
classes: str | None = None,
286286
disabled: bool = False,
287287
wrap: bool = True,
288+
tooltip: RenderableType | None = None,
288289
):
289290
"""Initialise the option list.
290291
@@ -295,6 +296,7 @@ def __init__(
295296
classes: The CSS classes of the option list.
296297
disabled: Whether the option list is disabled or not.
297298
wrap: Should prompts be auto-wrapped?
299+
tooltip: Optional tooltip.
298300
"""
299301
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
300302

@@ -361,6 +363,9 @@ def __init__(
361363
# the state of the option list in regard to its available options.
362364
self.action_first()
363365

366+
if tooltip is not None:
367+
self.tooltip = tooltip
368+
364369
def get_content_width(self, container: Size, viewport: Size) -> int:
365370
"""Get maximum width of options."""
366371
console = self.app.console

src/textual/widgets/_radio_set.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import ClassVar, Optional
66

77
import rich.repr
8+
from rich.console import RenderableType
89

910
from .. import _widget_navigation
1011
from ..binding import Binding, BindingType
@@ -121,6 +122,7 @@ def __init__(
121122
id: str | None = None,
122123
classes: str | None = None,
123124
disabled: bool = False,
125+
tooltip: RenderableType | None = None,
124126
) -> None:
125127
"""Initialise the radio set.
126128
@@ -130,6 +132,7 @@ def __init__(
130132
id: The ID of the radio set in the DOM.
131133
classes: The CSS classes of the radio set.
132134
disabled: Whether the radio set is disabled or not.
135+
tooltip: Optional tooltip.
133136
134137
Note:
135138
When a `str` label is provided, a
@@ -148,6 +151,8 @@ def __init__(
148151
classes=classes,
149152
disabled=disabled,
150153
)
154+
if tooltip is not None:
155+
self.tooltip = tooltip
151156

152157
def _on_mount(self, _: Mount) -> None:
153158
"""Perform some processing once mounted in the DOM."""

src/textual/widgets/_select.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ def __init__(
298298
id: str | None = None,
299299
classes: str | None = None,
300300
disabled: bool = False,
301+
tooltip: RenderableType | None = None,
301302
):
302303
"""Initialize the Select control.
303304
@@ -315,6 +316,7 @@ def __init__(
315316
id: The ID of the control in the DOM.
316317
classes: The CSS classes of the control.
317318
disabled: Whether the control is disabled or not.
319+
tooltip: Optional tooltip.
318320
319321
Raises:
320322
EmptySelectError: If no options are provided and `allow_blank` is `False`.
@@ -324,6 +326,8 @@ def __init__(
324326
self.prompt = prompt
325327
self._value = value
326328
self._setup_variables_for_options(options)
329+
if tooltip is not None:
330+
self.tooltip = tooltip
327331

328332
@classmethod
329333
def from_values(

src/textual/widgets/_switch.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from typing import TYPE_CHECKING, ClassVar
44

5+
from rich.console import RenderableType
6+
57
if TYPE_CHECKING:
68
from ..app import RenderResult
79
from ..binding import Binding, BindingType
@@ -110,6 +112,7 @@ def __init__(
110112
id: str | None = None,
111113
classes: str | None = None,
112114
disabled: bool = False,
115+
tooltip: RenderableType | None = None,
113116
):
114117
"""Initialise the switch.
115118
@@ -120,12 +123,15 @@ def __init__(
120123
id: The ID of the switch in the DOM.
121124
classes: The CSS classes of the switch.
122125
disabled: Whether the switch is disabled or not.
126+
tooltip: Optional tooltip.
123127
"""
124128
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
125129
if value:
126130
self.slider_pos = 1.0
127131
self.set_reactive(Switch.value, value)
128132
self._should_animate = animate
133+
if tooltip is not None:
134+
self.tooltip = tooltip
129135

130136
def watch_value(self, value: bool) -> None:
131137
target_slider_pos = 1.0 if value else 0.0

src/textual/widgets/_text_area.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99
from typing import TYPE_CHECKING, ClassVar, Iterable, Optional, Sequence, Tuple
1010

11+
from rich.console import RenderableType
1112
from rich.style import Style
1213
from rich.text import Text
1314
from typing_extensions import Literal
@@ -374,6 +375,7 @@ def __init__(
374375
id: str | None = None,
375376
classes: str | None = None,
376377
disabled: bool = False,
378+
tooltip: RenderableType | None = None,
377379
) -> None:
378380
"""Construct a new `TextArea`.
379381
@@ -390,6 +392,7 @@ def __init__(
390392
id: The ID of the widget, used to refer to it from Textual CSS.
391393
classes: One or more Textual CSS compatible class names separated by spaces.
392394
disabled: True if the widget is disabled.
395+
tooltip: Optional tooltip.
393396
"""
394397
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
395398

@@ -458,6 +461,9 @@ def __init__(
458461
# When `app.dark` is toggled, reset the theme (since it caches values).
459462
self.watch(self.app, "dark", self._app_dark_toggled, init=False)
460463

464+
if tooltip is not None:
465+
self.tooltip = tooltip
466+
461467
@classmethod
462468
def code_editor(
463469
cls,
@@ -474,6 +480,7 @@ def code_editor(
474480
id: str | None = None,
475481
classes: str | None = None,
476482
disabled: bool = False,
483+
tooltip: RenderableType | None = None,
477484
) -> TextArea:
478485
"""Construct a new `TextArea` with sensible defaults for editing code.
479486
@@ -491,6 +498,7 @@ def code_editor(
491498
id: The ID of the widget, used to refer to it from Textual CSS.
492499
classes: One or more Textual CSS compatible class names separated by spaces.
493500
disabled: True if the widget is disabled.
501+
tooltip: Optional tooltip
494502
"""
495503
return cls(
496504
text,
@@ -505,6 +513,7 @@ def code_editor(
505513
id=id,
506514
classes=classes,
507515
disabled=disabled,
516+
tooltip=tooltip,
508517
)
509518

510519
@staticmethod

0 commit comments

Comments
 (0)