Expose metas in fields as accessibility actions

This commit is contained in:
Jed Fox 2023-02-07 15:36:27 -05:00
parent 5a3afef978
commit 952ae0f621
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
4 changed files with 52 additions and 4 deletions

View File

@ -129,6 +129,40 @@ extension ProfileFieldCollectionViewCell {
UIMenuController.shared.showMenu(from: checkmark, rect: checkmark.bounds)
}
}
private var valueMetas: [(title: String, Meta)] {
var result: [(title: String, Meta)] = []
valueMetaLabel.textStorage.enumerateAttribute(NSAttributedString.Key("MetaAttributeKey.meta"), in: NSMakeRange(0, valueMetaLabel.textStorage.length)) { value, range, _ in
if let value = value as? Meta {
result.append((valueMetaLabel.textStorage.string.substring(with: range), value))
}
}
return result
}
override func accessibilityActivate() -> Bool {
if let (_, meta) = valueMetas.first {
delegate?.profileFieldCollectionViewCell(self, metaLabel: valueMetaLabel, didSelectMeta: meta)
return true
}
return false
}
override var accessibilityCustomActions: [UIAccessibilityCustomAction]? {
get {
let valueMetas = valueMetas
if valueMetas.count < 2 { return nil }
return valueMetas.compactMap { title, meta in
guard let name = meta.accessibilityLabel else { return nil }
return UIAccessibilityCustomAction(name: name) { [weak self] _ in
guard let self, let delegate = self.delegate else { return false }
delegate.profileFieldCollectionViewCell(self, metaLabel: self.valueMetaLabel, didSelectMeta: meta)
return true
}
}
}
set {}
}
}
// UIMenuController boilerplate

View File

@ -8,9 +8,9 @@
import Meta
import MastodonLocalization
extension Meta.Entity {
var accessibilityCustomActionLabel: String? {
switch meta {
extension Meta {
public var accessibilityLabel: String? {
switch self {
case .url(_, trimmed: _, url: let url, userInfo: _):
return L10n.Common.Controls.Status.MetaEntity.url(url)
case .hashtag(_, hashtag: let hashtag, userInfo: _):

View File

@ -152,4 +152,18 @@ extension MetaLabel {
]
}
public var backedString: String {
let string = textStorage.string
let nsString = NSMutableString(string: string)
textStorage.enumerateAttribute(
.attachment,
in: NSRange(location: 0, length: textStorage.length),
options: [.reverse])
{ value, range, _ in
guard let attachment = value as? MetaAttachment else { return }
nsString.replaceCharacters(in: range, with: attachment.string)
}
return nsString as String
}
}

View File

@ -889,7 +889,7 @@ extension StatusView.ViewModel {
.map { content, isRevealed in
guard isRevealed, let entities = content?.entities else { return [] }
return entities.compactMap { entity in
guard let name = entity.accessibilityCustomActionLabel else { return nil }
guard let name = entity.meta.accessibilityLabel else { return nil }
return UIAccessibilityCustomAction(name: name) { action in
statusView.delegate?.statusView(statusView, metaText: statusView.contentMetaText, didSelectMeta: entity.meta)
return true