Skip to content

Commit

Permalink
Merge branch 'master' into maddyhof-patch-1
Browse files Browse the repository at this point in the history
  • Loading branch information
maddyhof authored Aug 18, 2023
2 parents 930a49c + 6c3c63a commit b2c1110
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 b2c1110

Please sign in to comment.