mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
added borders utility
This commit is contained in:
70
src/textual/devtools/borders.py
Normal file
70
src/textual/devtools/borders.py
Normal file
@@ -0,0 +1,70 @@
|
||||
from textual.app import App, ComposeResult
|
||||
from textual.constants import BORDERS
|
||||
from textual.widgets import Button, Static
|
||||
from textual import layout
|
||||
|
||||
|
||||
TEXT = """I must not fear.
|
||||
Fear is the mind-killer.
|
||||
Fear is the little-death that brings total obliteration.
|
||||
I will face my fear.
|
||||
I will permit it to pass over me and through me.
|
||||
And when it has gone past, I will turn the inner eye to see its path.
|
||||
Where the fear has gone there will be nothing. Only I will remain."""
|
||||
|
||||
|
||||
class BorderButtons(layout.Vertical):
|
||||
CSS = """
|
||||
BorderButtons {
|
||||
dock: left;
|
||||
width: 20;
|
||||
}
|
||||
|
||||
BorderButtons > Button {
|
||||
width: 20;
|
||||
}
|
||||
"""
|
||||
|
||||
def compose(self) -> ComposeResult:
|
||||
for border in BORDERS:
|
||||
if border:
|
||||
yield Button(border, id=border)
|
||||
|
||||
|
||||
class BorderApp(App):
|
||||
"""Displays a pride flag."""
|
||||
|
||||
COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
|
||||
|
||||
CSS = """
|
||||
Screen {
|
||||
background: $surface;
|
||||
}
|
||||
Static {
|
||||
margin: 2 4;
|
||||
padding: 2 4;
|
||||
border: solid $warning;
|
||||
height: auto;
|
||||
background: $panel;
|
||||
color: $text-panel-fade-1;
|
||||
}
|
||||
"""
|
||||
|
||||
def compose(self):
|
||||
self.dark = True
|
||||
yield BorderButtons()
|
||||
self.text = Static(TEXT)
|
||||
yield self.text
|
||||
|
||||
def handle_pressed(self, event):
|
||||
self.text.styles.border = (
|
||||
event.button.id,
|
||||
self.stylesheet.variables["warning"],
|
||||
)
|
||||
self.bell()
|
||||
|
||||
|
||||
app = BorderApp()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
@@ -192,15 +192,19 @@ class Button(Widget, can_focus=True):
|
||||
if self.disabled:
|
||||
return
|
||||
# Manage the "active" effect:
|
||||
self._start_active_affect()
|
||||
# ...and let other components know that we've just been clicked:
|
||||
await self.emit(Button.Pressed(self))
|
||||
|
||||
def _start_active_affect(self) -> None:
|
||||
self.add_class("-active")
|
||||
self.set_timer(
|
||||
self.ACTIVE_EFFECT_DURATION, partial(self.remove_class, "-active")
|
||||
)
|
||||
# ...and let other components know that we've just been clicked:
|
||||
await self.emit(Button.Pressed(self))
|
||||
|
||||
async def on_key(self, event: events.Key) -> None:
|
||||
if event.key == "enter" and not self.disabled:
|
||||
self._start_active_affect()
|
||||
await self.emit(Button.Pressed(self))
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user