Skip to content

Commit

Permalink
Use enum instead of boolean to represent connection status.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbruens committed Jul 26, 2023
1 parent eff9b8d commit c7132ee
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import ServiceManagement
import OutlineShared

public class AppKitBridge: NSObject, AppKitBridgeProtocol {
private var statusItemController: OutlineStatusItemController?
private var statusItemController: StatusItemController?
static let kAppGroup = "QT8Z3Q9V3A.org.outline.macos.client"
static let kAppLauncherName = "launcher3"

Expand All @@ -31,12 +31,12 @@ public class AppKitBridge: NSObject, AppKitBridgeProtocol {
NSApp.terminate(self)
}

@objc public func setConnectionStatus(_ isConnected: Bool) {
@objc public func setConnectionStatus(_ status: ConnectionStatus) {
if statusItemController == nil {
DDLogInfo("[AppKitBridge] No status item controller found. Creating one now.")
statusItemController = OutlineStatusItemController()
statusItemController = StatusItemController()
}
statusItemController!.setStatus(isConnected: isConnected)
statusItemController!.setStatus(status: status)
}

// Enables or disables the embedded app launcher as a login item.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
#if canImport(AppKit)
import AppKit
import CocoaLumberjackSwift
import OutlineShared

var OutlineStatusItem = NSStatusItem()
var StatusItem = NSStatusItem()

class OutlineStatusItemController: NSObject {
class StatusItemController: NSObject {
let connectionStatusMenuItem = NSMenuItem(title: MenuTitle.statusDisconnected,
action: nil,
keyEquivalent: "")
Expand All @@ -39,9 +40,9 @@ class OutlineStatusItemController: NSObject {
override init() {
super.init()

DDLogInfo("[OutlineStatusItemController] Creating status menu")
OutlineStatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
setStatus(isConnected: false)
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")
Expand All @@ -52,13 +53,14 @@ class OutlineStatusItemController: NSObject {
let closeMenuItem = NSMenuItem(title: MenuTitle.quit, action: #selector(closeApplication), keyEquivalent: "q")
closeMenuItem.target = self
menu.addItem(closeMenuItem)
OutlineStatusItem.menu = menu
StatusItem.menu = menu
}

func setStatus(isConnected: Bool) {
func setStatus(status: ConnectionStatus) {
let isConnected = status == .connected
let appIconImage = isConnected ? AppIconImage.statusConnected : AppIconImage.statusDisconnected
appIconImage.isTemplate = true
OutlineStatusItem.button?.image = appIconImage
StatusItem.button?.image = appIconImage

let connectionStatusTitle = isConnected ? MenuTitle.statusConnected : MenuTitle.statusDisconnected
connectionStatusMenuItem.title = connectionStatusTitle
Expand All @@ -74,7 +76,7 @@ class OutlineStatusItemController: NSObject {
}

@objc func openApplication(_: AnyObject?) {
DDLogInfo("[OutlineStatusItemController] Opening application")
DDLogInfo("[StatusItemController] Opening application")
NSApp.activate(ignoringOtherApps: true)
guard let uiWindow = getUiWindow() else {
return
Expand All @@ -83,7 +85,7 @@ class OutlineStatusItemController: NSObject {
}

@objc func closeApplication(_: AnyObject?) {
DDLogInfo("[OutlineStatusItemController] Closing application")
DDLogInfo("[StatusItemController] Closing application")
NotificationCenter.default.post(name: .kAppQuit, object: nil)
NSApplication.shared.terminate(self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,20 @@ public class OutlineCatalystApp : NSObject {
windowScene.sizeRestrictions?.maximumSize = CGSizeMake(400, 550)
}

// Initiate the connection status menu in disabled state by default.
appKitBridge.setConnectionStatus(false)
// Initiate the connection status menu in unknown state by default.
appKitBridge.setConnectionStatus(.unknown)

NotificationCenter.default.addObserver(forName: NSNotification.kVpnConnected,
object: nil,
queue: nil)
{ _ in
appKitBridge.setConnectionStatus(true)
appKitBridge.setConnectionStatus(.connected)
}
NotificationCenter.default.addObserver(forName: NSNotification.kVpnDisconnected,
object: nil,
queue: nil)
{ _ in
appKitBridge.setConnectionStatus(false)
appKitBridge.setConnectionStatus(.disconnected)
}

// Enable app launcher to start on boot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public protocol AppKitBridgeProtocol: NSObjectProtocol {

func terminate()

func setConnectionStatus(_ isConnected: Bool)
func setConnectionStatus(_ status: ConnectionStatus)

func setAppLauncherEnabled(_ isEnabled: Bool)

Expand Down
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
}

0 comments on commit c7132ee

Please sign in to comment.