Skip to content

Commit 756798e

Browse files
committed
resolve comments
Signed-off-by: Wouter01 <[email protected]>
1 parent 0847bf5 commit 756798e

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

CodeEdit/Features/Extensions/Commands+ForEach.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ import SwiftUI
1313

1414
/// A structure that builds commandmenus on demand from an underlying collection of data.
1515
/// Maximum 10 items are supported.
16-
public struct CommandsForEach<Data: RandomAccessCollection, Content: Commands>: Commands where Data.Index == Int {
16+
struct CommandsForEach<Data: RandomAccessCollection, Content: Commands>: Commands where Data.Index == Int {
1717

18-
public var data: Data
18+
var data: Data
1919

20-
public var content: (Data.Element) -> Content
20+
var content: (Data.Element) -> Content
2121

22-
public init(_ data: Data, @CommandsBuilder content: @escaping (Data.Element) -> Content) {
22+
init(_ data: Data, @CommandsBuilder content: @escaping (Data.Element) -> Content) {
2323
self.data = data
2424
self.content = content
2525
}
2626

27-
public var body: some Commands {
27+
var body: some Commands {
2828
switch data.count {
2929
case 0:
3030
EmptyCommands()

CodeEdit/Features/Extensions/ExtensionInfo.swift

+27-12
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ struct ExtensionInfo: Identifiable, Hashable {
1717

1818
let isDebug: Bool
1919

20+
var bundleURL: URL
21+
22+
var bundle: Bundle?
23+
24+
var pid: Int32
25+
2026
var id: String {
2127
endpoint.bundleIdentifier
2228
}
@@ -29,12 +35,6 @@ struct ExtensionInfo: Identifiable, Hashable {
2935
bundle?.infoDictionary?["CFBundleShortVersionString"] as? String
3036
}
3137

32-
var bundleURL: URL
33-
34-
var bundle: Bundle?
35-
36-
var pid: Int32
37-
3838
func restart() {
3939
kill(pid, SIGKILL)
4040
}
@@ -52,30 +52,45 @@ struct ExtensionInfo: Identifiable, Hashable {
5252
connection.invalidate()
5353
}
5454

55-
self.pid = try await connection.withContinuation { (service: XPCWrappable, continuation) in
55+
self.pid = try await ExtensionInfo.getProcessID(connection)
56+
self.isDebug = try await ExtensionInfo.getDebugState(connection)
57+
self.availableFeatures = try await ExtensionInfo.getAvailableFeatures(connection)
58+
self.bundleURL = try await ExtensionInfo.getBundleURL(connection)
59+
self.bundle = Bundle(url: bundleURL)
60+
}
61+
}
62+
63+
// Functions to get basic information about extension
64+
extension ExtensionInfo {
65+
static private func getProcessID(_ connection: NSXPCConnection) async throws -> pid_t {
66+
try await connection.withContinuation { (service: XPCWrappable, continuation) in
5667
service.getExtensionProcessIdentifier {
5768
continuation.resumingHandler($0, .none)
5869
}
5970
}
71+
}
6072

61-
self.isDebug = try await connection.withContinuation { (service: XPCWrappable, continuation) in
73+
static private func getDebugState(_ connection: NSXPCConnection) async throws -> Bool {
74+
try await connection.withContinuation { (service: XPCWrappable, continuation) in
6275
service.isDebug {
6376
continuation.resumingHandler($0, .none)
6477
}
6578
}
79+
}
6680

81+
static private func getAvailableFeatures(_ connection: NSXPCConnection) async throws -> [ExtensionKind] {
6782
let encodedAvailableFeatures = try await connection.withContinuation { (service: XPCWrappable, continuation) in
6883
service.getExtensionKinds(reply: continuation.resumingHandler)
6984
}
85+
return try JSONDecoder().decode([ExtensionKind].self, from: encodedAvailableFeatures)
86+
}
7087

71-
self.availableFeatures = try JSONDecoder().decode([ExtensionKind].self, from: encodedAvailableFeatures)
72-
88+
static private func getBundleURL(_ connection: NSXPCConnection) async throws -> URL {
7389
let bundleURLEncoded = try await connection.withContinuation { (service: XPCWrappable, continuation) in
7490
service.getExtensionURL(reply: continuation.resumingHandler)
7591
}
7692

77-
self.bundleURL = try JSONDecoder().decode(URL.self, from: bundleURLEncoded)
78-
self.bundle = Bundle(url: bundleURL)
93+
return try JSONDecoder().decode(URL.self, from: bundleURLEncoded)
7994
}
8095
}
8196

CodeEdit/Features/Extensions/ExtensionSceneView.swift

+6-14
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
1818
let sceneID: String
1919

2020
init(with appExtension: AppExtensionIdentity, sceneID: String) {
21-
print("Setting appextension to", appExtension)
2221
self.appExtension = appExtension
2322
self.sceneID = sceneID
2423
}
2524

2625
func makeNSViewController(context: Context) -> EXHostViewController {
27-
print("Making...")
2826
let controller = EXHostViewController()
2927
controller.delegate = context.coordinator
3028
controller.configuration = .some(.init(appExtension: appExtension, sceneID: sceneID))
@@ -33,7 +31,6 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
3331
}
3432

3533
func updateNSViewController(_ nsViewController: EXHostViewController, context: Context) {
36-
print("Updating....", appExtension, sceneID)
3734
nsViewController.configuration = .init(appExtension: appExtension, sceneID: sceneID)
3835
context.coordinator.updateEnvironment(context.environment._ceEnvironment)
3936
}
@@ -47,7 +44,7 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
4744
}
4845
}
4946

50-
public class Coordinator: NSObject, EXHostViewControllerDelegate, EnvironmentPublisherObjc {
47+
class Coordinator: NSObject, EXHostViewControllerDelegate, EnvironmentPublisherObjc {
5148
var isOnline: Bool = false
5249
var toPublish: Data?
5350
var openWindow: (String) -> Void
@@ -56,28 +53,25 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
5653
self.openWindow = openWindow
5754
}
5855

59-
public var connection: NSXPCConnection?
56+
var connection: NSXPCConnection?
6057

61-
public func publishEnvironment(data: Data) {
58+
func publishEnvironment(data: Data) {
6259
@Decoded<Callbacks> var data = data
63-
print("RECEIVED DATA")
6460
guard let $data else { return }
6561
switch $data {
6662
case .openWindow(let id):
6763
openWindow(id)
6864
}
6965
}
7066

71-
public func updateEnvironment(@Encoded _ value: _CEEnvironment) {
67+
func updateEnvironment(@Encoded _ value: _CEEnvironment) {
7268
guard let $value else { return }
7369

7470
guard isOnline else {
7571
toPublish = $value
7672
return
7773
}
7874

79-
print("update: sending...")
80-
8175
Task {
8276
do {
8377
try await connection!.withService { (service: EnvironmentPublisherObjc) in
@@ -89,13 +83,12 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
8983
}
9084
}
9185

92-
public func hostViewControllerWillDeactivate(_ viewController: EXHostViewController, error: Error?) {
86+
func hostViewControllerWillDeactivate(_ viewController: EXHostViewController, error: Error?) {
9387
isOnline = false
9488
print("Host will deactivate", error as Any)
9589
}
9690

97-
public func hostViewControllerDidActivate(_ viewController: EXHostViewController) {
98-
print("Host will activate")
91+
func hostViewControllerDidActivate(_ viewController: EXHostViewController) {
9992
isOnline = true
10093
do {
10194
self.connection = try viewController.makeXPCConnection()
@@ -104,7 +97,6 @@ struct ExtensionSceneView: NSViewControllerRepresentable {
10497
connection?.remoteObjectInterface = .init(with: EnvironmentPublisherObjc.self)
10598
connection?.resume()
10699
if let toPublish {
107-
print("Sending first environment: \(String(describing: String(data: toPublish, encoding: .utf8)))")
108100
Task {
109101
try? await connection?.withService { (service: EnvironmentPublisherObjc) in
110102
service.publishEnvironment(data: toPublish)

0 commit comments

Comments
 (0)