Hello, World! Hiding a `ComposePanel` (with `isVis...
# compose-desktop
b
Hello, World! Hiding a
ComposePanel
(with
isVisible = false
) doesn't seem to work - the UI stays visible (but is no longer interactive). This looks like a bug but just wanted to check here first. Anyone has seen this before? Reproducer in 🧵
Copy code
fun main() = SwingUtilities.invokeLater {
  val composePanel = ComposePanel().apply {
    setContent {
      Button(
        onClick = {
          isVisible = false // <- this should hide the ComposePanel but doesn't
        }
      ) {
        Text("Hello World")
      }
    }
  }

  JFrame().apply {
    setSize(800, 600)
    contentPane.add(composePanel)
    isVisible = true
  }
}
a
It’s kind of on the boundary between Swing and Compose, so I’m not sure it’d be classed as a bug, although it does make sense we’d check whether the
ComposePanel
is visible before rendering it. Judging by the animation, though, maybe we aren’t rendering it when invisible, but also not clearing the last rendered frame. Any way, you can use something like this as a workaround:
Copy code
fun main() = SwingUtilities.invokeLater {
    val composePanel = ComposePanel().apply {
        setContent {
            var visible by remember{ mutableStateOf(true) }
            if (visible){
                Button(
                    onClick = {
                        visible = false
                    }
                ) {
                    Text("Hello World")
                }
            }
        }
    }

    JFrame().apply {
        setSize(800, 600)
        contentPane.add(composePanel)
        isVisible = true
    }
}
Does sound like a bug actually. If you wanted to make the panel invisible from Swing code, you wouldn’t be able to.
Not easily anyway
b
Thanks a lot! Yeah, I was gonna say, in my case I'm actually trying to produce the settings UI for an IntelliJ plugin (I know this is probably unorthodox), and so I don't really have access to the code showing / hiding the panel. It's called by the host.
a
As a workaround you can override setVisible of the panel and set a compose state variable accordingly
b
Ah cool idea 💡 ! I'll try that.
well that doesn't really work, because setVisible is actually never called. The way the plugin SDK works is that you implement an interface that returns a JPanel (I add the ContentPanel inside this JPanel), and then it will show it/hide it - but I think it does that by changing the visibility of one of its parents. I'm guessing a LayoutManager is managing the visibility of a parent several layers above my JPanel and I'm no sure how to get called when that happens.
also I guess I'd need an event sent before the component is hidden, so I can update the compose UI first
a
Thanks for opening the ticket. Maybe overriding removeNotify instead will help.
b
no unfortunately it appears this is not called when switching to a different tab, but only when closing the whole window. I guess switching to a different tab doesn't remove the component but only hides it.
a
b
Alas! It appears the event is received after the panel is already hidden, so my hiding the compose UI is too late and not reflected.