Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/surface.scoop

Selected production source · src/conversation/surface.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
7import scoop.window.accessibility.*
9public enum MessageAlignment {
10 Start, End
13public open class ConversationActionBuilder internal constructor() {
14 public var onClick: (() -> Unit)? = None
15 public var enabled: Boolean = true
16 public var focus: FocusOptions = FocusOptions()
17 public var focusOutline: OutlineStyle? = None
18 public var fonts: FontCollection? = None
19 public var theme: ThemeSource? = None
20 public var style: StyleOverride = styleOverride()
21 public var key: ViewKey = ViewKey.Position
22 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 }
31internal 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? = None
46/** Keeps one input and content identity when an optional action changes. */
47internal class ConversationSurface(
48 private val props: Props<ConversationSurfaceProps>,
49 scope: ComponentScope
50): 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? = None
60 private fun prepareSemantics(): SemanticsHandle? {
61 val input = props.value
62 val role =
63 input.semanticRole ?:
64 if (panelPresent(input.onClick))
65 SemanticRole.Button
66 else
67 SemanticRole.Group
68 when (semantics) {
69 case Some(val old) -> if (old.isClosed || old.role != role) {
70 if (!old.isClosed)
71 old.close()
72 semantics = None
73 }
74 case None -> {}
75 }
76 if (!panelPresent(semantics))
77 semantics = createComponentSemantics(scope, role)
78 when (semantics) {
79 case Some(val target) -> {
80 target.mergeLabels = true
81 target.bindFocus(
82 if (panelPresent(input.onClick))
83 Some(control.focus)
84 else
85 None
86 )
87 }
88 case None -> {}
89 }
90 scope.routeSemanticsChildren(semantics)
91 return semantics
92 }
93 public override fun render(): View {
94 val input = props.value
95 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.fonts
106 },
107 value.settings,
108 value.inlines,
109 value.theme,
110 value.baseStyle,
111 value.underline,
112 value.semantics
113 )
114 )
115 else -> input.content
116 }
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 false
146 )
147 )
148 ]
149 )
150 val ring =
151 if (!panelPresent(input.onClick))
152 0.0
153 else
154 input.theme.surfaceMetrics(
155 visual,
156 actionOptions(
157 input.theme,
158 shape,
159 ButtonVariant.Ghost,
160 input.style,
161 visual.interaction
162 ),
163 scope.layoutDirection
164 ).focusOutset
165 return panelFrame(body, Insets.all(ring))
166 }
169internal fun conversationSurface(input: ConversationSurfaceProps): View =
170 component(input, { current, mount -> ConversationSurface(current, mount) })
172internal fun conversationText(text: String): ButtonContent =
173 ButtonContent.Label(LabelProps(LabelText.Plain(text, None)))
175internal fun conversationText(text: StyledText): ButtonContent =
176 ButtonContent.Label(LabelProps(LabelText.Styled(text)))
178internal fun conversationCaption(
179 content: ButtonContent,
180 appearance: TextStyle,
181 fonts: FontCollection?,
182 layoutDirection: LayoutDirection,
183 lines: Long? = None
184): 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 -> fonts
193 },
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.semantics
206 )
207 )
208 else -> content
209 }
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 false
218 )
219 )
222internal 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.Plain
234 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 = outline
240 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. */
255internal fun conversationMeasure(
256 context: MeasureContext,
257 index: Long,
258 width: Double?,
259 height: Double? = None
260): 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 else
265 natural
268internal fun conversationOffset(alignment: MessageAlignment, width: Double, child: Double): Double =
269 if (alignment == MessageAlignment.End)
270 lowerBound(0.0, width - child)
271 else
272 0.0
274public fun bubbleGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)
276public fun messageGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)
278public fun traceGroup(configure: LinearBuilder.() -> Unit): View = conversationGroup(configure)
280internal fun conversationGroup(configure: LinearBuilder.() -> Unit): View =
281 makeColumn {
282 this.spacing = 8.0;
283 this.alignment = CrossAlignment.Stretch;
284 this.configure()
285 }