@onekeyfe/react-native-native-list 6.8.0-25 → 6.8.0-26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/SPEC.md CHANGED
@@ -262,6 +262,112 @@ and Web acceptance must cover narrow addresses, dynamic type/fonts, long press
262
262
  versus tap, keyboard interaction, and the horizontal warning border. Source or
263
263
  build success alone is not runtime evidence.
264
264
 
265
+ ### Identity title segments and leading title icon
266
+
267
+ Status: implemented in source for iOS, Android and Web; device and Web runtime
268
+ acceptance is pending. These fields travel in snapshot/patch JSON and do not
269
+ change the Nitro ABI. They serve recipient rows whose title is a wallet and an
270
+ account name in two styles, and address book rows marked by an icon before the
271
+ name.
272
+
273
+ `identity.titleSegments` is an optional list of 1...4 runs drawn on the title
274
+ line in place of `title`:
275
+
276
+ ```ts
277
+ titleSegments?: readonly {
278
+ text: string;
279
+ tone?: 'primary' | 'secondary';
280
+ fontWeight?: 'regular' | 'medium';
281
+ shrink?: boolean;
282
+ maxWidthRatio?: number; // (0, 1]
283
+ }[];
284
+ ```
285
+
286
+ - `title` stays required. It is the spoken label and the fallback text, so it
287
+ carries the same words as the runs; every renderer exposes `title`, not the
288
+ runs, to accessibility.
289
+ - Each run uses the title's font family, size and line height, including
290
+ `style.title` and the configured fonts. `tone` picks the primary or secondary
291
+ text color and defaults to the title color; `fontWeight` picks the weight and
292
+ defaults to the title weight. `style.title` only supplies those defaults: its
293
+ color and weight never replace a run's own `tone` or `fontWeight`, its
294
+ `lineHeight` is the line height of the runs on every platform, and its
295
+ `offsetY` moves the runs as it moves a plain title.
296
+ - The runs share one line. `titleLines` and `style.title.lines` do not apply
297
+ to them, for drawing or for auto-height measurement; a row that draws `title`
298
+ instead (it has `titleMatch`, or is a wallet sidebar row) keeps them for
299
+ both. A hard line break inside a run's text is drawn as one space; `title`
300
+ keeps it for the spoken label.
301
+ - Layout. The runs take the whole width the title line leaves after the
302
+ leading title icon and the title accessories (title icon, title image,
303
+ suffixes, badges), so those accessories sit at the line's end rather than
304
+ right after the text. W is that width:
305
+ 1. A run's base width is its natural width, capped at `maxWidthRatio x W`
306
+ when given. A run longer than its cap truncates at the cap with a tail
307
+ ellipsis.
308
+ 2. At most one run sets `shrink: true`. Every other run keeps its base width.
309
+ 3. The shrinking run gets `min(base, W - sum of the other runs)` and
310
+ truncates with a tail ellipsis; it may shrink to zero width.
311
+ 4. When the runs that do not shrink exceed W on their own, the line is cut at
312
+ its end. Native renderers end the last visible run with an ellipsis, also
313
+ when the cut falls exactly between two runs and that run fits in full; Web
314
+ clips it. This divergence is accepted: callers avoid the case with
315
+ `maxWidthRatio`. A shrinking run that gave up its whole width (rule 3) is
316
+ not such a cut: the runs that stay whole are drawn whole. A last visible
317
+ run narrower than the ellipsis is drawn whole as well, without one.
318
+ - Development validation rejects an empty, sparse or oversized list, blank or
319
+ oversized text (the 4096-code-unit text bound), unknown keys, tones or
320
+ weights, a ratio outside `(0, 1]`, more than one shrinking run, `titleMatch`
321
+ on the same row (its ranges index `title`), and
322
+ `presentation: 'walletSidebar'`. Production treats the runs as decoration:
323
+ an invalid list is dropped and the row shows `title`. Renderers draw `title`
324
+ instead of runs on a row that also has `titleMatch`, and ignore runs and the
325
+ leading icon with `presentation: 'walletSidebar'`.
326
+
327
+ `identity.titleLeadingIcon` is an optional `{ name, tintColor?,
328
+ accessibilityLabel? }`: a 16-point glyph before the title text or its runs,
329
+ with a 6-point gap, vertically centered on the title line. It keeps its 16
330
+ points: a long title, or its shrinking run, truncates first, and auto-height
331
+ measurement takes the glyph and its gap (22 points) out of the title's width.
332
+ The name comes from the same closed icon set as `titleIcon`; an unmapped name
333
+ leaves an empty 16-point slot, without a fallback text glyph. `tintColor` is a
334
+ hex `#RRGGBB` or `#RRGGBBAA` color and defaults to the theme's `iconSubdued`.
335
+ With `accessibilityLabel` the glyph is an image with that name; without it the
336
+ glyph is hidden from accessibility. It has no action of its own. Development validation
337
+ rejects unknown keys, a blank name, a malformed color and
338
+ `presentation: 'walletSidebar'`; production drops an invalid icon.
339
+
340
+ Both fields are identity patch fields. Omission in a full snapshot removes
341
+ them; omission in a patch keeps the current value. Development validation of a
342
+ patch on its own (`validatePatches`, `serializePatches`) applies the rules above
343
+ to a replacement. A production patch that
344
+ supplies an invalid replacement clears the current value, as `null` does, so
345
+ the row shows `title` instead of stale runs or a stale glyph. Rebind and recycle remove the
346
+ previous runs and glyph, so a row without them never shows another row's.
347
+
348
+ | Platform | Title runs | Leading icon |
349
+ | --- | --- | --- |
350
+ | iOS | `NativeListTitleSegmentsLabel` draws the runs at the widths that `NativeListTitleSegmentLayout.swift` allocates for its bounds; it fills the title row in place of the row's filler. | `NativeListIconImageView`, first in the title row stack. A plain title beside it drops below required horizontal compression resistance, as beside `titleIcon`, so the title truncates before the glyph narrows. |
351
+ | Android | `NativeListTitleSegmentsView`, a `TextView` whose text is rebuilt for each measured width from `NativeListTitleSegmentLayout.kt` and which measures to the width it is offered. `PackedTitleLineLayout.titleChild` names the flexible title view, so a view may precede it. | `OneKeyIconView`, first in the title line. |
352
+ | Web | A flex line that fills the title line: the shrinking run is `flex: 0 1 auto; min-width: 0`, the others `flex: 0 0 auto` with a `max-width` percentage for a cap. | An icon element before the title text. |
353
+
354
+ Conformance: `src/validation.ts` (`assertTitleSegments`,
355
+ `assertTitleLeadingIcon`, production sanitizing) with `validation.test.ts`;
356
+ `src/web/templates/IdentityRowRenderer.ts` with `IdentityTitleRunsWeb.test.ts`
357
+ (run styles under `style.title`, flex behavior, glyph size, accessibility,
358
+ rebind); `ios/NativeListTitleSegmentLayout.swift` (run widths, the overflow
359
+ ellipsis, one-line run text, the glyph's width, and when the runs and the
360
+ glyph show) with the SwiftPM `NativeListTitleSegmentLayoutTests`; the iOS
361
+ leading glyph beside a long plain title, wallet sidebar rows and the measured
362
+ height of a highlighted title, of a title beside the glyph and of runs with the
363
+ XCTest
364
+ `NativeListIdentityTitleIconLayoutTests`; `NativeListTitleSegmentLayout.kt`
365
+ with the JVM `NativeListTitleSegmentLayoutTest`. iOS, Android and Web runtime acceptance is pending: recipient rows
366
+ with long wallet and account names in both truncation directions, narrow rows,
367
+ dark mode, dynamic type and configured fonts, and recycling between rows with
368
+ and without runs or a leading icon. Source and build success alone are not
369
+ runtime evidence.
370
+
265
371
  ### DeFi position row controls
266
372
 
267
373
  Status: implemented in local source, unreleased. Focused validation and Web
@@ -1964,7 +2070,12 @@ secondary text and subscript text segments. Segments are caller-formatted and
1964
2070
  never interpreted as numbers by NativeList. Omission preserves the legacy tree.
1965
2071
 
1966
2072
  `presentation: stacked` places the amount lines after the identity column and
1967
- right-aligns them. In `table`, the identity column includes its leading visual
2073
+ right-aligns them. In stacked presentation the amount and fee columns stay at
2074
+ the width of their content (the widest amount line, including its 20pt icon);
2075
+ the identity column receives the remaining row width. That is the width measure
2076
+ uses for the description, on iOS and Android. On iOS the stacked amount column is
2077
+ capped at half the row width; amounts wider than that truncate.
2078
+ In `table`, the identity column includes its leading visual
1968
2079
  and gap; identity, amounts and an optional fee use stable widths in the ratio
1969
2080
  `1:1:0.6` after inter-column gaps. Without fee, identity and amounts split the
1970
2081
  remaining width equally. Amount content stays start-aligned within its column;
@@ -1973,7 +2084,14 @@ Fee text length and leading visibility cannot move the amount column. These
1973
2084
  native widths match the existing Web table layout. Fee supports label, primary
1974
2085
  and secondary text with their own segments; `hidden` preserves its column width
1975
2086
  while hiding its contents. Up to four uniquely keyed title badges use declared
1976
- tones. `descriptionActionKey` makes only the description emit that action with
2087
+ tones. The badges follow the title text directly: while badges are present the
2088
+ title line packs at its start on every platform (iOS a slack-absorbing filler at
2089
+ the end of the title line, Android `PackedTitleLineLayout`, Web flex `0 1 auto`),
2090
+ so badges never drift toward the amount column, and a long title truncates in
2091
+ front of its badges (OK-67497). On iOS and Android the title spans the line when
2092
+ there are no badges, so `style.title.alignment` keeps its effect and a recycled row
2093
+ that lost its badges lays out the title exactly like a fresh row; Web keeps its
2094
+ existing content-sized title element. `descriptionActionKey` makes only the description emit that action with
1977
2095
  source `description`; footer actions retain their existing source/keys and
1978
2096
  disabled behavior. All actions use the current host binding epoch.
1979
2097
 
@@ -2043,7 +2161,8 @@ legacy Activity rows without rich fields retain their existing behavior.
2043
2161
 
2044
2162
  Acceptance: 1/2/6/32 lines, changed keys/order, paired icons and network overlays,
2045
2163
  small-number segments in amounts/fiat/fee (ceil(0.6 × parent font size), unchanged
2046
- baseline, matching NumberSizeableText), badge tone/clear, hidden fee, disabled
2164
+ baseline, matching NumberSizeableText), badge tone/clear, badges adjacent to
2165
+ short and truncated long titles, centered title after badges are cleared, hidden fee, disabled
2047
2166
  actions, description anchors, dark/RTL/font scaling and explicit/automatic height.
2048
2167
 
2049
2168
 
@@ -9,6 +9,9 @@ final class NativeListActivityDetailsView: UIStackView {
9
9
  private let identity = UIStackView()
10
10
  private let titleLine = UIStackView()
11
11
  private let title = NativeListTextLabel()
12
+ // Absorbs the title line's slack so the title keeps its intrinsic width and the
13
+ // badges follow the text (OK-67497), matching the identity title row (OK-66041).
14
+ private let titleLineFiller = NativeListTitleLineFiller()
12
15
  private let descriptionLabel = NativeListTextLabel()
13
16
  private let status = NativeListTextLabel()
14
17
  private let amounts = UIStackView()
@@ -32,6 +35,9 @@ final class NativeListActivityDetailsView: UIStackView {
32
35
  identity.axis = .vertical; identity.alignment = .fill
33
36
  titleLine.axis = .horizontal; titleLine.alignment = .center; titleLine.spacing = 4
34
37
  titleLine.addArrangedSubview(title)
38
+ titleLine.addArrangedSubview(titleLineFiller)
39
+ titleLineFiller.setContentHuggingPriority(UILayoutPriority(1), for: .horizontal)
40
+ titleLineFiller.setContentCompressionResistancePriority(UILayoutPriority(1), for: .horizontal)
35
41
  [titleLine, descriptionLabel, status].forEach(identity.addArrangedSubview)
36
42
  amounts.axis = .vertical; amounts.alignment = .fill; amounts.spacing = 2
37
43
  fee.axis = .vertical; fee.alignment = .trailing
@@ -47,6 +53,11 @@ final class NativeListActivityDetailsView: UIStackView {
47
53
  title.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
48
54
  identity.setContentHuggingPriority(.defaultLow, for: .horizontal)
49
55
  amounts.setContentHuggingPriority(.defaultHigh, for: .horizontal)
56
+ // Stacked fee text hugs, or the fee column takes the row's slack the same
57
+ // way an unpinned amount column does. Required table widths still win.
58
+ [feeLabel, feePrimary, feeSecondary].forEach {
59
+ $0.setContentHuggingPriority(.defaultHigh, for: .horizontal)
60
+ }
50
61
  }
51
62
  required init(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
52
63
 
@@ -215,10 +226,12 @@ final class NativeListActivityDetailsView: UIStackView {
215
226
  ).bind(label)
216
227
  label.backgroundColor = tint.withAlphaComponent(0.1)
217
228
  label.layer.cornerRadius = 4; label.clipsToBounds = true
229
+ label.setContentHuggingPriority(.required, for: .horizontal)
218
230
  label.setContentCompressionResistancePriority(.required, for: .horizontal)
219
- titleLine.addArrangedSubview(label)
231
+ titleLine.insertArrangedSubview(label, at: titleLine.arrangedSubviews.count - 1)
220
232
  return label
221
233
  }
234
+ settleTitleLineFillerSpacing()
222
235
  let descriptors = data.dictionaries("amounts")
223
236
  while amountViews.count > descriptors.count {
224
237
  let view = amountViews.removeLast(); view.recycle(); amounts.removeArrangedSubview(view); view.removeFromSuperview()
@@ -269,6 +282,19 @@ final class NativeListActivityDetailsView: UIStackView {
269
282
  actions.arrangedSubviews.filter { !($0 is UIButton) }.forEach { actions.removeArrangedSubview($0); $0.removeFromSuperview() }
270
283
  actions.addArrangedSubview(spacer)
271
284
  }
285
+ /// The filler only packs the title while badges follow it. Without badges it is
286
+ /// hidden so the title keeps the whole line and its `alignment` (center/end) still
287
+ /// applies, exactly as before badges existed. With badges, the stack's spacing
288
+ /// before the filler would shave `titleBadgeGap` off the title, so that gap is zero.
289
+ private func settleTitleLineFillerSpacing() {
290
+ titleLineFiller.isHidden = badgeViews.isEmpty
291
+ // With badges the filler, not the title, takes the column's leftover width.
292
+ // Without badges the title has to be free to span the line so center/end
293
+ // alignment still applies.
294
+ title.setContentHuggingPriority(badgeViews.isEmpty ? .defaultLow : .defaultHigh, for: .horizontal)
295
+ if let last = badgeViews.last { titleLine.setCustomSpacing(0, after: last) }
296
+ }
297
+
272
298
  @objc private func descriptionPressed() { onAction?(descriptionKey, descriptionLabel, "description", 0) }
273
299
  @objc private func actionPressed(_ button: UIButton) {
274
300
  guard button.tag < actionKeys.count else { return }
@@ -280,6 +306,15 @@ final class NativeListActivityDetailsView: UIStackView {
280
306
  }
281
307
  }
282
308
 
309
+ /// Horizontal spacer with intrinsic width 0. Hugging and compression are 1, so
310
+ /// once the title line is as wide as the identity column the leftover width
311
+ /// lands here, after the badge (OK-67497).
312
+ private final class NativeListTitleLineFiller: UIView {
313
+ override var intrinsicContentSize: CGSize {
314
+ CGSize(width: 0, height: UIView.noIntrinsicMetric)
315
+ }
316
+ }
317
+
283
318
  private func nativeListActivityTone(_ tone: String, theme: [String: Any]?) -> UIColor {
284
319
  switch tone {
285
320
  case "positive", "success": return nativeListColor(theme, "positive", "#218358")
@@ -301,6 +336,15 @@ private final class NativeListActivityAmountView: UIStackView {
301
336
  super.init(frame: frame)
302
337
  axis = .horizontal; alignment = .center; spacing = 6
303
338
  labels.axis = .vertical; labels.alignment = .fill
339
+ // Stacked amounts stay at their text width (Android WRAP_CONTENT). The
340
+ // identity column then receives the leftover row width, which is what
341
+ // measure() already assumes. Table columns ignore this: their required
342
+ // widths win (OK-67497). Hugging stays below the labels' compression
343
+ // resistance (750): all amount labels share the column width, so a short
344
+ // label must never squeeze a longer one.
345
+ [primary, secondary].forEach {
346
+ $0.setContentHuggingPriority(UILayoutPriority(749), for: .horizontal)
347
+ }
304
348
  [primary, secondary].forEach(labels.addArrangedSubview)
305
349
  [visual, labels].forEach(addArrangedSubview)
306
350
  NSLayoutConstraint.activate([visual.widthAnchor.constraint(equalToConstant: 20), visual.heightAnchor.constraint(equalToConstant: 20)])
@@ -50,6 +50,13 @@ final class NativeListIdentityCell: NativeListRendererCell {
50
50
  stack.spacing = 10
51
51
  }
52
52
  }
53
+ private class func showsTitleRuns(_ item: NativeListItem) -> Bool {
54
+ nativeListShowsTitleRuns(
55
+ presentation: item.data.string("presentation"),
56
+ runCount: item.data.dictionaries("titleSegments").count,
57
+ matchCount: item.data.dictionaries("titleMatch").count)
58
+ }
59
+
53
60
  private static func collapsesTrailing(_ item: NativeListItem, descriptors: [[String: Any]]) -> Bool {
54
61
  descriptors.isEmpty && item.data.bool("collapseEmptyTrailing")
55
62
  }
@@ -84,10 +91,14 @@ final class NativeListIdentityCell: NativeListRendererCell {
84
91
  let textWidth = max(1,
85
92
  width - horizontalPadding - rightInset - imageWidth - leadingGap
86
93
  - trailingSize.width - contentGap)
94
+ // Runs are one line whatever `style.title.lines` asks for.
95
+ let showsRuns = showsTitleRuns(item)
96
+ var titleStyle = style.dictionary("title")
97
+ if showsRuns { titleStyle?.removeValue(forKey: "lines") }
87
98
  let title = NativeListResolvedText(
88
- item.data.string("title"), style: style.dictionary("title"), size: 16,
99
+ item.data.string("title"), style: titleStyle, size: 16,
89
100
  weight: .medium, color: .black, lineHeight: 24,
90
- lines: item.data.int("titleLines", default: 1), fonts: fonts)
101
+ lines: showsRuns ? 1 : item.data.int("titleLines", default: 1), fonts: fonts)
91
102
  let subtitle = NativeListResolvedText(
92
103
  item.data.string("subtitle"), style: style.dictionary("subtitle"), size: 14,
93
104
  color: .black, lineHeight: 20,
@@ -100,7 +111,13 @@ final class NativeListIdentityCell: NativeListRendererCell {
100
111
  let titleImageSize = CGFloat(item.data.dictionary("titleImage")?.dictionary("image")?.double("width") ?? 0)
101
112
  let titleImageWidth = titleImageSize > 0
102
113
  ? titleImageSize + 4 + (item.data.dictionary("titleIcon") == nil ? 0 : 24) : 0
103
- let textHeight = max(titleImageSize, title.measure(width: max(1, textWidth - titleImageWidth)))
114
+ // The leading glyph and its gap come out of the title's width as well.
115
+ let titleLeadingIconWidth = nativeListTitleLeadingIconWidth(
116
+ presentation: item.data.string("presentation"),
117
+ hasIcon: item.data.dictionary("titleLeadingIcon") != nil)
118
+ let textHeight = max(
119
+ titleImageSize,
120
+ title.measure(width: max(1, textWidth - titleImageWidth - titleLeadingIconWidth)))
104
121
  + subtitle.measure(width: textWidth)
105
122
  + tertiary.measure(width: textWidth)
106
123
  + (item.data.string("subtitle").isEmpty ? 0 : CGFloat(style.double("lineGap", default: 0)))
@@ -116,6 +133,12 @@ final class NativeListIdentityCell: NativeListRendererCell {
116
133
  private let titleRowStack = UIStackView()
117
134
  private let titleLabel = NativeListTextLabel()
118
135
  private let titleIconView = NativeListIconImageView()
136
+ private let titleLeadingIconView = NativeListIconImageView()
137
+ private let titleSegmentsLabel = NativeListTitleSegmentsLabel()
138
+ // Runs, when shown, take the title's place in the title row.
139
+ private var visibleTitleView: UIView {
140
+ titleSegmentsLabel.isHidden ? titleLabel : titleSegmentsLabel
141
+ }
119
142
  private var titleImageSlot: NativeListImageSlot?
120
143
  private var titleImageConstraints: [NSLayoutConstraint] = []
121
144
  // Absorbs the title row's slack so the title keeps its intrinsic width and the
@@ -176,7 +199,9 @@ final class NativeListIdentityCell: NativeListRendererCell {
176
199
  mainStack.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
177
200
  titleRowStack.axis = .horizontal
178
201
  titleRowStack.alignment = .center
202
+ titleRowStack.addArrangedSubview(titleLeadingIconView)
179
203
  titleRowStack.addArrangedSubview(titleLabel)
204
+ titleRowStack.addArrangedSubview(titleSegmentsLabel)
180
205
  titleRowStack.addArrangedSubview(titleIconView)
181
206
  titleRowStack.addArrangedSubview(badgeLabel)
182
207
  titleRowStack.addArrangedSubview(titleRowFiller)
@@ -191,6 +216,18 @@ final class NativeListIdentityCell: NativeListRendererCell {
191
216
  ])
192
217
  titleIconView.contentMode = .scaleAspectFit
193
218
  titleIconView.setContentHuggingPriority(.required, for: .horizontal)
219
+ titleLeadingIconView.translatesAutoresizingMaskIntoConstraints = false
220
+ let leadingIconWidth = titleLeadingIconView.widthAnchor.constraint(equalToConstant: 16)
221
+ leadingIconWidth.priority = UILayoutPriority(999)
222
+ NSLayoutConstraint.activate([
223
+ leadingIconWidth,
224
+ titleLeadingIconView.heightAnchor.constraint(equalToConstant: 16),
225
+ ])
226
+ titleLeadingIconView.contentMode = .scaleAspectFit
227
+ titleLeadingIconView.setContentHuggingPriority(.required, for: .horizontal)
228
+ titleLeadingIconView.setContentCompressionResistancePriority(.required, for: .horizontal)
229
+ titleLeadingIconView.isHidden = true
230
+ titleSegmentsLabel.isHidden = true
194
231
  [titleRowStack, subtitleLabel, tertiaryLabel].forEach(mainStack.addArrangedSubview)
195
232
  visual.translatesAutoresizingMaskIntoConstraints = false
196
233
  leadingWidth = visual.widthAnchor.constraint(equalToConstant: 40)
@@ -255,6 +292,15 @@ final class NativeListIdentityCell: NativeListRendererCell {
255
292
  titleIconView.isHidden = true
256
293
  titleIconView.accessibilityLabel = nil
257
294
  titleIconView.isAccessibilityElement = false
295
+ titleLeadingIconView.image = nil
296
+ titleLeadingIconView.isHidden = true
297
+ titleLeadingIconView.accessibilityLabel = nil
298
+ titleLeadingIconView.isAccessibilityElement = false
299
+ titleSegmentsLabel.isHidden = true
300
+ titleSegmentsLabel.reset()
301
+ titleRowFiller.isHidden = false
302
+ titleSegmentsLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
303
+ titleSegmentsLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
258
304
  titleImageSlot?.view.isHidden = true
259
305
  titleImageSlot?.view.accessibilityLabel = nil
260
306
  titleImageSlot?.view.isAccessibilityElement = false
@@ -317,7 +363,7 @@ final class NativeListIdentityCell: NativeListRendererCell {
317
363
  let label = titleIcon.string("accessibilityLabel")
318
364
  titleIconView.accessibilityLabel = label.isEmpty ? nil : label
319
365
  titleIconView.isAccessibilityElement = !label.isEmpty
320
- titleRowStack.setCustomSpacing(4, after: titleLabel)
366
+ titleRowStack.setCustomSpacing(4, after: visibleTitleView)
321
367
  // A long title truncates ahead of the 20pt glyph (Android caps the title
322
368
  // at row width minus accessories, Web shrinks the title first); with the
323
369
  // title at `.required` the icon's 999 width constraint collapsed to 0.
@@ -349,7 +395,7 @@ final class NativeListIdentityCell: NativeListRendererCell {
349
395
  ]
350
396
  titleImageConstraints.forEach { $0.priority = UILayoutPriority(999) }
351
397
  NSLayoutConstraint.activate(titleImageConstraints)
352
- titleRowStack.setCustomSpacing(4, after: titleIconView.isHidden ? titleLabel : titleIconView)
398
+ titleRowStack.setCustomSpacing(4, after: titleIconView.isHidden ? visibleTitleView : titleIconView)
353
399
  titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
354
400
  slot.bind(image, key: "\(item.key):titleImage", placeholder: "#00000000")
355
401
  } else {
@@ -392,7 +438,7 @@ final class NativeListIdentityCell: NativeListRendererCell {
392
438
  titleRowStack.setCustomSpacing(
393
439
  CGFloat(style.double("titleBadgeGap")),
394
440
  after: titleImageSlot?.view.isHidden == false ? titleImageSlot!.view
395
- : titleIconView.isHidden ? titleLabel : titleIconView)
441
+ : titleIconView.isHidden ? visibleTitleView : titleIconView)
396
442
  }
397
443
  }
398
444
  if style["trailingGap"] != nil { trailingStack.spacing = CGFloat(style.double("trailingGap")) }
@@ -406,6 +452,7 @@ final class NativeListIdentityCell: NativeListRendererCell {
406
452
  labels.forEach { NativeListTextStyles.applyStyledText($0, text, fonts: fonts) }
407
453
  }
408
454
  }
455
+ if !titleSegmentsLabel.isHidden { bindTitleRuns(item, theme: theme, style: style) }
409
456
  contentInsets = UIEdgeInsets(
410
457
  top: topInset, left: leftInset, bottom: -bottomInset, right: -rightInset)
411
458
  if root.axis == .horizontal {
@@ -471,6 +518,8 @@ final class NativeListIdentityCell: NativeListRendererCell {
471
518
  }
472
519
  override func recycleContent() {
473
520
  visual.recycle()
521
+ titleSegmentsLabel.reset()
522
+ titleLeadingIconView.image = nil
474
523
  titleImageSlot?.recycle()
475
524
  titleImageSlot?.view.isHidden = true
476
525
  titleImageSlot?.view.accessibilityLabel = nil
@@ -519,6 +568,32 @@ final class NativeListIdentityCell: NativeListRendererCell {
519
568
  visual.bind(
520
569
  descriptor, style: image, key: key, theme: currentTheme, isUnread: false, fonts: fonts)
521
570
  }
571
+ // Runs take the title's font, size and color unless they set their own tone
572
+ // or weight; the title label holds the final style by now.
573
+ private func bindTitleRuns(_ item: NativeListItem, theme: [String: Any]?, style: [String: Any]) {
574
+ let baseFont = titleLabel.font ?? nativeListFont(ofSize: 16, weight: .medium, fonts: fonts)
575
+ let baseColor = titleLabel.textColor ?? nativeListColor(theme, "primaryText", "#202020")
576
+ let runs = item.data.dictionaries("titleSegments").map { segment in
577
+ let weight = segment.string("fontWeight")
578
+ let tone = segment.string("tone")
579
+ return NativeListTitleSegmentsLabel.Run(
580
+ text: nativeListTitleRunText(segment.string("text")),
581
+ font: weight.isEmpty
582
+ ? baseFont
583
+ : nativeListFont(
584
+ ofSize: baseFont.pointSize, weight: weight == "medium" ? .medium : .regular, fonts: fonts),
585
+ color: tone == "secondary"
586
+ ? nativeListColor(theme, "secondaryText", "#646464")
587
+ : tone == "primary" ? nativeListColor(theme, "primaryText", "#202020") : baseColor,
588
+ shrink: segment.bool("shrink"),
589
+ maxWidthRatio: (segment["maxWidthRatio"] as? NSNumber).map { CGFloat(truncating: $0) })
590
+ }
591
+ titleSegmentsLabel.lineHeight = CGFloat(
592
+ style.dictionary("title")?.double("lineHeight", default: 24) ?? 24)
593
+ titleSegmentsLabel.offsetY = CGFloat(
594
+ style.dictionary("title")?.double("offsetY", default: 0) ?? 0)
595
+ titleSegmentsLabel.configure(runs, spokenText: item.data.string("title"))
596
+ }
522
597
  private func bindIdentity(
523
598
  _ item: NativeListItem,
524
599
  theme: [String: Any]?,
@@ -650,10 +725,36 @@ final class NativeListIdentityCell: NativeListRendererCell {
650
725
  }
651
726
  root.addArrangedSubview(mainStack)
652
727
  show(titleLabel, item.data.string("title"), lines: item.data.int("titleLines", default: 1))
728
+ // Runs replace the drawn title except where `title` itself is highlighted;
729
+ // their content is bound once the title style is final.
730
+ if Self.showsTitleRuns(item) {
731
+ titleLabel.isHidden = true
732
+ titleSegmentsLabel.isHidden = false
733
+ // The runs fill what the title row leaves, so a ratio is a share of it.
734
+ titleRowFiller.isHidden = true
735
+ titleSegmentsLabel.setContentHuggingPriority(UILayoutPriority(1), for: .horizontal)
736
+ }
737
+ if nativeListAllowsTitleDecorations(presentation: item.data.string("presentation")),
738
+ let icon = item.data.dictionary("titleLeadingIcon")
739
+ {
740
+ titleLeadingIconView.bindIcon(icon.string("name"))
741
+ titleLeadingIconView.tintColor = UIColor(
742
+ nativeListHex: icon.string("tintColor",
743
+ default: theme?["iconSubdued"] as? String ?? "#8D8D8D"),
744
+ fallback: .gray)
745
+ titleLeadingIconView.isHidden = false
746
+ let label = icon.string("accessibilityLabel")
747
+ titleLeadingIconView.accessibilityLabel = label.isEmpty ? nil : label
748
+ titleLeadingIconView.isAccessibilityElement = !label.isEmpty
749
+ titleRowStack.setCustomSpacing(6, after: titleLeadingIconView)
750
+ // As beside `titleIcon`: a plain title left at `.required` collapses the
751
+ // glyph's 999 width constraint instead of truncating.
752
+ titleLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
753
+ }
653
754
  let titleSuffixes = item.data.dictionaries("titleSuffixes")
654
755
  if !titleSuffixes.isEmpty {
655
- titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
656
- let minimumTitleWidth = titleLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 80)
756
+ visibleTitleView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
757
+ let minimumTitleWidth = visibleTitleView.widthAnchor.constraint(greaterThanOrEqualToConstant: 80)
657
758
  minimumTitleWidth.priority = UILayoutPriority(751)
658
759
  minimumTitleWidth.isActive = true
659
760
  selectorConstraints.append(minimumTitleWidth)
@@ -0,0 +1,72 @@
1
+ import Foundation
2
+
3
+ /// Widths for identity title runs (docs/SPEC.md, "Identity title segments
4
+ /// and leading title icon"). A run's base width is its natural width under
5
+ /// its cap; the one shrinking run takes what the others leave; runs that do
6
+ /// not shrink and still overflow are cut from the end of the line.
7
+ func nativeListTitleSegmentWidths(
8
+ natural: [CGFloat],
9
+ shrink: [Bool],
10
+ maxWidthRatio: [CGFloat?],
11
+ available: CGFloat
12
+ ) -> [CGFloat] {
13
+ let line = max(0, available)
14
+ var widths = natural.indices.map { index -> CGFloat in
15
+ let width = max(0, natural[index])
16
+ guard index < maxWidthRatio.count, let ratio = maxWidthRatio[index] else { return width }
17
+ return min(width, ratio * line)
18
+ }
19
+ if let shrinking = shrink.firstIndex(of: true), shrinking < widths.count {
20
+ let others = widths.indices.reduce(CGFloat(0)) { $1 == shrinking ? $0 : $0 + widths[$1] }
21
+ widths[shrinking] = max(0, min(widths[shrinking], line - others))
22
+ }
23
+ var overflow = widths.reduce(0, +) - line
24
+ var index = widths.count - 1
25
+ while overflow > 0, index >= 0 {
26
+ let cut = min(widths[index], overflow)
27
+ widths[index] -= cut
28
+ overflow -= cut
29
+ index -= 1
30
+ }
31
+ return widths
32
+ }
33
+
34
+ /// Wallet sidebar rows keep the plain title: no runs and no leading glyph
35
+ /// (docs/SPEC.md).
36
+ func nativeListAllowsTitleDecorations(presentation: String) -> Bool {
37
+ presentation != "walletSidebar"
38
+ }
39
+
40
+ /// Whether the runs replace `title`: not where `title` itself is highlighted
41
+ /// (`titleMatch` indexes it), and never on a wallet sidebar row. Measurement
42
+ /// and binding share this, so a row is sized for the title it draws.
43
+ func nativeListShowsTitleRuns(presentation: String, runCount: Int, matchCount: Int) -> Bool {
44
+ runCount > 0 && matchCount == 0 && nativeListAllowsTitleDecorations(presentation: presentation)
45
+ }
46
+
47
+ /// The run that has to end with an ellipsis because the line was cut exactly
48
+ /// after it: a run that does not shrink lost its whole width, and this one is
49
+ /// drawn in full, so nothing else would show that the title goes on. Nil when
50
+ /// the line is not cut, when it is cut inside a run, which then truncates by
51
+ /// itself, and when only the shrinking run gave up its width, which it may.
52
+ func nativeListTitleSegmentOverflowIndex(
53
+ natural: [CGFloat], shrink: [Bool], widths: [CGFloat]
54
+ ) -> Int? {
55
+ guard let last = widths.lastIndex(where: { $0 > 0 }), last < natural.count else { return nil }
56
+ let cutsAFixedRun = natural.indices.contains {
57
+ $0 > last && natural[$0] > 0 && !($0 < shrink.count && shrink[$0])
58
+ }
59
+ return cutsAFixedRun && widths[last] >= natural[last] ? last : nil
60
+ }
61
+
62
+ /// Width the leading title glyph takes from the title text: its 16 points and
63
+ /// the 6-point gap. Zero where the row keeps a plain title (docs/SPEC.md).
64
+ func nativeListTitleLeadingIconWidth(presentation: String, hasIcon: Bool) -> CGFloat {
65
+ hasIcon && nativeListAllowsTitleDecorations(presentation: presentation) ? 22 : 0
66
+ }
67
+
68
+ /// A run is one line: a hard break folds into one space, as in a one-line
69
+ /// title.
70
+ func nativeListTitleRunText(_ text: String) -> String {
71
+ text.replacingOccurrences(of: "\r\n|[\r\n]", with: " ", options: .regularExpression)
72
+ }
@@ -0,0 +1,92 @@
1
+ import UIKit
2
+
3
+ /// Draws identity title runs on one line, each at the width that
4
+ /// `nativeListTitleSegmentWidths` allocates for the current bounds. Its
5
+ /// intrinsic width is the runs' natural width, so the title row stack can
6
+ /// give it less, and accessibility reads the row's `title` instead of runs.
7
+ final class NativeListTitleSegmentsLabel: UIView {
8
+ struct Run {
9
+ let text: String
10
+ let font: UIFont
11
+ let color: UIColor
12
+ let shrink: Bool
13
+ let maxWidthRatio: CGFloat?
14
+ }
15
+
16
+ private var runs: [Run] = []
17
+ private var naturalWidths: [CGFloat] = []
18
+ var lineHeight: CGFloat = 24 {
19
+ didSet {
20
+ invalidateIntrinsicContentSize()
21
+ setNeedsDisplay()
22
+ }
23
+ }
24
+ /// `style.title.offsetY`: moves the drawn runs, not the line they sit on.
25
+ var offsetY: CGFloat = 0 { didSet { setNeedsDisplay() } }
26
+
27
+ override init(frame: CGRect) {
28
+ super.init(frame: frame)
29
+ isOpaque = false
30
+ backgroundColor = .clear
31
+ contentMode = .redraw
32
+ isAccessibilityElement = true
33
+ accessibilityTraits = .staticText
34
+ }
35
+
36
+ required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
37
+
38
+ func configure(_ runs: [Run], spokenText: String) {
39
+ self.runs = runs
40
+ naturalWidths = runs.map {
41
+ ceil(($0.text as NSString).size(withAttributes: [.font: $0.font]).width)
42
+ }
43
+ accessibilityLabel = spokenText.isEmpty ? nil : spokenText
44
+ invalidateIntrinsicContentSize()
45
+ setNeedsDisplay()
46
+ }
47
+
48
+ func reset() {
49
+ offsetY = 0
50
+ configure([], spokenText: "")
51
+ }
52
+
53
+ override var intrinsicContentSize: CGSize {
54
+ CGSize(width: naturalWidths.reduce(0, +), height: lineHeight)
55
+ }
56
+
57
+ override func layoutSubviews() {
58
+ super.layoutSubviews()
59
+ setNeedsDisplay()
60
+ }
61
+
62
+ override func draw(_ rect: CGRect) {
63
+ guard !runs.isEmpty else { return }
64
+ let widths = nativeListTitleSegmentWidths(
65
+ natural: naturalWidths, shrink: runs.map(\.shrink),
66
+ maxWidthRatio: runs.map(\.maxWidthRatio), available: bounds.width)
67
+ let overflowing = nativeListTitleSegmentOverflowIndex(
68
+ natural: naturalWidths, shrink: runs.map(\.shrink), widths: widths)
69
+ let isRightToLeft = effectiveUserInterfaceLayoutDirection == .rightToLeft
70
+ var x: CGFloat = isRightToLeft ? bounds.width : 0
71
+ for (index, (run, width)) in zip(runs, widths).enumerated() where width > 0 {
72
+ let paragraph = NSMutableParagraphStyle()
73
+ paragraph.lineBreakMode = .byTruncatingTail
74
+ // Text after this run is hidden: one character more than fits makes the
75
+ // tail truncation show it. A run too narrow for the ellipsis itself is
76
+ // drawn whole instead.
77
+ let ellipsis = "\u{2026}"
78
+ let showsOverflow = index == overflowing
79
+ && width >= ceil((ellipsis as NSString).size(withAttributes: [.font: run.font]).width)
80
+ let text = NSAttributedString(
81
+ string: showsOverflow ? run.text + ellipsis : run.text,
82
+ attributes: [.font: run.font, .foregroundColor: run.color, .paragraphStyle: paragraph])
83
+ let height = ceil(run.font.lineHeight)
84
+ let originX = isRightToLeft ? x - width : x
85
+ text.draw(
86
+ with: CGRect(
87
+ x: originX, y: (bounds.height - height) / 2 + offsetY, width: width, height: height),
88
+ options: [.usesLineFragmentOrigin, .truncatesLastVisibleLine], context: nil)
89
+ x += isRightToLeft ? -width : width
90
+ }
91
+ }
92
+ }