Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/marker.scoop

Selected production source · src/conversation/marker.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8public enum MarkerVariant {
9 Default, Border, Separator
12public class MarkerProps public constructor(
13 public val content: ButtonContent,
14 public val icon: View? = None,
15 public val variant: MarkerVariant = MarkerVariant.Default,
16 public val loading: Boolean = false,
17 public val shimmer: Boolean = true,
18 public val onClick: (() -> Unit)? = None,
19 public val enabled: Boolean = true,
20 public val focus: FocusOptions = FocusOptions(),
21 public val focusOutline: OutlineStyle? = None,
22 public val fonts: FontCollection? = None,
23 public val theme: ThemeSource? = None,
24 public val style: StyleOverride = styleOverride(),
25 public val semantics: ComponentSemantics = ComponentSemantics()
28public class MarkerBuilder internal constructor(): ConversationActionBuilder() {
29 public var icon: View? = None
30 public var variant: MarkerVariant = MarkerVariant.Default
31 public var loading: Boolean = false
32 public var shimmer: Boolean = true
33 internal fun snapshot(content: ButtonContent): MarkerProps =
34 MarkerProps(
35 content,
36 icon,
37 variant,
38 loading,
39 shimmer,
40 onClick,
41 enabled,
42 focus,
43 focusOutline,
44 fonts,
45 theme,
46 style,
47 semantics
48 )
51public class Marker(private val props: Props<MarkerProps>, scope: ComponentScope): Component(
52 scope
53) {
54 private val themes = ThemeAccess(scope)
55 private val text = ActionTextStyle()
56 public override fun onMount() {
57 themes.mount(props.value.theme)
58 }
59 public override fun render(): View {
60 val input = props.value
61 val theme = themes.read(input.theme)
62 val p = theme.tokens.palette
63 val base = text.resolve(theme.tokens.typography.body, p.muted.foreground, None)
64 val density = base.fontSize / 14.0
65 val hasIcon = panelPresent(input.icon) || input.loading
66 val icon =
67 input.icon ?:
68 spinner {
69 this.size = Some(16.0 * density);
70 this.running = input.loading && input.enabled
71 this.color = Some(p.muted.foreground);
72 this.theme = input.theme
73 }
74 val content = conversationCaption(input.content, base, input.fonts, scope.layoutDirection)
75 val line =
76 element(
77 MarkerLayoutProps(input.variant, hasIcon, density, p.border),
78 { current, mount -> MarkerLayout(current, mount) },
79 [
80 panelSlot(icon, 0L),
81 panelSlot(
82 conversationShimmer(
83 content,
84 input.loading && input.shimmer && input.enabled,
85 theme
86 ),
87 1L
88 )
89 ]
90 )
91 return conversationSurface(
92 ConversationSurfaceProps(
93 ButtonContent.Custom(line),
94 theme,
95 base,
96 conversationStyle(
97 Color.Transparent,
98 p.muted.foreground,
99 Insets(),
100 4.0 * density,
101 hover =
102 if (panelPresent(input.onClick))
103 p.muted.background
104 else
105 Color.Transparent,
106 outline = input.focusOutline,
107 local = styleOverride { this.border(None) }.then(input.style)
108 ),
109 input.onClick,
110 input.enabled,
111 input.focus,
112 input.fonts,
113 semantics = input.semantics.copy { busy = busy || input.loading }
114 )
115 )
116 }
119internal class MarkerLayoutProps(
120 val variant: MarkerVariant,
121 val icon: Boolean,
122 val density: Double,
123 val color: Color
126internal struct MarkerLines(val left: Double, val right: Double, val y: Double)
128internal class MarkerLayout(
129 private val props: Props<MarkerLayoutProps>,
130 scope: ComponentScope
131): Element<MarkerLines>(scope) {
132 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
133 MarkerLines
134 > {
135 val input = props.value
136 val d = input.density
137 val iconSize =
138 if (input.icon)
139 16.0 * d
140 else
141 0.0
142 val iconSpace =
143 if (input.icon)
144 iconSize + 8.0 * d
145 else
146 0.0
147 val reserve =
148 if (input.variant == MarkerVariant.Separator && panelPresent(constraints.maxWidth))
149 32.0 * d
150 else
151 0.0
152 val body =
153 conversationMeasure(
154 context,
155 1L,
156 insetMaximum(constraints.maxWidth, iconSpace + reserve)
157 )
158 if (input.icon)
159 context.measureChild(0L, Constraints.tight(Size(iconSize, iconSize)))
160 else
161 context.skipChild(0L)
162 val height = lowerBound(16.0 * d, lowerBound(iconSize, body.size.height))
163 val size =
164 constraints.constrain(
165 Size(
166 constraints.maxWidth ?: (iconSpace + body.size.width),
167 height +
168 if (input.variant == MarkerVariant.Border)
169 8.0 * d + 1.0
170 else
171 0.0
172 )
173 )
174 val contentWidth = iconSpace + body.size.width
175 val x =
176 if (input.variant == MarkerVariant.Separator)
177 lowerBound(0.0, (size.width - contentWidth) / 2.0)
178 else
179 0.0
180 if (input.icon)
181 context.placeChild(
182 0L,
183 Point(
184 logicalX(x, iconSize, size.width, context.layoutDirection),
185 (height - iconSize) / 2.0
186 )
187 )
188 context.placeChild(
189 1L,
190 Point(
191 logicalX(x + iconSpace, body.size.width, size.width, context.layoutDirection),
192 (height - body.size.height) / 2.0
193 )
194 )
195 return LayoutResult(
196 LayoutMetrics(size),
197 MarkerLines(
198 lowerBound(0.0, x - 12.0 * d),
199 lowerBound(0.0, size.width - x - contentWidth - 12.0 * d),
200 height / 2.0
201 )
202 )
203 }
204 public override fun scene(context: SceneContext, layout: MarkerLines) {
205 val input = props.value
206 val size = context.size
207 context.children()
208 context.draw {
209 paint ->
210 if (input.variant == MarkerVariant.Border && size.height >= 1.0)
211 paint.canvas.drawRect(
212 Rect(0.0, size.height - 1.0, size.width, 1.0),
213 Paint(input.color)
214 )
215 if (input.variant == MarkerVariant.Separator) {
216 paint.canvas.drawRect(Rect(0.0, layout.y, layout.left, 1.0), Paint(input.color))
217 paint.canvas.drawRect(
218 Rect(size.width - layout.right, layout.y, layout.right, 1.0),
219 Paint(input.color)
220 )
221 }
222 }
223 }
226public fun marker(text: String, configure: MarkerBuilder.() -> Unit = {}): View =
227 makeMarker(conversationText(text), configure)
229public fun marker(text: StyledText, configure: MarkerBuilder.() -> Unit = {}): View =
230 makeMarker(conversationText(text), configure)
232public fun marker(content: View, configure: MarkerBuilder.() -> Unit = {}): View =
233 makeMarker(ButtonContent.Custom(content), configure)
235public fun marker(
236 content: (ActionContentStyle) -> View,
237 configure: MarkerBuilder.() -> Unit = {}
238): View = makeMarker(ButtonContent.Resolved(content), configure)
240internal fun makeMarker(content: ButtonContent, configure: MarkerBuilder.() -> Unit): View {
241 val builder = MarkerBuilder();
242 builder.configure()
243 return component(
244 builder.snapshot(content),
245 { current, mount -> Marker(current, mount) },
246 builder.key
247 )