Get text under selection. Returns: Selected text, or `None` if no text was selected.
(self)
| 961 | ) |
| 962 | |
| 963 | def get_selected_text(self) -> str | None: |
| 964 | """Get text under selection. |
| 965 | |
| 966 | Returns: |
| 967 | Selected text, or `None` if no text was selected. |
| 968 | """ |
| 969 | if not self.selections: |
| 970 | return None |
| 971 | |
| 972 | widget_text: list[str] = [] |
| 973 | for widget, selection in self.selections.items(): |
| 974 | # Filter out widgets that may have been removed since the text was selected |
| 975 | if ( |
| 976 | widget.is_attached |
| 977 | and (selected_text_in_widget := widget.get_selection(selection)) |
| 978 | is not None |
| 979 | ): |
| 980 | widget_text.extend(selected_text_in_widget) |
| 981 | |
| 982 | selected_text = "".join(widget_text).rstrip("\n") |
| 983 | return selected_text |
| 984 | |
| 985 | def action_copy_text(self) -> None: |
| 986 | """Copy selected text to clipboard.""" |