Skip to content

Commit

Permalink
Add option to hide status bar icon
Browse files Browse the repository at this point in the history
  • Loading branch information
johnste committed Apr 9, 2019
1 parent f04199d commit 5cb2ff1
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
11 changes: 9 additions & 2 deletions Finicky/Finicky/AppDelegate.swift
Expand Up @@ -33,6 +33,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
statusItem.highlightMode = true
statusItem.image = img
_ = toggleDockIcon(showIcon: false)

func toggleIconCallback(show: Bool) {
guard statusItem != nil else { return }
statusItem.isVisible = !show
}

configLoader = FinickyConfig(toggleIconCallback: toggleIconCallback)
configLoader.reload(showSuccess: false)

}

@IBAction func reloadConfig(_ sender: NSMenuItem) {
Expand Down Expand Up @@ -170,8 +179,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele


func applicationWillFinishLaunching(_ aNotification: Notification) {
configLoader = FinickyConfig()
configLoader.reload(showSuccess: false)
shortUrlResolver = FNShortUrlResolver()
let appleEventManager:NSAppleEventManager = NSAppleEventManager.shared()
appleEventManager.setEventHandler(self, andSelector: #selector(AppDelegate.handleGetURLEvent(_:withReplyEvent:)), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL))
Expand Down
27 changes: 21 additions & 6 deletions Finicky/Finicky/Config.swift
Expand Up @@ -34,15 +34,16 @@ open class FinickyConfig {
var ctx: JSContext!
var validateConfigJS : String?;
var processUrlJS : String?;
var hasError: Bool;
var hasError: Bool = false;

var dispatchSource: DispatchSourceFileSystemObject?
var fileDescriptor: Int32 = -1
var fileManager = FileManager.init();
var lastModificationDate: Date? = nil;
var toggleIconCallback:(_ hide: Bool) -> Void;

public init() {
self.hasError = false;
public init(toggleIconCallback: @escaping (_ hide: Bool) -> Void ) {
self.toggleIconCallback = toggleIconCallback
listenToChanges();

if let path = Bundle.main.path(forResource: "validateConfig.js", ofType: nil ) {
Expand Down Expand Up @@ -134,18 +135,20 @@ open class FinickyConfig {
}

let validConfig = ctx.evaluateScript(validateConfigJS!)?.call(withArguments: [])

if let isBoolean = validConfig?.isBoolean {
if (isBoolean) {
let result = validConfig?.toBool()
if (!result!) {
if (!(validConfig?.toBool())!) {
print("Invalid config")
showNotification(title: "Invalid config")
return false;
} else {
return true;
}
}
}

return true;
return false;
}

func reload(showSuccess: Bool) {
Expand Down Expand Up @@ -174,9 +177,21 @@ open class FinickyConfig {
if config != nil {
let success = parseConfig(config!)
if (success && showSuccess) {
toggleIconCallback(getHideIcon())
showNotification(title: "Reloaded config successfully")
}
}


}

open func getHideIcon() -> Bool {
let hideIcon = ctx.evaluateScript("module.exports.options && module.exports.options.hideIcon")?.toBool()

if hideIcon == nil {
return false
}
return hideIcon!;
}

open func determineOpeningApp(url: URL) -> AppDescriptor? {
Expand Down
3 changes: 2 additions & 1 deletion Finicky/Finicky/Notifications.swift
@@ -1,7 +1,7 @@
import Foundation
import AppKit

func showNotification(title: String, subtitle: String? = nil, informativeText: String? = nil) -> Void {
func showNotification(title: String, subtitle: String? = nil, informativeText: String? = nil, error: Bool = false) -> Void {
let notification = NSUserNotification()

notification.title = title
Expand All @@ -14,5 +14,6 @@ func showNotification(title: String, subtitle: String? = nil, informativeText: S
}

notification.soundName = NSUserNotificationDefaultSoundName

NSUserNotificationCenter.default.deliver(notification)
}
39 changes: 31 additions & 8 deletions Finicky/Finicky/validateConfig.js
@@ -1,4 +1,13 @@
(function() {


function isOfType(value, type = []) {
const types = Array.isArray(type) ? type : [type];
return types.includes(typeof value);
}

const isBool = (value) => [isOfType(value, "boolean"), "boolean", typeof value];

return function validateConfig() {
if (!module || !module.exports) {
throw new Error("module.exports is not defined");
Expand All @@ -9,6 +18,25 @@
}

module.exports.handlers.forEach(validateHandler);

const options = module.exports.options;
if (options && typeof options === "object") {

const validateOptions = { hideIcon: isBool };

Object.keys(options).forEach(key => {
if (!validateOptions[key]) {
throw new Error("module.exports.options contained an invalid option: " + key);
}

const [valid, expectedType, actualType] = validateOptions[key](options[key]);
if (!valid) {
throw new Error(`module.exports.options.${key} expected to be type ${expectedType}, but was ${actualType}`);
}
});
}

return true;
};

function validateHandler(handler, index) {
Expand Down Expand Up @@ -48,14 +76,9 @@
}

if (typeof value === "object" && typeof value.value !== "string") {
throw new Error(
`Handler #${handlerNum} value: expected object to have string property ${typeof value.value}`
);
throw new Error(
`Handler #${handlerNum} value: expected object to have string property ${typeof value.value}`
);
}
}

function isOfType(value, type = []) {
const types = Array.isArray(type) ? type : [type];
return types.includes(typeof value);
}
})();

0 comments on commit 5cb2ff1

Please sign in to comment.