Skip to content

Commit 4ce6b79

Browse files
committed
added selection_updated method
1 parent 101f5a1 commit 4ce6b79

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/textual/screen.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from textual._spatial_map import SpatialMap
4343
from textual._types import CallbackType
44+
from textual.actions import SkipAction
4445
from textual.await_complete import AwaitComplete
4546
from textual.binding import ActiveBinding, Binding, BindingsMap
4647
from textual.css.match import match
@@ -342,7 +343,7 @@ async def _watch_selections(
342343
selections: dict[Widget, Selection],
343344
):
344345
for widget in old_selections.keys() | selections.keys():
345-
widget.refresh()
346+
widget.selection_updated(selections.get(widget, None))
346347

347348
def refresh_bindings(self) -> None:
348349
"""Call to request a refresh of bindings."""
@@ -868,9 +869,10 @@ def get_selected_text(self) -> str | None:
868869
def action_copy_text(self) -> None:
869870
"""Copy selected text to clipboard."""
870871
selection = self.get_selected_text()
871-
if selection is not None:
872-
self.app.copy_to_clipboard(selection)
873-
self.notify(selection)
872+
if selection is None:
873+
# No text selected
874+
raise SkipAction()
875+
self.app.copy_to_clipboard(selection)
874876

875877
def action_maximize(self) -> None:
876878
"""Action to maximize the currently focused widget."""

src/textual/widget.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,14 @@ def get_selection(self, selection: Selection) -> tuple[str, str] | None:
38323832
return None
38333833
return selection.extract(text), "\n"
38343834

3835+
def selection_updated(self, selection: Selection | None) -> None:
3836+
"""Called when the selection is updated.
3837+
3838+
Args:
3839+
selection: Selection information or `None` if no selection.
3840+
"""
3841+
self.refresh()
3842+
38353843
def _render_content(self) -> None:
38363844
"""Render all lines."""
38373845
width, height = self.size

src/textual/widgets/_log.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,10 @@ def get_selection(self, selection: Selection) -> tuple[str, str] | None:
272272
text = "\n".join(self._lines)
273273
return selection.extract(text), "\n"
274274

275+
def selection_updated(self, selection: Selection | None) -> None:
276+
self._render_line_cache.clear()
277+
self.refresh()
278+
275279
def render_line(self, y: int) -> Strip:
276280
"""Render a line of content.
277281
@@ -322,7 +326,9 @@ def _render_line_strip(self, y: int, rich_style: Style) -> Strip:
322326

323327
_line = self._process_line(self._lines[y])
324328

325-
line_text = Text(_line, style=rich_style, no_wrap=True)
329+
line_text = Text(_line, no_wrap=True)
330+
line_text.stylize(rich_style)
331+
326332
if self.highlight:
327333
line_text = self.highlighter(line_text)
328334
if selection is not None:

0 commit comments

Comments
 (0)