Permalink
Browse files

Refactor

  • Loading branch information...
saagarjha committed Feb 11, 2018
1 parent 2f3bed4 commit 678fcc4fcefc938f9c10acb27754f6b6ff745133
@@ -5,7 +5,6 @@ target 'Shifty' do
pod 'Sparkle'
pod 'MASPreferences+Shifty'
pod 'MASShortcut'
pod 'AXSwift'
pod 'PublicSuffix'
pod 'SwiftLog'
end
@@ -0,0 +1,165 @@
//
// ScriptingBridge.swift
// Shifty
//
// Created by Saagar Jha on 1/14/18.
//
import Cocoa
import ScriptingBridge
@objc protocol Browser {
@objc optional var windows: SBElementArray { get }
}
@objc protocol Window {
@objc optional var currentTab: Tab { get }
@objc optional var activeTab: Tab { get }
}
@objc protocol Tab {
@objc optional var URL: String { get }
}
extension SBApplication: Browser {
convenience init?(_ application: NSRunningApplication) {
self.init(processIdentifier: application.processIdentifier)
}
}
extension SBObject: Window, Tab { }
//@objc protocol Browser {
// var windows: [Window] { get }
//}
//
//protocol Window {
// var currentTab: Tab { get }
//}
//
//protocol Tab {
// var URL: String { get }
//}
//
//// MARK: - Safari Scripting Bridge "header"
//
//@objc protocol SafariApplication: Browser {
// @objc optional var windows: SBElementArray { get }
//}
//
//@objc protocol SafariWindow {
// @objc optional var currentTab: SafariTab { get }
//}
//
//@objc protocol SafariTab {
// @objc optional var URL: String { get }
//}
//
//// MARK: - Chrome Scripting Bridge "header"
//
//@objc protocol ChromeApplication {
// @objc optional var windows: SBElementArray { get }
//}
//
//@objc protocol ChromeWindow {
// @objc optional var activeTab: ChromeTab { get }
//}
//
//extension ChromeWindow {
// @objc optional var currentTab: ChromeTab {
// return activeTab
// }
//}
//
//@objc protocol ChromeTab {
// @objc optional var URL: String { get }
//}
//
//extension SBApplication: SafariApplication, ChromeApplication { }
//extension SBObject: SafariWindow, SafariTab, ChromeWindow, ChromeTab { }
//@objcMembers class Foo: NSObject {
// var windows: [Window]? {
// return nil
// }
// var currentTab: Tab? {
// return nil
// }
// var activeTab: Tab? {
// return nil
// }
// func URL() -> String? {
// return nil
// }
//}
//
//@objc class Browser: SBApplication {
// var windows: [Window]? {
// return (perform(Selector(("windows"))).takeUnretainedValue() as? SBElementArray)?.flatMap {
// return $0 as? Window
// }
// }
//}
//
//@objc class Window: SBObject {
// var currentTab: Tab? {
// let selector: Selector
// if let c = NSClassFromString("SafariWindow"),
// isKind(of: c) {
// selector = Selector(("currentTab"))
// } else if let c = NSClassFromString("ChromeWindow"),
// isKind(of: c) {
// selector = Selector(("activeTab"))
// } else {
// return nil
// }
// return perform(selector).takeUnretainedValue() as? Tab
// }
//}
//
//@objc class Tab: SBObject {
// var URL: String? {
// return perform(Selector(("URL"))).takeUnretainedValue() as? String
// }
//}
//
//protocol SwiftBrowser {
// associatedtype Sequence: Swift.Sequence where Sequence.Element == Window
// var _windows: Sequence { get }
//}
//
//protocol ObjcBrowser {
// var windows: SBElementArray { get }
//}
//
//typealias Browser = SwiftBrowser & ObjcBrowser
//
//extension SwiftBrowser where Self: ObjcBrowser {
// var _windows: [Window] {
// return windows.flatMap {
// $0 as? Window
// }
// }
//}
//
//protocol Window {
// var currentTab: Tab { get }
//}
//
//@objc protocol Tab {
//}
//
//extension SafariApplication: Browser { }
//
//extension SafariTab: Tab {
//}
//
//extension SafariWindow: Window {
//// var _currentTab: Tab {
//// return currentTab
//// }
//
//}
//
//extension ChromeApplication: Browser { }
//

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,35 +1,35 @@
//
// AccessibilityPromptWindow.swift
//
//
//
// Created by Nate Thompson on 12/2/17.
//
import Cocoa
class AccessibilityPromptWindow: NSWindowController {
@IBOutlet weak var openSysPrefsButton: NSButton!
override var windowNibName: NSNib.Name {
return NSNib.Name("AccessibilityPromptWindow")
}
override func windowDidLoad() {
super.windowDidLoad()
self.window?.center()
self.window?.styleMask.remove(.resizable)
self.window?.level = .floating
openSysPrefsButton.title = NSLocalizedString("alert.open_preferences", comment: "Open System Preferences")
}
@IBAction func openSysPrefsClicked(sender: AnyObject) {
window?.close()
NSWorkspace.shared.open(URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")!)
}
@IBAction func notNowClicked(_ sender: Any) {
window?.close()
}
@@ -21,7 +21,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var statusMenu: NSMenu!
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
var statusItemClicked: (() -> Void)?
lazy var preferenceWindowController: PrefWindowController = {
return PrefWindowController(
viewControllers: [
@@ -30,12 +30,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
PrefAboutViewController()],
title: NSLocalizedString("prefs.title", comment: "Preferences"))
}()
func applicationDidFinishLaunching(_ aNotification: Notification) {
UserDefaults.standard.register(defaults: ["NSApplicationCrashOnExceptions": true])
Fabric.with([Crashlytics.self])
Event.appLaunched.record()
if !ProcessInfo().isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 12, patchVersion: 4)) {
Event.oldMacOSVersion(version: ProcessInfo().operatingSystemVersionString).record()
let alert: NSAlert = NSAlert()
@@ -44,42 +44,39 @@ class AppDelegate: NSObject, NSApplicationDelegate {
alert.alertStyle = NSAlert.Style.warning
alert.addButton(withTitle: NSLocalizedString("general.ok", comment: "OK"))
alert.runModal()
NSApplication.shared.terminate(self)
}
if !CBBlueLightClient.supportsBlueLightReduction() {
if !NightShiftManager.supportsNightShift {
Event.unsupportedHardware.record()
let alert: NSAlert = NSAlert()
alert.messageText = NSLocalizedString("alert.hardware_message", comment: "Your Mac does not support Night Shift")
alert.informativeText = NSLocalizedString("alert.hardware_informative", comment: "A newer Mac is required to use Shifty.")
alert.alertStyle = NSAlert.Style.warning
alert.addButton(withTitle: NSLocalizedString("general.ok", comment: "OK"))
alert.runModal()
NSApplication.shared.terminate(self)
}
let launcherAppIdentifier = "io.natethompson.ShiftyHelper"
var startedAtLogin = false
for app in NSWorkspace.shared.runningApplications {
if app.bundleIdentifier == launcherAppIdentifier {
startedAtLogin = true
}
let startedAtLogin = NSWorkspace.shared.runningApplications.contains {
$0.bundleIdentifier == launcherAppIdentifier
}
if startedAtLogin {
DistributedNotificationCenter.default().post(name: Notification.Name("killme"), object: Bundle.main.bundleIdentifier!)
DistributedNotificationCenter.default().post(name: .terminateApp, object: Bundle.main.bundleIdentifier!)
}
//Show alert if accessibility permissions have been revoked while app is not running
if UserDefaults.standard.bool(forKey: Keys.isWebsiteControlEnabled) &&
!UIElement.isProcessTrusted(withPrompt: false) {
let alert: NSAlert = NSAlert()
alert.messageText = NSLocalizedString("alert.accessibility_disabled_message", comment: "Accessibility permissions for Shifty have been disabled")
alert.informativeText = NSLocalizedString("alert.accessibility_informative", comment: "Grant access to Shifty in Security & Privacy preferences, located in System Preferences.")
alert.alertStyle = NSAlert.Style.warning
alert.alertStyle = .warning
alert.addButton(withTitle: NSLocalizedString("alert.open_preferences", comment: "Open System Preferences"))
alert.addButton(withTitle: NSLocalizedString("alert.not_now", comment: "Not now"))
if alert.runModal() == .alertFirstButtonReturn {
@@ -88,23 +85,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
UserDefaults.standard.set(false, forKey: Keys.isWebsiteControlEnabled)
}
}
Log.logger.directory = "~/Library/Logs/Shifty"
Log.logger.name = "Shifty"
logw("log file created")
setMenuBarIcon()
updateMenuBarIcon()
setStatusToggle()
}
func setMenuBarIcon() {
func updateMenuBarIcon() {
var icon: NSImage
if UserDefaults.standard.bool(forKey: Keys.isIconSwitchingEnabled) {
if !BLClient.isNightShiftEnabled {
icon = #imageLiteral(resourceName: "sunOpenIcon")
} else {
icon = #imageLiteral(resourceName: "shiftyMenuIcon")
}
if UserDefaults.standard.bool(forKey: Keys.isIconSwitchingEnabled),
!NightShiftManager.isNightShiftEnabled {
icon = #imageLiteral(resourceName: "sunOpenIcon")
} else {
icon = #imageLiteral(resourceName: "shiftyMenuIcon")
}
@@ -113,23 +107,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
self.statusItem.button?.image = icon
}
}
func setStatusToggle() {
if prefs.bool(forKey: Keys.isStatusToggleEnabled) {
statusItem.menu = nil
if let button = statusItem.button {
button.action = #selector(self.statusBarButtonClicked(sender:))
button.action = #selector(statusBarButtonClicked)
button.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
} else {
statusItem.menu = statusMenu
}
}
@objc func statusBarButtonClicked(sender: NSStatusBarButton) {
let event = NSApp.currentEvent!
if event.type == NSEvent.EventType.rightMouseUp || event.modifierFlags.contains(.control) {
if event.type == NSEvent.EventType.rightMouseUp || event.modifierFlags.contains(.control) {
statusItem.menu = statusMenu
statusItem.popUpMenu(statusMenu)
statusItem.menu = nil

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 678fcc4

Please sign in to comment.