@@ -49,6 +49,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
49
49
50
50
icon = content. icon
51
51
iconDisabled = content. iconDisabled
52
+ iconWidth = content. iconWidth
52
53
53
54
details = content. details
54
55
@@ -102,6 +103,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
102
103
var title : Title ?
103
104
var icon : Icon ?
104
105
var iconDisabled : Bool = false
106
+ var iconWidth : CGFloat ?
105
107
106
108
var details : [ SegmentViewItem ] ?
107
109
@@ -127,7 +129,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
127
129
return view
128
130
} ( )
129
131
130
- private let iconSize : CGSize = CGSize ( width: 40 , height: 40 )
132
+ static public let defaultIconSize : CGSize = CGSize ( width: 40 , height: 40 )
131
133
public let thumbnailSize : CGSize = CGSize ( width: 60 , height: 60 ) // when changing size, also update .iconView.fallbackSize
132
134
open var iconView : ResourceViewHost = ResourceViewHost ( fallbackSize: CGSize ( width: 60 , height: 60 ) ) // when changing size, also update .thumbnailSize
133
135
@@ -306,10 +308,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
306
308
hasSecondaryDetailView = false
307
309
}
308
310
311
+ let iconWidthConstraint = updateIconWidth ( content? . iconWidth, defaultWidth: ( iconViewHeight / 0.75 ) ) // 4:3
312
+
309
313
constraints = [
310
314
iconView. leadingAnchor. constraint ( equalTo: self . contentView. leadingAnchor, constant: horizontalMargin) ,
311
315
iconView. trailingAnchor. constraint ( equalTo: titleLabel. leadingAnchor, constant: - spacing) ,
312
- iconView . widthAnchor . constraint ( equalToConstant : floor ( iconViewHeight / 0.75 ) ) , // 4:3
316
+ iconWidthConstraint ,
313
317
iconView. heightAnchor. constraint ( equalToConstant: iconViewHeight) ,
314
318
iconView. topAnchor. constraint ( equalTo: self . contentView. topAnchor, constant: verticalIconMargin) ,
315
319
iconView. bottomAnchor. constraint ( equalTo: self . contentView. bottomAnchor, constant: - verticalIconMargin) ,
@@ -328,7 +332,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
328
332
let verticalLabelMargin : CGFloat = 10
329
333
let verticalIconMargin : CGFloat = 10
330
334
let spacing : CGFloat = 15
331
- let iconViewWidth : CGFloat = floor ( iconSize . width / 2 )
335
+ let iconViewWidth : CGFloat = floor ( type ( of : self ) . defaultIconSize . width / 2 )
332
336
let titleDetailSpacing : CGFloat = 15
333
337
334
338
titleLabel. numberOfLines = 1
@@ -341,10 +345,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
341
345
hasSecondaryDetailView = false
342
346
}
343
347
348
+ let iconWidthConstraint = updateIconWidth ( content? . iconWidth, defaultWidth: iconViewWidth)
349
+
344
350
constraints = [
345
351
iconView. leadingAnchor. constraint ( equalTo: self . contentView. leadingAnchor, constant: horizontalMargin) ,
346
352
iconView. trailingAnchor. constraint ( equalTo: titleLabel. leadingAnchor, constant: - spacing) ,
347
- iconView . widthAnchor . constraint ( equalToConstant : iconViewWidth ) ,
353
+ iconWidthConstraint ,
348
354
iconView. topAnchor. constraint ( equalTo: self . contentView. topAnchor, constant: verticalIconMargin) ,
349
355
iconView. bottomAnchor. constraint ( equalTo: self . contentView. bottomAnchor, constant: - verticalIconMargin) ,
350
356
@@ -362,7 +368,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
362
368
let verticalLabelMargin : CGFloat = 10
363
369
let verticalIconMargin : CGFloat = 10
364
370
let spacing : CGFloat = 15
365
- let iconViewWidth : CGFloat = iconSize . width
371
+ let iconViewWidth : CGFloat = type ( of : self ) . defaultIconSize . width
366
372
let verticalLabelMarginFromCenter : CGFloat = 1
367
373
368
374
titleLabel. numberOfLines = 1
@@ -377,10 +383,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
377
383
378
384
truncationMode = . truncateTail
379
385
386
+ let iconWidthConstraint = updateIconWidth ( content? . iconWidth, defaultWidth: iconViewWidth)
387
+
380
388
constraints = [
381
389
iconView. leadingAnchor. constraint ( equalTo: self . contentView. leadingAnchor, constant: horizontalMargin) ,
382
390
iconView. trailingAnchor. constraint ( equalTo: titleLabel. leadingAnchor, constant: - spacing) ,
383
- iconView . widthAnchor . constraint ( equalToConstant : iconViewWidth ) ,
391
+ iconWidthConstraint ,
384
392
iconView. topAnchor. constraint ( equalTo: self . contentView. topAnchor, constant: verticalIconMargin) ,
385
393
iconView. bottomAnchor. constraint ( equalTo: self . contentView. bottomAnchor, constant: - verticalIconMargin) ,
386
394
@@ -406,6 +414,28 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
406
414
}
407
415
}
408
416
417
+ private var iconWidthConstraint : NSLayoutConstraint ?
418
+ private var lastIconWidth : CGFloat ?
419
+ private var defaultIconWidthForCellLayout : CGFloat ? // default width for current cell layout
420
+ private func updateIconWidth( _ newWidth: CGFloat ? , defaultWidth: CGFloat ? = nil ) -> NSLayoutConstraint {
421
+ if let iconWidthConstraint {
422
+ iconWidthConstraint. isActive = false
423
+ }
424
+
425
+ if let defaultWidth {
426
+ // Store default width for this cell type if one is provided
427
+ defaultIconWidthForCellLayout = defaultWidth
428
+ }
429
+
430
+ // Fall back to default icon size if necessary
431
+ let effectiveWidth = newWidth ?? defaultIconWidthForCellLayout ?? type ( of: self ) . defaultIconSize. width
432
+
433
+ let widthConstraint = iconView. widthAnchor. constraint ( equalToConstant: effectiveWidth)
434
+ iconWidthConstraint = widthConstraint
435
+
436
+ return widthConstraint
437
+ }
438
+
409
439
// MARK: - Content
410
440
var title : NSAttributedString ? {
411
441
didSet {
@@ -502,6 +532,11 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
502
532
}
503
533
}
504
534
535
+ if content? . iconWidth != lastIconWidth {
536
+ updateIconWidth ( content? . iconWidth) . isActive = true
537
+ lastIconWidth = content? . iconWidth
538
+ }
539
+
505
540
iconView. request = iconRequest
506
541
if let iconViewProvider {
507
542
iconView. activeViewProvider = iconViewProvider
0 commit comments