scoop:scoop.ui.component:0.1.0
src/conversation/surface.scoop
Selected production source · src/conversation/surface.scoop
1
package scoop.ui.component3
import scoop.graphics.*4
import scoop.graphics.text.*5
import scoop.ui.*6
import scoop.ui.style.*7
import scoop.window.accessibility.*9
public enum MessageAlignment {10
Start, End11
}13
public open class ConversationActionBuilder internal constructor() {14
public var onClick: (() -> Unit)? = None15
public var enabled: Boolean = true16
public var focus: FocusOptions = FocusOptions()17
public var focusOutline: OutlineStyle? = None18
public var fonts: FontCollection? = None19
public var theme: ThemeSource? = None20
public var style: StyleOverride = styleOverride()21
public var key: ViewKey = ViewKey.Position22
public var semantics: ComponentSemantics = ComponentSemantics()23
public fun style(configure: StyleOverrideBuilder.() -> Unit) {24
style = style.copy(configure)25
}26
public fun focus(configure: FocusOptionsBuilder.() -> Unit) {27
focus = focus.copy(configure)28
}29
}31
internal class ConversationSurfaceProps(32
val content: ButtonContent,33
val theme: Theme,34
val appearance: TextStyle,35
val style: ButtonStyle,36
val onClick: (() -> Unit)?,37
val enabled: Boolean,38
val focus: FocusOptions,39
val fonts: FontCollection? = None,40
val semantics: ComponentSemantics = ComponentSemantics(),41
val semanticRole: SemanticRole? = None,42
val semanticState: SemanticProperties? = None,43
val semanticActions: SemanticActions? = None44
)46
/** Keeps one input and content identity when an optional action changes. */47
internal class ConversationSurface(48
private val props: Props<ConversationSurfaceProps>,49
scope: ComponentScope50
): Component(scope) {51
private val click: () -> Unit =52
{53
when (props.value.onClick) {54
case Some(val action) -> action();55
case None -> {}56
}57
}58
private val control = PressControl(scope, PressOptions(false), props.value.focus, click)59
private var semantics: SemanticsHandle? = None60
private fun prepareSemantics(): SemanticsHandle? {61
val input = props.value62
val role =63
input.semanticRole ?:64
if (panelPresent(input.onClick))65
SemanticRole.Button66
else67
SemanticRole.Group68
when (semantics) {69
case Some(val old) -> if (old.isClosed || old.role != role) {70
if (!old.isClosed)71
old.close()72
semantics = None73
}74
case None -> {}75
}76
if (!panelPresent(semantics))77
semantics = createComponentSemantics(scope, role)78
when (semantics) {79
case Some(val target) -> {80
target.mergeLabels = true81
target.bindFocus(82
if (panelPresent(input.onClick))83
Some(control.focus)84
else85
None86
)87
}88
case None -> {}89
}90
scope.routeSemanticsChildren(semantics)91
return semantics92
}93
public override fun render(): View {94
val input = props.value95
val visual =96
SurfaceVisual(SurfaceKind.Plain, control.press.interaction.asVisual(input.enabled))97
val shape = ActionShape.Button(ButtonSize.Default)98
val content =99
when (input.content) {100
case Label(val value) -> ButtonContent.Label(101
LabelProps(102
value.text,103
when (value.fonts) {104
case Some(_) -> value.fonts;105
case None -> input.fonts106
},107
value.settings,108
value.inlines,109
value.theme,110
value.baseStyle,111
value.underline,112
value.semantics113
)114
)115
else -> input.content116
}117
val body =118
element(119
ActionInkProps(120
input.theme,121
shape,122
ButtonVariant.Ghost,123
input.style,124
visual,125
ActionBehavior.Press(control, click, input.focus),126
actionable = panelPresent(input.onClick),127
semanticOptions = input.semantics,128
semanticState =129
Some(130
input.semanticState ?:131
input.semantics.properties(enabled = input.enabled)132
),133
semanticActions = input.semanticActions,134
prepareSemantics = Some { prepareSemantics() }135
),136
{ current, mount -> ActionInk(current, mount) },137
[138
actionContent(139
content,140
ActionContentStyle(141
input.appearance,142
16.0 * input.appearance.fontSize / 14.0,143
8.0 * input.appearance.fontSize / 14.0,144
visual.interaction,145
false146
)147
)148
]149
)150
val ring =151
if (!panelPresent(input.onClick))152
0.0153
else154
input.theme.surfaceMetrics(155
visual,156
actionOptions(157
input.theme,158
shape,159
ButtonVariant.Ghost,160
input.style,161
visual.interaction162
),163
scope.layoutDirection164
).focusOutset165
return panelFrame(body, Insets.all(ring))166
}167
}169
internal fun conversationSurface(input: ConversationSurfaceProps): View =170
component(input, { current, mount -> ConversationSurface(current, mount) })172
internal fun conversationText(text: String): ButtonContent =173
ButtonContent.Label(LabelProps(LabelText.Plain(text, None)))175
internal fun conversationText(text: StyledText): ButtonContent =176
ButtonContent.Label(LabelProps(LabelText.Styled(text)))178
internal fun conversationCaption(179
content: ButtonContent,180
appearance: TextStyle,181
fonts: FontCollection?,182
layoutDirection: LayoutDirection,183
lines: Long? = None184
): View {185
val resolved =186
when (content) {187
case Label(val value) -> ButtonContent.Label(188
LabelProps(189
value.text,190
when (value.fonts) {191
case Some(_) -> value.fonts;192
case None -> fonts193
},194
Some(195
value.settings ?:196
paragraphStyle {197
this.maxLines = lines;198
this.direction = layoutDirection.textDirection()199
}200
),201
value.inlines,202
value.theme,203
value.baseStyle,204
value.underline,205
value.semantics206
)207
)208
else -> content209
}210
return actionContent(211
resolved,212
ActionContentStyle(213
appearance,214
16.0 * appearance.fontSize / 14.0,215
8.0 * appearance.fontSize / 14.0,216
InteractionVisual(),217
false218
)219
)220
}222
internal fun conversationStyle(223
background: Color,224
foreground: Color,225
padding: Insets,226
radius: Double,227
border: Color = Color.Transparent,228
hover: Color = background,229
outline: OutlineStyle? = None,230
local: StyleOverride = styleOverride()231
): ButtonStyle =232
buttonStyle {233
this.kind = SurfaceKind.Plain234
this.text { this.color = Some(foreground) }235
this.surface {236
this.minimumSize = Some(Size(0.0, 0.0))237
this.suggestedSize = Some(Size(0.0, 0.0))238
this.padding = Some(padding)239
this.focus = outline240
this.style =241
styleOverride {242
this.background(Some(Paint(background)))243
this.border(Some(Border.all(1.0, border)))244
this.corners(Corners.all(radius))245
this.boxShadows([])246
}.then(local)247
}248
this.states {249
this.hovered { this.background(Some(Paint(hover))) }250
this.pressed { this.background(Some(Paint(mixThemeColor(hover, foreground, 0.08)))) }251
}252
}254
/** A bounded natural measurement, reused by conversation slots and their overlays. */255
internal fun conversationMeasure(256
context: MeasureContext,257
index: Long,258
width: Double?,259
height: Double? = None260
): LayoutMetrics {261
val natural = context.measureChild(index, Constraints(maxHeight = height))262
return if (panelPresent(width) && natural.size.width > width!!)263
context.measureChild(index, Constraints(maxWidth = width, maxHeight = height))264
else265
natural266
}268
internal fun conversationOffset(alignment: MessageAlignment, width: Double, child: Double): Double =269
if (alignment == MessageAlignment.End)270
lowerBound(0.0, width - child)271
else272
0.0274
public fun bubbleGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)276
public fun messageGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)278
public fun traceGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)280
internal fun conversationGroup(configure: LinearBuilder.() -> Unit): View =281
makeColumn {282
this.spacing = 8.0;283
this.alignment = CrossAlignment.Stretch;284
this.configure()285
}