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."""
|
"""Update from CSS if has focus state changes."""
|
||||||
self._update_styles()
|
self._update_styles()
|
||||||
|
|
||||||
|
def watch_disabled(self) -> None:
|
||||||
|
# self.can_focus = not self.disabled
|
||||||
|
self._update_styles()
|
||||||
|
|
||||||
def _size_updated(
|
def _size_updated(
|
||||||
self, size: Size, virtual_size: Size, container_size: Size
|
self, size: Size, virtual_size: Size, container_size: Size
|
||||||
) -> None:
|
) -> None:
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ class Button(Static, can_focus=True):
|
|||||||
text-style: bold;
|
text-style: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
Button.-disabled {
|
Button:disabled {
|
||||||
opacity: 0.4;
|
opacity: 0.4;
|
||||||
text-opacity: 0.7;
|
text-opacity: 0.7;
|
||||||
}
|
}
|
||||||
@@ -173,34 +173,30 @@ class Button(Static, can_focus=True):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
label: TextType | None = None,
|
label: TextType | None = None,
|
||||||
disabled: bool = False,
|
|
||||||
variant: ButtonVariant = "default",
|
variant: ButtonVariant = "default",
|
||||||
*,
|
*,
|
||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
id: str | None = None,
|
id: str | None = None,
|
||||||
classes: str | None = None,
|
classes: str | None = None,
|
||||||
|
disabled: bool = False,
|
||||||
):
|
):
|
||||||
"""Create a Button widget.
|
"""Create a Button widget.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
label: The text that appears within the button.
|
label: The text that appears within the button.
|
||||||
disabled: Whether the button is disabled or not.
|
|
||||||
variant: The variant of the button.
|
variant: The variant of the button.
|
||||||
name: The name of the button.
|
name: The name of the button.
|
||||||
id: The ID of the button in the DOM.
|
id: The ID of the button in the DOM.
|
||||||
classes: The CSS classes of the button.
|
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:
|
if label is None:
|
||||||
label = self.css_identifier_styled
|
label = self.css_identifier_styled
|
||||||
|
|
||||||
self.label = self.validate_label(label)
|
self.label = self.validate_label(label)
|
||||||
|
|
||||||
self.disabled = disabled
|
|
||||||
if disabled:
|
|
||||||
self.add_class("-disabled")
|
|
||||||
|
|
||||||
self.variant = self.validate_variant(variant)
|
self.variant = self.validate_variant(variant)
|
||||||
|
|
||||||
def __rich_repr__(self) -> rich.repr.Result:
|
def __rich_repr__(self) -> rich.repr.Result:
|
||||||
@@ -224,9 +220,9 @@ class Button(Static, can_focus=True):
|
|||||||
self.remove_class(f"-{old_variant}")
|
self.remove_class(f"-{old_variant}")
|
||||||
self.add_class(f"-{variant}")
|
self.add_class(f"-{variant}")
|
||||||
|
|
||||||
def watch_disabled(self, disabled: bool) -> None:
|
# def watch_disabled(self, disabled: bool) -> None:
|
||||||
self.set_class(disabled, "-disabled")
|
# self.set_class(disabled, "-disabled")
|
||||||
self.can_focus = not disabled
|
# self.can_focus = not disabled
|
||||||
|
|
||||||
def validate_label(self, label: RenderableType) -> RenderableType:
|
def validate_label(self, label: RenderableType) -> RenderableType:
|
||||||
"""Parse markup for self.label"""
|
"""Parse markup for self.label"""
|
||||||
@@ -269,11 +265,11 @@ class Button(Static, can_focus=True):
|
|||||||
def success(
|
def success(
|
||||||
cls,
|
cls,
|
||||||
label: TextType | None = None,
|
label: TextType | None = None,
|
||||||
disabled: bool = False,
|
|
||||||
*,
|
*,
|
||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
id: str | None = None,
|
id: str | None = None,
|
||||||
classes: str | None = None,
|
classes: str | None = None,
|
||||||
|
disabled: bool = False,
|
||||||
) -> Button:
|
) -> Button:
|
||||||
"""Utility constructor for creating a success Button variant.
|
"""Utility constructor for creating a success Button variant.
|
||||||
|
|
||||||
@@ -289,22 +285,22 @@ class Button(Static, can_focus=True):
|
|||||||
"""
|
"""
|
||||||
return Button(
|
return Button(
|
||||||
label=label,
|
label=label,
|
||||||
disabled=disabled,
|
|
||||||
variant="success",
|
variant="success",
|
||||||
name=name,
|
name=name,
|
||||||
id=id,
|
id=id,
|
||||||
classes=classes,
|
classes=classes,
|
||||||
|
disabled=disabled,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def warning(
|
def warning(
|
||||||
cls,
|
cls,
|
||||||
label: TextType | None = None,
|
label: TextType | None = None,
|
||||||
disabled: bool = False,
|
|
||||||
*,
|
*,
|
||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
id: str | None = None,
|
id: str | None = None,
|
||||||
classes: str | None = None,
|
classes: str | None = None,
|
||||||
|
disabled: bool = False,
|
||||||
) -> Button:
|
) -> Button:
|
||||||
"""Utility constructor for creating a warning Button variant.
|
"""Utility constructor for creating a warning Button variant.
|
||||||
|
|
||||||
@@ -320,22 +316,22 @@ class Button(Static, can_focus=True):
|
|||||||
"""
|
"""
|
||||||
return Button(
|
return Button(
|
||||||
label=label,
|
label=label,
|
||||||
disabled=disabled,
|
|
||||||
variant="warning",
|
variant="warning",
|
||||||
name=name,
|
name=name,
|
||||||
id=id,
|
id=id,
|
||||||
classes=classes,
|
classes=classes,
|
||||||
|
disabled=disabled,
|
||||||
)
|
)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def error(
|
def error(
|
||||||
cls,
|
cls,
|
||||||
label: TextType | None = None,
|
label: TextType | None = None,
|
||||||
disabled: bool = False,
|
|
||||||
*,
|
*,
|
||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
id: str | None = None,
|
id: str | None = None,
|
||||||
classes: str | None = None,
|
classes: str | None = None,
|
||||||
|
disabled: bool = False,
|
||||||
) -> Button:
|
) -> Button:
|
||||||
"""Utility constructor for creating an error Button variant.
|
"""Utility constructor for creating an error Button variant.
|
||||||
|
|
||||||
@@ -351,9 +347,9 @@ class Button(Static, can_focus=True):
|
|||||||
"""
|
"""
|
||||||
return Button(
|
return Button(
|
||||||
label=label,
|
label=label,
|
||||||
disabled=disabled,
|
|
||||||
variant="error",
|
variant="error",
|
||||||
name=name,
|
name=name,
|
||||||
id=id,
|
id=id,
|
||||||
classes=classes,
|
classes=classes,
|
||||||
|
disabled=disabled,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -56,8 +56,9 @@ class Static(Widget, inherit_bindings=False):
|
|||||||
name: str | None = None,
|
name: str | None = None,
|
||||||
id: str | None = None,
|
id: str | None = None,
|
||||||
classes: str | None = None,
|
classes: str | None = None,
|
||||||
|
disabled: bool = False,
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__(name=name, id=id, classes=classes)
|
super().__init__(name=name, id=id, classes=classes, disabled=disabled)
|
||||||
self.expand = expand
|
self.expand = expand
|
||||||
self.shrink = shrink
|
self.shrink = shrink
|
||||||
self.markup = markup
|
self.markup = markup
|
||||||
|
|||||||
Reference in New Issue
Block a user