Skip to content

Commit ab3f29b

Browse files
committed
macOS: 添付メディアのサムネイルを表示するように (クリックで開けるがキーボード操作ではまだ開けない)
1 parent 1ef7975 commit ab3f29b

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

Sources/Mac/App/Timeline/PostView.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ class PostView: NSTableRowView {
6464
$0.wantsLayer = true
6565
$0.layer?.backgroundColor = Asset.barBoost.color.cgColor
6666
}
67+
let attachedMediaStackView = NSStackView(views: []) {
68+
$0.orientation = .vertical
69+
$0.spacing = 8
70+
$0.setHuggingPriority(.required, for: .vertical)
71+
}
6772
let guardTextField = NSTextField(labelWithString: "\(L10n.Menu.post)\(L10n.Menu.hidePrivatePosts)")
6873

6974
init(post: MastodonPost) {
@@ -78,6 +83,7 @@ class PostView: NSTableRowView {
7883
$0.setHuggingPriority(.required, for: .vertical)
7984
},
8085
textView,
86+
attachedMediaStackView,
8187
]) {
8288
$0.spacing = 4
8389
$0.alignment = .leading
@@ -105,8 +111,9 @@ class PostView: NSTableRowView {
105111
make.leading.equalTo(iconView.snp.trailing).offset(8)
106112
}
107113
if post.originalPost.visibility != .public, post.originalPost.visibility != .unlisted {
108-
iconView.bind(.hidden, to: NSUserDefaultsController.appGroup, withKeyPath: "values.hide_private_posts", options: nil)
109-
stackView.bind(.hidden, to: NSUserDefaultsController.appGroup, withKeyPath: "values.hide_private_posts", options: nil)
114+
for view in [boostedPostIndicator, iconView, stackView] {
115+
view.bind(.hidden, to: NSUserDefaultsController.appGroup, withKeyPath: "values.hide_private_posts", options: nil)
116+
}
110117
addSubview(guardTextField)
111118
guardTextField.snp.makeConstraints { make in
112119
make.size.lessThanOrEqualToSuperview()
@@ -141,6 +148,18 @@ class PostView: NSTableRowView {
141148
} else {
142149
textView.textStorage?.setAttributedString(NSAttributedString(string: original.status, attributes: attributes))
143150
}
151+
if original.attachments.count > 0 {
152+
for media in original.attachments {
153+
let imageView = AttachmentView(attachment: media)
154+
imageView.snp.makeConstraints { make in
155+
make.height.equalTo(40)
156+
}
157+
attachedMediaStackView.addArrangedSubview(imageView)
158+
}
159+
attachedMediaStackView.isHidden = false
160+
} else {
161+
attachedMediaStackView.isHidden = true
162+
}
144163
}
145164

146165
override var isSelected: Bool {
@@ -166,3 +185,26 @@ class PostView: NSTableRowView {
166185
}
167186
}
168187
}
188+
189+
class AttachmentView: LayeredImageView {
190+
lazy var clickRecognizer = NSClickGestureRecognizer(target: self, action: #selector(open))
191+
let attachment: MastodonAttachment
192+
193+
init(attachment: MastodonAttachment) {
194+
self.attachment = attachment
195+
super.init()
196+
layer?.contentsGravity = .resizeAspectFill
197+
loadImage(url: URL(string: attachment.previewUrl ?? ""))
198+
addGestureRecognizer(clickRecognizer)
199+
}
200+
201+
required init?(coder: NSCoder) {
202+
fatalError("init(coder:) has not been implemented")
203+
}
204+
205+
@objc func open() {
206+
if let url = URL(string: attachment.url) {
207+
NSWorkspace.shared.open(url)
208+
}
209+
}
210+
}

0 commit comments

Comments
 (0)