scoop:scoop.window:0.1.0
src/preferences.scoop
Selected production source · src/preferences.scoop
1
package scoop.window3
import scoop.time.Duration5
/** System facts; None is unknown, not a request to disable a feature. */6
public 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? = None17
)19
/** The latest owner-published snapshot; reading does not query the OS. */20
public val ActiveApplication.interactionPreferences: InteractionPreferences get() {21
owner.checkPreferencesActive()22
return owner.interactionPreferences23
}25
/** Uses this window's application without acquiring a native window lease. */26
public val Window.interactionPreferences: InteractionPreferences get() {27
owner.application.checkPreferencesActive()28
return owner.application.interactionPreferences29
}31
/** Observes actual changes; registration does not invoke the callback. */32
public fun ActiveApplication.observeInteractionPreferences(33
callback: (InteractionPreferences) -> Unit34
): Closeable = owner.observePreferences(callback)36
/** The registration belongs to the application; close it with the consumer. */37
public fun Window.observeInteractionPreferences(38
callback: (InteractionPreferences) -> Unit39
): Closeable = owner.application.observePreferences(callback)41
internal fun ApplicationOwner.checkPreferencesActive() {42
checkThread()43
if (native == None || exiting)44
throw WindowException("interaction preferences", WindowErrorKind.InvalidState)45
}47
internal fun ApplicationOwner.observePreferences(48
callback: (InteractionPreferences) -> Unit49
): Closeable {50
checkPreferencesActive()51
val observer = InteractionPreferencesObserver(this, callback)52
preferencesObservers.add(observer)53
return observer54
}56
internal class InteractionPreferencesObserver(57
private val owner: ApplicationOwner,58
callback: (InteractionPreferences) -> Unit59
): Closeable {60
private var handler: ((InteractionPreferences) -> Unit)? = Some(callback)62
fun deliver(value: InteractionPreferences) {63
handler?.invoke(value)64
}66
fun retire() {67
handler = None68
}70
public override fun close() {71
owner.checkThread()72
when (handler) {73
case None -> return74
case Some(_) -> Unit75
}76
retire()77
for (index in 0L ..< owner.preferencesObservers.size)78
if (owner.preferencesObservers[index] === this) {79
owner.preferencesObservers.removeAt(index)80
return81
}82
}83
}85
internal fun ApplicationOwner.deliverPreferences(value: InteractionPreferences) {86
deliverBusiness {87
if (interactionPreferences != value) {88
interactionPreferences = value89
for (observer in preferencesObservers.toArray()) {90
if (exiting)91
break92
observer.deliver(value)93
}94
if (!exiting)95
handler!!.onEvent(active, ApplicationEvent.InteractionPreferencesChanged(value))96
}97
}98
}100
internal fun ApplicationOwner.retirePreferences() {101
for (observer in preferencesObservers)102
observer.retire()103
preferencesObservers.clear()104
}