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

tooltip params #4625

Merged
merged 8 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- `get_content_height` will now return 0 if the renderable is Falsey https://github.com/Textualize/textual/pull/4617
- Buttons may not be pressed within their "active_effect_duration" to prevent inadvertent activations https://github.com/Textualize/textual/pull/4621
- `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
- Increased window for escape processing to 100ms
- Increased window for escape processing to 100ms https://github.com/Textualize/textual/pull/4625
- Tooltips are now hidden when any key is pressed https://github.com/Textualize/textual/pull/4625

### Added

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

## [0.65.2] - 2023-06-06

Expand Down
5 changes: 5 additions & 0 deletions src/textual/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3035,6 +3035,11 @@ async def on_event(self, event: events.Event) -> None:
pass

elif isinstance(event, events.Key):
if self.focused:
try:
self.screen._clear_tooltip()
except NoScreen:
pass
if not await self.check_bindings(event.key, priority=True):
forward_target = self.focused or self.screen
forward_target._forward_event(event)
Expand Down
1 change: 1 addition & 0 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,7 @@ def _forward_event(self, event: events.Event) -> None:
if event.is_forwarded:
return
event._set_forwarded()

if isinstance(event, (events.Enter, events.Leave)):
self.post_message(event)

Expand Down
6 changes: 5 additions & 1 deletion src/textual/widgets/_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
):
"""Create a Button widget.

Expand All @@ -194,6 +195,7 @@ def __init__(
id: The ID of the button in the DOM.
classes: The CSS classes of the button.
disabled: Whether the button is disabled or not.
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)

Expand All @@ -202,8 +204,10 @@ def __init__(

self.label = label
self.variant = variant
self.active_effect_duration = 0.3
self.active_effect_duration = 0.2
"""Amount of time in seconds the button 'press' animation lasts."""
if tooltip is not None:
self.tooltip = tooltip

def get_content_width(self, container: Size, viewport: Size) -> int:
try:
Expand Down
4 changes: 4 additions & 0 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
) -> None:
"""Initialise the `Input` widget.

Expand All @@ -279,6 +280,7 @@ def __init__(
id: Optional ID for the widget.
classes: Optional initial classes for the widget.
disabled: Whether the input is disabled or not.
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)

Expand Down Expand Up @@ -335,6 +337,8 @@ def __init__(

if value is not None:
self.value = value
if tooltip is not None:
self.tooltip = tooltip

def _position_to_cell(self, position: int) -> int:
"""Convert an index within the value to cell position."""
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_option_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ def __init__(
classes: str | None = None,
disabled: bool = False,
wrap: bool = True,
tooltip: RenderableType | None = None,
):
"""Initialise the option list.

Expand All @@ -295,6 +296,7 @@ def __init__(
classes: The CSS classes of the option list.
disabled: Whether the option list is disabled or not.
wrap: Should prompts be auto-wrapped?
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)

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

if tooltip is not None:
self.tooltip = tooltip

def get_content_width(self, container: Size, viewport: Size) -> int:
"""Get maximum width of options."""
console = self.app.console
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_radio_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import ClassVar, Optional

import rich.repr
from rich.console import RenderableType

from .. import _widget_navigation
from ..binding import Binding, BindingType
Expand Down Expand Up @@ -121,6 +122,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
) -> None:
"""Initialise the radio set.

Expand All @@ -130,6 +132,7 @@ def __init__(
id: The ID of the radio set in the DOM.
classes: The CSS classes of the radio set.
disabled: Whether the radio set is disabled or not.
tooltip: Optional tooltip.

Note:
When a `str` label is provided, a
Expand All @@ -148,6 +151,8 @@ def __init__(
classes=classes,
disabled=disabled,
)
if tooltip is not None:
self.tooltip = tooltip

def _on_mount(self, _: Mount) -> None:
"""Perform some processing once mounted in the DOM."""
Expand Down
4 changes: 4 additions & 0 deletions src/textual/widgets/_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
):
"""Initialize the Select control.

Expand All @@ -315,6 +316,7 @@ def __init__(
id: The ID of the control in the DOM.
classes: The CSS classes of the control.
disabled: Whether the control is disabled or not.
tooltip: Optional tooltip.

Raises:
EmptySelectError: If no options are provided and `allow_blank` is `False`.
Expand All @@ -324,6 +326,8 @@ def __init__(
self.prompt = prompt
self._value = value
self._setup_variables_for_options(options)
if tooltip is not None:
self.tooltip = tooltip

@classmethod
def from_values(
Expand Down
6 changes: 6 additions & 0 deletions src/textual/widgets/_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from typing import TYPE_CHECKING, ClassVar

from rich.console import RenderableType

if TYPE_CHECKING:
from ..app import RenderResult
from ..binding import Binding, BindingType
Expand Down Expand Up @@ -110,6 +112,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
):
"""Initialise the switch.

Expand All @@ -120,12 +123,15 @@ def __init__(
id: The ID of the switch in the DOM.
classes: The CSS classes of the switch.
disabled: Whether the switch is disabled or not.
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
if value:
self.slider_pos = 1.0
self.set_reactive(Switch.value, value)
self._should_animate = animate
if tooltip is not None:
self.tooltip = tooltip

def watch_value(self, value: bool) -> None:
target_slider_pos = 1.0 if value else 0.0
Expand Down
9 changes: 9 additions & 0 deletions src/textual/widgets/_text_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pathlib import Path
from typing import TYPE_CHECKING, ClassVar, Iterable, Optional, Sequence, Tuple

from rich.console import RenderableType
from rich.style import Style
from rich.text import Text
from typing_extensions import Literal
Expand Down Expand Up @@ -374,6 +375,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
) -> None:
"""Construct a new `TextArea`.

Expand All @@ -390,6 +392,7 @@ def __init__(
id: The ID of the widget, used to refer to it from Textual CSS.
classes: One or more Textual CSS compatible class names separated by spaces.
disabled: True if the widget is disabled.
tooltip: Optional tooltip.
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)

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

if tooltip is not None:
self.tooltip = tooltip

@classmethod
def code_editor(
cls,
Expand All @@ -474,6 +480,7 @@ def code_editor(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
) -> TextArea:
"""Construct a new `TextArea` with sensible defaults for editing code.

Expand All @@ -491,6 +498,7 @@ def code_editor(
id: The ID of the widget, used to refer to it from Textual CSS.
classes: One or more Textual CSS compatible class names separated by spaces.
disabled: True if the widget is disabled.
tooltip: Optional tooltip
"""
return cls(
text,
Expand All @@ -505,6 +513,7 @@ def code_editor(
id=id,
classes=classes,
disabled=disabled,
tooltip=tooltip,
)

@staticmethod
Expand Down
5 changes: 5 additions & 0 deletions src/textual/widgets/_toggle_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from typing import TYPE_CHECKING, ClassVar

from rich.console import RenderableType
from rich.style import Style
from rich.text import Text, TextType

Expand Down Expand Up @@ -130,6 +131,7 @@ def __init__(
id: str | None = None,
classes: str | None = None,
disabled: bool = False,
tooltip: RenderableType | None = None,
) -> None:
"""Initialise the toggle.

Expand All @@ -141,13 +143,16 @@ def __init__(
id: The ID of the toggle in the DOM.
classes: The CSS classes of the toggle.
disabled: Whether the button is disabled or not.
tooltip: RenderableType | None = None,
"""
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
self._button_first = button_first
# NOTE: Don't send a Changed message in response to the initial set.
with self.prevent(self.Changed):
self.value = value
self._label = self._make_label(label)
if tooltip is not None:
self.tooltip = tooltip

def _make_label(self, label: TextType) -> Text:
"""Make a `Text` label from a `TextType` value.
Expand Down
Loading