The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): composer chips match chat chips (#22518)
oliver committed
Oct 20, 2026 at 18:57 UTC
ecfe4f7955bd826a4d465aa52200ea4354a0cb7f
12 files changed
+247
-375
apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ChipIcons.kt
new
+60
@@ -0,0 +1,60 @@
1
+package expo.modules.t3composereditor
2
+
3
+import android.content.Context
4
+import android.graphics.Bitmap
5
+import android.graphics.BitmapFactory
6
+import android.net.Uri
7
+import android.os.Handler
8
+import android.os.Looper
9
+import java.net.URL
10
+import java.util.concurrent.Executors
11
+
12
+internal object T3ChipIcons {
13
+ private val images = HashMap<String, Bitmap>()
14
+ private val pending = HashMap<String, MutableSet<(String) -> Unit>>()
15
+ private val downloads = Executors.newSingleThreadExecutor()
16
+ private val main = Handler(Looper.getMainLooper())
17
+
18
+ fun cached(uri: String): Bitmap? = images[uri]
19
+
20
+ fun load(context: Context, uri: String, onLoaded: (String) -> Unit): Bitmap? =
21
+ images[uri] ?: when {
22
+ uri.isEmpty() -> null
23
+ uri.startsWith("http://") || uri.startsWith("https://") -> {
24
+ download(uri, onLoaded)
25
+ null
26
+ }
27
+ else -> decodeLocal(context, uri)?.also { images[uri] = it }
28
+ }
29
+
30
+ private fun download(uri: String, onLoaded: (String) -> Unit) {
31
+ val waiting = pending[uri]
32
+ if (waiting != null) {
33
+ waiting.add(onLoaded)
34
+ return
35
+ }
36
+ pending[uri] = mutableSetOf(onLoaded)
37
+ downloads.execute {
38
+ val bitmap = runCatching {
39
+ URL(uri).openStream().use(BitmapFactory::decodeStream)
40
+ }.getOrNull()
41
+ main.post {
42
+ val callbacks = pending.remove(uri).orEmpty()
43
+ if (bitmap != null) {
44
+ images[uri] = bitmap
45
+ callbacks.forEach { it(uri) }
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ private fun decodeLocal(context: Context, uri: String): Bitmap? = runCatching {
52
+ when {
53
+ uri.startsWith("/") -> BitmapFactory.decodeFile(uri)
54
+ uri.startsWith("file:") -> Uri.parse(uri).path?.let(BitmapFactory::decodeFile)
55
+ else -> context.resources.getIdentifier(uri, "drawable", context.packageName)
56
+ .takeIf { it != 0 }
57
+ ?.let { BitmapFactory.decodeResource(context.resources, it) }
58
+ }
59
+ }.getOrNull()
60
+}
apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt
+26
-42
@@ -61,7 +61,20 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
61
private var contentInsetVertical = 0
62
private var tokensJson = "[]"
63
private var tokens: List<ComposerToken> = emptyList()
64
- private var chipTheme = ComposerChipTheme.default()
64
+ private val redrawChipsWithIcon: (String) -> Unit = { uri ->
65
+ editor.text?.let { editable ->
66
+ editable.getSpans(0, editable.length, ComposerChipSpan::class.java)
67
+ .filter { it.iconUri == uri }
68
+ .forEach {
69
+ editable.setSpan(
70
+ it,
71
+ editable.getSpanStart(it),
72
+ editable.getSpanEnd(it),
73
+ editable.getSpanFlags(it)
74
+ )
75
+ }
76
+ }
77
+ }
78
private var autoCorrect = true
79
private var spellCheck = true
80
private var nativeEventCount = 0
@@ -241,17 +254,6 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
254
} else {
255
applySelectionTheme(parseColor(theme.optString("selection"), editor.currentTextColor))
256
}
244
- chipTheme = ComposerChipTheme(
245
- chipBackground = parseColor(theme.optString("chipBackground"), chipTheme.chipBackground),
246
- chipBorder = parseColor(theme.optString("chipBorder"), chipTheme.chipBorder),
247
- chipText = parseColor(theme.optString("chipText"), chipTheme.chipText),
248
- skillBackground = parseColor(
249
- theme.optString("skillBackground"),
250
- chipTheme.skillBackground,
251
- ),
252
- skillBorder = parseColor(theme.optString("skillBorder"), chipTheme.skillBorder),
253
- skillText = parseColor(theme.optString("skillText"), chipTheme.skillText),
254
- )
257
applyTokenSpans()
258
} catch (_: Exception) {
259
}
@@ -452,20 +454,18 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
454
if (token.start < 0 || token.end <= token.start || token.end > editable.length) return@forEach
455
val expectedSource = editable.substring(token.start, token.end)
456
if (expectedSource != token.source) return@forEach
457
+ T3ChipIcons.load(context, token.iconUri, redrawChipsWithIcon)
458
editable.setSpan(
459
ComposerChipSpan(
460
T3ContextChip(
461
content = T3ContextChip.Content(
462
label = token.label,
460
- symbol = token.symbol,
461
- detail = token.detail
462
- ),
463
- fontSize = editor.textSize * 0.8f,
464
- colors = T3ContextChip.Colors(
465
- accent = T3ContextChip.color(token.accent, chipTheme.chipText),
466
- foreground = chipTheme.chipText,
467
- border = chipTheme.chipBorder
463
+ detail = token.detail,
464
+ iconUri = token.iconUri,
465
+ tintsIcon = token.tintsIcon
466
),
467
+ fontSize = editor.textSize,
468
+ color = T3ContextChip.color(token.accent, editor.currentTextColor),
469
maximumWidth = (
470
editor.width.takeIf {
471
it > 0
@@ -508,34 +508,17 @@ private data class ComposerToken(
508
val label: String,
509
val detail: String,
510
val accent: String,
511
- val symbol: String,
511
+ val iconUri: String,
512
+ val tintsIcon: Boolean,
513
val start: Int,
514
val end: Int
515
)
516
516
-private data class ComposerChipTheme(
517
- val chipBackground: Int,
518
- val chipBorder: Int,
519
- val chipText: Int,
520
- val skillBackground: Int,
521
- val skillBorder: Int,
522
- val skillText: Int
523
-) {
524
- companion object {
525
- fun default() = ComposerChipTheme(
526
- chipBackground = Color.rgb(238, 240, 243),
527
- chipBorder = Color.rgb(210, 214, 220),
528
- chipText = Color.rgb(35, 39, 45),
529
- skillBackground = Color.rgb(233, 239, 255),
530
- skillBorder = Color.rgb(185, 200, 245),
531
- skillText = Color.rgb(45, 72, 155),
532
- )
533
- }
534
-}
535
-
517
private class ComposerChipSpan(
518
private val chip: T3ContextChip
519
) : ReplacementSpan() {
520
+ val iconUri get() = chip.iconUri
521
+
522
override fun getSize(
523
paint: Paint,
524
text: CharSequence,
@@ -581,7 +564,8 @@ private fun parseTokens(value: String): List<ComposerToken> = try {
564
label = token.optString("label"),
565
detail = token.optString("detail"),
566
accent = token.optString("accent"),
584
- symbol = token.optString("symbol", "doc"),
567
+ iconUri = if (token.isNull("iconUri")) "" else token.optString("iconUri"),
568
+ tintsIcon = token.optBoolean("tintsIcon"),
569
start = token.optInt("start"),
570
end = token.optInt("end"),
571
)
apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ContextChip.kt
+31
-111
@@ -3,7 +3,8 @@ package expo.modules.t3composereditor
3
import android.graphics.Canvas
4
import android.graphics.Color
5
import android.graphics.Paint
6
-import android.graphics.Path
6
+import android.graphics.PorterDuff
7
+import android.graphics.PorterDuffColorFilter
8
import android.graphics.RectF
9
import android.graphics.Typeface
10
import android.text.TextPaint
@@ -11,24 +12,25 @@ import android.text.TextUtils
12
import kotlin.math.ceil
13
import kotlin.math.min
14
14
-/** Shared by editable spans and inline chat images so their metrics and colors agree. */
15
class T3ContextChip(
16
content: Content,
17
fontSize: Float,
18
- colors: Colors,
18
+ private val color: Int,
19
maximumWidth: Float,
20
- private val density: Float
20
+ density: Float
21
) {
22
/** What the chip says: its name, the size beside it, and the glyph that leads it. */
23
- data class Content(val label: String, val symbol: String, val detail: String = "")
24
-
25
- data class Colors(val accent: Int, val foreground: Int, val border: Int)
26
-
27
- private val symbol = content.symbol
23
+ data class Content(
24
+ val label: String,
25
+ val detail: String = "",
26
+ val iconUri: String = "",
27
+ val tintsIcon: Boolean = false
28
+ )
29
30
private val paint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
31
textSize = fontSize
32
typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
33
+ color = this@T3ContextChip.color
34
}
35
private val em = fontSize
36
@@ -37,72 +39,47 @@ class T3ContextChip(
39
private val detailPaint = TextPaint(Paint.ANTI_ALIAS_FLAG).apply {
40
textSize = fontSize * 0.84f
41
typeface = Typeface.create("sans-serif-medium", Typeface.NORMAL)
42
+ color = this@T3ContextChip.color
43
}
44
private val detailText = if (content.detail.isEmpty()) "" else " ${content.detail}"
45
private val detailWidth = if (detailText.isEmpty()) 0f else detailPaint.measureText(detailText)
46
44
- // The border is stroked, and a stroke straddles the path it follows, so the box has to
45
- // reserve a stroke on every side it insets. Reserving it once left the right edge a
46
- // stroke short, and antialiasing shaved that curve flat against the span bounds.
47
- private val strokeWidth = density
48
- private val inset = strokeWidth * 2f
47
+ val iconUri = content.iconUri
48
+ private val iconPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.FILTER_BITMAP_FLAG).apply {
49
+ if (content.tintsIcon) {
50
+ colorFilter =
51
+ PorterDuffColorFilter(this@T3ContextChip.color, PorterDuff.Mode.SRC_IN)
52
+ }
53
+ }
54
+ private val padding = density * 5
55
+ private val iconSlot = if (iconUri.isEmpty()) 0f else em * 1.25f
56
val width = ceil(
57
min(
58
maximumWidth.coerceAtLeast(em * 3),
52
- paint.measureText(content.label) + detailWidth + em * 2.5f + inset
59
+ paint.measureText(content.label) + detailWidth + iconSlot + padding * 2
60
)
61
)
55
- val height = ceil(em * 1.41f + inset)
62
+ val height = ceil(paint.fontMetrics.descent - paint.fontMetrics.ascent)
63
private val text = TextUtils.ellipsize(
64
content.label,
65
paint,
59
- (width - em * 2.5f - inset - detailWidth).coerceAtLeast(0f),
66
+ (width - iconSlot - padding * 2 - detailWidth).coerceAtLeast(0f),
67
TextUtils.TruncateAt.MIDDLE
68
).toString()
62
- private val fill = Color.argb(
63
- 28,
64
- Color.red(colors.accent),
65
- Color.green(colors.accent),
66
- Color.blue(colors.accent)
67
- )
68
- private val textColor = blend(colors.accent, colors.foreground, 0.22f)
69
- private val borderColor = blend(colors.accent, colors.border, 0.34f)
70
-
71
- // Half a stroke keeps the border inside the box; the rest of the reserved margin is
72
- // slack, so an antialiased edge fades out before it reaches the span bounds.
73
- private val shape =
74
- RectF(inset / 2f, inset / 2f, width - inset / 2f, height - inset / 2f)
75
- private val icon = iconPath(symbol)
69
70
fun draw(canvas: Canvas, x: Float, y: Float) {
71
canvas.save()
72
canvas.translate(x, y)
80
- paint.style = Paint.Style.FILL
81
- paint.color = fill
82
- canvas.drawRoundRect(shape, em / 2, em / 2, paint)
83
- paint.style = Paint.Style.STROKE
84
- paint.strokeWidth = strokeWidth
85
- paint.color = borderColor
86
- canvas.drawRoundRect(shape, em / 2, em / 2, paint)
87
- paint.color = textColor
88
- canvas.save()
89
- val iconSize = em * 1.17f
90
- canvas.translate(em / 2 + inset / 2f, (height - iconSize) / 2)
91
- canvas.scale(iconSize / 24, iconSize / 24)
92
- paint.strokeWidth = 1.7f
93
- paint.strokeJoin = Paint.Join.ROUND
94
- paint.strokeCap = Paint.Cap.ROUND
95
- canvas.drawPath(icon, paint)
96
- canvas.restore()
97
- paint.style = Paint.Style.FILL
98
- val metrics = paint.fontMetrics
99
- val baseline = (height - metrics.descent - metrics.ascent) / 2
100
- canvas.drawText(text, em * 2 + inset / 2f, baseline, paint)
73
+ T3ChipIcons.cached(iconUri)?.let {
74
+ val top = (height - em) / 2
75
+ canvas.drawBitmap(it, null, RectF(padding, top, padding + em, top + em), iconPaint)
76
+ }
77
+ val baseline = -paint.fontMetrics.ascent
78
+ canvas.drawText(text, padding + iconSlot, baseline, paint)
79
if (detailText.isNotEmpty()) {
102
- detailPaint.color = paint.color
80
canvas.drawText(
81
detailText,
105
- em * 2 + inset / 2f + paint.measureText(text),
82
+ padding + iconSlot + paint.measureText(text),
83
baseline,
84
detailPaint
85
)
@@ -123,62 +100,5 @@ class T3ContextChip(
100
}
101
Color.parseColor(androidColor)
102
}.getOrDefault(fallback)
126
-
127
- private fun blend(accent: Int, base: Int, weight: Float): Int = Color.rgb(
128
- (Color.red(accent) * weight + Color.red(base) * (1 - weight)).toInt(),
129
- (Color.green(accent) * weight + Color.green(base) * (1 - weight)).toInt(),
130
- (Color.blue(accent) * weight + Color.blue(base) * (1 - weight)).toInt(),
131
- )
132
-
133
- private fun iconPath(symbol: String) = Path().apply {
134
- fun line(vararg points: Float) {
135
- moveTo(points[0], points[1])
136
- for (index in 2 until points.size step 2) lineTo(points[index], points[index + 1])
137
- }
138
- when (symbol) {
139
- "cube" -> {
140
- line(12f, 2f, 21f, 7f, 21f, 17f, 12f, 22f, 3f, 17f, 3f, 7f, 12f, 2f)
141
- line(3f, 7f, 12f, 12f, 21f, 7f)
142
- line(12f, 12f, 12f, 22f)
143
- }
144
- // lucide `git-pull-request`, the glyph web draws: two nodes, an elbow, and a stem.
145
- "git-pull-request" -> {
146
- addCircle(18f, 18f, 3f, Path.Direction.CW)
147
- addCircle(6f, 6f, 3f, Path.Direction.CW)
148
- moveTo(13f, 6f)
149
- lineTo(16f, 6f)
150
- cubicTo(17.1f, 6f, 18f, 6.9f, 18f, 8f)
151
- lineTo(18f, 15f)
152
- line(6f, 9f, 6f, 21f)
153
- }
154
- "cursorarrow.click" -> {
155
- line(4f, 3f, 19f, 12f, 12f, 14f, 9f, 21f, 4f, 3f)
156
- line(13f, 15f, 18f, 21f)
157
- }
158
- "text.bubble" -> {
159
- line(3f, 4f, 21f, 4f, 21f, 17f, 10f, 17f, 5f, 21f, 5f, 17f, 3f, 17f, 3f, 4f)
160
- line(7f, 8f, 17f, 8f)
161
- line(7f, 12f, 14f, 12f)
162
- }
163
- "terminal", "play.rectangle", "photo" -> {
164
- addRoundRect(2f, 4f, 22f, 20f, 2f, 2f, Path.Direction.CW)
165
- when (symbol) {
166
- "terminal" -> {
167
- line(6f, 8f, 10f, 12f, 6f, 16f)
168
- line(13f, 16f, 18f, 16f)
169
- }
170
- "play.rectangle" -> line(9f, 8f, 16f, 12f, 9f, 16f, 9f, 8f)
171
- else -> {
172
- addCircle(8f, 9f, 1.5f, Path.Direction.CW)
173
- line(3f, 18f, 11f, 12f, 15f, 15f, 18f, 12f, 21f, 16f)
174
- }
175
- }
176
- }
177
- else -> {
178
- line(5f, 2f, 14f, 2f, 20f, 8f, 20f, 22f, 5f, 22f, 5f, 2f)
179
- line(14f, 2f, 14f, 8f, 20f, 8f)
180
- }
181
- }
182
- }
103
}
104
}
apps/mobile/modules/t3-composer-editor/ios/T3ComposerEditorView.swift
+42
-143
@@ -7,7 +7,7 @@ private struct ComposerTokenPayload: Decodable {
7
let label: String
8
let iconUri: String?
9
let accent: String?
10
- let symbol: String?
10
+ let tintsIcon: Bool?
11
let detail: String?
12
let start: Int
13
let end: Int
@@ -29,20 +29,6 @@ private struct ComposerControlledDocumentPayload: Decodable {
29
private struct ComposerThemePayload: Decodable {
30
let text: String
31
let placeholder: String
32
- let chipBackground: String
33
- let chipBorder: String
34
- let chipText: String
35
- let skillBackground: String
36
- let skillBorder: String
37
- let skillText: String
38
- let fileTint: String
39
-}
40
-
41
-private struct ComposerChipStyle {
42
- let tint: UIColor
43
- let backgroundColor: UIColor
44
- let borderColor: UIColor
45
- let textColor: UIColor
32
}
33
34
private enum ComposerEnterBehavior: String {
@@ -446,14 +432,7 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
432
private var requestedSelection: ComposerSelectionPayload?
433
private var theme = ComposerThemePayload(
434
text: "#262626",
449
- placeholder: "#8e8e93",
450
- chipBackground: "#f2f2f7",
451
- chipBorder: "#dedee3",
452
- chipText: "#262626",
453
- skillBackground: "#f9e8fb",
454
- skillBorder: "#e5a6eb",
455
- skillText: "#a21caf",
456
- fileTint: "#737373"
435
+ placeholder: "#8e8e93"
436
)
437
private var fontFamily = "DMSans-Regular"
438
private var fontSize: CGFloat = 14
@@ -906,40 +885,26 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
885
}
886
887
private func makeAttachmentString(_ token: ComposerTokenPayload) -> NSAttributedString {
909
- let isSkill = token.type == "skill"
910
- let accent = token.accent.flatMap { UIColor(composerHex: $0) }
911
- let foreground = UIColor(composerHex: theme.chipText) ?? .label
912
- let border = UIColor(composerHex: theme.chipBorder) ?? .separator
913
- let tint = accent.map { blend($0, over: foreground, weight: 0.22) }
914
- ?? UIColor(composerHex: isSkill ? theme.skillText : theme.fileTint) ?? .secondaryLabel
915
- let iconName = token.symbol ?? (isSkill ? "cube" : "doc")
916
- let iconImage = token.iconUri.flatMap(iconImage(for:))
917
- let style = ComposerChipStyle(
918
- tint: tint,
919
- backgroundColor: accent?.withAlphaComponent(0.11) ?? UIColor(
920
- composerHex: isSkill ? theme.skillBackground : theme.chipBackground
921
- ) ?? .secondarySystemFill,
922
- borderColor: accent.map { blend($0, over: border, weight: 0.34) } ?? UIColor(
923
- composerHex: isSkill ? theme.skillBorder : theme.chipBorder
924
- ) ?? .separator,
925
- textColor: tint
926
- )
888
+ let color = token.accent.flatMap { UIColor(composerHex: $0) }
889
+ ?? UIColor(composerHex: theme.text) ?? .label
890
+ let chipFont = UIFont(name: "DMSans-Medium", size: fontSize)
891
+ ?? UIFont.systemFont(ofSize: fontSize, weight: .medium)
892
+ let icon = token.iconUri.flatMap(iconImage(for:)).map {
893
+ token.tintsIcon == true ? Self.tinted($0, color: color) : $0
894
+ }
895
let image = renderChip(
896
label: token.label,
897
detail: token.detail,
930
- iconName: iconName,
931
- iconImage: iconImage,
932
- style: style
898
+ icon: icon,
899
+ color: color,
900
+ font: chipFont
901
)
934
- let font = UIFont(name: fontFamily, size: fontSize)
935
- ?? UIFont.systemFont(ofSize: fontSize)
936
- let baselineOffset = floor((font.capHeight - image.size.height) / 2)
902
let attachment = ComposerTextAttachment(
903
source: token.source,
904
label: token.label,
905
image: image,
906
size: image.size,
942
- baselineOffset: baselineOffset
907
+ baselineOffset: chipFont.ascender - image.size.height
908
)
909
let attributedAttachment = NSMutableAttributedString(attachment: attachment)
910
attributedAttachment.addAttributes(
@@ -949,78 +914,35 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
914
return attributedAttachment
915
}
916
952
- /// Chip glyphs with no SF Symbol that reads correctly. A pull request would otherwise land on
953
- /// `arrow.triangle.branch`, a road-sign fork that says "branch", not "pull request", so it is
954
- /// drawn from the same lucide geometry web and Android use.
955
- private static func vectorIcon(named name: String, size: CGFloat, color: UIColor) -> UIImage? {
956
- guard name == "git-pull-request" else { return nil }
957
- return UIGraphicsImageRenderer(size: CGSize(width: size, height: size)).image { _ in
958
- let s = size / 24 // lucide authors on a 24pt grid.
959
- let path = UIBezierPath()
960
- for centre in [CGPoint(x: 18 * s, y: 18 * s), CGPoint(x: 6 * s, y: 6 * s)] {
961
- path.append(UIBezierPath(arcCenter: centre, radius: 3 * s, startAngle: 0,
962
- endAngle: .pi * 2, clockwise: true))
963
- }
964
- path.move(to: CGPoint(x: 13 * s, y: 6 * s))
965
- path.addLine(to: CGPoint(x: 16 * s, y: 6 * s))
966
- path.addCurve(to: CGPoint(x: 18 * s, y: 8 * s),
967
- controlPoint1: CGPoint(x: 17.1 * s, y: 6 * s),
968
- controlPoint2: CGPoint(x: 18 * s, y: 6.9 * s))
969
- path.addLine(to: CGPoint(x: 18 * s, y: 15 * s))
970
- path.move(to: CGPoint(x: 6 * s, y: 9 * s))
971
- path.addLine(to: CGPoint(x: 6 * s, y: 21 * s))
972
- path.lineWidth = 2 * s
973
- path.lineCapStyle = .round
974
- path.lineJoinStyle = .round
975
- color.setStroke()
976
- path.stroke()
977
- }
978
- }
979
-
917
private func renderChip(
918
label: String,
919
detail: String?,
983
- iconName: String,
984
- iconImage: UIImage?,
985
- style: ComposerChipStyle
920
+ icon: UIImage?,
921
+ color: UIColor,
922
+ font: UIFont
923
) -> UIImage {
987
- // Kept in step with `T3ContextChipVectorIcon` in the markdown module: a chip drawn here and
988
- // the same chip drawn in a sent message have to be the same picture.
989
- let chipFontSize = fontSize * 0.86
990
- let font = UIFont(name: "DMSans-Medium", size: chipFontSize)
991
- ?? UIFont.systemFont(ofSize: chipFontSize, weight: .medium)
992
- let fallbackIcon = Self.vectorIcon(named: iconName, size: 14, color: style.textColor)
993
- ?? UIImage(
994
- systemName: iconName,
995
- withConfiguration: UIImage.SymbolConfiguration(pointSize: 12, weight: .medium)
996
- )
997
- let icon = iconImage ?? fallbackIcon
998
- // The size reads as metadata, not part of the name, so it renders a step down from the
999
- // label the way the web chip does.
924
let paragraph = NSMutableParagraphStyle()
925
paragraph.alignment = .left
1002
- let detailFont = UIFont(name: "DMSans-Medium", size: chipFontSize * 0.84)
1003
- ?? UIFont.systemFont(ofSize: chipFontSize * 0.84, weight: .medium)
926
+ paragraph.lineBreakMode = .byTruncatingMiddle
927
let attributedLabel = NSMutableAttributedString(
928
string: label,
1006
- attributes: [.font: font, .foregroundColor: style.textColor, .paragraphStyle: paragraph]
929
+ attributes: [.font: font, .foregroundColor: color, .paragraphStyle: paragraph]
930
)
931
+ // The size reads as metadata, not part of the name, so it renders a step down from the
932
+ // label the way the web chip does.
933
if let detail, !detail.isEmpty {
934
+ let detailFont = font.withSize(font.pointSize * 0.84)
935
attributedLabel.append(
936
NSAttributedString(
937
string: " \(detail)",
1012
- attributes: [
1013
- .font: detailFont,
1014
- .foregroundColor: style.textColor,
1015
- .paragraphStyle: paragraph,
1016
- ]
938
+ attributes: [.font: detailFont, .foregroundColor: color, .paragraphStyle: paragraph]
939
)
940
)
941
}
1020
- let iconWidth: CGFloat = icon == nil ? 0 : chipFontSize * 1.17
1021
- let iconGap: CGFloat = icon == nil ? 0 : chipFontSize * 0.33
1022
- let padding = chipFontSize * 0.5
1023
- let height = ceil(chipFontSize * 1.41)
942
+ let iconSide = font.pointSize
943
+ let iconSlot: CGFloat = icon == nil ? 0 : iconSide * 1.25
944
+ let padding: CGFloat = 5
945
+ let height = ceil(font.ascender - font.descender)
946
// A long path would otherwise draw a chip wider than the composer and clip. Cap the label
947
// to the text the editor can actually show and truncate inside it, as Android's
948
// `maximumWidth` does, so the chip always fits the line it sits on.
@@ -1030,58 +952,35 @@ public final class T3ComposerEditorView: ExpoView, UITextViewDelegate, UITextDro
952
if availableWidth <= 0 {
953
chipsNeedMeasuredWidth = true
954
}
1033
- let maximumLabelWidth = max(chipFontSize * 3, availableWidth - padding * 2 - iconWidth - iconGap)
1034
- paragraph.lineBreakMode = .byTruncatingMiddle
1035
- attributedLabel.addAttribute(
1036
- .paragraphStyle,
1037
- value: paragraph,
1038
- range: NSRange(location: 0, length: attributedLabel.length)
1039
- )
1040
- let measured = attributedLabel.size()
1041
- let textSize = CGSize(width: min(measured.width, maximumLabelWidth), height: measured.height)
1042
- let width = ceil(padding * 2 + iconWidth + iconGap + textSize.width)
955
+ let maximumLabelWidth = max(font.pointSize * 3, availableWidth - padding * 2 - iconSlot)
956
+ let labelWidth = min(attributedLabel.size().width, maximumLabelWidth)
957
+ let width = ceil(padding * 2 + iconSlot + labelWidth)
958
let format = UIGraphicsImageRendererFormat.preferred()
959
format.opaque = false
960
let renderer = UIGraphicsImageRenderer(size: CGSize(width: width, height: height), format: format)
1046
- return renderer.image { context in
1047
- let rect = CGRect(origin: .zero, size: CGSize(width: width, height: height))
1048
- let path = UIBezierPath(roundedRect: rect.insetBy(dx: 0.5, dy: 0.5), cornerRadius: chipFontSize * 0.5)
1049
- style.backgroundColor.setFill()
1050
- path.fill()
1051
- style.borderColor.setStroke()
1052
- path.lineWidth = 1
1053
- path.stroke()
1054
-
1055
- var x = padding
961
+ return renderer.image { _ in
962
if let icon {
1057
- let renderedIcon = iconImage == nil
1058
- ? icon.withTintColor(style.tint, renderingMode: .alwaysOriginal)
1059
- : icon
1060
- renderedIcon.draw(
1061
- in: CGRect(x: x, y: (height - iconWidth) / 2, width: iconWidth, height: iconWidth)
963
+ icon.draw(
964
+ in: CGRect(x: padding, y: (height - iconSide) / 2, width: iconSide, height: iconSide)
965
)
1063
- x += iconWidth + iconGap
966
}
967
// Exactly the measured width: a spare pixel here would let a capped label draw past
968
// the cap instead of truncating inside it.
969
attributedLabel.draw(
1068
- in: CGRect(x: x, y: (height - textSize.height) / 2, width: textSize.width, height: textSize.height)
970
+ in: CGRect(x: padding + iconSlot, y: 0, width: labelWidth, height: height)
971
)
1070
- context.cgContext.setAllowsAntialiasing(true)
972
}
973
}
974
1074
- private func blend(_ accent: UIColor, over base: UIColor, weight: CGFloat) -> UIColor {
1075
- var ar: CGFloat = 0, ag: CGFloat = 0, ab: CGFloat = 0, aa: CGFloat = 0
1076
- var br: CGFloat = 0, bg: CGFloat = 0, bb: CGFloat = 0, ba: CGFloat = 0
1077
- accent.getRed(&ar, green: &ag, blue: &ab, alpha: &aa)
1078
- base.getRed(&br, green: &bg, blue: &bb, alpha: &ba)
1079
- return UIColor(
1080
- red: ar * weight + br * (1 - weight),
1081
- green: ag * weight + bg * (1 - weight),
1082
- blue: ab * weight + bb * (1 - weight),
1083
- alpha: aa * weight + ba * (1 - weight)
1084
- )
975
+ private static func tinted(_ image: UIImage, color: UIColor) -> UIImage {
976
+ let format = UIGraphicsImageRendererFormat.preferred()
977
+ format.scale = image.scale
978
+ format.opaque = false
979
+ return UIGraphicsImageRenderer(size: image.size, format: format).image { _ in
980
+ color.setFill()
981
+ UIRectFill(CGRect(origin: .zero, size: image.size))
982
+ image.draw(at: .zero, blendMode: .destinationIn, alpha: 1)
983
+ }
984
}
985
986
private func iconImage(for uri: String) -> UIImage? {
apps/mobile/src/lib/composerChipPresentation.ts
+32
-17
@@ -3,12 +3,12 @@ import { imageMimeType } from "@t3tools/shared/image";
3
import { videoMimeType } from "@t3tools/shared/video";
4
5
import type { CONTEXT_CHIP_ICON_SOURCES } from "./contextChipIcons.generated";
6
+import { resolveMarkdownFileIcon, type MarkdownFileIcon } from "./markdownLinks";
7
8
export type ContextChipIcon = keyof typeof CONTEXT_CHIP_ICON_SOURCES;
9
10
interface ContextChipPresentation {
11
readonly accent?: string;
11
- readonly symbol: string;
12
readonly icon: ContextChipIcon | null;
13
}
14
@@ -17,33 +17,29 @@ interface ContextChipPresentation {
17
* surface. See `ContextChip.tsx`.
18
*/
19
const CONTEXT_CHIP_PRESENTATIONS = {
20
- image: { accent: "#d55665", symbol: "photo", icon: "image" },
21
- video: { accent: "#d06217", symbol: "play.rectangle", icon: "film" },
22
- file: { accent: "#0090cd", symbol: "doc", icon: null },
23
- mention: { accent: "#0096af", symbol: "doc", icon: null },
24
- terminal: { accent: "#009f6e", symbol: "terminal", icon: "terminal" },
25
- element: { accent: "#b87501", symbol: "cursorarrow.click", icon: "mouse-pointer-click" },
20
+ image: { accent: "#d55665", icon: "image" },
21
+ video: { accent: "#d06217", icon: "film" },
22
+ file: { accent: "#0090cd", icon: null },
23
+ mention: { accent: "#0096af", icon: null },
24
+ terminal: { accent: "#009f6e", icon: "terminal" },
25
+ element: { accent: "#b87501", icon: "mouse-pointer-click" },
26
"preview-annotation": {
27
accent: "#b87501",
28
- symbol: "cursorarrow.click",
28
icon: "mouse-pointer-click",
29
},
31
- "review-comment": { accent: "#8a70dd", symbol: "text.bubble", icon: "message-circle" },
30
+ "review-comment": { accent: "#8a70dd", icon: "message-circle" },
31
"pull-request": {
32
accent: "#7079e4",
34
- symbol: "git-pull-request",
33
icon: "git-pull-request-arrow",
34
},
37
- skill: { accent: "#b261be", symbol: "cube", icon: "skill" },
35
+ skill: { accent: "#b261be", icon: "skill" },
36
thread: {
37
accent: "#009c96",
40
- symbol: "bubble.left.and.bubble.right",
38
icon: "messages-square",
39
},
40
} as const satisfies Record<string, ContextChipPresentation>;
41
42
export const UNAVAILABLE_CONTEXT_CHIP_PRESENTATION: ContextChipPresentation = {
46
- symbol: "doc",
43
icon: "circle-dashed",
44
};
45
@@ -56,10 +52,10 @@ export function composerChipSizeSuffix(record?: {
52
}
53
54
const PULL_REQUEST_CHIP_PRESENTATIONS = {
59
- open: { accent: "#009f6e", symbol: "git-pull-request", icon: "git-pull-request-arrow" },
60
- draft: { accent: "#7f8793", symbol: "git-pull-request", icon: "git-pull-request-draft" },
61
- merged: { accent: "#8a70dd", symbol: "git-pull-request", icon: "git-merge" },
62
- closed: { accent: "#d55665", symbol: "git-pull-request", icon: "git-pull-request-closed" },
55
+ open: { accent: "#009f6e", icon: "git-pull-request-arrow" },
56
+ draft: { accent: "#7f8793", icon: "git-pull-request-draft" },
57
+ merged: { accent: "#8a70dd", icon: "git-merge" },
58
+ closed: { accent: "#d55665", icon: "git-pull-request-closed" },
59
} as const satisfies Record<string, ContextChipPresentation>;
60
61
export function contextChipPresentation(
@@ -102,3 +98,22 @@ export function contextChipPresentation(
98
? CONTEXT_CHIP_PRESENTATIONS[presentationKind as keyof typeof CONTEXT_CHIP_PRESENTATIONS]
99
: UNAVAILABLE_CONTEXT_CHIP_PRESENTATION;
100
}
101
+
102
+export type ContextChipIconRef =
103
+ | { readonly kind: "chip"; readonly name: ContextChipIcon }
104
+ | { readonly kind: "file"; readonly name: MarkdownFileIcon };
105
+
106
+export function contextChipIcon(
107
+ presentation: Pick<ContextChipPresentation, "icon">,
108
+ fileName: string,
109
+): ContextChipIconRef {
110
+ return presentation.icon
111
+ ? { kind: "chip", name: presentation.icon }
112
+ : { kind: "file", name: resolveMarkdownFileIcon(fileName) };
113
+}
114
+
115
+export function contextRecordFileName(record?: object): string {
116
+ if (record && "path" in record && typeof record.path === "string") return record.path;
117
+ if (record && "name" in record && typeof record.name === "string") return record.name;
118
+ return "";
119
+}
apps/mobile/src/lib/contextChipIconSource.ts
new
+13
@@ -0,0 +1,13 @@
1
+import type { ContextChipIconRef } from "./composerChipPresentation";
2
+import { CONTEXT_CHIP_ICON_SOURCES } from "./contextChipIcons.generated";
3
+import { markdownFileIconSource, markdownIconAssetUri } from "./markdownFileIcons";
4
+
5
+export function contextChipIconSource(icon: ContextChipIconRef) {
6
+ return icon.kind === "chip"
7
+ ? CONTEXT_CHIP_ICON_SOURCES[icon.name]
8
+ : markdownFileIconSource(icon.name);
9
+}
10
+
11
+export function contextChipIconUri(icon: ContextChipIconRef): string | null {
12
+ return markdownIconAssetUri(contextChipIconSource(icon)) ?? null;
13
+}
apps/mobile/src/lib/enrichedLinkPresentation.ts
+5
-12
@@ -1,22 +1,19 @@
1
import type { ComposerContextRecord } from "@t3tools/contracts";
2
import {
3
composerChipSizeSuffix,
4
+ contextChipIcon,
5
contextChipPresentation,
5
- type ContextChipIcon,
6
+ contextRecordFileName,
7
+ type ContextChipIconRef,
8
UNAVAILABLE_CONTEXT_CHIP_PRESENTATION,
9
} from "./composerChipPresentation";
8
-import { resolveMarkdownFileIcon, type MarkdownFileIcon } from "./markdownLinks";
10
import type { SelectableMarkdownSkill } from "../native/SelectableMarkdownText.types";
11
import { parseComposerContextHref } from "@t3tools/shared/composerContextReferences";
12
12
-export type EnrichedChipIcon =
13
- | { readonly kind: "chip"; readonly name: ContextChipIcon }
14
- | { readonly kind: "file"; readonly name: MarkdownFileIcon };
15
-
13
export function enrichedContextLinkPresentation(
14
href: string,
15
records?: ReadonlyArray<ComposerContextRecord>,
19
-): { accent?: string; icon: EnrichedChipIcon; label?: string } | undefined {
16
+): { accent?: string; icon: ContextChipIconRef; label?: string } | undefined {
17
const reference = parseComposerContextHref(href);
18
if (!reference) return undefined;
19
const record = records?.find((candidate) => candidate.contextId === reference.contextId);
@@ -25,13 +22,9 @@ export function enrichedContextLinkPresentation(
22
? UNAVAILABLE_CONTEXT_CHIP_PRESENTATION
23
: contextChipPresentation(reference.kind, record);
24
const size = composerChipSizeSuffix(record);
28
- const fileName =
29
- record && "path" in record ? record.path : record && "name" in record ? record.name : "";
25
return {
26
...(presentation.accent ? { accent: presentation.accent } : {}),
32
- icon: presentation.icon
33
- ? { kind: "chip", name: presentation.icon }
34
- : { kind: "file", name: resolveMarkdownFileIcon(fileName) },
27
+ icon: contextChipIcon(presentation, contextRecordFileName(record)),
28
...(record && size ? { label: `${record.label} · ${size}` } : {}),
29
};
30
}
apps/mobile/src/lib/nativeComposerTheme.test.ts
-5
@@ -9,13 +9,8 @@ describe("native composer colors", () => {
9
...getMobileThemeVariables("t3-code", "light"),
10
"--color-screen": "#ffffffff",
11
"--color-composer-surface": "#ffffffff",
12
- "--color-subtle": "#0000000d",
12
"--color-placeholder": "#0000009e",
14
- "--color-inline-skill-background": "#ffffffff",
15
- "--color-inline-skill-border": "#00000080",
13
});
17
- expect(theme.chipBackground).toBe("#f2f2f2");
14
expect(theme.placeholder).toBe("#616161");
19
- expect(theme.skillBorder).toBe("#7f7f7f");
15
});
16
});
apps/mobile/src/lib/nativeComposerTheme.ts
-9
@@ -3,17 +3,8 @@ import { flattenThemeColor, type MobileThemeVariables } from "./mobileTheme";
3
/** Native chip parsers need opaque hex instead of CSS rgba or platform-specific alpha order. */
4
export function createNativeComposerTheme(theme: MobileThemeVariables) {
5
const surface = flattenThemeColor(theme["--color-composer-surface"], theme["--color-screen"]);
6
- const chipBackground = flattenThemeColor(theme["--color-subtle"], surface);
7
- const skillBackground = flattenThemeColor(theme["--color-inline-skill-background"], surface);
6
return {
7
text: flattenThemeColor(theme["--color-foreground"], surface),
8
placeholder: flattenThemeColor(theme["--color-placeholder"], surface),
11
- chipBackground,
12
- chipBorder: flattenThemeColor(theme["--color-border"], chipBackground),
13
- chipText: flattenThemeColor(theme["--color-foreground"], chipBackground),
14
- skillBackground,
15
- skillBorder: flattenThemeColor(theme["--color-inline-skill-border"], skillBackground),
16
- skillText: flattenThemeColor(theme["--color-inline-skill-foreground"], skillBackground),
17
- fileTint: flattenThemeColor(theme["--color-icon-muted"], chipBackground),
9
};
10
}
apps/mobile/src/native/T3ComposerEditor.ios.tsx
+18
-14
@@ -15,9 +15,13 @@ import {
15
import type { NativeSyntheticEvent, StyleProp, ViewProps, ViewStyle } from "react-native";
16
import { StyleSheet } from "react-native";
17
18
-import { markdownFileIconSource, markdownIconAssetUri } from "../lib/markdownFileIcons";
19
-import { composerChipSizeSuffix, contextChipPresentation } from "../lib/composerChipPresentation";
20
-import { resolveMarkdownFileIcon } from "../lib/markdownLinks";
18
+import {
19
+ composerChipSizeSuffix,
20
+ contextChipIcon,
21
+ contextChipPresentation,
22
+ contextRecordFileName,
23
+} from "../lib/composerChipPresentation";
24
+import { contextChipIconUri } from "../lib/contextChipIconSource";
25
import { useUniwindTheme } from "../lib/useUniwindTheme";
26
import { createNativeComposerTheme } from "../lib/nativeComposerTheme";
27
import { FONT_FAMILIES } from "../lib/fontFamilies";
@@ -111,10 +115,6 @@ function basename(path: string): string {
115
return separator >= 0 ? path.slice(separator + 1) : path;
116
}
117
114
-function fileIconUri(path: string): string | null {
115
- return markdownIconAssetUri(markdownFileIconSource(resolveMarkdownFileIcon(path))) ?? null;
116
-}
117
-
118
export function ComposerEditor({
119
ref,
120
skills = EMPTY_SKILLS,
@@ -168,12 +168,20 @@ export function ComposerEditor({
168
token.type === "context"
169
? props.context?.records.find((record) => record.contextId === token.contextId)
170
: undefined;
171
+ const presentation = contextChipPresentation(
172
+ token.type === "context" ? token.kind : token.type,
173
+ record,
174
+ );
175
+ const icon = contextChipIcon(
176
+ presentation,
177
+ token.type === "mention" ? token.value : contextRecordFileName(record),
178
+ );
179
return {
180
type: token.type,
181
source: token.source,
182
start: token.start,
183
end: token.end,
176
- ...contextChipPresentation(token.type === "context" ? token.kind : token.type, record),
184
+ accent: presentation.accent,
185
label:
186
token.type === "skill"
187
? (skillLabels.get(token.value) ?? token.value)
@@ -181,12 +189,8 @@ export function ComposerEditor({
189
? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}`
190
: basename(token.value),
191
detail: token.type === "context" ? composerChipSizeSuffix(record) : "",
184
- iconUri:
185
- token.type === "mention"
186
- ? fileIconUri(token.value)
187
- : record?.kind === "mention" && "path" in record
188
- ? fileIconUri(record.path)
189
- : null,
192
+ iconUri: contextChipIconUri(icon),
193
+ tintsIcon: icon.kind === "chip",
194
};
195
}),
196
);
apps/mobile/src/native/T3ComposerEditor.native.tsx
+18
-14
@@ -16,9 +16,13 @@ import {
16
import type { NativeSyntheticEvent, ViewProps } from "react-native";
17
import { Image, Platform, StyleSheet } from "react-native";
18
19
-import { markdownFileIconSource, markdownIconAssetUri } from "../lib/markdownFileIcons";
20
-import { composerChipSizeSuffix, contextChipPresentation } from "../lib/composerChipPresentation";
21
-import { resolveMarkdownFileIcon } from "../lib/markdownLinks";
19
+import {
20
+ composerChipSizeSuffix,
21
+ contextChipIcon,
22
+ contextChipPresentation,
23
+ contextRecordFileName,
24
+} from "../lib/composerChipPresentation";
25
+import { contextChipIconUri } from "../lib/contextChipIconSource";
26
import { MOBILE_TYPOGRAPHY } from "../lib/typography";
27
import { useNativePaste } from "../lib/useNativePaste";
28
import { FONT_FAMILIES } from "../lib/fontFamilies";
@@ -110,10 +114,6 @@ function basename(path: string): string {
114
return separator >= 0 ? path.slice(separator + 1) : path;
115
}
116
113
-function fileIconUri(path: string): string | null {
114
- return markdownIconAssetUri(markdownFileIconSource(resolveMarkdownFileIcon(path))) ?? null;
115
-}
116
-
117
export function ComposerEditor({
118
ref,
119
skills = EMPTY_SKILLS,
@@ -166,12 +166,20 @@ export function ComposerEditor({
166
token.type === "context"
167
? props.context?.records.find((record) => record.contextId === token.contextId)
168
: undefined;
169
+ const presentation = contextChipPresentation(
170
+ token.type === "context" ? token.kind : token.type,
171
+ record,
172
+ );
173
+ const icon = contextChipIcon(
174
+ presentation,
175
+ token.type === "mention" ? token.value : contextRecordFileName(record),
176
+ );
177
return {
178
type: token.type,
179
source: token.source,
180
start: token.start,
181
end: token.end,
174
- ...contextChipPresentation(token.type === "context" ? token.kind : token.type, record),
182
+ accent: presentation.accent,
183
label:
184
token.type === "skill"
185
? (skillLabels.get(token.value) ?? token.value)
@@ -179,12 +187,8 @@ export function ComposerEditor({
187
? `${token.label}${props.context?.records.some((record) => record.contextId === token.contextId) ? "" : " · unavailable"}`
188
: basename(token.value),
189
detail: token.type === "context" ? composerChipSizeSuffix(record) : "",
182
- iconUri:
183
- token.type === "mention"
184
- ? fileIconUri(token.value)
185
- : record?.kind === "mention" && "path" in record
186
- ? fileIconUri(record.path)
187
- : null,
190
+ iconUri: contextChipIconUri(icon),
191
+ tintsIcon: icon.kind === "chip",
192
};
193
}),
194
);
apps/mobile/src/native/useEnrichedLinkVariants.ts
+2
-8
@@ -9,6 +9,7 @@ import type { ComposerContextRecord } from "@t3tools/contracts";
9
import { contextChipPresentation } from "../lib/composerChipPresentation";
10
import { CONTEXT_CHIP_ICON_SOURCES } from "../lib/contextChipIcons.generated";
11
import { FONT_FAMILIES } from "../lib/fontFamilies";
12
+import { contextChipIconSource } from "../lib/contextChipIconSource";
13
import { markdownFileIconSource } from "../lib/markdownFileIcons";
14
import { markdownLinkIconSource } from "../lib/markdownLinkIcons";
15
import { resolveMarkdownLinkIcon, resolveMarkdownLinkPresentation } from "../lib/markdownLinks";
@@ -21,15 +22,8 @@ import {
22
enrichedLinkVariantPattern,
23
enrichedSkillDisplayName,
24
enrichedSkillReference,
24
- type EnrichedChipIcon,
25
} from "../lib/enrichedLinkPresentation";
26
27
-function chipIconSource(icon: EnrichedChipIcon) {
28
- return icon.kind === "chip"
29
- ? CONTEXT_CHIP_ICON_SOURCES[icon.name]
30
- : markdownFileIconSource(icon.name);
31
-}
32
-
27
export function enrichedContextLinkCustomization(
28
href: string,
29
records?: ReadonlyArray<ComposerContextRecord>,
@@ -38,7 +32,7 @@ export function enrichedContextLinkCustomization(
32
if (!context) return undefined;
33
return {
34
label: context.label,
41
- icon: chipIconSource(context.icon),
35
+ icon: contextChipIconSource(context.icon),
36
chip: { accent: context.accent, tintsIcon: context.icon.kind === "chip" },
37
};
38
}