Skip to content

Commit

Permalink
feat(cordova/apple/ios): add mac catalyst support (#1630)
Browse files Browse the repository at this point in the history
* 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
sbruens committed Aug 17, 2023
1 parent 9875803 commit 6c3c63a
Show file tree
Hide file tree
Showing 28 changed files with 1,478 additions and 236 deletions.
293 changes: 267 additions & 26 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"copy-webpack-plugin": "^5.1.1",
"cordova-android": "^11.0.0",
"cordova-browser": "~6.0.0",
"cordova-ios": "~6.3.0",
"cordova-ios": "github:apache/cordova-ios#1a5cd45e2243b239b5045a0ade9d2da1d779b72a",
"cordova-lib": "^11.0.0",
"cordova-osx": "github:apache/cordova-osx",
"cordova-plugin-clipboard": "github:Jigsaw-Code/outline-cordova-plugin-clipboard#v2.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/cordova/apple/OutlineAppleLib/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.build
/Packages
/*.xcodeproj
/*.xcframework
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
Expand Down
51 changes: 42 additions & 9 deletions src/cordova/apple/OutlineAppleLib/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,71 @@ let package = Package(
products: [
.library(
name: "OutlineAppleLib",
targets: ["Tun2socks", "OutlineSentryLogger", "OutlineTunnel"]),
targets: ["Tun2socks", "OutlineSentryLogger", "OutlineTunnel", "OutlineCatalystApp", "OutlineNotification"]
),
.library(
name: "OutlineLauncher",
targets: ["OutlineLauncher"]
),
.library(
name: "OutlineAppKitBridge",
targets: ["OutlineAppKitBridge"]
),
.library(
name: "PacketTunnelProvider",
targets: ["PacketTunnelProvider"]),
targets: ["PacketTunnelProvider"]
),
],
dependencies: [
.package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack.git", from: "3.7.4"),
.package(url: "https://github.com/getsentry/sentry-cocoa", from: "7.31.3"),
],
targets: [
.target(
name: "OutlineLauncher",
dependencies:
["CocoaLumberjack",
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
"OutlineCatalystApp"]
),
.target(
name: "OutlineCatalystApp",
dependencies: [
"OutlineAppKitBridge",
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
]
),
.target(
name: "OutlineAppKitBridge",
dependencies: [
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
"OutlineNotification",
]
),
.target(
name: "PacketTunnelProvider",
dependencies:
["CocoaLumberjack",
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
"Tun2socks",
"OutlineTunnel"
],
["CocoaLumberjack",
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
"Tun2socks",
"OutlineTunnel"],
cSettings: [
.headerSearchPath("Internal"),
]
),
.target(name: "OutlineNotification"),
.target(
name: "OutlineSentryLogger",
dependencies: [
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
.product(name: "Sentry", package: "sentry-cocoa")
.product(name: "Sentry", package: "sentry-cocoa"),
]
),
.target(
name: "OutlineTunnel",
dependencies: [
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
"Tun2socks",
]
),
.binaryTarget(
Expand All @@ -50,6 +82,7 @@ let package = Package(
),
.testTarget(
name: "OutlineTunnelTest",
dependencies: ["OutlineTunnel", "PacketTunnelProvider"]),
dependencies: ["OutlineTunnel", "PacketTunnelProvider"]
),
]
)
5 changes: 3 additions & 2 deletions src/cordova/apple/OutlineAppleLib/README.md
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.
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
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)
}
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading

0 comments on commit 6c3c63a

Please sign in to comment.