Files
NetNewsWire/Mac/MainWindow/SharingServicePickerDelegate.swift
T

55 lines
1.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//
// SharingServicePickerDelegate.swift
// NetNewsWire
//
// Created by Brent Simmons on 2/17/18.
// Copyright © 2018 Ranchero Software. All rights reserved.
//
import AppKit
import RSCore
@objc final class SharingServicePickerDelegate: NSObject, @MainActor NSSharingServicePickerDelegate {
var selectedHTML: String?
private let sharingServiceDelegate: SharingServiceDelegate
init(_ window: NSWindow?) {
sharingServiceDelegate = SharingServiceDelegate(window)
}
@MainActor func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, sharingServicesForItems items: [Any], proposedSharingServices proposedServices: [NSSharingService]) -> [NSSharingService] {
SharingServicePickerDelegate.filteredSharingServices(proposedServices) + SharingServicePickerDelegate.customSharingServices(for: items, selectedHTML: selectedHTML)
}
func sharingServicePicker(_ sharingServicePicker: NSSharingServicePicker, delegateFor sharingService: NSSharingService) -> NSSharingServiceDelegate? {
return sharingServiceDelegate
}
/// Removes NetNewsWire’s own share extension.
@MainActor static func filteredSharingServices(_ services: [NSSharingService]) -> [NSSharingService] {
services.filter { $0.menuItemTitle != "NetNewsWire" }
}
@MainActor static func customSharingServices(for items: [Any], selectedHTML: String? = nil) -> [NSSharingService] {
let customServices: [SendToCommand] = [SendToMarsEditCommand(), SendToMicroBlogCommand()]
return customServices.compactMap { (sendToCommand) -> NSSharingService? in
guard let object = items.first else {
return nil
}
guard sendToCommand.canSendObject(object, selectedText: selectedHTML) else {
return nil
}
let image = sendToCommand.image ?? NSImage()
return NSSharingService(title: sendToCommand.title, image: image, alternateImage: nil) {
sendToCommand.sendObject(object, selectedText: selectedHTML)
}
}
}
}