Skip to content

Commit 6309208

Browse files
committed
Refactoring browser code
1 parent 2802f61 commit 6309208

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

Finicky/Finicky/AppDelegate.swift

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
3030

3131
let invalidImg: NSImage! = NSImage(named: "statusitemerror")
3232
invalidImg.isTemplate = true
33-
34-
let bar = NSStatusBar.system
33+
3534
// Workaround for some bug: -1 instead of NSVariableStatusItemLength
36-
statusItem = bar.statusItem(withLength: CGFloat(-1))
35+
statusItem = NSStatusBar.system.statusItem(withLength: CGFloat(-1))
3736
statusItem.menu = statusItemMenu
38-
statusItem.highlightMode = true
39-
statusItem.image = invalidImg
37+
(statusItem.button?.cell! as! NSButtonCell).highlightsBy = NSCell.StyleMask.changeBackgroundCellMask
38+
statusItem.button?.image = invalidImg
39+
4040
toggleDockIcon(showIcon: false)
4141

4242
func configureAppOptions(
@@ -57,9 +57,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
5757

5858
func updateStatus(valid: Bool) {
5959
if valid {
60-
statusItem.image = img
60+
statusItem.button?.image = img
6161
} else {
62-
statusItem.image = invalidImg
62+
statusItem.button?.image = invalidImg
6363
}
6464
}
6565

@@ -235,31 +235,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
235235
})
236236
}
237237

238-
func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
239-
if browsers.count == 0 {
240-
return nil
241-
}
242-
243-
if browsers.count == 1 {
244-
return browsers.first
245-
}
246-
247-
for browser in browsers {
248-
if let bundleId = browser.bundleId {
249-
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId)
250-
if !apps.isEmpty {
251-
let app: NSRunningApplication = apps[0]
252-
let bundleIdentifier = app.bundleIdentifier
253-
if bundleIdentifier != nil {
254-
return browser
255-
}
256-
}
257-
}
258-
}
259-
260-
// If we are here, no apps are running, so we return the first bundleIds in the array instead.
261-
return browsers.first
262-
}
263238

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

297-
func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
298-
print("Opening \(browserOpts) at: " + url.absoluteString)
299-
let command = getBrowserCommand(browserOpts, url: url)
300-
shell(command)
301-
}
302-
303272
func application(_: NSApplication, openFiles filenames: [String]) {
304273
toggleDockIcon(showIcon: false)
305274
for filename in filenames {

Finicky/Finicky/Browsers.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11

2+
import AppKit
23
import Foundation
34

5+
public func getActiveApp(browsers: [BrowserOpts]) -> BrowserOpts? {
6+
if browsers.count == 0 {
7+
return nil
8+
}
9+
10+
if browsers.count == 1 {
11+
return browsers.first
12+
}
13+
14+
for browser in browsers {
15+
if let bundleId = browser.bundleId {
16+
let apps = NSRunningApplication.runningApplications(withBundleIdentifier: bundleId)
17+
if !apps.isEmpty {
18+
let app: NSRunningApplication = apps[0]
19+
let bundleIdentifier = app.bundleIdentifier
20+
if bundleIdentifier != nil {
21+
return browser
22+
}
23+
}
24+
}
25+
}
26+
27+
// If we are here, no apps are running, so we return the first bundleIds in the array instead.
28+
return browsers.first
29+
}
30+
31+
public func openUrlWithBrowser(_ url: URL, browserOpts: BrowserOpts) {
32+
print("Opening \(browserOpts) at: " + url.absoluteString)
33+
let command = getBrowserCommand(browserOpts, url: url)
34+
shell(command)
35+
}
36+
437
enum Browser: String {
538
case Chrome = "com.google.chrome"
639
case ChromeCanary = "com.google.chrome.canary"

0 commit comments

Comments
 (0)