Skip to content

Commit 7f3971a

Browse files
committed
Media keys are useful now. | #15
1 parent 84453a3 commit 7f3971a

File tree

7 files changed

+199
-93
lines changed

7 files changed

+199
-93
lines changed

WebShell.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
children = (
9090
6E6055631C5EA0FC0099AD18 /* WebShell Core */,
9191
6E6055601C5EA04F0099AD18 /* HTML5 Support */,
92-
25B2225A1C25328E00F848B7 /* AppDelegate.swift */,
9392
6EE64A951CCBEAA000E9DA9B /* Settings.swift */,
9493
6E2DD7DD1CC56209000FA61A /* Credits.rtf */,
9594
25B2225E1C25328E00F848B7 /* Assets.xcassets */,
@@ -113,6 +112,7 @@
113112
6E6055631C5EA0FC0099AD18 /* WebShell Core */ = {
114113
isa = PBXGroup;
115114
children = (
115+
25B2225A1C25328E00F848B7 /* AppDelegate.swift */,
116116
6EED81641CC95FBD00FFB883 /* WebShellMediaKeysSupport.swift */,
117117
6EED81651CC95FBD00FFB883 /* WebShellTrackpadGestures.swift */,
118118
6E6055641C5EA1140099AD18 /* WebShellDebug.swift */,

WebShell/Settings.swift

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ import Foundation
1010

1111
/**
1212
WebShell Class
13-
13+
1414
This class is the main class for WebShell.
1515
*/
1616
class WebShell {
17-
/**
18-
The settings Dictionary
19-
*/
17+
/**
18+
The settings Dictionary
19+
*/
2020
var Settings: [String: Any] = [
21-
2221
// Url to browse to.
2322
"url": "https://www.google.com",
2423

@@ -62,6 +61,14 @@ class WebShell {
6261
// Please paste here the CSS you want to load on a website
6362
"CSSInject": "",
6463

64+
"MediaKeys": [
65+
// Enable "Back" & "Forward"
66+
"BackAndForward": true,
67+
68+
// Media Player support (experimental)
69+
"MediaPlayers": false
70+
],
71+
6572
"Contextmenu": [
6673
// Enable "Back" & "Forward"
6774
"BackAndForward": true,

WebShell/WebShellCore.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ extension ViewController {
3535
func delay(delay: Double, _ closure: () -> ()) {
3636
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))), dispatch_get_main_queue(), closure)
3737
}
38+
39+
func runOnMain(run: () -> ()) {
40+
dispatch_async(dispatch_get_main_queue(), run)
41+
}
3842
}

WebShell/WebShellDebug.swift

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ extension ViewController {
8484
// if can back
8585
if (contextMenu["BackAndForward"]!) {
8686
if (mainWebview.canGoBack) {
87-
NewMenu.append(NSMenuItem.init(title: "Back", action: #selector(ViewController.goBack(_:)), keyEquivalent: ""))
87+
NewMenu.append(NSMenuItem.init(title: "Back", action: #selector(ViewController._goBack(_:)), keyEquivalent: ""))
8888
}
8989
if (mainWebview.canGoForward) {
90-
NewMenu.append(NSMenuItem.init(title: "Forward", action: #selector(ViewController.goForward(_:)), keyEquivalent: ""))
90+
NewMenu.append(NSMenuItem.init(title: "Forward", action: #selector(ViewController._goForward(_:)), keyEquivalent: ""))
9191
}
9292
}
9393
if (contextMenu["Reload"]!) {
94-
NewMenu.append(NSMenuItem.init(title: "Reload", action: #selector(ViewController.reloadPage(_:)), keyEquivalent: ""))
94+
NewMenu.append(NSMenuItem.init(title: "Reload", action: #selector(ViewController._reloadPage(_:)), keyEquivalent: ""))
9595
}
9696

9797
if (download) {
@@ -233,25 +233,29 @@ extension ViewController {
233233
}
234234
}
235235

236-
func goBack(Sender: AnyObject) -> Void {
237-
mainWebview.goBack(Sender)
236+
func _goBack(Sender: AnyObject) -> Void {
237+
if (mainWebview.canGoBack) {
238+
mainWebview.goBack(Sender)
239+
}
238240
}
239241

240-
func goForward(Sender: AnyObject) -> Void {
241-
mainWebview.goForward(Sender)
242+
func _goForward(Sender: AnyObject) -> Void {
243+
if (mainWebview.canGoForward) {
244+
mainWebview.goForward(Sender)
245+
}
242246
}
243247

244-
func reloadPage(Sender: AnyObject) -> Void {
248+
func _reloadPage(Sender: AnyObject) -> Void {
245249
mainWebview.reload(Sender)
246250
}
247-
248-
// Debug: Open new window
249-
func createNewInstance(Sender: AnyObject) -> Void {
250-
openNewWindow(url: "\(lastURL)", height: "0", width: "0")
251-
}
252-
253-
func downloadFileWithURL(Sender: AnyObject) -> Void {
254-
let wsDM = WebShelllDownloadManager.init(url: lastURL)
255-
wsDM.endDownloadTask()
256-
}
251+
252+
// Debug: Open new window
253+
func createNewInstance(Sender: AnyObject) -> Void {
254+
openNewWindow(url: "\(lastURL)", height: "0", width: "0")
255+
}
256+
257+
func downloadFileWithURL(Sender: AnyObject) -> Void {
258+
let wsDM = WebShelllDownloadManager.init(url: lastURL)
259+
wsDM.endDownloadTask()
260+
}
257261
}

WebShell/WebShellMediaKeysSupport.swift

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ import Cocoa
1212

1313
/**
1414
Class WebShellMediaKeysSupport
15-
16-
This class will support the WebShell media keys.
15+
16+
This class will support the WebShell media keys. \
17+
\
18+
!important note, this class can not communicate with the ViewController.\
19+
The communication goes via NSUserDefaults.
1720
*/
1821
class WebShellMediaKeysSupport: NSApplication {
19-
22+
let MediaKeysSettings = WebShell().Settings["MediaKeys"] as! [String: Bool]
23+
2024
override func sendEvent(theEvent: NSEvent) {
2125
if theEvent.type == .SystemDefined && theEvent.subtype.rawValue == 8 {
2226
let keyCode = ((theEvent.data1 & 0xFFFF0000) >> 16)
@@ -26,37 +30,115 @@ class WebShellMediaKeysSupport: NSApplication {
2630
let keyRepeat = (keyFlags & 0x1)
2731
mediaKeyEvent(Int32(keyCode), state: keyState, keyRepeat: Bool(keyRepeat))
2832
}
29-
33+
3034
super.sendEvent(theEvent)
3135
}
3236

3337
func mediaKeyEvent(key: Int32, state: Bool, keyRepeat: Bool) {
3438
// Only send events on KeyDown. Without this check, these events will happen twice
3539
if (state) {
3640
switch (key) {
37-
case NX_KEYTYPE_PLAY:
38-
print("Play!")
39-
// F8 pressed
40-
break
41-
case NX_KEYTYPE_FAST:
42-
print("Fast!")
43-
// F9 pressed
41+
case NX_KEYTYPE_PLAY: // F8 / Play
42+
if (MediaKeysSettings["BackAndForward"] == true) {
43+
self.goReloadPage()
44+
} else {
45+
self.playPausePressed()
46+
}
4447
break
45-
case NX_KEYTYPE_REWIND:
46-
print("Rewind!")
47-
// F7 pressed
48+
case NX_KEYTYPE_FAST, NX_KEYTYPE_NEXT: // F9 / Forward
49+
if (MediaKeysSettings["BackAndForward"] == true) {
50+
self.goForwardIfPossible()
51+
} else {
52+
self.nextItem()
53+
}
4854
break
49-
case NX_KEYTYPE_PREVIOUS: // Not called?
50-
print("Previous!")
51-
// F7
52-
break
53-
case NX_KEYTYPE_NEXT: // Not called?
54-
print("Next!")
55-
// F9 pressed
55+
case NX_KEYTYPE_REWIND, NX_KEYTYPE_PREVIOUS: // F7 / Backward
56+
if (MediaKeysSettings["BackAndForward"] == true) {
57+
self.goBackIfPossible()
58+
} else {
59+
self.previousItem()
60+
}
5661
break
5762
default:
5863
break
5964
}
6065
}
6166
}
67+
68+
/**
69+
goBackIfPossible
70+
71+
Since we can't communicate with the ViewController.\
72+
We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
73+
*/
74+
func goBackIfPossible() {
75+
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoBack")
76+
NSUserDefaults.standardUserDefaults().synchronize()
77+
}
78+
79+
/**
80+
goForwardIfPossible
81+
82+
Since we can't communicate with the ViewController.\
83+
We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
84+
*/
85+
func goForwardIfPossible() {
86+
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoForward")
87+
NSUserDefaults.standardUserDefaults().synchronize()
88+
}
89+
90+
/**
91+
goReloadPage
92+
93+
Since we can't communicate with the ViewController.\
94+
We'll set a NSUserDefaults, and the `WSMediaLoop` does the Job for us.
95+
*/
96+
func goReloadPage() {
97+
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "WSGoReload")
98+
NSUserDefaults.standardUserDefaults().synchronize()
99+
}
100+
101+
func nextItem() -> Bool {
102+
// ...
103+
return false
104+
}
105+
106+
func previousItem() -> Bool {
107+
// ...
108+
return false
109+
}
110+
111+
func playPausePressed() -> Bool {
112+
// ...
113+
return false
114+
}
115+
}
116+
117+
extension ViewController {
118+
/**
119+
Communication for the WebShellMediaKeysSupport class
120+
121+
- Parameter Sender: AnyObject (used for #selector use self)
122+
*/
123+
func WSMediaLoop(Sender: AnyObject) -> Void {
124+
self.performSelector(#selector(ViewController.WSMediaLoop(_:)), withObject: nil, afterDelay: 0.5)
125+
126+
if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoBack")) {
127+
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoBack")
128+
NSUserDefaults.standardUserDefaults().synchronize()
129+
self._goBack(self)
130+
}
131+
132+
if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoForward")) {
133+
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoForward")
134+
NSUserDefaults.standardUserDefaults().synchronize()
135+
self._goForward(self)
136+
}
137+
138+
if (NSUserDefaults.standardUserDefaults().boolForKey("WSGoReload")) {
139+
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "WSGoReload")
140+
NSUserDefaults.standardUserDefaults().synchronize()
141+
self._reloadPage(self)
142+
}
143+
}
62144
}

0 commit comments

Comments
 (0)