Scoop API

scoop:scoop.window:0.1.0

src/input/primary_selection.scoop

Selected production source · src/input/primary_selection.scoop

1package scoop.window
3/** A platform PRIMARY selection, separate from the explicit copy/paste clipboard. */
4public class PrimarySelection internal constructor(private val owner: ApplicationOwner) {
5 /** False when the backend only supplies a process-local placeholder. */
6 public val isAvailable: Boolean get() {
7 checkActive("query primary selection")
8 return platformPrimaryFlags(owner.native!!).and(1) != 0
9 }
10 /** Resolves the native middle-button preference, or the platform's enabled default. */
11 public val defaultPasteEnabled: Boolean get() {
12 checkActive("query primary paste preference")
13 return platformPrimaryFlags(owner.native!!).and(2) != 0
14 }
15 public fun readText(): String? {
16 checkActive("read primary selection")
17 return when (platformReadPrimary(owner.native!!)) {
18 case Some(val text) -> Some(clipboardText(text, false, true))
19 case None -> None
20 }
21 }
22 /** Claims one owned plain-text value; unavailable platforms return None. */
23 public fun claimText(text: String): PrimarySelectionClaim? {
24 checkActive("claim primary selection")
25 val value = clipboardText(text, true, true)
26 val generation = platformClaimPrimary(owner.native!!, value)
27 return if (generation == 0L)
28 None
29 else
30 Some(PrimarySelectionClaim(owner, generation))
31 }
32 internal fun checkActive(operation: String) {
33 owner.checkThread()
34 if (owner.native == None || owner.exiting)
35 throw WindowException(operation, WindowErrorKind.InvalidState)
36 }
39/** Closing a claim never clears the contents of a later or external owner. */
40public class PrimarySelectionClaim internal constructor(
41 private val owner: ApplicationOwner,
42 private val generation: Long
43): Closeable {
44 private var closed = false
45 public val isCurrent: Boolean get() {
46 owner.checkThread()
47 if (closed || owner.exiting)
48 return false
49 return when (owner.native) {
50 case Some(val native) -> platformPrimaryCurrent(native, generation)
51 case None -> false
52 }
53 }
54 public override fun close() {
55 owner.checkThread()
56 if (closed)
57 return
58 closed = true
59 when (owner.native) {
60 case Some(val native) -> platformReleasePrimary(native, generation)
61 case None -> {}
62 }
63 }
66public val ActiveApplication.primarySelection: PrimarySelection get() {
67 val value = PrimarySelection(owner)
68 value.checkActive("get primary selection")
69 return value
72public val Window.primarySelection: PrimarySelection get() {
73 val value = PrimarySelection(owner.application)
74 value.checkActive("get primary selection")
75 return value