Skip to content

Commit

Permalink
- UniversalItemListCell: add new Content.iconWidth property to contro…
Browse files Browse the repository at this point in the history
…l icon width in the universal cell, with fallback to defaults

- OCShareRole+UniversalItemListCellContentProvider: adapt icon width to size of option icons in ShareViewController
  • Loading branch information
felix-schwarz authored and hosy committed Dec 1, 2023
1 parent 9d38a2e commit 2b23e84
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension OCShareRole: UniversalItemListCellContentProvider {

if let icon = OCSymbol.icon(forSymbolName: symbolName) {
content.icon = .icon(image: icon)
content.iconWidth = UniversalItemListCell.defaultIconSize.width / 2
}

content.title = .text(localizedName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {

icon = content.icon
iconDisabled = content.iconDisabled
iconWidth = content.iconWidth

details = content.details

Expand Down Expand Up @@ -102,6 +103,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
var title: Title?
var icon: Icon?
var iconDisabled: Bool = false
var iconWidth: CGFloat?

var details: [SegmentViewItem]?

Expand All @@ -127,7 +129,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
return view
}()

private let iconSize : CGSize = CGSize(width: 40, height: 40)
static public let defaultIconSize : CGSize = CGSize(width: 40, height: 40)
public let thumbnailSize : CGSize = CGSize(width: 60, height: 60) // when changing size, also update .iconView.fallbackSize
open var iconView: ResourceViewHost = ResourceViewHost(fallbackSize: CGSize(width: 60, height: 60)) // when changing size, also update .thumbnailSize

Expand Down Expand Up @@ -306,10 +308,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
hasSecondaryDetailView = false
}

let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: (iconViewHeight / 0.75)) // 4:3

constraints = [
iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin),
iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing),
iconView.widthAnchor.constraint(equalToConstant: floor(iconViewHeight / 0.75)), // 4:3
iconWidthConstraint,
iconView.heightAnchor.constraint(equalToConstant: iconViewHeight),
iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin),
iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin),
Expand All @@ -328,7 +332,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
let verticalLabelMargin : CGFloat = 10
let verticalIconMargin : CGFloat = 10
let spacing : CGFloat = 15
let iconViewWidth : CGFloat = floor(iconSize.width / 2)
let iconViewWidth : CGFloat = floor(type(of: self).defaultIconSize.width / 2)
let titleDetailSpacing: CGFloat = 15

titleLabel.numberOfLines = 1
Expand All @@ -341,10 +345,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
hasSecondaryDetailView = false
}

let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: iconViewWidth)

constraints = [
iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin),
iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing),
iconView.widthAnchor.constraint(equalToConstant: iconViewWidth),
iconWidthConstraint,
iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin),
iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin),

Expand All @@ -362,7 +368,7 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
let verticalLabelMargin : CGFloat = 10
let verticalIconMargin : CGFloat = 10
let spacing : CGFloat = 15
let iconViewWidth : CGFloat = iconSize.width
let iconViewWidth : CGFloat = type(of: self).defaultIconSize.width
let verticalLabelMarginFromCenter : CGFloat = 1

titleLabel.numberOfLines = 1
Expand All @@ -377,10 +383,12 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {

truncationMode = .truncateTail

let iconWidthConstraint = updateIconWidth(content?.iconWidth, defaultWidth: iconViewWidth)

constraints = [
iconView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: horizontalMargin),
iconView.trailingAnchor.constraint(equalTo: titleLabel.leadingAnchor, constant: -spacing),
iconView.widthAnchor.constraint(equalToConstant: iconViewWidth),
iconWidthConstraint,
iconView.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: verticalIconMargin),
iconView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -verticalIconMargin),

Expand All @@ -406,6 +414,28 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
}
}

private var iconWidthConstraint: NSLayoutConstraint?
private var lastIconWidth: CGFloat?
private var defaultIconWidthForCellLayout: CGFloat? // default width for current cell layout
private func updateIconWidth(_ newWidth: CGFloat?, defaultWidth: CGFloat? = nil) -> NSLayoutConstraint {
if let iconWidthConstraint {
iconWidthConstraint.isActive = false
}

if let defaultWidth {
// Store default width for this cell type if one is provided
defaultIconWidthForCellLayout = defaultWidth
}

// Fall back to default icon size if necessary
let effectiveWidth = newWidth ?? defaultIconWidthForCellLayout ?? type(of: self).defaultIconSize.width

let widthConstraint = iconView.widthAnchor.constraint(equalToConstant: effectiveWidth)
iconWidthConstraint = widthConstraint

return widthConstraint
}

// MARK: - Content
var title: NSAttributedString? {
didSet {
Expand Down Expand Up @@ -502,6 +532,11 @@ open class UniversalItemListCell: ThemeableCollectionViewListCell {
}
}

if content?.iconWidth != lastIconWidth {
updateIconWidth(content?.iconWidth).isActive = true
lastIconWidth = content?.iconWidth
}

iconView.request = iconRequest
if let iconViewProvider {
iconView.activeViewProvider = iconViewProvider
Expand Down

0 comments on commit 2b23e84

Please sign in to comment.