Page left and right

This commit is contained in:
Darren Burns
2024-06-10 17:20:58 +01:00
parent e31705cb89
commit b69f2ce246
3 changed files with 64 additions and 12 deletions

View File

@@ -47,6 +47,8 @@ class ScrollableContainer(Widget, can_focus=True, inherit_bindings=False):
Binding("end", "scroll_end", "Scroll End", show=False),
Binding("pageup", "page_up", "Page Up", show=False),
Binding("pagedown", "page_down", "Page Down", show=False),
Binding("ctrl+pageup", "page_left", "Page Left", show=False),
Binding("ctrl+pagedown", "page_right", "Page Right", show=False),
]
"""Keyboard bindings for scrollable containers.
@@ -60,6 +62,8 @@ class ScrollableContainer(Widget, can_focus=True, inherit_bindings=False):
| end | Scroll to the end position, if scrolling is available. |
| pageup | Scroll up one page, if vertical scrolling is available. |
| pagedown | Scroll down one page, if vertical scrolling is available. |
| ctrl+pageup | Scroll left one page, if horizontal scrolling is available. |
| ctrl+pagedown | Scroll right one page, if horizontal scrolling is available. |
"""

View File

@@ -3890,6 +3890,18 @@ class Widget(DOMNode):
self._clear_anchor()
self.scroll_page_up()
def action_page_left(self) -> None:
if not self.allow_horizontal_scroll:
raise SkipAction()
self._clear_anchor()
self.scroll_page_left()
def action_page_right(self) -> None:
if not self.allow_horizontal_scroll:
raise SkipAction()
self._clear_anchor()
self.scroll_page_right()
def notify(
self,
message: str,

View File

@@ -222,17 +222,25 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
Binding("left,h", "cursor_left", "Cursor Left", show=False),
Binding("pageup", "page_up", "Page Up", show=False),
Binding("pagedown", "page_down", "Page Down", show=False),
Binding("g,home", "scroll_home", "Home", show=False),
Binding("G,end", "scroll_end", "End", show=False),
Binding("g", "scroll_top", "Top", show=False),
Binding("G", "scroll_bottom", "Bottom", show=False),
Binding("home", "scroll_home", "Home", show=False),
Binding("end", "scroll_end", "End", show=False),
]
"""
| Key(s) | Description |
| :- | :- |
| enter | Select cells under the cursor. |
| up | Move the cursor up. |
| down | Move the cursor down. |
| right | Move the cursor right. |
| left | Move the cursor left. |
| up,k | Move the cursor up. |
| down,j | Move the cursor down. |
| right,l | Move the cursor right. |
| left,h | Move the cursor left. |
| pageup | Move one page up. |
| pagedown | Move one page down. |
| g | Move to the top. |
| G | Move to the bottom. |
| home | Move to the home position (leftmost column). |
| end | Move to the end position (rightmost column). |
"""
COMPONENT_CLASSES: ClassVar[set[str]] = {
@@ -2526,26 +2534,54 @@ class DataTable(ScrollView, Generic[CellType], can_focus=True):
else:
super().action_page_up()
def action_scroll_home(self) -> None:
"""Scroll to the top of the data table."""
def action_page_left(self) -> None:
"""Move the cursor one page left."""
self._set_hover_cursor(False)
super().scroll_page_left()
def action_page_right(self) -> None:
"""Move the cursor one page right."""
self._set_hover_cursor(False)
super().scroll_page_right()
def action_scroll_top(self) -> None:
"""Move the cursor and scroll to the top."""
self._set_hover_cursor(False)
cursor_type = self.cursor_type
if self.show_cursor and (cursor_type == "cell" or cursor_type == "row"):
row_index, column_index = self.cursor_coordinate
_, column_index = self.cursor_coordinate
self.cursor_coordinate = Coordinate(0, column_index)
else:
super().action_scroll_home()
def action_scroll_end(self) -> None:
"""Scroll to the bottom of the data table."""
def action_scroll_bottom(self) -> None:
"""Move the cursor and scroll to the bottom."""
self._set_hover_cursor(False)
cursor_type = self.cursor_type
if self.show_cursor and (cursor_type == "cell" or cursor_type == "row"):
row_index, column_index = self.cursor_coordinate
_, column_index = self.cursor_coordinate
self.cursor_coordinate = Coordinate(self.row_count - 1, column_index)
else:
super().action_scroll_end()
def action_scroll_home(self) -> None:
"""Move the cursor and scroll to the leftmost column."""
self._set_hover_cursor(False)
cursor_type = self.cursor_type
if self.show_cursor and (cursor_type == "cell" or cursor_type == "column"):
self.move_cursor(column=0)
else:
self.scroll_x = 0
def action_scroll_end(self) -> None:
"""Move the cursor and scroll to the rightmost column."""
self._set_hover_cursor(False)
cursor_type = self.cursor_type
if self.show_cursor and (cursor_type == "cell" or cursor_type == "column"):
self.move_cursor(column=len(self.columns) - 1)
else:
self.scroll_x = self.max_scroll_x
def action_cursor_up(self) -> None:
self._set_hover_cursor(False)
cursor_type = self.cursor_type