Skip to content

Commit

Permalink
Refactoring browser code
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Aug 29, 2020
1 parent 2802f61 commit 6309208
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
45 changes: 7 additions & 38 deletions Finicky/Finicky/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele

let invalidImg: NSImage! = NSImage(named: "statusitemerror")
invalidImg.isTemplate = true

let bar = NSStatusBar.system

// Workaround for some bug: -1 instead of NSVariableStatusItemLength
statusItem = bar.statusItem(withLength: CGFloat(-1))
statusItem = NSStatusBar.system.statusItem(withLength: CGFloat(-1))
statusItem.menu = statusItemMenu
statusItem.highlightMode = true
statusItem.image = invalidImg
(statusItem.button?.cell! as! NSButtonCell).highlightsBy = NSCell.StyleMask.changeBackgroundCellMask
statusItem.button?.image = invalidImg

toggleDockIcon(showIcon: false)

func configureAppOptions(
Expand All @@ -57,9 +57,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele

func updateStatus(valid: Bool) {
if valid {
statusItem.image = img
statusItem.button?.image = img
} else {
statusItem.image = invalidImg
statusItem.button?.image = invalidImg
}
}

Expand Down Expand Up @@ -235,31 +235,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
})
}

func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
if browsers.count == 0 {
return nil
}

if browsers.count == 1 {
return browsers.first
}

for browser in browsers {
if let bundleId = browser.bundleId {
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId)
if !apps.isEmpty {
let app: NSRunningApplication = apps[0]
let bundleIdentifier = app.bundleIdentifier
if bundleIdentifier != nil {
return browser
}
}
}
}

// If we are here, no apps are running, so we return the first bundleIds in the array instead.
return browsers.first
}

@objc func callUrlHandlers(_ sourceBundleIdentifier: String?, url: URL, sourceProcessPath: String?) {
if let appDescriptor = configLoader.determineOpeningApp(url: url, sourceBundleIdentifier: sourceBundleIdentifier, sourceProcessPath: sourceProcessPath) {
Expand Down Expand Up @@ -294,12 +269,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
showTestConfigWindow(nil)
}

func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
print("Opening \(browserOpts) at: " + url.absoluteString)
let command = getBrowserCommand(browserOpts, url: url)
shell(command)
}

func application(_: NSApplication, openFiles filenames: [String]) {
toggleDockIcon(showIcon: false)
for filename in filenames {
Expand Down
33 changes: 33 additions & 0 deletions Finicky/Finicky/Browsers.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@

import AppKit
import Foundation

public func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
if browsers.count == 0 {
return nil
}

if browsers.count == 1 {
return browsers.first
}

for browser in browsers {
if let bundleId = browser.bundleId {
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId)
if !apps.isEmpty {
let app: NSRunningApplication = apps[0]
let bundleIdentifier = app.bundleIdentifier
if bundleIdentifier != nil {
return browser
}
}
}
}

// If we are here, no apps are running, so we return the first bundleIds in the array instead.
return browsers.first
}

public func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
print("Opening \(browserOpts) at: " + url.absoluteString)
let command = getBrowserCommand(browserOpts, url: url)
shell(command)
}

enum Browser: String {
case Chrome = "com.google.chrome"
case ChromeCanary = "com.google.chrome.canary"
Expand Down

0 comments on commit 6309208

Please sign in to comment.