All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmMain.ru.casperix.multiplatform.clipboard.JvmClipboard.kt Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package ru.casperix.multiplatform.clipboard

import ru.casperix.renderer.pixel_map.PixelMap
import ru.casperix.signals.concrete.*
import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection


actual val clipboard: ClipboardApi = JvmClipboard()

class JvmClipboard : ClipboardApi {
    private val awtClipboard = Toolkit.getDefaultToolkit().systemClipboard

    override fun pushString(value: String): EitherFuture {
        val selection = StringSelection(value)
        awtClipboard.setContents(selection, selection)
        return EitherSignal().apply {
            accept(Unit)
        }
    }

    override fun popString(): EitherFuture {
        return EitherSignal().apply {
            val contents = awtClipboard.getContents(null)
            if (contents == null) {
                reject(Exception("No content in clipboard"))
            } else if (!contents.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                reject(Exception("No string in clipboard"))
            } else {
                val data = contents.getTransferData(DataFlavor.stringFlavor) as String
                accept(data)
            }
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy