Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
UIKeyCommand in macOS Catalyst Menu Items
Browse files Browse the repository at this point in the history
A simple macOS Catalyst app, with multiple menu items in multiple view controllers.
  • Loading branch information
Ryan Grier committed Apr 20, 2021
1 parent c52ca3c commit d8c3341
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 21 deletions.
8 changes: 8 additions & 0 deletions KeyboardShortcuts/KeyboardShortcuts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
8F039EE726208D4F006C1CD4 /* TableViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewController.swift; sourceTree = "<group>"; };
8F039EEB26208D62006C1CD4 /* DetailViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailViewController.swift; sourceTree = "<group>"; };
8F179F0B2624CC2700D6FAE7 /* UIResponder+KeyboardShortcuts.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIResponder+KeyboardShortcuts.swift"; sourceTree = "<group>"; };
8F4A2DEB262E50F300897945 /* KeyboardShortcuts.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = KeyboardShortcuts.entitlements; sourceTree = "<group>"; };
8FD588402620DA59006024B9 /* AlertBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlertBuilder.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -62,6 +63,7 @@
8F039ED02620899E006C1CD4 /* KeyboardShortcuts */ = {
isa = PBXGroup;
children = (
8F4A2DEB262E50F300897945 /* KeyboardShortcuts.entitlements */,
8FD588432620DA5F006024B9 /* Support */,
8F039EE626208D39006C1CD4 /* View Controllers */,
8F039ED12620899E006C1CD4 /* AppDelegate.swift */,
Expand Down Expand Up @@ -314,14 +316,17 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = KeyboardShortcuts/KeyboardShortcuts.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = G8C3PMN49A;
INFOPLIST_FILE = KeyboardShortcuts/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.ryangrier.KeyboardShortcuts;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand All @@ -332,14 +337,17 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = KeyboardShortcuts/KeyboardShortcuts.entitlements;
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = G8C3PMN49A;
INFOPLIST_FILE = KeyboardShortcuts/Info.plist;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
);
PRODUCT_BUNDLE_IDENTIFIER = com.ryangrier.KeyboardShortcuts;
PRODUCT_NAME = "$(TARGET_NAME)";
SUPPORTS_MACCATALYST = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
Expand Down
28 changes: 27 additions & 1 deletion KeyboardShortcuts/KeyboardShortcuts/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,33 @@

import UIKit

@main class AppDelegate: UIResponder { }
extension UIMenu.Identifier {
static var show = UIMenu.Identifier("show")
}

@main class AppDelegate: UIResponder {

// Build the Catalyst (macOS) menu
override func buildMenu(with builder: UIMenuBuilder) {
super.buildMenu(with: builder)

// Ignore the contextual menu builder.
guard builder.system == .main else { return }

// Remove some extra menus we don't need for this example.
builder.remove(menu: .services)
builder.remove(menu: .format)

let menu = UIMenu(title: "Show",
identifier: .show,
options: .displayInline,
children: [TableViewController.showTableAlert,
DetailViewController.showDetailAlert])


builder.insertChild(menu, atStartOfMenu: .file)
}
}

// MARK: - UIApplicationDelegate

Expand Down
2 changes: 2 additions & 0 deletions KeyboardShortcuts/KeyboardShortcuts/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIApplicationSceneManifest</key>
Expand Down
10 changes: 10 additions & 0 deletions KeyboardShortcuts/KeyboardShortcuts/KeyboardShortcuts.entitlements
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
import UIKit

class DetailViewController: UIViewController {
class var showDetailAlert: UIKeyCommand {
return UIKeyCommand(title: "Show Detail",
action: #selector(detailViewAction),
input: "d",
modifierFlags: .command)
}

class var showInfoAlert: UIKeyCommand {
return UIKeyCommand(title: "Show Info",
action: #selector(showInfo),
input: "i",
modifierFlags: .command)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

Expand Down Expand Up @@ -48,17 +62,7 @@ class DetailViewController: UIViewController {
// MARK: - Keyboard Shortcuts

extension DetailViewController {
override var keyCommands: [UIKeyCommand]? {
let infoCommand = UIKeyCommand(title: "Show Info",
action: #selector(showInfo),
input: "i",
modifierFlags: .command)

let detailCommand = UIKeyCommand(title: "Show Detail",
action: #selector(detailViewAction),
input: "d",
modifierFlags: .command)

return [infoCommand, detailCommand]
override var keyCommands: [UIKeyCommand]? {
return [DetailViewController.showInfoAlert, DetailViewController.showDetailAlert]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
import UIKit

class TableViewController: UITableViewController {
class var showTableAlert: UIKeyCommand {
return UIKeyCommand(title: "Show Table",
action: #selector(tableViewAction),
input: "t",
modifierFlags: .command)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

// print is for demonstration purposes only.
print("[TableViewController] Responder Chain: \(_fullResponderChain)")
}

@objc private func tableViewAction() {
@objc internal func tableViewAction() {
let title = NSLocalizedString("Table View Action", comment: "")
let message = NSLocalizedString("This action was triggered from the table view controller.", comment: "")

Expand Down Expand Up @@ -73,12 +80,7 @@ extension TableViewController {
action: #selector(showInfo),
input: "i",
modifierFlags: .command)

let tableCommand = UIKeyCommand(title: "Show Table",
action: #selector(tableViewAction),
input: "t",
modifierFlags: .command)

return [infoCommand, tableCommand]

return [infoCommand, TableViewController.showTableAlert]
}
}

0 comments on commit d8c3341

Please sign in to comment.