-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1143 from Textualize/list-view
List view
- Loading branch information
Showing
15 changed files
with
525 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: textual.widgets.ListItem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
::: textual.widgets.ListView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Screen { | ||
align: center middle; | ||
} | ||
|
||
ListView { | ||
width: 30; | ||
height: auto; | ||
margin: 2 2; | ||
} | ||
|
||
Label { | ||
padding: 1 2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.widgets import ListView, ListItem, Label, Footer | ||
|
||
|
||
class ListViewExample(App): | ||
def compose(self) -> ComposeResult: | ||
yield ListView( | ||
ListItem(Label("One")), | ||
ListItem(Label("Two")), | ||
ListItem(Label("Three")), | ||
) | ||
yield Footer() | ||
|
||
|
||
app = ListViewExample(css_path="list_view.css") | ||
if __name__ == "__main__": | ||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# List Item | ||
|
||
`ListItem` is the type of the elements in a `ListView`. | ||
|
||
- [] Focusable | ||
- [] Container | ||
|
||
## Example | ||
|
||
The example below shows an app with a simple `ListView`, consisting | ||
of multiple `ListItem`s. The arrow keys can be used to navigate the list. | ||
|
||
=== "Output" | ||
|
||
```{.textual path="docs/examples/widgets/list_view.py"} | ||
``` | ||
|
||
=== "list_view.py" | ||
|
||
```python | ||
--8<-- "docs/examples/widgets/list_view.py" | ||
``` | ||
|
||
## Reactive Attributes | ||
|
||
| Name | Type | Default | Description | | ||
|---------------|--------|---------|--------------------------------------| | ||
| `highlighted` | `bool` | `False` | True if this ListItem is highlighted | | ||
|
||
## Messages | ||
|
||
### Selected | ||
|
||
The `ListItem.Selected` message is sent when the item is selected. | ||
|
||
- [x] Bubbles | ||
|
||
#### Attributes | ||
|
||
| attribute | type | purpose | | ||
|-----------|------------|-----------------------------| | ||
| `item` | `ListItem` | The item that was selected. | | ||
|
||
## See Also | ||
|
||
* [ListItem](../api/list_item.md) code reference |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# List View | ||
|
||
Displays a vertical list of `ListItem`s which can be highlighted and selected. | ||
Supports keyboard navigation. | ||
|
||
- [x] Focusable | ||
- [x] Container | ||
|
||
## Example | ||
|
||
The example below shows an app with a simple `ListView`. | ||
|
||
=== "Output" | ||
|
||
```{.textual path="docs/examples/widgets/list_view.py"} | ||
``` | ||
|
||
=== "list_view.py" | ||
|
||
```python | ||
--8<-- "docs/examples/widgets/list_view.py" | ||
``` | ||
|
||
## Reactive Attributes | ||
|
||
| Name | Type | Default | Description | | ||
|---------|-------|---------|---------------------------------| | ||
| `index` | `int` | `0` | The currently highlighted index | | ||
|
||
## Messages | ||
|
||
### Highlighted | ||
|
||
The `ListView.Highlighted` message is emitted when the highlight changes. | ||
This happens when you use the arrow keys on your keyboard and when you | ||
click on a list item. | ||
|
||
- [x] Bubbles | ||
|
||
#### Attributes | ||
|
||
| attribute | type | purpose | | ||
|-----------|------------|--------------------------------| | ||
| `item` | `ListItem` | The item that was highlighted. | | ||
|
||
### Selected | ||
|
||
The `ListView.Selected` message is emitted when a list item is selected. | ||
You can select a list item by pressing ++enter++ while it is highlighted, | ||
or by clicking on it. | ||
|
||
- [x] Bubbles | ||
|
||
#### Attributes | ||
|
||
| attribute | type | purpose | | ||
|-----------|------------|-----------------------------| | ||
| `item` | `ListItem` | The item that was selected. | | ||
|
||
|
||
### ChildrenUpdated | ||
|
||
The `ListView.ChildrenUpdated` message is emitted when the elements in the `ListView` | ||
are changed (e.g. a child is added, or the list is cleared). | ||
|
||
- [x] Bubbles | ||
|
||
#### Attributes | ||
|
||
| attribute | type | purpose | | ||
|------------|------------------|---------------------------| | ||
| `children` | `list[ListItem]` | The new ListView children | | ||
|
||
|
||
|
||
## See Also | ||
|
||
* [ListView](../api/list_view.md) code reference |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from textual import events | ||
from textual.message import Message | ||
from textual.reactive import reactive | ||
from textual.widget import Widget | ||
|
||
|
||
class ListItem(Widget, can_focus=False): | ||
DEFAULT_CSS = """ | ||
ListItem { | ||
color: $text; | ||
height: auto; | ||
background: $panel-lighten-1; | ||
overflow: hidden hidden; | ||
} | ||
ListItem > Widget :hover { | ||
background: $boost; | ||
} | ||
ListView > ListItem.--highlight { | ||
background: $accent 50%; | ||
} | ||
ListView:focus > ListItem.--highlight { | ||
background: $accent; | ||
} | ||
ListItem > Widget { | ||
height: auto; | ||
} | ||
""" | ||
highlighted = reactive(False) | ||
|
||
def on_click(self, event: events.Click) -> None: | ||
self.emit_no_wait(self._ChildClicked(self)) | ||
|
||
def watch_highlighted(self, value: bool) -> None: | ||
self.set_class(value, "--highlight") | ||
|
||
class _ChildClicked(Message): | ||
"""For informing with the parent ListView that we were clicked""" | ||
|
||
pass |
Oops, something went wrong.