mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-07-24 17:20:51 +02:00
feat(clipboard): add plugin (#323)
This commit is contained in:
committed by
GitHub
parent
4539c03f95
commit
8cd7d3501b
@@ -0,0 +1,3 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
</manifest>
|
||||
@@ -0,0 +1,70 @@
|
||||
package app.tauri.clipboard
|
||||
|
||||
import android.R.attr.value
|
||||
import android.app.Activity
|
||||
import android.content.ClipData
|
||||
import android.content.ClipDescription
|
||||
import android.content.ClipboardManager
|
||||
import android.content.Context
|
||||
import app.tauri.annotation.Command
|
||||
import app.tauri.annotation.TauriPlugin
|
||||
import app.tauri.plugin.Invoke
|
||||
import app.tauri.plugin.JSObject
|
||||
import app.tauri.plugin.Plugin
|
||||
|
||||
|
||||
@TauriPlugin
|
||||
class ClipboardPlugin(private val activity: Activity) : Plugin(activity) {
|
||||
private val manager: ClipboardManager =
|
||||
activity.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
|
||||
|
||||
@Command
|
||||
@Suppress("MoveVariableDeclarationIntoWhen")
|
||||
fun write(invoke: Invoke) {
|
||||
val options = invoke.getObject("options")
|
||||
if (options == null) {
|
||||
invoke.reject("Missing `options` input")
|
||||
return
|
||||
}
|
||||
val kind = invoke.getString("kind", "")
|
||||
|
||||
val clipData = when (kind) {
|
||||
"PlainText" -> {
|
||||
val label = options.getString("label", "")
|
||||
val text = options.getString("text", "")
|
||||
ClipData.newPlainText(label, text)
|
||||
}
|
||||
|
||||
else -> {
|
||||
invoke.reject("Unknown kind $kind")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
manager.setPrimaryClip(clipData)
|
||||
|
||||
invoke.resolve()
|
||||
}
|
||||
|
||||
@Command
|
||||
fun read(invoke: Invoke) {
|
||||
val (kind, options) = if (manager.hasPrimaryClip()) {
|
||||
if (manager.primaryClipDescription?.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN) == true) {
|
||||
val item: ClipData.Item = manager.primaryClip!!.getItemAt(0)
|
||||
Pair("PlainText", item.text)
|
||||
} else {
|
||||
// TODO
|
||||
invoke.reject("Clipboard content reader not implemented")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
invoke.reject("Clipboard is empty")
|
||||
return
|
||||
}
|
||||
|
||||
val response = JSObject()
|
||||
response.put("kind", kind)
|
||||
response.put("options", options)
|
||||
invoke.resolve(response)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user