Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keyboard target wip #69

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/apple-targets/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export type ExtensionType =
| "action"
| "safari"
| "app-intent"
| "device-activity-monitor";
| "device-activity-monitor"
| "custom-keyboard";

export const KNOWN_EXTENSION_POINT_IDENTIFIERS: Record<string, ExtensionType> =
{
Expand All @@ -46,6 +47,7 @@ export const KNOWN_EXTENSION_POINT_IDENTIFIERS: Record<string, ExtensionType> =
"com.apple.appintents-extension": "app-intent",
"com.apple.deviceactivity.monitor-extension": "device-activity-monitor",
// "com.apple.intents-service": "intents",
"com.apple.keyboard-service": "custom-keyboard",
};

// An exhaustive list of extension types that should sync app groups from the main target by default when
Expand All @@ -72,6 +74,7 @@ export const SHOULD_USE_APP_GROUPS_BY_DEFAULT: Record<ExtensionType, boolean> =
safari: false,
spotlight: false,
watch: false,
"custom-keyboard": true,
};

// TODO: Maybe we can replace `NSExtensionPrincipalClass` with the `@main` annotation that newer extensions use?
Expand Down Expand Up @@ -279,6 +282,17 @@ export function getTargetInfoPlistForType(type: ExtensionType) {
NSExtensionPointIdentifier,
},
});
} else if (type === "custom-keyboard") {
return plist.build({
NSExtension: {
NSExtensionAttributes: {
RequestsOpenAccess: true,
},
NSExtensionPointIdentifier,
NSExtensionPrincipalClass:
"$(PRODUCT_MODULE_NAME).KeyboardViewController",
},
});
}

// Default: used for widget and bg-download
Expand Down Expand Up @@ -313,6 +327,7 @@ export function needsEmbeddedSwift(type: ExtensionType) {
"quicklook-thumbnail",
"matter",
"clip",
"custom-keyboard",
].includes(type);
}

Expand Down
4 changes: 4 additions & 0 deletions packages/create-target/src/createAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export function getTemplateConfig(target: string) {
"safari",
"share",
"watch",
"custom-keyboard",
].includes(target);

const lines = [
Expand Down Expand Up @@ -256,4 +257,7 @@ const RECOMMENDED_ENTITLEMENTS: Record<Partial<ExtensionType>, any> = {
"device-activity-monitor": {
"com.apple.developer.family-controls": true,
},
sandbox: {
"com.apple.security.app-sandbox": true,
},
};
1 change: 1 addition & 0 deletions packages/create-target/src/promptTarget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const TARGETS = [
{ title: "Siri Intent UI", value: "intent-ui", description: "" },
{ title: "Share Extension", value: "share", description: "" },
{ title: "Watch", value: "watch", description: "" },
{ title: "Keyboard", value: "custom-keyboard", description: "" },
];

export function assertValidTarget(target: any): asserts target is string {
Expand Down
24 changes: 24 additions & 0 deletions packages/create-target/templates/keyboard/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>IsASCIICapable</key>
<true/>
<key>PrefersRightToLeft</key>
<false/>
<key>PrimaryLanguage</key>
<string>en-US</string>
<key>RequestsOpenAccess</key>
<false/>
</dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.keyboard-service</string>
<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).KeyboardViewController</string>
</dict>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import UIKit
import SwiftUI

class KeyboardViewController: UIInputViewController {

private var keyboardView: UIView?

override func viewDidLoad() {
super.viewDidLoad()

// Setup the custom keyboard UI
setupKeyboardUI()
}

private func setupKeyboardUI() {
// Create and setup the SwiftUI keyboard view
let customKeyboardView = CustomKeyboardView(viewController: self)
let hostingController = UIHostingController(rootView: customKeyboardView)

addChild(hostingController)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingController.view)

NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

hostingController.didMove(toParent: self)
}
}

struct CustomKeyboardView: View {
weak var viewController: UIInputViewController?

init(viewController: UIInputViewController) {
self.viewController = viewController
}

var body: some View {
VStack(spacing: 10) {
HStack(spacing: 5) {
KeyButton(text: "Q") { insertText("Q") }
KeyButton(text: "W") { insertText("W") }
KeyButton(text: "E") { insertText("E") }
KeyButton(text: "R") { insertText("R") }
KeyButton(text: "T") { insertText("T") }
}
// Add more rows of keys as needed

HStack {
KeyButton(text: "Space") { insertText(" ") }
.frame(maxWidth: .infinity)
KeyButton(text: "⌫") { deleteBackward() }
}
}
.padding()
.background(Color(.systemBackground))
}

private func insertText(_ text: String) {
viewController?.textDocumentProxy.insertText(text)
}

private func deleteBackward() {
viewController?.textDocumentProxy.deleteBackward()
}
}

struct KeyButton: View {
let text: String
let action: () -> Void

var body: some View {
Button(action: action) {
Text(text)
.font(.system(size: 20))
.frame(minWidth: 30, minHeight: 40)
.background(Color(.systemGray5))
.cornerRadius(5)
}
}
}
11 changes: 11 additions & 0 deletions targets/keyboard/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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>NSExtension</key>
<dict>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.keyboard-service</string>
</dict>
</dict>
</plist>
85 changes: 85 additions & 0 deletions targets/keyboard/KeyboardViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import UIKit
import SwiftUI

class KeyboardViewController: UIInputViewController {

private var keyboardView: UIView?

override func viewDidLoad() {
super.viewDidLoad()

// Setup the custom keyboard UI
setupKeyboardUI()
}

private func setupKeyboardUI() {
// Create and setup the SwiftUI keyboard view
let customKeyboardView = CustomKeyboardView(viewController: self)
let hostingController = UIHostingController(rootView: customKeyboardView)

addChild(hostingController)
hostingController.view.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(hostingController.view)

NSLayoutConstraint.activate([
hostingController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor),
hostingController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor),
hostingController.view.topAnchor.constraint(equalTo: view.topAnchor),
hostingController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor)
])

hostingController.didMove(toParent: self)
}
}

struct CustomKeyboardView: View {
weak var viewController: UIInputViewController?

init(viewController: UIInputViewController) {
self.viewController = viewController
}

var body: some View {
VStack(spacing: 10) {
HStack(spacing: 5) {
KeyButton(text: "Q") { insertText("Q") }
KeyButton(text: "W") { insertText("W") }
KeyButton(text: "E") { insertText("E") }
KeyButton(text: "R") { insertText("R") }
KeyButton(text: "T") { insertText("T") }
}
// Add more rows of keys as needed

HStack {
KeyButton(text: "Space") { insertText(" ") }
.frame(maxWidth: .infinity)
KeyButton(text: "⌫") { deleteBackward() }
}
}
.padding()
.background(Color(.systemBackground))
}

private func insertText(_ text: String) {
viewController?.textDocumentProxy.insertText(text)
}

private func deleteBackward() {
viewController?.textDocumentProxy.deleteBackward()
}
}

struct KeyButton: View {
let text: String
let action: () -> Void

var body: some View {
Button(action: action) {
Text(text)
.font(.system(size: 20))
.frame(minWidth: 30, minHeight: 40)
.background(Color(.systemGray5))
.cornerRadius(5)
}
}
}
6 changes: 6 additions & 0 deletions targets/keyboard/expo-target.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** @type {import('@bacons/apple-targets/app.plugin').ConfigFunction} */
module.exports = config => ({
type: "custom-keyboard",
icon: 'https://github.com/expo.png',
entitlements: { /* Add entitlements */ },
});