From 5d0e1c4719bf9a7aee7a62058be5eee7728da068 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Mon, 18 Feb 2019 20:18:44 -0500 Subject: [PATCH] Spacebar triggers play/pause Use the responder chain to handle the keypress. --- Persephone.xcodeproj/project.pbxproj | 12 +++++++++++ .../Controllers/MainSplitViewController.swift | 20 +++++++++++++++++++ .../Controllers/QueueViewController.swift | 9 +++++++++ Persephone/Controllers/WindowController.swift | 9 +++++++++ Persephone/Extensions/NSEvent.swift | 13 ++++++++++++ .../Resources/Base.lproj/Main.storyboard | 6 +++--- Persephone/Views/MainWindow.swift | 20 +++++++++++++++++++ 7 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 Persephone/Controllers/MainSplitViewController.swift create mode 100644 Persephone/Extensions/NSEvent.swift create mode 100644 Persephone/Views/MainWindow.swift diff --git a/Persephone.xcodeproj/project.pbxproj b/Persephone.xcodeproj/project.pbxproj index b05f28a..c050135 100644 --- a/Persephone.xcodeproj/project.pbxproj +++ b/Persephone.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ E408D3CA220E341D0006D9BE /* AlbumItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = E408D3C8220E341D0006D9BE /* AlbumItem.swift */; }; E408D3CB220E341D0006D9BE /* AlbumItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = E408D3C9220E341D0006D9BE /* AlbumItem.xib */; }; E40FE719221B48E300A4223F /* AlbumViewLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40FE718221B48E300A4223F /* AlbumViewLayout.swift */; }; + E40FE71B221B904300A4223F /* NSEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E40FE71A221B904300A4223F /* NSEvent.swift */; }; E41B22C021FB6BBA00D544F6 /* libmpdclient.2.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (Required, ); }; }; E41B22C121FB6C3300D544F6 /* libmpdclient.2.dylib in Embed Libraries */ = {isa = PBXBuildFile; fileRef = E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E41B22C521FB932700D544F6 /* MPDClient.swift */; }; @@ -27,6 +28,8 @@ E42A8F3B22176D6400A13ED9 /* LICENSE.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3922176D6400A13ED9 /* LICENSE.md */; }; E42A8F3C22176D6400A13ED9 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = E42A8F3A22176D6400A13ED9 /* README.md */; }; E465049A21E94DF500A70F4C /* WindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E465049921E94DF500A70F4C /* WindowController.swift */; }; + E47E2FD122205C4600F747E6 /* MainSplitViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E47E2FD022205C4600F747E6 /* MainSplitViewController.swift */; }; + E47E2FD322205D2500F747E6 /* MainWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = E47E2FD222205D2500F747E6 /* MainWindow.swift */; }; E4928E0B2218D62A001D4BEA /* CGColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4928E0A2218D62A001D4BEA /* CGColor.swift */; }; E4A642DA22090CBE00067D21 /* Status.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4A642D922090CBE00067D21 /* Status.swift */; }; E4E8CC902204EC7F0024217A /* Delegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E4E8CC8F2204EC7F0024217A /* Delegate.swift */; }; @@ -88,6 +91,7 @@ E408D3C8220E341D0006D9BE /* AlbumItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumItem.swift; sourceTree = ""; }; E408D3C9220E341D0006D9BE /* AlbumItem.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = AlbumItem.xib; sourceTree = ""; }; E40FE718221B48E300A4223F /* AlbumViewLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlbumViewLayout.swift; sourceTree = ""; }; + E40FE71A221B904300A4223F /* NSEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSEvent.swift; sourceTree = ""; }; E41B22BF21FB6BBA00D544F6 /* libmpdclient.2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libmpdclient.2.dylib; path = libmpdclient/output/libmpdclient.2.dylib; sourceTree = ""; }; E41B22C421FB715A00D544F6 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; path = module.modulemap; sourceTree = ""; }; E41B22C521FB932700D544F6 /* MPDClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MPDClient.swift; sourceTree = ""; }; @@ -131,6 +135,8 @@ E42A8F3922176D6400A13ED9 /* LICENSE.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = ""; }; E42A8F3A22176D6400A13ED9 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; E465049921E94DF500A70F4C /* WindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowController.swift; sourceTree = ""; }; + E47E2FD022205C4600F747E6 /* MainSplitViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainSplitViewController.swift; sourceTree = ""; }; + E47E2FD222205D2500F747E6 /* MainWindow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainWindow.swift; sourceTree = ""; }; E4928E0A2218D62A001D4BEA /* CGColor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGColor.swift; sourceTree = ""; }; E4A642D922090CBE00067D21 /* Status.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Status.swift; sourceTree = ""; }; E4E8CC8F2204EC7F0024217A /* Delegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Delegate.swift; sourceTree = ""; }; @@ -233,6 +239,7 @@ E4928E0A2218D62A001D4BEA /* CGColor.swift */, E408D3B5220DD8970006D9BE /* Notification.swift */, E408D3B8220DE98F0006D9BE /* NSUserInterfaceItemIdentifier.swift */, + E40FE71A221B904300A4223F /* NSEvent.swift */, ); path = Extensions; sourceTree = ""; @@ -249,6 +256,7 @@ isa = PBXGroup; children = ( E408D3C8220E341D0006D9BE /* AlbumItem.swift */, + E47E2FD222205D2500F747E6 /* MainWindow.swift */, ); path = Views; sourceTree = ""; @@ -366,6 +374,7 @@ E4E8CC932206097F0024217A /* NotificationsController.swift */, E4E8CC912204F4B80024217A /* QueueViewController.swift */, E465049921E94DF500A70F4C /* WindowController.swift */, + E47E2FD022205C4600F747E6 /* MainSplitViewController.swift */, ); path = Controllers; sourceTree = ""; @@ -522,6 +531,7 @@ buildActionMask = 2147483647; files = ( E408D3C2220E134F0006D9BE /* AlbumViewController.swift in Sources */, + E40FE71B221B904300A4223F /* NSEvent.swift in Sources */, E4928E0B2218D62A001D4BEA /* CGColor.swift in Sources */, E4A642DA22090CBE00067D21 /* Status.swift in Sources */, E4E8CC942206097F0024217A /* NotificationsController.swift in Sources */, @@ -533,6 +543,8 @@ E465049A21E94DF500A70F4C /* WindowController.swift in Sources */, E41B22C621FB932700D544F6 /* MPDClient.swift in Sources */, E407861C2110CE6E006887B1 /* AppDelegate.swift in Sources */, + E47E2FD322205D2500F747E6 /* MainWindow.swift in Sources */, + E47E2FD122205C4600F747E6 /* MainSplitViewController.swift in Sources */, E4E8CC9A22075D370024217A /* Song.swift in Sources */, E408D3CA220E341D0006D9BE /* AlbumItem.swift in Sources */, E41EA46C221636AF0068EF46 /* PreferencesViewController.swift in Sources */, diff --git a/Persephone/Controllers/MainSplitViewController.swift b/Persephone/Controllers/MainSplitViewController.swift new file mode 100644 index 0000000..5d78ffa --- /dev/null +++ b/Persephone/Controllers/MainSplitViewController.swift @@ -0,0 +1,20 @@ +// +// MainSplitViewController.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/22. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Cocoa + +class MainSplitViewController: NSSplitViewController { + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case NSEvent.keyCodeSpace: + nextResponder?.keyDown(with: event) + default: + super.keyDown(with: event) + } + } +} diff --git a/Persephone/Controllers/QueueViewController.swift b/Persephone/Controllers/QueueViewController.swift index 17fedbf..9919625 100644 --- a/Persephone/Controllers/QueueViewController.swift +++ b/Persephone/Controllers/QueueViewController.swift @@ -59,6 +59,15 @@ class QueueViewController: NSViewController, NSOutlineViewDataSource, NSOutlineV ) } + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case NSEvent.keyCodeSpace: + nextResponder?.keyDown(with: event) + default: + super.keyDown(with: event) + } + } + @IBAction func playTrack(_ sender: Any) { guard let view = sender as? NSOutlineView else { return } diff --git a/Persephone/Controllers/WindowController.swift b/Persephone/Controllers/WindowController.swift index e95e853..d1ea8f8 100644 --- a/Persephone/Controllers/WindowController.swift +++ b/Persephone/Controllers/WindowController.swift @@ -28,6 +28,15 @@ class WindowController: NSWindowController { ) } + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case NSEvent.keyCodeSpace: + AppDelegate.mpdClient.playPause() + default: + nextResponder?.keyDown(with: event) + } + } + @objc func stateChanged(_ notification: Notification) { guard let state = notification.userInfo?[Notification.stateKey] as? MPDClient.Status.State else { return } diff --git a/Persephone/Extensions/NSEvent.swift b/Persephone/Extensions/NSEvent.swift new file mode 100644 index 0000000..5298e76 --- /dev/null +++ b/Persephone/Extensions/NSEvent.swift @@ -0,0 +1,13 @@ +// +// NSEvent.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/18. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Cocoa + +extension NSEvent { + static let keyCodeSpace: UInt16 = 49 +} diff --git a/Persephone/Resources/Base.lproj/Main.storyboard b/Persephone/Resources/Base.lproj/Main.storyboard index 2db03f7..5a59a1b 100644 --- a/Persephone/Resources/Base.lproj/Main.storyboard +++ b/Persephone/Resources/Base.lproj/Main.storyboard @@ -691,7 +691,7 @@ - + @@ -738,10 +738,10 @@ - + - + diff --git a/Persephone/Views/MainWindow.swift b/Persephone/Views/MainWindow.swift new file mode 100644 index 0000000..b7220e3 --- /dev/null +++ b/Persephone/Views/MainWindow.swift @@ -0,0 +1,20 @@ +// +// MainWindow.swift +// Persephone +// +// Created by Daniel Barber on 2019/2/22. +// Copyright © 2019 Dan Barber. All rights reserved. +// + +import Cocoa + +class MainWindow: NSWindow { + override func keyDown(with event: NSEvent) { + switch event.keyCode { + case NSEvent.keyCodeSpace: + nextResponder?.keyDown(with: event) + default: + super.keyDown(with: event) + } + } +}