diff --git a/WebShell.xcodeproj/project.pbxproj b/WebShell.xcodeproj/project.pbxproj index 0b7afec..05f8605 100755 --- a/WebShell.xcodeproj/project.pbxproj +++ b/WebShell.xcodeproj/project.pbxproj @@ -89,7 +89,6 @@ children = ( 6E6055631C5EA0FC0099AD18 /* WebShell Core */, 6E6055601C5EA04F0099AD18 /* HTML5 Support */, - 25B2225A1C25328E00F848B7 /* AppDelegate.swift */, 6EE64A951CCBEAA000E9DA9B /* Settings.swift */, 6E2DD7DD1CC56209000FA61A /* Credits.rtf */, 25B2225E1C25328E00F848B7 /* Assets.xcassets */, @@ -113,6 +112,7 @@ 6E6055631C5EA0FC0099AD18 /* WebShell Core */ = { isa = PBXGroup; children = ( + 25B2225A1C25328E00F848B7 /* AppDelegate.swift */, 6EED81641CC95FBD00FFB883 /* WebShellMediaKeysSupport.swift */, 6EED81651CC95FBD00FFB883 /* WebShellTrackpadGestures.swift */, 6E6055641C5EA1140099AD18 /* WebShellDebug.swift */, diff --git a/WebShell/Settings.swift b/WebShell/Settings.swift index 3b782d7..eda0a1d 100755 --- a/WebShell/Settings.swift +++ b/WebShell/Settings.swift @@ -10,15 +10,14 @@ import Foundation /** WebShell Class - + This class is the main class for WebShell. */ class WebShell { - /** - The settings Dictionary - */ + /** + The settings Dictionary + */ var Settings: [String: Any] = [ - // Url to browse to. "url": "https://www.google.com", @@ -62,6 +61,14 @@ class WebShell { // Please paste here the CSS you want to load on a website "CSSInject": "", + "MediaKeys": [ + // Enable "Back" & "Forward" + "BackAndForward": true, + + // Media Player support (experimental) + "MediaPlayers": false + ], + "Contextmenu": [ // Enable "Back" & "Forward" "BackAndForward": true, diff --git a/WebShell/WebShellCore.swift b/WebShell/WebShellCore.swift index bb3f6df..79d4825 100755 --- a/WebShell/WebShellCore.swift +++ b/WebShell/WebShellCore.swift @@ -35,4 +35,8 @@ extension ViewController { func delay(delay: Double, _ closure: () -> ()) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure) } + + func runOnMain(run: () -> ()) { + dispatch_async(dispatch_get_main_queue(), run) + } } diff --git a/WebShell/WebShellDebug.swift b/WebShell/WebShellDebug.swift index 203c8c5..402d890 100755 --- a/WebShell/WebShellDebug.swift +++ b/WebShell/WebShellDebug.swift @@ -84,14 +84,14 @@ extension ViewController { // if can back if (contextMenu["BackAndForward"]!) { if (mainWebview.canGoBack) { - NewMenu.append(NSMenuItem.init(title: "Back", action: #selector(ViewController.goBack(_:)), keyEquivalent: "")) + NewMenu.append(NSMenuItem.init(title: "Back", action: #selector(ViewController._goBack(_:)), keyEquivalent: "")) } if (mainWebview.canGoForward) { - NewMenu.append(NSMenuItem.init(title: "Forward", action: #selector(ViewController.goForward(_:)), keyEquivalent: "")) + NewMenu.append(NSMenuItem.init(title: "Forward", action: #selector(ViewController._goForward(_:)), keyEquivalent: "")) } } if (contextMenu["Reload"]!) { - NewMenu.append(NSMenuItem.init(title: "Reload", action: #selector(ViewController.reloadPage(_:)), keyEquivalent: "")) + NewMenu.append(NSMenuItem.init(title: "Reload", action: #selector(ViewController._reloadPage(_:)), keyEquivalent: "")) } if (download) { @@ -233,25 +233,29 @@ extension ViewController { } } - func goBack(Sender: AnyObject) -> Void { - mainWebview.goBack(Sender) + func _goBack(Sender: AnyObject) -> Void { + if (mainWebview.canGoBack) { + mainWebview.goBack(Sender) + } } - func goForward(Sender: AnyObject) -> Void { - mainWebview.goForward(Sender) + func _goForward(Sender: AnyObject) -> Void { + if (mainWebview.canGoForward) { + mainWebview.goForward(Sender) + } } - func reloadPage(Sender: AnyObject) -> Void { + func _reloadPage(Sender: AnyObject) -> Void { mainWebview.reload(Sender) } - - // Debug: Open new window - func createNewInstance(Sender: AnyObject) -> Void { - openNewWindow(url: "\(lastURL)", height: "0", width: "0") - } - - func downloadFileWithURL(Sender: AnyObject) -> Void { - let wsDM = WebShelllDownloadManager.init(url: lastURL) - wsDM.endDownloadTask() - } + + // Debug: Open new window + func createNewInstance(Sender: AnyObject) -> Void { + openNewWindow(url: "\(lastURL)", height: "0", width: "0") + } + + func downloadFileWithURL(Sender: AnyObject) -> Void { + let wsDM = WebShelllDownloadManager.init(url: lastURL) + wsDM.endDownloadTask() + } } diff --git a/WebShell/WebShellMediaKeysSupport.swift b/WebShell/WebShellMediaKeysSupport.swift index f22309a..cefc91e 100755 --- a/WebShell/WebShellMediaKeysSupport.swift +++ b/WebShell/WebShellMediaKeysSupport.swift @@ -12,11 +12,15 @@ import Cocoa /** Class WebShellMediaKeysSupport - - This class will support the WebShell media keys. + + This class will support the WebShell media keys. \ + \ + !important note, this class can not communicate with the ViewController.\ + The communication goes via NSUserDefaults. */ class WebShellMediaKeysSupport: NSApplication { - + let MediaKeysSettings = WebShell().Settings["MediaKeys"] as! [String: Bool] + override func sendEvent(theEvent: NSEvent) { if theEvent.type == .SystemDefined && theEvent.subtype.rawValue == 8 { let keyCode = ((theEvent.data1 & 0xFFFF0000) >> 16) @@ -26,7 +30,7 @@ class WebShellMediaKeysSupport: NSApplication { let keyRepeat = (keyFlags & 0x1) mediaKeyEvent(Int32(keyCode), state: keyState, keyRepeat: Bool(keyRepeat)) } - + super.sendEvent(theEvent) } @@ -34,29 +38,107 @@ class WebShellMediaKeysSupport: NSApplication { // Only send events on KeyDown. Without this check, these events will happen twice if (state) { switch (key) { - case NX_KEYTYPE_PLAY: - print("Play!") - // F8 pressed - break - case NX_KEYTYPE_FAST: - print("Fast!") - // F9 pressed + case NX_KEYTYPE_PLAY: // F8 / Play + if (MediaKeysSettings["BackAndForward"] == true) { + self.goReloadPage() + } else { + self.playPausePressed() + } break - case NX_KEYTYPE_REWIND: - print("Rewind!") - // F7 pressed + case NX_KEYTYPE_FAST, NX_KEYTYPE_NEXT: // F9 / Forward + if (MediaKeysSettings["BackAndForward"] == true) { + self.goForwardIfPossible() + } else { + self.nextItem() + } break - case NX_KEYTYPE_PREVIOUS: // Not called? - print("Previous!") - // F7 - break - case NX_KEYTYPE_NEXT: // Not called? - print("Next!") - // F9 pressed + case NX_KEYTYPE_REWIND, NX_KEYTYPE_PREVIOUS: // F7 / Backward + if (MediaKeysSettings["BackAndForward"] == true) { + self.goBackIfPossible() + } else { + self.previousItem() + } break default: break } } } + + /** + goBackIfPossible + + Since we can't communicate with the ViewController.\ + We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us. + */ + func goBackIfPossible() { + NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoBack") + NSUserDefaults.standardUserDefaults().synchronize() + } + + /** + goForwardIfPossible + + Since we can't communicate with the ViewController.\ + We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us. + */ + func goForwardIfPossible() { + NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoForward") + NSUserDefaults.standardUserDefaults().synchronize() + } + + /** + goReloadPage + + Since we can't communicate with the ViewController.\ + We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us. + */ + func goReloadPage() { + NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoReload") + NSUserDefaults.standardUserDefaults().synchronize() + } + + func nextItem() -> Bool { + // ... + return false + } + + func previousItem() -> Bool { + // ... + return false + } + + func playPausePressed() -> Bool { + // ... + return false + } +} + +extension ViewController { + /** + Communication for the WebShellMediaKeysSupport class + + - Parameter Sender: AnyObject (used for #selector use self) + */ + func WSMediaLoop(Sender: AnyObject) -> Void { + self.performSelector(#selector(ViewController.WSMediaLoop(_:)), withObject: nil, afterDelay: 0.5) + + if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoBack")) { + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoBack") + NSUserDefaults.standardUserDefaults().synchronize() + self._goBack(self) + } + + if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoForward")) { + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoForward") + NSUserDefaults.standardUserDefaults().synchronize() + self._goForward(self) + } + + if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoReload")) { + NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoReload") + NSUserDefaults.standardUserDefaults().synchronize() + self._reloadPage(self) + } + } } \ No newline at end of file diff --git a/WebShell/WebShellTrackpadGestures.swift b/WebShell/WebShellTrackpadGestures.swift index 3d4bd9f..771af0c 100755 --- a/WebShell/WebShellTrackpadGestures.swift +++ b/WebShell/WebShellTrackpadGestures.swift @@ -9,18 +9,17 @@ import Foundation import AppKit import WebKit - // TODO: Function 'swipeWithEvent' is not being called. /** This extension will support the swipe gestures -*/ + */ extension ViewController { - /** - SwipeWithEvent - - - Parameter event: NSEvent - */ + /** + SwipeWithEvent + + - Parameter event: NSEvent + */ override func swipeWithEvent(event: NSEvent) { let x: CGFloat = event.deltaX let y: CGFloat = event.deltaY @@ -58,26 +57,29 @@ extension ViewController { return true } - var userInteractionEnabled: Bool { - return true - } - - override func touchesBeganWithEvent(event: NSEvent) { - print("Touch \(event)") - } + var setAcceptsTouchEvents: Bool { + return true + } - override func touchesMovedWithEvent(event: NSEvent) { - print("Moved \(event)") - } + var userInteractionEnabled: Bool { + return true + } - override func touchesEndedWithEvent(event: NSEvent) { - print("Ended \(event)") - } + override func touchesBeganWithEvent(event: NSEvent) { + print("Touch \(event)") + } - override func touchesCancelledWithEvent(event: NSEvent) { - print("Cancelled \(event)") - } + override func touchesMovedWithEvent(event: NSEvent) { + print("Moved \(event)") + } + + override func touchesEndedWithEvent(event: NSEvent) { + print("Ended \(event)") + } + override func touchesCancelledWithEvent(event: NSEvent) { + print("Cancelled \(event)") + } } // TODO: Function 'swipeWithEvent' is not being called. @@ -86,30 +88,34 @@ extension ViewController { This extension will support the swipe gestures */ class x: WebView { - override var acceptsFirstResponder: Bool { - return true - } - - var userInteractionEnabled: Bool { - return true - } - - override func touchesBeganWithEvent(event: NSEvent) { - Swift.print("WV: Touch \(event)") - } - - override func touchesMovedWithEvent(event: NSEvent) { - Swift.print("WV: Moved \(event)") - } - - override func touchesEndedWithEvent(event: NSEvent) { - Swift.print("WV: Ended \(event)") - } - - override func touchesCancelledWithEvent(event: NSEvent) { - Swift.print("WV: Cancelled \(event)") - } - + override var acceptsFirstResponder: Bool { + return true + } + + var setAcceptsTouchEvents: Bool { + return true + } + + var userInteractionEnabled: Bool { + return true + } + + override func touchesBeganWithEvent(event: NSEvent) { + Swift.print("WV: Touch \(event)") + } + + override func touchesMovedWithEvent(event: NSEvent) { + Swift.print("WV: Moved \(event)") + } + + override func touchesEndedWithEvent(event: NSEvent) { + Swift.print("WV: Ended \(event)") + } + + override func touchesCancelledWithEvent(event: NSEvent) { + Swift.print("WV: Cancelled \(event)") + } + override func swipeWithEvent(event: NSEvent) { let deltaX = event.deltaX if deltaX > 0 { // Left diff --git a/WebShell/WebshellViewDid.swift b/WebShell/WebshellViewDid.swift index e0b1547..661fd03 100755 --- a/WebShell/WebshellViewDid.swift +++ b/WebShell/WebshellViewDid.swift @@ -51,6 +51,7 @@ extension ViewController { addObservers() initSettings() goHome() + WSMediaLoop(self) } } @@ -60,10 +61,12 @@ extension ViewController { mainWebview.UIDelegate = self mainWebview.resourceLoadDelegate = self mainWebview.downloadDelegate = self - + WebShell().Settings["WSMW"] = mainWebview; + checkSettings() addObservers() initSettings() goHome() + WSMediaLoop(self) } } \ No newline at end of file