scoop:scoop.ui.component:0.1.0
src/toast/semantics.scoop
Selected production source · src/toast/semantics.scoop
1
package scoop.ui.component3
import scoop.graphics.Rect4
import scoop.surface.FrameSubmission5
import scoop.ui.*6
import scoop.window.accessibility.*8
/** Notification content and dismissal share the mounted toast and its ordinary close path. */9
internal class ToastSemantics(10
private val scope: ComponentScope,11
private val read: () -> ToastItemInput12
) {13
private val handle = createComponentSemantics(scope, SemanticRole.Group)14
private var announced = false15
private var message: String? = None16
init {17
scope.routeSemanticsChildren(handle)18
}19
fun update() {20
val target = handle ?: return21
val input = read()22
val data = input.entry.data.value23
val title = semanticCaption(Some(data.title))24
val description = semanticCaption(data.description)25
val caption =26
when ((title, description)) {27
case(Some(val first), Some(val second)) -> Some(first + "\n" + second)28
case(Some(_), None) -> title29
case(None, _) -> description30
}31
message =32
when (val overrideName = data.semantics.name) {33
case Some(_) -> overrideName34
case None -> caption35
}36
target.mergeLabels = true37
target.bindReference(data.semantics.reference)38
val dismissible = data.dismiss.closeButton || data.dismiss.escape || data.dismiss.swipe39
target.update(40
data.semantics.properties(enabled = input.props.enabled).copy {41
name = this @ToastSemantics.message42
busy = data.semantics.busy || data.kind == ToastKind.Loading43
hidden =44
data.semantics.hidden ||45
input.entry.closing.value !=46
None ||47
!(input.front || input.expanded)48
},49
SemanticActions(50
dismiss =51
if (dismissible)52
Some { dismiss() }53
else54
None55
),56
data.semantics.relations57
)58
}59
private fun dismiss(): SemanticActionResult {60
val input = read()61
if (!scope.isMounted || input.entry.closing.value != None)62
return SemanticActionResult.Unavailable63
if (!input.props.enabled)64
return SemanticActionResult.Rejected65
return if (input.entry.model.close(input.entry.id, ToastCloseReason.Explicit))66
SemanticActionResult.Completed67
else68
SemanticActionResult.Rejected69
}70
fun scene(context: SceneContext) {71
val target = handle ?: return72
context.semantics(target, Rect(0.0, 0.0, context.size.width, context.size.height))73
if (announced)74
return75
val input = read()76
val data = input.entry.data.value77
val text = message ?: return78
context.onFrameComplete {79
frame -> if (80
frame.submission ==81
FrameSubmission.Accepted &&82
!announced &&83
!target.isClosed &&84
input.entry.data.value ===85
data &&86
input.entry.closing.value ==87
None &&88
(input.front || input.expanded) &&89
!data.semantics.hidden &&90
!data.semantics.ignored &&91
scope.input.isInputAllowed &&92
(target.geometry?.visibleBounds ?: None) !=93
None94
) {95
target.announce(text, data.semantics.live)96
announced = true97
}98
}99
}100
}