Scoop API

scoop:scoop.ui.component:0.1.0

src/conversation/message.scoop

Selected production source · src/conversation/message.scoop

1package scoop.ui.component
3import scoop.graphics.*
4import scoop.graphics.text.*
5import scoop.ui.*
6import scoop.ui.style.*
8public class MessageProps public constructor(
9 public val content: (MessageAlignment) -> View,
10 public val align: MessageAlignment = MessageAlignment.Start,
11 public val avatar: View? = None,
12 public val reserveAvatar: Boolean = false,
13 public val header: PanelContent? = None,
14 public val footer: PanelContent? = None,
15 public val captionInset: Double = 12.0,
16 public val fonts: FontCollection? = None,
17 public val theme: ThemeSource? = None,
18 public val style: StyleOverride = styleOverride()
19) {
20 init {
21 nonnegative(captionInset, "invalid message caption inset")
22 }
25public class MessageBuilder internal constructor() {
26 public var align: MessageAlignment = MessageAlignment.Start
27 public var avatar: View? = None
28 public var reserveAvatar: Boolean = false
29 public var header: PanelContent? = None
30 public var footer: PanelContent? = None
31 public var captionInset: Double = 12.0
32 public var fonts: FontCollection? = None
33 public var theme: ThemeSource? = None
34 public var style: StyleOverride = styleOverride()
35 public var key: ViewKey = ViewKey.Position
36 public fun header(text: String) {
37 header = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))
38 }
39 public fun header(text: StyledText) {
40 header = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))
41 }
42 public fun header(content: View) {
43 header = Some(PanelContent.Custom(content))
44 }
45 public fun footer(text: String) {
46 footer = Some(PanelContent.Label(LabelProps(LabelText.Plain(text, None))))
47 }
48 public fun footer(text: StyledText) {
49 footer = Some(PanelContent.Label(LabelProps(LabelText.Styled(text))))
50 }
51 public fun footer(content: View) {
52 footer = Some(PanelContent.Custom(content))
53 }
54 public fun style(configure: StyleOverrideBuilder.() -> Unit) {
55 style = style.copy(configure)
56 }
57 internal fun snapshot(content: (MessageAlignment) -> View): MessageProps =
58 MessageProps(
59 content,
60 align,
61 avatar,
62 reserveAvatar,
63 header,
64 footer,
65 captionInset,
66 fonts,
67 theme,
68 style
69 )
72public class Message(private val props: Props<MessageProps>, scope: ComponentScope): Component(
73 scope
74) {
75 private val themes = ThemeAccess(scope)
76 private val captions = ActionTextStyle()
77 private val medium = spanStyle { this.weight = Some(500) }
78 public override fun onMount() {
79 themes.mount(props.value.theme)
80 }
81 public override fun render(): View {
82 val input = props.value
83 val theme = themes.read(input.theme)
84 val density = theme.tokens.typography.body.fontSize / 14.0
85 val caption =
86 captions.resolve(
87 theme.tokens.typography.body,
88 theme.tokens.palette.muted.foreground,
89 Some(medium),
90 12.0 / 14.0
91 )
92 val header =
93 messageCaption(input.header, caption, input.fonts, input.captionInset * density)
94 val footer =
95 messageCaption(input.footer, caption, input.fonts, input.captionInset * density)
96 return panelFrame(
97 element(
98 MessageLayoutProps(
99 input.align,
100 panelPresent(input.avatar) || input.reserveAvatar,
101 panelPresent(input.header),
102 panelPresent(input.footer),
103 density
104 ),
105 { current, mount -> MessageLayout(current, mount) },
106 [
107 panelSlot(input.avatar ?: empty(), 0L),
108 panelSlot(header, 1L),
109 panelSlot(input.content(input.align), 2L),
110 panelSlot(footer, 3L)
111 ]
112 ),
113 visual = input.style
114 )
115 }
118private fun messageCaption(
119 content: PanelContent?,
120 style: TextStyle,
121 fonts: FontCollection?,
122 inset: Double
123): View =
124 when (content) {
125 case Some(val value) -> panelFrame(
126 panelCaption(value, style, fonts),
127 Insets(inset, 0.0, inset, 0.0)
128 )
129 case None -> empty()
130 }
132public fun message(content: View, configure: MessageBuilder.() -> Unit = {}): View =
133 makeMessage({ _ -> content }, configure)
135public fun message(
136 content: (MessageAlignment) -> View,
137 configure: MessageBuilder.() -> Unit = {}
138): View = makeMessage(content, configure)
140internal fun makeMessage(
141 content: (MessageAlignment) -> View,
142 configure: MessageBuilder.() -> Unit
143): View {
144 val builder = MessageBuilder();
145 builder.configure()
146 return component(
147 builder.snapshot(content),
148 { current, mount -> Message(current, mount) },
149 builder.key
150 )
153internal class MessageLayoutProps(
154 val align: MessageAlignment,
155 val avatar: Boolean,
156 val header: Boolean,
157 val footer: Boolean,
158 val density: Double
161internal class MessageLayout(
162 private val props: Props<MessageLayoutProps>,
163 scope: ComponentScope
164): Element<Unit>(scope) {
165 public override fun measure(context: MeasureContext, constraints: Constraints): LayoutResult<
166 Unit
167 > {
168 val input = props.value
169 val avatarSide =
170 if (input.avatar)
171 upperBound(32.0 * input.density, constraints.maxWidth ?: 32.0 * input.density)
172 else
173 0.0
174 if (input.avatar)
175 context.measureChild(0L, Constraints.tight(Size(avatarSide, avatarSide)))
176 else
177 context.skipChild(0L)
178 val avatarSpace =
179 if (input.avatar)
180 avatarSide + 8.0 * input.density
181 else
182 0.0
183 val maximum = insetMaximum(constraints.maxWidth, avatarSpace)
184 val header =
185 if (input.header)
186 conversationMeasure(context, 1L, maximum)
187 else {
188 context.skipChild(1L);
189 LayoutMetrics(Size(0.0, 0.0))
190 }
191 val body = context.measureChild(2L, Constraints(maxWidth = maximum))
192 val footer =
193 if (input.footer)
194 conversationMeasure(context, 3L, maximum)
195 else {
196 context.skipChild(3L);
197 LayoutMetrics(Size(0.0, 0.0))
198 }
199 val top =
200 if (input.header)
201 header.size.height + 10.0 * input.density
202 else
203 0.0
204 val bodyBottom = lowerBound(avatarSide, top + body.size.height)
205 val total =
206 bodyBottom +
207 if (input.footer)
208 10.0 * input.density + footer.size.height
209 else
210 0.0
211 val naturalWidth =
212 avatarSpace +
213 lowerBound(body.size.width, lowerBound(header.size.width, footer.size.width))
214 val size = constraints.constrain(Size(constraints.maxWidth ?: naturalWidth, total))
215 val columnWidth = lowerBound(0.0, size.width - avatarSpace)
216 val start =
217 if (input.align == MessageAlignment.Start)
218 avatarSpace
219 else
220 0.0
221 if (input.avatar) {
222 val along =
223 if (input.align == MessageAlignment.Start)
224 0.0
225 else
226 size.width - avatarSide
227 context.placeChild(
228 0L,
229 Point(
230 logicalX(along, avatarSide, size.width, context.layoutDirection),
231 bodyBottom - avatarSide
232 )
233 )
234 }
235 if (input.header)
236 place(context, 1L, header.size.width, start, columnWidth, size.width, 0.0)
237 place(
238 context,
239 2L,
240 body.size.width,
241 start,
242 columnWidth,
243 size.width,
244 bodyBottom - body.size.height
245 )
246 if (input.footer)
247 place(
248 context,
249 3L,
250 footer.size.width,
251 start,
252 columnWidth,
253 size.width,
254 bodyBottom + 10.0 * input.density
255 )
256 return LayoutResult(LayoutMetrics(size), Unit)
257 }
258 private fun place(
259 context: MeasureContext,
260 index: Long,
261 width: Double,
262 start: Double,
263 column: Double,
264 total: Double,
265 y: Double
266 ) {
267 val x = start + conversationOffset(props.value.align, column, width)
268 context.placeChild(index, Point(logicalX(x, width, total, context.layoutDirection), y))
269 }
270 public override fun scene(context: SceneContext, measured: Unit) {
271 context.children()
272 }