Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
willmcgugan committed Jan 4, 2025
1 parent 49d2a85 commit 458a8e5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `screen--selection` component class to define style for selection
- Added `Widget.scrollable_container` property
- Added `Widget.select_all`
- Added `Region.bottom_right_inclusive`

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion src/textual/_compositor.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def get_widget_and_offset_at(
return None, None

if y >= widget.content_region.bottom:
x, y = widget.content_region.bottom_right - (1, 1)
x, y = widget.content_region.bottom_right_inclusive

x -= region.x
y -= region.y
Expand Down
6 changes: 6 additions & 0 deletions src/textual/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,12 @@ def bottom_right(self) -> Offset:
x, y, width, height = self
return Offset(x + width, y + height)

@property
def bottom_right_inclusive(self) -> Offset:
"""Bottom right corner of the region, within its boundaries."""
x, y, width, height = self
return Offset(x + width - 1, y + height - 1)

@property
def size(self) -> Size:
"""Get the size of the region."""
Expand Down
27 changes: 16 additions & 11 deletions src/textual/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -1481,15 +1481,6 @@ def _forward_event(self, event: events.Event) -> None:
if isinstance(event, (events.Enter, events.Leave)):
self.post_message(event)

elif isinstance(event, events.MouseUp):
if (
self._mouse_down_offset is not None
and self._mouse_down_offset == event.screen_offset
):
self.clear_selection()
self._mouse_down_offset = None
self._selecting = False

elif isinstance(event, events.MouseMove):
event.style = self.get_style_at(event.screen_x, event.screen_y)
self._handle_mouse_move(event)
Expand All @@ -1498,15 +1489,29 @@ def _forward_event(self, event: events.Event) -> None:
select_widget, select_offset = self.get_widget_and_offset_at(
event.x, event.y
)
if (
if self._select_end is not None and select_offset is None:
end_widget = self._select_end[0]
select_offset = end_widget.content_region.bottom_right_inclusive
self._select_end = (end_widget, event.offset, select_offset)

elif (
select_widget is not None
and select_widget.allow_select
and select_offset is not None
):
self._select_end = (select_widget, event.offset, select_offset)

elif isinstance(event, events.MouseEvent):
if isinstance(event, events.MouseDown) and not self.app.mouse_captured:
if isinstance(event, events.MouseUp):
if (
self._mouse_down_offset is not None
and self._mouse_down_offset == event.screen_offset
):
self.clear_selection()
self._mouse_down_offset = None
self._selecting = False

elif isinstance(event, events.MouseDown) and not self.app.mouse_captured:
self._mouse_down_offset = event.screen_offset
select_widget, select_offset = self.get_widget_and_offset_at(
event.screen_x, event.screen_y
Expand Down
12 changes: 8 additions & 4 deletions src/textual/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,14 @@ def extract(self, text: str) -> str:
return lines[start_line][start_offset:end_offset]

selection: list[str] = []
first_line, *mid_lines, last_line = lines[start_line:end_line]
selection.append(first_line[start_offset:])
selection.extend(mid_lines)
selection.append(last_line[: end_offset + 1])
selected_lines = lines[start_line:end_line]
if len(selected_lines) >= 2:
first_line, *mid_lines, last_line = selected_lines
selection.append(first_line[start_offset:])
selection.extend(mid_lines)
selection.append(last_line[: end_offset + 1])
else:
return lines[start_line][start_offset:end_offset]
return "\n".join(selection)

def get_span(self, y: int) -> tuple[int, int] | None:
Expand Down

0 comments on commit 458a8e5

Please sign in to comment.