-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cordova/apple/ios): add mac catalyst support (#1630)
* Replace the `copy-www-build-step` script with build phases. * Add Mac Catalyst as a destination. * Update README to load xcworkspace from `./src/cordova/apple/` instead of `./platforms/ios/`. * Set separate bundle identifier for the Mac app built with Mac Catalyst. * Add `Package.resolved` back. It was likely accidentally removed in #1536. * Update local storage source directory for UIWebView->WKWebView migration for catalyst. * Pin `cordova-ios` dep to fix for splashscreen aspect ratio. * Rename `Images.xcassets` to `Assets.xcassets`. * Add the status bar button images. * Use `LSUIElement` to indicate this is a background app and shouldn't appear in the Dock. * Add an AppKit bridge and use it to create a status bar menu in catalyst. * Re-open the application window when use selects "Open" command from the status menu. * Stop any VPN connections on app termination. * Use `clang-format` to format Objective-C files. * Use `SwiftFormat` to format Swift files. * Set separate bundle identifier for the Mac Catalyst app built for release. Previously set it on the `Debug` build, but need to also set it on `Release` build. * Configure the app window to be a specific size. * Enable the embedded app launcher as a login item. * Use the macos app group in `PacketTunnelProvider` when on Catalyst. * Add some logs to the `OutlineStatusItemController` for easier debugging. * Do not try and add a `StatusItemController` automatically on AppKitBridge loads. We'll need to re-use this bridge in the upcoming launcher, which should not add its own menu item to the status bar. * Ignore locally loaded XCFramework bundles. * Add launcher to Catalyst. We set the launcher as a login item in Catalyst, and open the app if there Outline was connected at shutdown. * Run `clang-format` over `.m` files. * Ensure Sentry logs the right app group on Catalyst. * Run macos code on Catalyst in a few more places. * Do not run some code for `macOS` on `Catalyst`. * Add prefix to log message for consistency. * Use the `NETunnelProviderManager` to determine whether Outline was connected and the launcher should launch the application. * Automatic project data changes made by Xcode. * Set miminum macos deployment target to 10.15. * Set bundle ID for launcher to `launcher3`. * Move `AppDelegate` logic to category extension instead of overwriting the `AppDelegate` file auto-generated by Cordova. * Move Catalyst specific `AppDelegate` code to Swift. * Use `SwiftFormat` to format Swift files. * Replace `NSLog` with `DDLog`. * Use `SwiftFormat` to format Swift files. * Fix notifications for macos bundle and undo formatting to make the diff smaller. * Remove local `Tun2socks.xcframework` which got accidentally committed. * Undo README changes as they have already been updated in a previous PR. * Move catalyst code into `OutlineAppleLib`. * Remove reference to `OutlineLauncher`'s plist. * Fix some bad merges. * Remove an unnecessary `#if` guard. * Add a TODO to i18n the menu strings, which will happen in a fast-follow. * Review changes. * More changes for review. * Mark static function as public. * More changes. * Use enum instead of boolean to represent connection status. * Clean up some entitlement changes that aren't actually needed. * Undo some entitlement changes that were needed to create outgoing connections on macos. * Remove a single unneeded entitlement. * Only compile `OutlineCatalystApp` on Catalyst. * Fix ios build. * Add some method docs and change methods to standalone functions where no state is needed. * Remove unneeded `Sources` sections. * Revert "Remove unneeded `Sources` sections." This reverts commit 863ac93. * Remove unneeded build phases. * Remove seemingly unneeded info list items. * Add the "compile sources" build phase back to the `AppKitBridge` bundle. * Fix macos build. * Move `OutlineCatalystApp` target into `OutlineAppleLib` product. * Rely on `NSNotification` extension from `OutlineAppleLib`. * Remove `Tun2socks` and `OutlineTunnel` dependency from launcher and add "compile sources" buildphase back in. * Select "Optimize interface for Mac" at the project level for Mac Catalyst. * Move much of the OutlinePlugin into a Swift package, leaving only Cordova-specific code in the plugin, renamed as `CDVOutline`. * Move status menu assets into Swift package. * No need to explicitly state resources for the Assets catalog as Xcode treats these as resources automatically. * Update the macos client to use the renamed `CDVOutline` plugin. * Revert `OutlinePlugin` changes and instead use a `NSNotification` target. * `OutlineNotifications` to `OutlineNotification`. * Add some TODOs. * Remove unnecessary parameter.
- Loading branch information
Showing
28 changed files
with
1,478 additions
and
236 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# OutlineTun2Socks | ||
# OutlineAppleLib | ||
|
||
This package provides the Outline Tun2socks framework as a Swift Package. | ||
This package provides all Outline Apple logic, including the Outline Tun2socks | ||
framework, as a Swift Package. |
83 changes: 83 additions & 0 deletions
83
src/cordova/apple/OutlineAppleLib/Sources/OutlineAppKitBridge/AppKitBridge.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2023 The Outline Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if os(macOS) | ||
import AppKit | ||
import CocoaLumberjackSwift | ||
import ServiceManagement | ||
|
||
public class AppKitBridge: NSObject, AppKitBridgeProtocol { | ||
private var statusItemController: StatusItemController? | ||
static let kAppGroup = "QT8Z3Q9V3A.org.outline.macos.client" | ||
static let kAppLauncherName = "launcher3" | ||
|
||
override public required init() { | ||
super.init() | ||
} | ||
|
||
/// Terminates the application. | ||
@objc public func terminate() { | ||
NSApp.terminate(self) | ||
} | ||
|
||
/// Set the connection status in the app's menu in the system-wide menu bar. | ||
@objc public func setConnectionStatus(_ status: ConnectionStatus) { | ||
if statusItemController == nil { | ||
DDLogInfo("[AppKitBridge] No status item controller found. Creating one now.") | ||
statusItemController = StatusItemController() | ||
} | ||
statusItemController!.setStatus(status: status) | ||
} | ||
|
||
/// Enables or disables the embedded app launcher as a login item. | ||
@objc public func setAppLauncherEnabled(_ isEnabled: Bool) { | ||
guard let launcherBundleId = getLauncherBundleId() else { | ||
return DDLogError("[AppKitBridge] Unable to set launcher for missing bundle ID.") | ||
} | ||
|
||
if !SMLoginItemSetEnabled(launcherBundleId as! CFString, isEnabled) { | ||
return DDLogError("[AppKitBridge] Failed to set enable=\(isEnabled) for launcher \(launcherBundleId).") | ||
} | ||
|
||
return DDLogInfo("[AppKitBridge] Successfully set enable=\(isEnabled) for launcher \(launcherBundleId).") | ||
} | ||
|
||
/// Loads the main application from a given launcher bundle. | ||
@objc public func loadMainApp(_ launcherBundleId: String) { | ||
// Retrieve the main app's bundle ID programmatically from the embedded launcher bundle ID. | ||
let mainAppBundleId = getMainBundleId(launcherBundleId) | ||
DDLogInfo("[AppKitBridge] Loading main app \(mainAppBundleId) from launcher \(launcherBundleId).") | ||
|
||
let descriptor = NSAppleEventDescriptor(string: launcherBundleId) | ||
NSWorkspace.shared.launchApplication(withBundleIdentifier: mainAppBundleId, | ||
options: [.withoutActivation, .andHide], | ||
additionalEventParamDescriptor: descriptor, | ||
launchIdentifier: nil) | ||
} | ||
} | ||
|
||
/// Returns the embedded launcher application's bundle ID. | ||
private func getLauncherBundleId() -> String? { | ||
guard let bundleId = Bundle.main.bundleIdentifier else { | ||
DDLogError("[AppKitBridge] Failed to retrieve the application's bundle ID.") | ||
return nil | ||
} | ||
return String(format: "%@.%@", bundleId, AppKitBridge.kAppLauncherName) | ||
} | ||
|
||
/// Returns the main application's bundle ID from the embedded launcher bundle ID. | ||
private func getMainBundleId(_ launcherBundleId: String) -> String { | ||
return (launcherBundleId as NSString).deletingPathExtension | ||
} | ||
#endif |
28 changes: 28 additions & 0 deletions
28
src/cordova/apple/OutlineAppleLib/Sources/OutlineAppKitBridge/AppKitBridgeProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2023 The Outline Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
|
||
@objc | ||
public protocol AppKitBridgeProtocol: NSObjectProtocol { | ||
init() | ||
|
||
func terminate() | ||
|
||
func setConnectionStatus(_ status: ConnectionStatus) | ||
|
||
func setAppLauncherEnabled(_ isEnabled: Bool) | ||
|
||
func loadMainApp(_ launcherBundleId: String) | ||
} |
19 changes: 19 additions & 0 deletions
19
src/cordova/apple/OutlineAppleLib/Sources/OutlineAppKitBridge/ConnectionStatus.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2023 The Outline Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
@objc public enum ConnectionStatus: Int { | ||
case unknown | ||
case connected | ||
case disconnected | ||
} |
6 changes: 6 additions & 0 deletions
6
...apple/OutlineAppleLib/Sources/OutlineAppKitBridge/Resources/Assets.xcassets/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...lineAppKitBridge/Resources/Assets.xcassets/status_bar_button_image.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "outline-black-off-2x.png", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+578 Bytes
...urces/Assets.xcassets/status_bar_button_image.imageset/outline-black-off-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions
21
...Bridge/Resources/Assets.xcassets/status_bar_button_image_connected.imageset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "1x" | ||
}, | ||
{ | ||
"filename" : "outline-black-on-2x.png", | ||
"idiom" : "universal", | ||
"scale" : "2x" | ||
}, | ||
{ | ||
"idiom" : "universal", | ||
"scale" : "3x" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+563 Bytes
...ets.xcassets/status_bar_button_image_connected.imageset/outline-black-on-2x.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions
101
src/cordova/apple/OutlineAppleLib/Sources/OutlineAppKitBridge/StatusItemController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2023 The Outline Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if os(macOS) | ||
import AppKit | ||
import CocoaLumberjackSwift | ||
import OutlineNotification | ||
|
||
var StatusItem = NSStatusItem() | ||
|
||
class StatusItemController: NSObject { | ||
let connectionStatusMenuItem = NSMenuItem(title: MenuTitle.statusDisconnected, | ||
action: nil, | ||
keyEquivalent: "") | ||
|
||
private enum AppIconImage { | ||
static let statusConnected = getImage(name: "status_bar_button_image_connected") | ||
static let statusDisconnected = getImage(name: "status_bar_button_image") | ||
} | ||
|
||
// TODO: Internationalize these user-facing strings. | ||
private enum MenuTitle { | ||
static let open = "Open" | ||
static let quit = "Quit" | ||
static let statusConnected = "Connected" | ||
static let statusDisconnected = "Disconnected" | ||
} | ||
|
||
override init() { | ||
super.init() | ||
|
||
DDLogInfo("[StatusItemController] Creating status menu") | ||
StatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength) | ||
setStatus(status: .disconnected) | ||
|
||
let menu = NSMenu() | ||
let openMenuItem = NSMenuItem(title: MenuTitle.open, action: #selector(openApplication), keyEquivalent: "o") | ||
openMenuItem.target = self | ||
menu.addItem(openMenuItem) | ||
menu.addItem(connectionStatusMenuItem) | ||
menu.addItem(NSMenuItem.separator()) | ||
let closeMenuItem = NSMenuItem(title: MenuTitle.quit, action: #selector(closeApplication), keyEquivalent: "q") | ||
closeMenuItem.target = self | ||
menu.addItem(closeMenuItem) | ||
StatusItem.menu = menu | ||
} | ||
|
||
func setStatus(status: ConnectionStatus) { | ||
let isConnected = status == .connected | ||
let appIconImage = isConnected ? AppIconImage.statusConnected : AppIconImage.statusDisconnected | ||
appIconImage.isTemplate = true | ||
StatusItem.button?.image = appIconImage | ||
|
||
let connectionStatusTitle = isConnected ? MenuTitle.statusConnected : MenuTitle.statusDisconnected | ||
connectionStatusMenuItem.title = connectionStatusTitle | ||
} | ||
|
||
@objc func openApplication(_: AnyObject?) { | ||
DDLogInfo("[StatusItemController] Opening application") | ||
NSApp.activate(ignoringOtherApps: true) | ||
guard let uiWindow = getUiWindow() else { | ||
return | ||
} | ||
uiWindow.makeKeyAndOrderFront(self) | ||
} | ||
|
||
@objc func closeApplication(_: AnyObject?) { | ||
DDLogInfo("[StatusItemController] Closing application") | ||
NotificationCenter.default.post(name: .kAppQuit, object: nil) | ||
NSApplication.shared.terminate(self) | ||
} | ||
} | ||
|
||
private func getUiWindow() -> NSWindow? { | ||
for window in NSApp.windows { | ||
if String(describing: window).contains("UINSWindow") { | ||
return window | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
private func getImage(name: String) -> NSImage { | ||
guard let image = Bundle.module.image(forResource: NSImage.Name(name)) else { | ||
fatalError("Unable to load image asset named \(name).") | ||
} | ||
return image | ||
} | ||
|
||
#endif |
Oops, something went wrong.