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

UI doesn't react to visibility flag change #1355

Closed
polidore opened this issue Dec 13, 2022 · 6 comments · Fixed by #1423
Closed

UI doesn't react to visibility flag change #1355

polidore opened this issue Dec 13, 2022 · 6 comments · Fixed by #1423
Assignees
Labels
bug Something isn't working

Comments

@polidore
Copy link

I have an app that has a dialogue box that is invisible unless someone clicks a button. When they do, I want the dialogue to appear instantly so they can confirm their action. The dialogue is in a "higher" layer than the rest of the app, and it starts invisible based on CSS.

When they click a button, I change it to visible, but it doesn't appear until something else makes the window redraw. For eg, if i resize the window, it appears instantly.

starting CSS:

#confirm_panel {
    width: 30%;
    height: 16%;
    border: lime double;
    overflow: hidden;
    layer: above;
    offset: 50 20;
    visibility: hidden;
}

Change to visible. I have tried it via styles and via the new visible flag. Both have the same behavior:

        self.query_one('#confirm_panel').styles.visibility = 'visible'

Using 0.6.0

@polidore
Copy link
Author

OK, the solution is to call Widget.refresh(layout=True). It seems that visibility should be reactive? I'm setting it on a Container here.

@davep
Copy link
Contributor

davep commented Dec 13, 2022

At first glance this looks like it may be a bug lower down. I can't remember off the top of my head if setting styles via code like that are fully reactive, but setting them via class changes in a stylesheet should work. With that in mind:

from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, Label, Button
from textual.containers import Vertical

class ShowHideApp( App[ None ] ):

    CSS = """
    Horizontal {
        height: 1fr;
        border: solid red;
    }
    Button {
       width: 100%;
       height: 1fr;
    }
    Label {
        width: 100%;
        height: 9fr;
        content-align: center middle;
        background: #888800;
        visibility: visible;
    }

    .hidden {
        visibility: hidden;
    }
    """

    def compose(self) -> ComposeResult:
        yield Header()
        yield Vertical(
            Vertical(
                Button( "Toggle show/hide via code", id="via-code" ),
                Label( "I can be toggled via code; press the button", id="via-code-label" )
            ),
            Vertical(
                Button( "Toggle show/hide via classes", id="via-classes" ),
                Label( "I can be toggled via classes; press the button", id="via-classes-label" )
            )
        )
        yield Footer()

    def on_button_pressed( self, event: Button.Pressed ) -> None:
        if event.button.id == "via-code":
            label = self.query_one( "#via-code-label" )
            label.styles.visibility = "visible" if label.styles.visibility == "hidden" else "hidden"
            self.refresh(layout=True)
        elif event.button.id == "via-classes":
            self.query_one( "#via-classes-label" ).toggle_class( "hidden" )

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

Note that the via-classes version doesn't work. Unless, that is, I move the refresh to the end of on_button_pressed rather than just in the via-code version.

I'll label this as a bug and, if nobody gets to it sooner, I'll have a proper look in the morning.

@davep davep added the bug Something isn't working label Dec 13, 2022
davep added a commit to davep/textual-sandbox that referenced this issue Dec 13, 2022
@davep davep self-assigned this Dec 21, 2022
@davep davep closed this as completed in 765dd13 Dec 21, 2022
@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

1 similar comment
@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

@davep
Copy link
Contributor

davep commented Dec 21, 2022

@polidore Just to let you know, #1423 fixes this. Thanks again for raising it!

@polidore
Copy link
Author

polidore commented Dec 21, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants