Move focus logic to screen, add more key replacements, collapse bindings in footer (#880)

* Move focusing logic to the Screen level

* Update tests to support per-screen focus management

* Some additional key name replacements

* Improve rendering of bindings in footer when multiple items have same action

* Clean up footer, allow key_displays csv

* Prevent exception when widget is not in screen
This commit is contained in:
darrenburns
2022-10-13 10:43:16 +01:00
committed by GitHub
parent a16db13157
commit 36ac94734f
13 changed files with 257 additions and 155 deletions

View File

@@ -7,8 +7,14 @@ from textual.widgets import Static, Footer, Header
class JustABox(App):
BINDINGS = [
Binding(key="t", action="text_fade_out", description="text-opacity fade out"),
Binding(key="o,f,w", action="widget_fade_out", description="opacity fade out"),
Binding(
key="ctrl+t", action="text_fade_out", description="text-opacity fade out"
),
Binding(
key="o,f,w",
action="widget_fade_out",
description="opacity fade out",
),
]
def compose(self) -> ComposeResult:
@@ -28,6 +34,9 @@ class JustABox(App):
print(self.screen.styles.get_rules())
print(self.screen.styles.css)
def key_plus(self):
print("plus!")
app = JustABox(watch_css=True, css_path="../darren/just_a_box.css")

View File

@@ -0,0 +1,9 @@
Focusable {
padding: 1 2;
background: $panel;
margin-bottom: 1;
}
Focusable:focus {
outline: solid dodgerblue;
}

View File

@@ -0,0 +1,47 @@
from textual.app import App, ComposeResult, ScreenStackError
from textual.binding import Binding
from textual.screen import Screen
from textual.widgets import Static, Footer, Input
class Focusable(Static, can_focus=True):
pass
class CustomScreen(Screen):
def compose(self) -> ComposeResult:
yield Focusable(f"Screen {id(self)} - two")
yield Focusable(f"Screen {id(self)} - three")
yield Focusable(f"Screen {id(self)} - four")
yield Input(placeholder="Text input")
yield Footer()
class ScreensFocusApp(App):
BINDINGS = [
Binding("plus", "push_new_screen", "Push"),
Binding("minus", "pop_top_screen", "Pop"),
]
def compose(self) -> ComposeResult:
yield Focusable("App - one")
yield Input(placeholder="Text input")
yield Input(placeholder="Text input")
yield Focusable("App - two")
yield Focusable("App - three")
yield Focusable("App - four")
yield Footer()
def action_push_new_screen(self):
self.push_screen(CustomScreen())
def action_pop_top_screen(self):
try:
self.pop_screen()
except ScreenStackError:
pass
app = ScreensFocusApp(css_path="screens_focus.css")
if __name__ == "__main__":
app.run()