mirror of
https://github.com/Textualize/textual.git
synced 2025-10-17 02:38:12 +03:00
Move the main handling of disabled up to Widget
There's still a bit to do here, but this migrates the main work up to the `Widget`. At this point `Button` is pretty much built expressed as a function of what `Widget` provides in terms of things being disabled. Focus can still move into disabled controls (or in this case right now, into a disabled `Button`). The next step is to add something that works alongside `can_focus` to say if a control is currently capable of receiving focus (in other words, it's `not disabled and can_focus`).
This commit is contained in:
@@ -2140,6 +2140,10 @@ class Widget(DOMNode):
|
||||
"""Update from CSS if has focus state changes."""
|
||||
self._update_styles()
|
||||
|
||||
def watch_disabled(self) -> None:
|
||||
# self.can_focus = not self.disabled
|
||||
self._update_styles()
|
||||
|
||||
def _size_updated(
|
||||
self, size: Size, virtual_size: Size, container_size: Size
|
||||
) -> None:
|
||||
|
||||
@@ -39,7 +39,7 @@ class Button(Static, can_focus=True):
|
||||
text-style: bold;
|
||||
}
|
||||
|
||||
Button.-disabled {
|
||||
Button:disabled {
|
||||
opacity: 0.4;
|
||||
text-opacity: 0.7;
|
||||
}
|
||||
@@ -173,34 +173,30 @@ class Button(Static, can_focus=True):
|
||||
def __init__(
|
||||
self,
|
||||
label: TextType | None = None,
|
||||
disabled: bool = False,
|
||||
variant: ButtonVariant = "default",
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
):
|
||||
"""Create a Button widget.
|
||||
|
||||
Args:
|
||||
label: The text that appears within the button.
|
||||
disabled: Whether the button is disabled or not.
|
||||
variant: The variant of the button.
|
||||
name: The name of the button.
|
||||
id: The ID of the button in the DOM.
|
||||
classes: The CSS classes of the button.
|
||||
disabled: Whether the button is disabled or not.
|
||||
"""
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||
|
||||
if label is None:
|
||||
label = self.css_identifier_styled
|
||||
|
||||
self.label = self.validate_label(label)
|
||||
|
||||
self.disabled = disabled
|
||||
if disabled:
|
||||
self.add_class("-disabled")
|
||||
|
||||
self.variant = self.validate_variant(variant)
|
||||
|
||||
def __rich_repr__(self) -> rich.repr.Result:
|
||||
@@ -224,9 +220,9 @@ class Button(Static, can_focus=True):
|
||||
self.remove_class(f"-{old_variant}")
|
||||
self.add_class(f"-{variant}")
|
||||
|
||||
def watch_disabled(self, disabled: bool) -> None:
|
||||
self.set_class(disabled, "-disabled")
|
||||
self.can_focus = not disabled
|
||||
# def watch_disabled(self, disabled: bool) -> None:
|
||||
# self.set_class(disabled, "-disabled")
|
||||
# self.can_focus = not disabled
|
||||
|
||||
def validate_label(self, label: RenderableType) -> RenderableType:
|
||||
"""Parse markup for self.label"""
|
||||
@@ -269,11 +265,11 @@ class Button(Static, can_focus=True):
|
||||
def success(
|
||||
cls,
|
||||
label: TextType | None = None,
|
||||
disabled: bool = False,
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
) -> Button:
|
||||
"""Utility constructor for creating a success Button variant.
|
||||
|
||||
@@ -289,22 +285,22 @@ class Button(Static, can_focus=True):
|
||||
"""
|
||||
return Button(
|
||||
label=label,
|
||||
disabled=disabled,
|
||||
variant="success",
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def warning(
|
||||
cls,
|
||||
label: TextType | None = None,
|
||||
disabled: bool = False,
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
) -> Button:
|
||||
"""Utility constructor for creating a warning Button variant.
|
||||
|
||||
@@ -320,22 +316,22 @@ class Button(Static, can_focus=True):
|
||||
"""
|
||||
return Button(
|
||||
label=label,
|
||||
disabled=disabled,
|
||||
variant="warning",
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def error(
|
||||
cls,
|
||||
label: TextType | None = None,
|
||||
disabled: bool = False,
|
||||
*,
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
) -> Button:
|
||||
"""Utility constructor for creating an error Button variant.
|
||||
|
||||
@@ -351,9 +347,9 @@ class Button(Static, can_focus=True):
|
||||
"""
|
||||
return Button(
|
||||
label=label,
|
||||
disabled=disabled,
|
||||
variant="error",
|
||||
name=name,
|
||||
id=id,
|
||||
classes=classes,
|
||||
disabled=disabled,
|
||||
)
|
||||
|
||||
@@ -56,8 +56,9 @@ class Static(Widget, inherit_bindings=False):
|
||||
name: str | None = None,
|
||||
id: str | None = None,
|
||||
classes: str | None = None,
|
||||
disabled: bool = False,
|
||||
) -> None:
|
||||
super().__init__(name=name, id=id, classes=classes)
|
||||
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||
self.expand = expand
|
||||
self.shrink = shrink
|
||||
self.markup = markup
|
||||
|
||||
Reference in New Issue
Block a user