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

Support text paste on iOS #653

Merged
merged 4 commits into from
Nov 1, 2024
Merged
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
6 changes: 3 additions & 3 deletions packages/vscode-extension/src/devices/DeviceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DeviceInfo, DevicePlatform } from "../common/DeviceManager";
import { tryAcquiringLock } from "../utilities/common";

export abstract class DeviceBase implements Disposable {
private preview: Preview | undefined;
protected preview: Preview | undefined;
private previewStartPromise: Promise<string> | undefined;
private acquired = false;

Expand Down Expand Up @@ -69,8 +69,8 @@ export abstract class DeviceBase implements Disposable {
this.preview?.sendKey(keyCode, direction);
}

public sendPaste(text: string) {
this.preview?.sendPaste(text);
public async sendPaste(text: string) {
return this.preview?.sendPaste(text);
}

async startPreview() {
Expand Down
44 changes: 44 additions & 0 deletions packages/vscode-extension/src/devices/IosSimulatorDevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { AppPermissionType, DeviceSettings, Locale } from "../common/Project";
import { EXPO_GO_BUNDLE_ID, fetchExpoLaunchDeeplink } from "../builders/expoGo";
import { IOSBuildResult } from "../builders/buildIOS";

const LEFT_META_HID_CODE = 0xe3;
const RIGHT_META_HID_CODE = 0xe7;
const V_KEY_HID_CODE = 0x19;

interface SimulatorInfo {
availability?: string;
state?: string;
Expand Down Expand Up @@ -207,6 +211,46 @@ export class IosSimulatorDevice extends DeviceBase {
]);
}

private pressingLeftMetaKey = false;
private pressingRightMetaKey = false;

public sendKey(keyCode: number, direction: "Up" | "Down"): void {
// iOS simulator has a buggy behavior when sending cmd+V key combination.
// It sometimes triggers paste action but with a very low success rate.
// Other times it kicks in before the pasteboard is filled with the content
// therefore pasting the previously compied content instead.
// As a temporary workaround, we disable sending cmd+V as key combination
// entirely to prevent this buggy behavior. Users can still paste content
// using the context menu method as they'd do on an iOS device.
// This is not an ideal workaround as people may still trigger cmd+v by
// pressing V first and then cmd, but it is good enough to filter out
// the majority of the noisy behavior since typically you press cmd first.
if (keyCode === LEFT_META_HID_CODE) {
this.pressingLeftMetaKey = direction === "Down";
} else if (keyCode === RIGHT_META_HID_CODE) {
this.pressingRightMetaKey = direction === "Down";
}
if ((this.pressingLeftMetaKey || this.pressingRightMetaKey) && keyCode === V_KEY_HID_CODE) {
// ignore sending V when meta key is pressed
} else {
this.preview?.sendKey(keyCode, direction);
}
}

public async sendPaste(text: string) {
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
const subprocess = exec("xcrun", [
"simctl",
"--set",
deviceSetLocation,
"pbcopy",
this.deviceUDID,
]);
subprocess.stdin?.write(text);
subprocess.stdin?.end();
await subprocess;
}

private async changeLocale(newLocale: Locale): Promise<boolean> {
const deviceSetLocation = getOrCreateDeviceSet(this.deviceUDID);
const languageCode = newLocale.match(/([^_-]*)/)![1];
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/devices/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class Preview implements Disposable {
this.subprocess?.stdin?.write(`key ${direction} ${keyCode}\n`);
}

public sendPaste(text: string) {
public async sendPaste(text: string) {
this.subprocess?.stdin?.write(`paste ${text}\n`);
}
}
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/project/deviceSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class DeviceSession implements Disposable {
}

public sendPaste(text: string) {
this.device.sendPaste(text);
return this.device.sendPaste(text);
}

public inspectElementAt(
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Project
}

async dispatchPaste(text: string) {
this.deviceSession?.sendPaste(text);
await this.deviceSession?.sendPaste(text);
}

onBundleError(): void {
Expand Down