-
Notifications
You must be signed in to change notification settings - Fork 815
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tabs widget #2020
Merged
+695
−24
Merged
tabs widget #2020
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
d732fa3
tabs widget
willmcgugan bd8f96a
click underline
willmcgugan f200e24
color tweak
willmcgugan 9d32b52
docs
willmcgugan 04f03fd
docs update
willmcgugan d5aa564
expose Tab
willmcgugan fae1ffa
added remove_tab and clear
willmcgugan bbc4a2b
fix cycling
willmcgugan df7c907
add animation
willmcgugan 138afa0
docs
willmcgugan 244d250
changelog
willmcgugan c469611
remove recompose
willmcgugan 1e5facc
docstrings
willmcgugan 84a0e3a
Update docs/guide/actions.md
willmcgugan 28dd46a
Rodrigoed the tabs
willmcgugan dbb4ffe
Update docs/widgets/tabs.md
willmcgugan 48db878
Update docs/widgets/tabs.md
willmcgugan be228dd
copy
willmcgugan 1a97931
docstrings
willmcgugan efeead0
docstring
willmcgugan 7f64175
docstring
willmcgugan 578760f
Apply suggestions from code review
willmcgugan 3da60f3
stop click
willmcgugan 27843c4
docstring
willmcgugan 9ea0c0e
auto assign consistent IDs
willmcgugan b0b501e
Apply suggestions from code review
willmcgugan cd29e87
Document bindings
willmcgugan dfb693f
document bindings
willmcgugan 228caf0
Apply suggestions from code review
willmcgugan aef2e16
Apply suggestions from code review
willmcgugan 6e814d6
Merge branch 'main' into tabs-widget
willmcgugan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,2 @@ | ||
::: textual.widgets.Tab | ||
::: textual.widgets.Tabs |
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,82 @@ | ||
from textual.app import App, ComposeResult | ||
from textual.widgets import Footer, Label, Tabs | ||
|
||
NAMES = [ | ||
"Paul Atreidies", | ||
"Duke Leto Atreides", | ||
"Lady Jessica", | ||
"Gurney Halleck", | ||
"Baron Vladimir Harkonnen", | ||
"Glossu Rabban", | ||
"Chani", | ||
"Silgar", | ||
] | ||
|
||
|
||
class TabsApp(App): | ||
"""Demonstrates the Tabs widget.""" | ||
|
||
CSS = """ | ||
Tabs { | ||
dock: top; | ||
} | ||
Screen { | ||
align: center middle; | ||
} | ||
Label { | ||
margin:1 1; | ||
width: 100%; | ||
height: 100%; | ||
background: $panel; | ||
border: tall $primary; | ||
content-align: center middle; | ||
} | ||
""" | ||
|
||
BINDINGS = [ | ||
("a", "add", "Add tab"), | ||
("r", "remove", "Remove active tab"), | ||
("c", "clear", "Clear tabs"), | ||
] | ||
|
||
def compose(self) -> ComposeResult: | ||
yield Tabs(NAMES[0]) | ||
yield Label() | ||
yield Footer() | ||
|
||
def on_mount(self) -> None: | ||
"""Focus the tabs when the app starts.""" | ||
self.query_one(Tabs).focus() | ||
|
||
def on_tabs_tab_activated(self, event: Tabs.TabActivated) -> None: | ||
"""Handle TabActivated message sent by Tabs.""" | ||
label = self.query_one(Label) | ||
if event.tab is None: | ||
# When the tabs are cleared, event.tab will be None | ||
label.visible = False | ||
else: | ||
label.visible = True | ||
label.update(event.tab.label) | ||
|
||
def action_add(self) -> None: | ||
"""Add a new tab.""" | ||
tabs = self.query_one(Tabs) | ||
# Cycle the names | ||
NAMES[:] = [*NAMES[1:], NAMES[0]] | ||
tabs.add_tab(NAMES[0]) | ||
|
||
def action_remove(self) -> None: | ||
"""Remove active tab.""" | ||
tabs = self.query_one(Tabs) | ||
active_tab = tabs.active_tab | ||
if active_tab is not None: | ||
tabs.remove_tab(active_tab.id) | ||
|
||
def action_clear(self) -> None: | ||
"""Clear the tabs.""" | ||
self.query_one(Tabs).clear() | ||
|
||
|
||
if __name__ == "__main__": | ||
app = TabsApp() | ||
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
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,75 @@ | ||
# Tabs | ||
|
||
Displays a number of tab headers which may be activated with a click or navigated with cursor keys. | ||
|
||
- [x] Focusable | ||
- [ ] Container | ||
|
||
Construct a `Tabs` widget with strings or [Text][rich.text.Text] objects as positional arguments, which will set the labels in the tabs. Here's an example with three tabs: | ||
|
||
```python | ||
def compose(self) -> ComposeResult: | ||
yield Tabs("First tab", "Second tab", Text.from_markup("[u]Third[/u] tab")) | ||
``` | ||
|
||
This will create [Tab][textual.widgets.Tab] widgets internally, with auto-incrementing `id` attributes (`"tab-1"`, `"tab-2"` etc). | ||
You can also supply `Tab` objects directly in the constructor, which will allow you to explicitly set an `id`. Here's an example: | ||
|
||
```python | ||
def compose(self) -> ComposeResult: | ||
yield Tabs( | ||
Tab("First tab", id="one"), | ||
Tab("Second tab", id="two"), | ||
) | ||
``` | ||
|
||
When the user switches to a tab by clicking or pressing keys, then `Tabs` will send a [Tabs.TabActivated][textual.widgets.Tabs.TabActivated] message which contains the `tab` that was activated. | ||
You can then use `event.tab.id` attribute to perform any related actions. | ||
|
||
## Clearing tabs | ||
|
||
Clear tabs by calling the [clear][textual.widgets.Tabs.clear] method. Clearing the tabs will send a [Tabs.TabActivated][textual.widgets.Tabs.TabActivated] message with the `tab` attribute set to `None`. | ||
|
||
## Adding tabs | ||
|
||
Tabs may be added dynamically with the [add_tab][textual.widgets.Tabs.add_tab] method, which accepts strings, [Text][rich.text.Text], or [Tab][textual.widgets.Tab] objects. | ||
|
||
## Example | ||
|
||
The following example adds a `Tabs` widget above a text label. Press ++a++ to add a tab, ++c++ to clear the tabs. | ||
|
||
=== "Output" | ||
|
||
```{.textual path="docs/examples/widgets/tabs.py" press="a,a,a,a,right,right"} | ||
``` | ||
|
||
=== "tabs.py" | ||
|
||
```python | ||
--8<-- "docs/examples/widgets/tabs.py" | ||
``` | ||
|
||
|
||
## Reactive Attributes | ||
|
||
| Name | Type | Default | Description | | ||
| -------- | ----- | ------- | ---------------------------------------------------------------------------------- | | ||
| `active` | `str` | `""` | The ID of the active tab. Set this attribute to a tab ID to change the active tab. | | ||
|
||
|
||
## Messages | ||
|
||
### ::: textual.widgets.Tabs.TabActivated | ||
|
||
## Bindings | ||
|
||
The Tabs widget defines the following bindings: | ||
|
||
::: textual.widgets.Tabs.BINDINGS | ||
options: | ||
show_root_heading: false | ||
show_root_toc_entry: false | ||
|
||
## See Also | ||
|
||
- [Tabs](../api/tabs.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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't clearing the tabs send a
TabsCleared
message?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know if this warrants adding a new message. I will consider it for a later revision.