Skip to content

Commit

Permalink
✨ system tray support (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Nov 19, 2023
1 parent 0dff7f4 commit fe5e97a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
9 changes: 9 additions & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ compose.desktop {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "com.clipevery"
packageVersion = "1.0.0"
macOS {
bundleID = "com.clipevery.desktop"
infoPlist {
extraKeysRawXml = """
<key>LSUIElement</key>
<string>true</string>
""".trimIndent()
}
}
}
}
}
34 changes: 33 additions & 1 deletion composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@ import androidx.compose.desktop.ui.tooling.preview.Preview
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.painter.BitmapPainter
import androidx.compose.ui.graphics.toComposeImageBitmap
import androidx.compose.ui.window.Tray
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application
import com.clipevery.ClipeveryApp
import com.clipevery.getClipboard
import org.jetbrains.skia.Image
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.Transferable
import java.io.InputStream
import java.util.function.Consumer
import kotlin.system.exitProcess


fun main() = application {
Expand All @@ -25,11 +31,37 @@ fun main() = application {
}
val clipboard = getClipboard(consumer)
clipboard.start()
Window(onCloseRequest = ::exitApplication) {

val imageBitmap = loadIconFromResources("clipevery_icon.png")


Tray(icon = imageBitmap,
menu = {
Item(
"Exit",
onClick = { exitProcess(1) }
)
}
)

Window(onCloseRequest = ::exitApplication,
title = "Clipevery",
icon = imageBitmap,
undecorated = true,
resizable = false) {
ClipeveryApp(clipboard, copyText)
}
}

fun loadIconFromResources(resourceName: String): BitmapPainter {
val resourceStream: InputStream? = Thread.currentThread().contextClassLoader.getResourceAsStream(resourceName)
resourceStream?.let {
val image = Image.makeFromEncoded(it.readAllBytes())
return BitmapPainter(image.toComposeImageBitmap())
}
throw IllegalArgumentException("Resource not found: $resourceName")
}

@Preview
@Composable
fun AppDesktopPreview() {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit fe5e97a

Please sign in to comment.