mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
remove boilerplate
This commit is contained in:
@@ -54,7 +54,7 @@ class FigletText:
|
|||||||
class Numbers(Widget):
|
class Numbers(Widget):
|
||||||
"""The digital display of the calculator."""
|
"""The digital display of the calculator."""
|
||||||
|
|
||||||
value: Reactive[str] = Reactive("0")
|
value = Reactive("0")
|
||||||
|
|
||||||
def render(self) -> RenderableType:
|
def render(self) -> RenderableType:
|
||||||
"""Build a Rich renderable to render the calculator display."""
|
"""Build a Rich renderable to render the calculator display."""
|
||||||
@@ -84,10 +84,10 @@ class Calculator(GridView):
|
|||||||
"=": YELLOW,
|
"=": YELLOW,
|
||||||
}
|
}
|
||||||
|
|
||||||
display: Reactive[str] = Reactive("0")
|
display = Reactive("0")
|
||||||
show_ac: Reactive[bool] = Reactive(True)
|
show_ac = Reactive(True)
|
||||||
|
|
||||||
async def watch_display(self, value: str) -> None:
|
def watch_display(self, value: str) -> None:
|
||||||
"""Called when self.display is modified."""
|
"""Called when self.display is modified."""
|
||||||
# self.numbers is a widget that displays the calculator result
|
# self.numbers is a widget that displays the calculator result
|
||||||
# Setting the attribute value changes the display
|
# Setting the attribute value changes the display
|
||||||
@@ -99,13 +99,13 @@ class Calculator(GridView):
|
|||||||
# Condition to show AC button over C
|
# Condition to show AC button over C
|
||||||
return self.value in ("", "0") and self.display == "0"
|
return self.value in ("", "0") and self.display == "0"
|
||||||
|
|
||||||
async def watch_show_ac(self, show_ac: bool) -> None:
|
def watch_show_ac(self, show_ac: bool) -> None:
|
||||||
"""When the show_ac attribute change we need to update the buttons."""
|
"""When the show_ac attribute change we need to update the buttons."""
|
||||||
# Show AC and hide C or vice versa
|
# Show AC and hide C or vice versa
|
||||||
self.c.visible = not show_ac
|
self.c.visible = not show_ac
|
||||||
self.ac.visible = show_ac
|
self.ac.visible = show_ac
|
||||||
|
|
||||||
async def on_mount(self, event: events.Mount) -> None:
|
def on_mount(self) -> None:
|
||||||
"""Event when widget is first mounted (added to a parent view)."""
|
"""Event when widget is first mounted (added to a parent view)."""
|
||||||
|
|
||||||
# Attributes to store the current calculation
|
# Attributes to store the current calculation
|
||||||
@@ -153,7 +153,7 @@ class Calculator(GridView):
|
|||||||
*self.buttons.values(), clear=self.ac, numbers=self.numbers, zero=self.zero
|
*self.buttons.values(), clear=self.ac, numbers=self.numbers, zero=self.zero
|
||||||
)
|
)
|
||||||
|
|
||||||
async def message_button_pressed(self, message: ButtonPressed) -> None:
|
def message_button_pressed(self, message: ButtonPressed) -> None:
|
||||||
"""A message sent by the button widget"""
|
"""A message sent by the button widget"""
|
||||||
|
|
||||||
assert isinstance(message.sender, Button)
|
assert isinstance(message.sender, Button)
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ from rich.console import RenderableType
|
|||||||
from rich.syntax import Syntax
|
from rich.syntax import Syntax
|
||||||
from rich.traceback import Traceback
|
from rich.traceback import Traceback
|
||||||
|
|
||||||
from textual import events
|
|
||||||
from textual.app import App
|
from textual.app import App
|
||||||
from textual.widgets import Header, Footer, FileClick, ScrollView, DirectoryTree
|
from textual.widgets import Header, Footer, FileClick, ScrollView, DirectoryTree
|
||||||
|
|
||||||
@@ -13,7 +12,7 @@ from textual.widgets import Header, Footer, FileClick, ScrollView, DirectoryTree
|
|||||||
class MyApp(App):
|
class MyApp(App):
|
||||||
"""An example of a very simple Textual App"""
|
"""An example of a very simple Textual App"""
|
||||||
|
|
||||||
async def on_load(self, event: events.Load) -> None:
|
async def on_load(self) -> None:
|
||||||
"""Sent before going in to application mode."""
|
"""Sent before going in to application mode."""
|
||||||
|
|
||||||
# Bind our basic keys
|
# Bind our basic keys
|
||||||
@@ -28,7 +27,7 @@ class MyApp(App):
|
|||||||
os.path.join(os.path.basename(__file__), "../../")
|
os.path.join(os.path.basename(__file__), "../../")
|
||||||
)
|
)
|
||||||
|
|
||||||
async def on_mount(self, event: events.Mount) -> None:
|
async def on_mount(self) -> None:
|
||||||
"""Call after terminal goes in to application mode"""
|
"""Call after terminal goes in to application mode"""
|
||||||
|
|
||||||
# Create our widgets
|
# Create our widgets
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ from textual.widgets import Placeholder
|
|||||||
|
|
||||||
|
|
||||||
class GridTest(App):
|
class GridTest(App):
|
||||||
async def on_mount(self, event: events.Mount) -> None:
|
async def on_mount(self) -> None:
|
||||||
"""Make a simple grid arrangement."""
|
"""Make a simple grid arrangement."""
|
||||||
|
|
||||||
grid = await self.view.dock_grid(edge="left", size=70, name="left")
|
grid = await self.view.dock_grid(edge="left", size=70, name="left")
|
||||||
|
|||||||
@@ -25,4 +25,5 @@ async def invoke(callback: Callable, *params: object) -> Any:
|
|||||||
|
|
||||||
result = callback(*params[:parameter_count])
|
result = callback(*params[:parameter_count])
|
||||||
if isawaitable(result):
|
if isawaitable(result):
|
||||||
await result
|
result = await result
|
||||||
|
return result
|
||||||
|
|||||||
@@ -255,7 +255,7 @@ class MessagePump:
|
|||||||
method = getattr(self, method_name, None)
|
method = getattr(self, method_name, None)
|
||||||
if method is not None:
|
if method is not None:
|
||||||
log(message, ">>>", self, verbosity=message.verbosity)
|
log(message, ">>>", self, verbosity=message.verbosity)
|
||||||
await method(message)
|
await invoke(method, message)
|
||||||
|
|
||||||
if message.bubble and self._parent and not message._stop_propagation:
|
if message.bubble and self._parent and not message._stop_propagation:
|
||||||
if message.sender == self._parent:
|
if message.sender == self._parent:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import inspect
|
from inspect import isawaitable
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from typing import (
|
from typing import (
|
||||||
Any,
|
Any,
|
||||||
@@ -16,7 +16,7 @@ from typing import (
|
|||||||
from . import log
|
from . import log
|
||||||
from . import events
|
from . import events
|
||||||
|
|
||||||
from ._callback import count_parameters
|
from ._callback import count_parameters, invoke
|
||||||
from ._types import MessageTarget
|
from ._types import MessageTarget
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@@ -90,9 +90,11 @@ class Reactive(Generic[ReactiveType]):
|
|||||||
) -> None:
|
) -> None:
|
||||||
_rich_traceback_guard = True
|
_rich_traceback_guard = True
|
||||||
if count_parameters(watch_function) == 2:
|
if count_parameters(watch_function) == 2:
|
||||||
await watch_function(old_value, value)
|
watch_result = watch_function(old_value, value)
|
||||||
else:
|
else:
|
||||||
await watch_function(value)
|
watch_result = watch_function(value)
|
||||||
|
if isawaitable(watch_result):
|
||||||
|
await watch_result
|
||||||
await Reactive.compute(obj)
|
await Reactive.compute(obj)
|
||||||
|
|
||||||
watch_function = getattr(obj, f"watch_{name}", None)
|
watch_function = getattr(obj, f"watch_{name}", None)
|
||||||
@@ -125,7 +127,8 @@ class Reactive(Generic[ReactiveType]):
|
|||||||
compute_method = getattr(obj, f"compute_{compute}")
|
compute_method = getattr(obj, f"compute_{compute}")
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
continue
|
continue
|
||||||
value = await compute_method()
|
value = await invoke(compute_method)
|
||||||
|
# value = await compute_method()
|
||||||
setattr(obj, compute, value)
|
setattr(obj, compute, value)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user