compose-multiplatform icon indicating copy to clipboard operation
compose-multiplatform copied to clipboard

Cannot set Color.Transparent background on SwingPanel

Open felixdivo opened this issue 3 years ago • 5 comments

Using org.jetbrains.compose @ 1.0.0-alpha4-build310.

image

Minimal example to reproduce:

import androidx.compose.desktop.DesktopMaterialTheme
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.SwingPanel
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import androidx.compose.ui.window.rememberWindowState
import com.arkivanov.decompose.DefaultComponentContext
import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.decompose.extensions.compose.jetbrains.lifecycle.LifecycleController
import com.arkivanov.essenty.lifecycle.LifecycleRegistry
import javax.swing.JLabel

@OptIn(ExperimentalComposeUiApi::class, ExperimentalDecomposeApi::class)
fun main() {
    val lifecycle = LifecycleRegistry()

    application {
        val windowState = rememberWindowState()

        LifecycleController(lifecycle, windowState)

        Window(
            onCloseRequest = ::exitApplication,
            state = windowState,
        ) {

            Surface(modifier = Modifier.fillMaxSize()) {
                DesktopMaterialTheme {
                    actualContent()
                }
            }
        }
    }
}


@Composable
fun actualContent() {

    Box(modifier = Modifier.fillMaxSize().background(Color.Red)) {

        SwingPanel(
            modifier = Modifier.width(100.dp).height(100.dp).background(Color.Transparent),
            factory = {
                JLabel("Hey")
            },
        )
    }
}

felixdivo avatar Aug 16 '21 11:08 felixdivo

The constructor of JPanel calls setUIProperty("opaque", Boolean.TRUE). Maybe it would be sufficient to set it to non-opaque.

felixdivo avatar Sep 27 '21 09:09 felixdivo

Yeah we likely need to set the JPanel's opaque property to FALSE if needed. Another case may be when Color.Unspecified is passed in, as setting it as an AWT colour will not work either.

rock3r avatar May 24 '22 14:05 rock3r

According to this it already works on macOS. We didn't intentionally implemented it for macOS though, but overall it is useful feature, and should be supported someday in Compose for all OS'es.

igordmn avatar Nov 10 '22 14:11 igordmn

Any updates on this? It can be helpful to use SwingPanel as a placeholder for drawing which will temporarily fix the current latency issues with Compose Desktop.

MohamedRejeb avatar May 07 '24 13:05 MohamedRejeb

@MohamedRejeb we have a few similar issues regarding "transparent interop" for different platforms - but it's likely not to be implemented due to various kinda fundamental reasons.

Regarding this issue on desktop/swing - it might work only in the case of rendering compose to swing graphics instead of on GPU surface directly. We have such mode under compose.swing.render.on.graphics flag, but it's definitely not the thing to solve performance/latency issues.

MatkovIvan avatar May 07 '24 13:05 MatkovIvan

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

okushnikov avatar Jul 14 '24 15:07 okushnikov