Skip to content

Commit

Permalink
Add ability to change the global shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
jodelamo committed Jun 19, 2019
1 parent 577d681 commit 794687a
Show file tree
Hide file tree
Showing 73 changed files with 3,302 additions and 1,281 deletions.
30 changes: 28 additions & 2 deletions AnyIpsum.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
462FA5901D312BDB0099FD9E /* Ipsum.plist in Resources */ = {isa = PBXBuildFile; fileRef = 462FA58F1D312BDB0099FD9E /* Ipsum.plist */; };
462FA5921D31341D0099FD9E /* MenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 462FA5911D31341D0099FD9E /* MenuItem.swift */; };
46445B3220273635008E071A /* Paragraph.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46445B3120273635008E071A /* Paragraph.swift */; };
4691A1F922AA5CA000C55E72 /* Preferences.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 4691A1F822AA5CA000C55E72 /* Preferences.storyboard */; };
4691A1FC22AA5F3B00C55E72 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4691A1FB22AA5F3B00C55E72 /* PreferencesViewController.swift */; };
4691A1FE22AA601700C55E72 /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4691A1FD22AA601700C55E72 /* PreferencesWindowController.swift */; };
469992361D36505900D073CD /* String+words.swift in Sources */ = {isa = PBXBuildFile; fileRef = 469992351D36505900D073CD /* String+words.swift */; };
46B95C4F22ABB0D50061AFF4 /* ShortcutManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46B95C4E22ABB0D50061AFF4 /* ShortcutManager.swift */; };
46E666881D3CB873004F476F /* String+condenseWhitespace.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E666871D3CB873004F476F /* String+condenseWhitespace.swift */; };
46E6668A1D3CB8A6004F476F /* String+capitalizeFirstLetter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 46E666891D3CB8A6004F476F /* String+capitalizeFirstLetter.swift */; };
693826CB56962D22650F6002 /* Pods_AnyIpsum.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D96B53FA2326EA377E2C7E4F /* Pods_AnyIpsum.framework */; };
Expand All @@ -28,7 +32,12 @@
462FA58F1D312BDB0099FD9E /* Ipsum.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Ipsum.plist; sourceTree = "<group>"; };
462FA5911D31341D0099FD9E /* MenuItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MenuItem.swift; sourceTree = "<group>"; };
46445B3120273635008E071A /* Paragraph.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Paragraph.swift; sourceTree = "<group>"; };
4691A1F822AA5CA000C55E72 /* Preferences.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = Preferences.storyboard; sourceTree = "<group>"; };
4691A1FB22AA5F3B00C55E72 /* PreferencesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesViewController.swift; sourceTree = "<group>"; };
4691A1FD22AA601700C55E72 /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
4691A1FF22AA66AC00C55E72 /* AnyIpsum-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "AnyIpsum-Bridging-Header.h"; sourceTree = "<group>"; };
469992351D36505900D073CD /* String+words.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+words.swift"; sourceTree = "<group>"; };
46B95C4E22ABB0D50061AFF4 /* ShortcutManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShortcutManager.swift; sourceTree = "<group>"; };
46E666871D3CB873004F476F /* String+condenseWhitespace.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+condenseWhitespace.swift"; sourceTree = "<group>"; };
46E666891D3CB8A6004F476F /* String+capitalizeFirstLetter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "String+capitalizeFirstLetter.swift"; sourceTree = "<group>"; };
7E02F71A2B5A014ADC0641F9 /* Pods-AnyIpsum.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AnyIpsum.debug.xcconfig"; path = "Pods/Target Support Files/Pods-AnyIpsum/Pods-AnyIpsum.debug.xcconfig"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -69,13 +78,15 @@
462FA5811D30C1AF0099FD9E /* AnyIpsum */ = {
isa = PBXGroup;
children = (
4691A1FA22AA5D0500C55E72 /* Preferences */,
4643712B1D3A826B007B76B1 /* Classes */,
4643712A1D3A825D007B76B1 /* Extensions */,
462FA5821D30C1AF0099FD9E /* AppDelegate.swift */,
462FA5841D30C1AF0099FD9E /* Assets.xcassets */,
462FA5861D30C1AF0099FD9E /* MainMenu.xib */,
462FA5891D30C1AF0099FD9E /* Info.plist */,
462FA58F1D312BDB0099FD9E /* Ipsum.plist */,
4691A1FF22AA66AC00C55E72 /* AnyIpsum-Bridging-Header.h */,
);
path = AnyIpsum;
sourceTree = "<group>";
Expand All @@ -95,10 +106,21 @@
children = (
462FA5911D31341D0099FD9E /* MenuItem.swift */,
46445B3120273635008E071A /* Paragraph.swift */,
46B95C4E22ABB0D50061AFF4 /* ShortcutManager.swift */,
);
name = Classes;
sourceTree = "<group>";
};
4691A1FA22AA5D0500C55E72 /* Preferences */ = {
isa = PBXGroup;
children = (
4691A1F822AA5CA000C55E72 /* Preferences.storyboard */,
4691A1FB22AA5F3B00C55E72 /* PreferencesViewController.swift */,
4691A1FD22AA601700C55E72 /* PreferencesWindowController.swift */,
);
name = Preferences;
sourceTree = "<group>";
};
69E15F794C047FAFF120DC31 /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -181,6 +203,7 @@
462FA5901D312BDB0099FD9E /* Ipsum.plist in Resources */,
462FA5851D30C1AF0099FD9E /* Assets.xcassets in Resources */,
462FA5881D30C1AF0099FD9E /* MainMenu.xib in Resources */,
4691A1F922AA5CA000C55E72 /* Preferences.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -225,11 +248,11 @@
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-AnyIpsum/Pods-AnyIpsum-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/HotKey/HotKey.framework",
"${BUILT_PRODUCTS_DIR}/MASShortcut/MASShortcut.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/HotKey.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/MASShortcut.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
Expand All @@ -244,10 +267,13 @@
buildActionMask = 2147483647;
files = (
46E666881D3CB873004F476F /* String+condenseWhitespace.swift in Sources */,
46B95C4F22ABB0D50061AFF4 /* ShortcutManager.swift in Sources */,
462FA5831D30C1AF0099FD9E /* AppDelegate.swift in Sources */,
46445B3220273635008E071A /* Paragraph.swift in Sources */,
469992361D36505900D073CD /* String+words.swift in Sources */,
462FA5921D31341D0099FD9E /* MenuItem.swift in Sources */,
4691A1FC22AA5F3B00C55E72 /* PreferencesViewController.swift in Sources */,
4691A1FE22AA601700C55E72 /* PreferencesWindowController.swift in Sources */,
46E6668A1D3CB8A6004F476F /* String+capitalizeFirstLetter.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
1 change: 1 addition & 0 deletions AnyIpsum/AnyIpsum-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import <MASShortcut/Shortcut.h>
38 changes: 26 additions & 12 deletions AnyIpsum/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Cocoa
import HotKey
import MASShortcut

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

@IBOutlet weak var window: NSWindow!
@IBOutlet weak var menuBar: NSMenu!

let hotKey = HotKey(key: .a, modifiers: [.control, .command])

var preferencesController: NSWindowController?
var shortcutManager: ShortcutManager?

let statusItem = NSStatusBar
.system
.statusItem(withLength: NSStatusItem.variableLength)
Expand All @@ -21,13 +21,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
menuIcon!.isTemplate = true
statusItem.image = menuIcon
statusItem.menu = menuBar

// Path to list of variations
// Initialize lorem ipsum variations
guard let path = Bundle.main.path(forResource: "Ipsum", ofType: "plist") else {
return
}

// Dictionary of variations
guard let ipsumTexts = NSDictionary(contentsOfFile: path) else {
return
}
Expand All @@ -50,11 +49,17 @@ class AppDelegate: NSObject, NSApplicationDelegate {

index += 1
}

// Open menu bar on Ctrl+Cmd+A
hotKey.keyDownHandler = {
self.statusItem.popUpMenu(self.menuBar)
}

// Register keyboard shortcut
MASShortcutBinder.shared()?.bindShortcut(withDefaultsKey: ShortcutManager.defaultsKey, toAction: openMenu)
}

func openMenu() {
self.statusItem.popUpMenu(self.menuBar)
}

func applicationWillTerminate(aNotification: NSNotification) {
MASShortcutMonitor.shared().unregisterAllShortcuts()
}

func writeToPasteboard(_ text: String) {
Expand All @@ -68,4 +73,13 @@ class AppDelegate: NSObject, NSApplicationDelegate {
@IBAction func quit(_ sender: NSMenuItem) {
NSApplication.shared.terminate(self)
}

@IBAction func showPreferences(_ sender: Any) {
if preferencesController == nil {
let storyboard = NSStoryboard(name: "Preferences", bundle: nil)
preferencesController = storyboard.instantiateInitialController() as? NSWindowController
}

preferencesController?.showWindow(sender)
}
}
14 changes: 10 additions & 4 deletions AnyIpsum/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="10117"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand Down Expand Up @@ -669,14 +669,20 @@
</menu>
<menu id="Z4o-af-g2R">
<items>
<menuItem isSeparatorItem="YES" id="qAe-gG-nUa"/>
<menuItem title="Preferences..." keyEquivalent="," id="5wD-Zy-AjW">
<connections>
<action selector="showPreferences:" target="Voe-Tx-rLC" id="OWZ-oV-i9I"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="pI9-nf-wv9"/>
<menuItem title="Quit" keyEquivalent="q" id="bm9-hP-NQl">
<connections>
<action selector="quit:" target="Voe-Tx-rLC" id="AgW-3g-erO"/>
</connections>
</menuItem>
</items>
<point key="canvasLocation" x="163.5" y="169.5"/>
<point key="canvasLocation" x="151" y="285"/>
</menu>
</objects>
</document>
84 changes: 84 additions & 0 deletions AnyIpsum/Preferences.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="F0s-24-o6u">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14490.70"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--Window Controller-->
<scene sceneID="Qme-Kj-1fk">
<objects>
<windowController id="F0s-24-o6u" customClass="PreferencesWindowController" customModule="AnyIpsum" customModuleProvider="target" sceneMemberID="viewController">
<window key="window" title="Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" frameAutosaveName="" animationBehavior="default" id="fPl-Sy-pn9">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="245" y="301" width="480" height="270"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<connections>
<outlet property="delegate" destination="F0s-24-o6u" id="vAX-D1-oyh"/>
</connections>
</window>
<connections>
<segue destination="8mW-aE-maF" kind="relationship" relationship="window.shadowedContentViewController" id="7Ye-LO-H0X"/>
</connections>
</windowController>
<customObject id="1R8-GW-ZIk" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
</scene>
<!--Preferences View Controller-->
<scene sceneID="tF7-2X-Utr">
<objects>
<viewController id="8mW-aE-maF" customClass="PreferencesViewController" customModule="AnyIpsum" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" id="rzl-CS-CJI">
<rect key="frame" x="0.0" y="0.0" width="480" height="161"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="fMg-iw-nhe">
<rect key="frame" x="18" y="123" width="265" height="17"/>
<autoresizingMask key="autoresizingMask"/>
<textFieldCell key="cell" lineBreakMode="clipping" title="Record a custom global keyboard shortcut:" id="HgO-Hx-NPi">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="O5d-LP-iVl" customClass="MASShortcutView">
<rect key="frame" x="289" y="122" width="171" height="19"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="OqC-2i-2jS">
<rect key="frame" x="14" y="87" width="247" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Use the default keyboard shortcut" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="qpu-r0-XK8">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="useDefault:" target="8mW-aE-maF" id="ih0-Ai-2wX"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ca4-8X-Vtv">
<rect key="frame" x="171" y="20" width="138" height="21"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="bevel" title="Visit AnyIpsum on GitHub" bezelStyle="rounded" alignment="center" controlSize="small" imageScaling="proportionallyDown" inset="2" id="qIz-wa-OvL">
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="smallSystem"/>
</buttonCell>
<color key="contentTintColor" name="linkColor" catalog="System" colorSpace="catalog"/>
<connections>
<action selector="visitGitHubRepository:" target="8mW-aE-maF" id="lIo-Pu-avO"/>
</connections>
</button>
</subviews>
</view>
<connections>
<outlet property="shortcutView" destination="O5d-LP-iVl" id="ETL-ul-eyg"/>
</connections>
</viewController>
<customObject id="PqX-Bq-cKA" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="139" y="730.5"/>
</scene>
</scenes>
</document>
30 changes: 30 additions & 0 deletions AnyIpsum/PreferencesViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Cocoa
import MASShortcut

class PreferencesViewController: NSViewController, NSApplicationDelegate {

@IBOutlet weak var shortcutView: MASShortcutView!

let appDelegate = NSApp.delegate as! AppDelegate

override func viewDidLoad() {
super.viewDidLoad()

shortcutView.associatedUserDefaultsKey = ShortcutManager.defaultsKey;

shortcutView.shortcutValueChange = { (sender) in
MASShortcutMonitor.shared().register(self.shortcutView.shortcutValue, withAction: self.appDelegate.openMenu)
}
}

// MARK: - Actions

@IBAction func useDefault(_ sender: Any) {
shortcutView.shortcutValue = ShortcutManager.defaultShortcut
}

@IBAction func visitGitHubRepository(_ sender: Any) {
let url = URL(string: "https://github.com/jlowgren/AnyIpsum")!
NSWorkspace.shared.open(url)
}
}
17 changes: 17 additions & 0 deletions AnyIpsum/PreferencesWindowController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Cocoa

class PreferencesWindowController: NSWindowController, NSWindowDelegate {

override func windowDidLoad() {
super.windowDidLoad()

self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}

func windowShouldClose(_ sender: NSWindow) -> Bool {
self.window?.orderOut(sender)
return false
}
}
15 changes: 15 additions & 0 deletions AnyIpsum/ShortcutManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Cocoa
import MASShortcut

struct ShortcutManager {

static let defaultsKey = "shortcutDefaultsKey"

static var defaultShortcut: MASShortcut {
// Returns the default shortcut, Ctrl+Cmd+A
let keyCode = UInt(kVK_ANSI_A)
let modifierFlags: NSEvent.ModifierFlags = [NSEvent.ModifierFlags.control, NSEvent.ModifierFlags.command]
return MASShortcut(keyCode: keyCode, modifierFlags: modifierFlags.rawValue)
}

}
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ platform :osx, '10.14'
target 'AnyIpsum' do
use_frameworks!

pod 'HotKey'
pod 'MASShortcut', '~> 2.3.6'
end
Loading

0 comments on commit 794687a

Please sign in to comment.