neuos
neuos

Reputation: 63

How to hide the Dock Icon in Jetpack Compose Desktop?

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.

Upvotes: 2

Views: 625

Answers (2)

MiguelMunoz
MiguelMunoz

Reputation: 4962

Here's the most general solution: To hide Your application from the dock, your Info.plist file must have this element:

<key>LSUIElement</key>
<string>true</string>

Note that this is a string property, not boolean, even though it can only be true or false, (or missing, which is the same as false). If you write this as a boolean property, it won't work.

Since I'm doing this in Java using a Maven application bundler plugin, so for me I needed to add this to my plugin configuration:

<LSUIElement>
  true
</LSUIElement>

But that's a language-specific detail. Whatever language you're using to build your application, you just need to make sure the property is set correctly in your Info.plist file.

Upvotes: 0

Guiyanakuang
Guiyanakuang

Reputation: 451

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()
  }
}

Upvotes: 2

Related Questions