|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Wouter Hennen on 30/12/2022. |
| 6 | +// |
| 7 | + |
| 8 | +import ExtensionKit |
| 9 | +import ExtensionFoundation |
| 10 | + |
| 11 | +public protocol CodeEditExtension: AppExtension { |
| 12 | + |
| 13 | + /// UI scenes of the extension. |
| 14 | + associatedtype Body: AppExtensionScene |
| 15 | + |
| 16 | + /// Extension Configuration. |
| 17 | + associatedtype Configuration = AppExtensionSceneConfiguration |
| 18 | + |
| 19 | + /// A brief description of the extension. Should be max a few words. |
| 20 | + var description: String { get } |
| 21 | + |
| 22 | + /// A list of Entitlements the app needs, e.g. Network Access. |
| 23 | + var entitlements: [Entitlement] { get } |
| 24 | + |
| 25 | + /// UI scenes of the extension. |
| 26 | + /// Use the default implementation. |
| 27 | + var body: Body { get } |
| 28 | +} |
| 29 | + |
| 30 | +extension CodeEditExtension { |
| 31 | + |
| 32 | + var extensionURL: URL { |
| 33 | + Bundle.main.bundleURL |
| 34 | + } |
| 35 | + |
| 36 | + func gatherAvailableExtensions() -> [ExtensionKind] { |
| 37 | + var extensions: [ExtensionKind] = [] |
| 38 | + |
| 39 | + if self is any SettingsExtension { |
| 40 | + extensions.append(.settings) |
| 41 | + } |
| 42 | + |
| 43 | + if let self = self as? any SidebarExtension { |
| 44 | + extensions.append(contentsOf: self.availableExtensions) |
| 45 | + } |
| 46 | + |
| 47 | + return extensions |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +public extension CodeEditExtension where Self: AnyObject { |
| 52 | + /// XPC Configuration for communication with CodeEdit. |
| 53 | + /// Use the default implementation. |
| 54 | + var configuration: AppExtensionSceneConfiguration { |
| 55 | + AppExtensionSceneConfiguration(self.body, configuration: SettingsExtensionConfiguration(self)) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +struct SettingsExtensionConfiguration<E: CodeEditExtension & AnyObject>: AppExtensionConfiguration { |
| 60 | + public func accept(connection: NSXPCConnection) -> Bool { |
| 61 | + guard let appExtension else { |
| 62 | + return false |
| 63 | + } |
| 64 | + |
| 65 | + connection.exportedInterface = .init(with: XPCWrappable.self) |
| 66 | + connection.exportedObject = XPCWrapper(appExtension) |
| 67 | + connection.resume() |
| 68 | + |
| 69 | + return true |
| 70 | + } |
| 71 | + |
| 72 | + weak var appExtension: E? |
| 73 | + |
| 74 | + public init(_ appExtension: E) { |
| 75 | + self.appExtension = appExtension |
| 76 | + } |
| 77 | +} |
0 commit comments