Skip to content

Commit

Permalink
Media keys are useful now. | #15
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG committed Apr 24, 2016
1 parent 84453a3 commit 7f3971a
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 93 deletions.
2 changes: 1 addition & 1 deletion WebShell.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
children = (
6E6055631C5EA0FC0099AD18 /* WebShell Core */,
6E6055601C5EA04F0099AD18 /* HTML5 Support */,
25B2225A1C25328E00F848B7 /* AppDelegate.swift */,
6EE64A951CCBEAA000E9DA9B /* Settings.swift */,
6E2DD7DD1CC56209000FA61A /* Credits.rtf */,
25B2225E1C25328E00F848B7 /* Assets.xcassets */,
Expand All @@ -113,6 +112,7 @@
6E6055631C5EA0FC0099AD18 /* WebShell Core */ = {
isa = PBXGroup;
children = (
25B2225A1C25328E00F848B7 /* AppDelegate.swift */,
6EED81641CC95FBD00FFB883 /* WebShellMediaKeysSupport.swift */,
6EED81651CC95FBD00FFB883 /* WebShellTrackpadGestures.swift */,
6E6055641C5EA1140099AD18 /* WebShellDebug.swift */,
Expand Down
17 changes: 12 additions & 5 deletions WebShell/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions WebShell/WebShellCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
40 changes: 22 additions & 18 deletions WebShell/WebShellDebug.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
}
}
124 changes: 103 additions & 21 deletions WebShell/WebShellMediaKeysSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -26,37 +30,115 @@ class WebShellMediaKeysSupport: NSApplication {
let keyRepeat = (keyFlags & 0x1)
mediaKeyEvent(Int32(keyCode), state: keyState, keyRepeat: Bool(keyRepeat))
}

super.sendEvent(theEvent)
}

func mediaKeyEvent(key: Int32, state: Bool, keyRepeat: Bool) {
// 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)
}
}
}
Loading

0 comments on commit 7f3971a

Please sign in to comment.