Scoop API

scoop:scoop.window:0.1.0

src/preferences.scoop

Selected production source · src/preferences.scoop

1package scoop.window
3import scoop.time.Duration
5/** System facts; None is unknown, not a request to disable a feature. */
6public struct InteractionPreferences(
7 val autoHideScrollbars: Boolean? = None,
8 val reduceMotion: Boolean? = None,
9 val highContrast: Boolean? = None,
10 val reduceTransparency: Boolean? = None,
11 val textScaleFactor: Double? = None,
12 /** One visible/hidden transition interval; zero keeps the caret visible. */
13 val caretBlinkInterval: Duration? = None,
14 val caretWidthPixels: Int? = None,
15 val caretAspectRatio: Double? = None,
16 val doubleClickInterval: Duration? = None
19/** The latest owner-published snapshot; reading does not query the OS. */
20public val ActiveApplication.interactionPreferences: InteractionPreferences get() {
21 owner.checkPreferencesActive()
22 return owner.interactionPreferences
25/** Uses this window's application without acquiring a native window lease. */
26public val Window.interactionPreferences: InteractionPreferences get() {
27 owner.application.checkPreferencesActive()
28 return owner.application.interactionPreferences
31/** Observes actual changes; registration does not invoke the callback. */
32public fun ActiveApplication.observeInteractionPreferences(
33 callback: (InteractionPreferences) -> Unit
34): Closeable = owner.observePreferences(callback)
36/** The registration belongs to the application; close it with the consumer. */
37public fun Window.observeInteractionPreferences(
38 callback: (InteractionPreferences) -> Unit
39): Closeable = owner.application.observePreferences(callback)
41internal fun ApplicationOwner.checkPreferencesActive() {
42 checkThread()
43 if (native == None || exiting)
44 throw WindowException("interaction preferences", WindowErrorKind.InvalidState)
47internal fun ApplicationOwner.observePreferences(
48 callback: (InteractionPreferences) -> Unit
49): Closeable {
50 checkPreferencesActive()
51 val observer = InteractionPreferencesObserver(this, callback)
52 preferencesObservers.add(observer)
53 return observer
56internal class InteractionPreferencesObserver(
57 private val owner: ApplicationOwner,
58 callback: (InteractionPreferences) -> Unit
59): Closeable {
60 private var handler: ((InteractionPreferences) -> Unit)? = Some(callback)
62 fun deliver(value: InteractionPreferences) {
63 handler?.invoke(value)
64 }
66 fun retire() {
67 handler = None
68 }
70 public override fun close() {
71 owner.checkThread()
72 when (handler) {
73 case None -> return
74 case Some(_) -> Unit
75 }
76 retire()
77 for (index in 0L ..< owner.preferencesObservers.size)
78 if (owner.preferencesObservers[index] === this) {
79 owner.preferencesObservers.removeAt(index)
80 return
81 }
82 }
85internal fun ApplicationOwner.deliverPreferences(value: InteractionPreferences) {
86 deliverBusiness {
87 if (interactionPreferences != value) {
88 interactionPreferences = value
89 for (observer in preferencesObservers.toArray()) {
90 if (exiting)
91 break
92 observer.deliver(value)
93 }
94 if (!exiting)
95 handler!!.onEvent(active, ApplicationEvent.InteractionPreferencesChanged(value))
96 }
97 }
100internal fun ApplicationOwner.retirePreferences() {
101 for (observer in preferencesObservers)
102 observer.retire()
103 preferencesObservers.clear()