Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retired: Add a switch to isolate a widget class from CSS type inheritance #2753

Closed
wants to merge 1 commit into from

Conversation

davep
Copy link
Contributor

@davep davep commented Jun 8, 2023

This PR seeks to satisfy #2749.

It introduces an isolate_css_type switch (name still to be finally decided upon, although I think this carries the correct intent), whose default is False, which when set to True will isolate the new widget type within CSS. While it will inherit all of the mechanics of its ancestors, it will inherit none of their styling and won't be queryable via an ancestor type.

With this PR, this code:

from textual.app        import App, ComposeResult, RenderResult
from textual.containers import Horizontal, Vertical
from textual.widgets    import Static, TextLog

class CustomParent( Static ):

    def __init__( self ) -> None:
        super().__init__()
        self.border_title = self.__class__.__name__

    def render(self) -> RenderResult:
        return str( self._css_type_names )

class CustomChild( CustomParent ):
    pass

class CustomGrandChild( CustomChild, isolate_css_type=True ):
    pass

class CSSTypeExplorerApp( App[ None ] ):

    CSS = """
    Static {
        border: panel green;
    }

    CustomGrandChild {
        border: panel red;
    }
    """

    def compose( self ) -> ComposeResult:
        with Horizontal():
            with Vertical():
                yield CustomParent()
                yield CustomChild()
                yield CustomGrandChild()
            yield TextLog()

    def on_mount( self ) -> None:
        for static in self.query( Static ):
            self.query_one( TextLog ).write( f"{static!r}" )

if __name__ == "__main__":
    CSSTypeExplorerApp().run()

produces this:

Screenshot 2023-06-08 at 11 33 08

(Note Tooltip appearing here, which relates to a motivation behind this quite nicely - #2723)

There are rare occasions where you want to create a widget that has the
workings of another widget, but should not appear to be the other widget
from a CSS standpoint. A good example would be things inheriting from
`Static`, which is common and then also a common gotcha when people style
`Static` and other things behave in ways they didn't expect.

See Textualize#2749.
@davep davep added enhancement New feature or request Task labels Jun 8, 2023
@davep davep self-assigned this Jun 8, 2023
@davep davep linked an issue Jun 8, 2023 that may be closed by this pull request
davep added a commit to davep/textual-sandbox that referenced this pull request Jun 8, 2023
@davep
Copy link
Contributor Author

davep commented Jun 8, 2023

See this comment on the originating PR. I've since noticed that Textual seems to already have a facility to do this anyway. Going to double-check I'm not missing anything obvious here but I think this PR can be closed as it's aiming to duplicate existing functionality that we forgot we had.

@davep davep changed the title WiP: Add a switch to isolate a widget class from CSS type inheritance Retired: Add a switch to isolate a widget class from CSS type inheritance Jun 8, 2023
@davep
Copy link
Contributor Author

davep commented Jun 8, 2023

Confirmed: inherit_css is this anyway and it seems we just forgot we had this. Closing this PR as it's not necessary; and Tooltip can be fixed with this.

@davep davep closed this Jun 8, 2023
@davep davep deleted the css-type-isolation branch June 8, 2023 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow widgets to limit types
1 participant