Add gif/alt badges to attachments

Closes #255, #338
This commit is contained in:
Shadowfacts 2023-01-26 19:16:34 -05:00
parent e11784904b
commit d638ea054b
1 changed files with 74 additions and 0 deletions

View File

@ -127,6 +127,15 @@ class AttachmentView: GIFImageView {
} }
} }
var badges: Badges = []
if attachment.description?.isEmpty == false {
badges.formUnion(.alt)
}
if attachment.kind == .gifv || attachment.url.pathExtension == "gif" {
badges.formUnion(.gif)
}
createBadgesView(badges)
switch attachment.kind { switch attachment.kind {
case .image: case .image:
loadImage() loadImage()
@ -302,6 +311,64 @@ class AttachmentView: GIFImageView {
} }
} }
private func createBadgesView(_ badges: Badges) {
guard !badges.isEmpty else {
return
}
let stack = UIStackView()
stack.axis = .horizontal
stack.spacing = 2
stack.translatesAutoresizingMaskIntoConstraints = false
let font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .systemFont(ofSize: 13, weight: .bold))
func makeBadgeView(text: String) {
let container = UIView()
container.backgroundColor = .secondarySystemBackground.resolvedColor(with: UITraitCollection(userInterfaceStyle: .dark))
let label = UILabel()
label.font = font
label.adjustsFontForContentSizeCategory = true
label.textColor = .white
label.text = text
label.translatesAutoresizingMaskIntoConstraints = false
container.addSubview(label)
NSLayoutConstraint.activate([
label.leadingAnchor.constraint(equalTo: container.leadingAnchor, constant: 2),
label.trailingAnchor.constraint(equalTo: container.trailingAnchor, constant: -2),
label.topAnchor.constraint(equalTo: container.topAnchor, constant: 2),
label.bottomAnchor.constraint(equalTo: container.bottomAnchor, constant: -2),
])
stack.addArrangedSubview(container)
}
if badges.contains(.gif) {
makeBadgeView(text: "GIF")
}
if badges.contains(.alt) {
makeBadgeView(text: "ALT")
}
let first = stack.arrangedSubviews.first!
first.layer.masksToBounds = true
first.layer.cornerRadius = 4
if stack.arrangedSubviews.count > 1 {
first.layer.maskedCorners = [.layerMinXMinYCorner, .layerMinXMaxYCorner]
let last = stack.arrangedSubviews.last!
last.layer.masksToBounds = true
last.layer.cornerRadius = 4
last.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
}
addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4),
stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -4),
])
}
// MARK: Interaction
func showGallery() { func showGallery() {
if let delegate = delegate, if let delegate = delegate,
let gallery = delegate.attachmentViewGallery(startingAt: index) { let gallery = delegate.attachmentViewGallery(startingAt: index) {
@ -328,6 +395,13 @@ fileprivate extension AttachmentView {
case gifData(URL, Data) case gifData(URL, Data)
case cgImage(URL, CGImage) case cgImage(URL, CGImage)
} }
struct Badges: OptionSet {
static let gif = Badges(rawValue: 1 << 0)
static let alt = Badges(rawValue: 1 << 1)
let rawValue: Int
}
} }
extension AttachmentView: UIContextMenuInteractionDelegate { extension AttachmentView: UIContextMenuInteractionDelegate {