How to hide the Dock Icon in Jetpack Compose Desktop?

228 views Asked by At

I want to create a compose desktop application that only lives in the tray, but doesn't show an app icon in the Windows Taskbar / macOS Dock.

I started with the tutorial Tray application without Window.

fun main() = application {
    Tray(
        icon = TrayIcon,
        menu = {
            Item(
                "Exit",
                onClick = ::exitApplication
            )
        }
    )
}

object TrayIcon : Painter() {
    override val intrinsicSize = Size(256f, 256f)

    override fun DrawScope.onDraw() {
        drawOval(Color(0xFFFFA500))
    }
}

This successfully shows the tray icon and no window, but it also shows a useless app icon in the Dock. How can I change the code to hide that taskbar icon?

Edit: I now got hold of a windows PC and it doesn't show an icon there. So it just doesn't work on mac. Still I want to hide it. But now I think it might be a bug and not a missing/undocumented feature that it does show in the Dock.

1

There are 1 answers

0
Guiyanakuang On BEST ANSWER

For macos, we need to add the LSUIElement key-value pair to info.plist.

compose.desktop {
    application {
        mainClass = "MainKt"

        nativeDistributions {
            targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
            packageName = "com.XXXXXXX"
            packageVersion = "1.0.0"
            macOS {
                bundleID = "com.XXXXXX.desktop"
                infoPlist {
                    extraKeysRawXml = """
                        <key>LSUIElement</key>
                        <string>true</string>
                    """.trimIndent()
                }
            }
        }
    }
}

Next you gradle packageDmg, you can local an turn to verify the effect of the hidden.

For win, we just need to control whether or not the window is displayed via a state variable.

var showWindow by remember { mutableStateOf{ false } }
Tray(icon = image,
   menu = {
      Item("show or hide", onClick = {showWindow = !showWindow})
   }
)
if (showWinodw) {
  Window(onCloseRequest = ::exitApplication) {
    App()
  }
}