-
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.
Merge branch 'master' into maddyhof-patch-1
- 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.