Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: truncate pills if they are too long #7455

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Riot/Modules/Pills/PillAttachmentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class PillAttachmentView: UIView {
label.font = pillData.font
label.textColor = pillData.isHighlighted ? theme.baseTextPrimaryColor : theme.textPrimaryColor
label.translatesAutoresizingMaskIntoConstraints = false
label.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
stack.addArrangedSubview(label)

computedWidth += label.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: sizes.pillBackgroundHeight)).width
Expand Down Expand Up @@ -146,10 +145,12 @@ class PillAttachmentView: UIView {
computedWidth += 2 * sizes.horizontalMargin
}

computedWidth = min(pillData.maxWidth, computedWidth)

let pillBackgroundView = UIView(frame: CGRect(x: 0,
y: sizes.verticalMargin,
width: computedWidth,
height: sizes.pillBackgroundHeight))
y: sizes.verticalMargin,
width: computedWidth,
height: sizes.pillBackgroundHeight))

pillBackgroundView.vc_addSubViewMatchingParent(stack, withInsets: UIEdgeInsets(top: sizes.verticalMargin, left: leadingStackMargin, bottom: -sizes.verticalMargin, right: -sizes.horizontalMargin))

Expand Down
8 changes: 6 additions & 2 deletions Riot/Modules/Pills/PillAttachmentViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ import UIKit
return
}

guard let pillData = textAttachment.data else {
guard var pillData = textAttachment.data else {
MXLog.debug("[PillAttachmentViewProvider]: attachment misses pill data")
return
}


if let messageTextView {
pillData.maxWidth = messageTextView.bounds.width - 8
}

let mainSession = AppDelegate.theDelegate().mxSessions.first as? MXSession

let pillView = PillAttachmentView(frame: CGRect(origin: .zero, size: textAttachment.size(forFont: pillData.font)),
Expand Down
2 changes: 2 additions & 0 deletions Riot/Modules/Pills/PillTextAttachment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class PillTextAttachment: NSTextAttachment {
width += 2 * sizes.horizontalMargin
}

width = min(width, data.maxWidth)

return CGSize(width: width,
height: sizes.pillHeight)
}
Expand Down
9 changes: 8 additions & 1 deletion Riot/Modules/Pills/PillTextAttachmentData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ struct PillTextAttachmentData: Codable {
var alpha: CGFloat
/// Font for the display name
var font: UIFont
/// Max width
var maxWidth: CGFloat

/// Helper for preferred text to display.
var displayText: String {
Expand All @@ -93,12 +95,14 @@ struct PillTextAttachmentData: Codable {
items: [PillTextAttachmentItem],
isHighlighted: Bool,
alpha: CGFloat,
font: UIFont) {
font: UIFont,
maxWidth: CGFloat = CGFloat.greatestFiniteMagnitude) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be able to infer the type of the default here :)

Suggested change
maxWidth: CGFloat = CGFloat.greatestFiniteMagnitude) {
maxWidth: CGFloat = .greatestFiniteMagnitude) {

self.pillType = pillType
self.items = items
self.isHighlighted = isHighlighted
self.alpha = alpha
self.font = font
self.maxWidth = maxWidth
}

// MARK: - Codable
Expand All @@ -108,6 +112,7 @@ struct PillTextAttachmentData: Codable {
case isHighlighted
case alpha
case font
case maxWidth
Copy link
Member

@pixlwave pixlwave Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only question is about encoding/decoding these pills - I'm not sure where/when we do it.

Does it make sense to encode a computed maxWidth with them? Could it be decoded to be displayed in a different size window (e.g. If the user is now a different split size on iPad)? If so, maybe it would make sense to not encode it, and instead set it back to .greatestFiniteMagnitude?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @pixlwave, I've updated the code to reflect your suggestions.

}

enum PillTextAttachmentDataError: Error {
Expand All @@ -126,6 +131,7 @@ struct PillTextAttachmentData: Codable {
} else {
throw PillTextAttachmentDataError.noFontData
}
maxWidth = try container.decode(CGFloat.self, forKey: .maxWidth)
}

func encode(to encoder: Encoder) throws {
Expand All @@ -136,6 +142,7 @@ struct PillTextAttachmentData: Codable {
try container.encode(alpha, forKey: .alpha)
let fontData = try NSKeyedArchiver.archivedData(withRootObject: font, requiringSecureCoding: false)
try container.encode(fontData, forKey: .font)
try container.encode(maxWidth, forKey: .maxWidth)
}

// MARK: - Pill representations
Expand Down
1 change: 1 addition & 0 deletions changelog.d/7413.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Long pills are now truncated.