mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-05 19:57:31 +00:00
55 lines
1.9 KiB
Swift
55 lines
1.9 KiB
Swift
//
|
||
// 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)
|
||
}
|
||
}
|
||
}
|
||
}
|