Skip to content

Commit 0bfa784

Browse files
committed
fix(mobile): long-pressing the iOS terminal offers Paste
The iOS terminal had no edit menu, so on a phone the only paste was a toolbar button that scrolls off screen. Hardware ⌘V and the iPad shortcut bar pasted through the hidden text field as typed input, so a newline arrived as a space, control bytes were not stripped, and large pastes were not chunked. A long press on the terminal now shows the system edit menu with Paste when the clipboard has text. That action, ⌘V, and the shortcut bar read the clipboard natively and send it to JS, which writes it through the same paste session as the toolbar button.
1 parent dd5b540 commit 0bfa784

5 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class T3TerminalModule: Module {
77
// Bumped when native hardware-keyboard handling changes; surfaced in the JS debug
88
// logs so a stale native binary is distinguishable from a broken key pipeline.
99
Constants([
10-
"hardwareKeyRevision": 3,
10+
"hardwareKeyRevision": 4,
1111
"outputRevision": 1,
1212
])
1313

@@ -63,7 +63,7 @@ public class T3TerminalModule: Module {
6363
Prop("captureRequest") { (view: T3TerminalView, request: Double) in
6464
view.captureRequest = request
6565
}
66-
Events("onInput", "onResize", "onCapture")
66+
Events("onInput", "onResize", "onCapture", "onPaste")
6767
}
6868
}
6969
}

‎apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift‎

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ private enum TerminalInputSequence {
128128
private final class TerminalInputField: UITextField {
129129
var onDeleteBackward: (() -> Void)?
130130
var onInsert: ((String) -> Void)?
131+
var onPaste: (() -> Void)?
131132

132133
private static let hardwareKeyCommands = TerminalHardwareKeyEncoder.makeKeyCommands(
133134
action: #selector(handleHardwareKeyCommand(_:))
@@ -142,6 +143,12 @@ private final class TerminalInputField: UITextField {
142143
super.deleteBackward()
143144
}
144145

146+
/// ⌘V, the iPad shortcut bar, and the edit menu land here. Inserting the text instead
147+
/// would send it as typed input, skipping paste encoding and write chunking.
148+
override func paste(_ sender: Any?) {
149+
onPaste?()
150+
}
151+
145152
@objc
146153
private func handleHardwareKeyCommand(_ command: UIKeyCommand) {
147154
guard let input = command.input else { return }
@@ -184,14 +191,16 @@ private extension UIColor {
184191
}
185192
}
186193

187-
public final class T3TerminalView: ExpoView, UITextFieldDelegate {
194+
public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInteractionDelegate {
188195
private static let minimumVerticalScrollStepPoints: CGFloat = 18
189196
private static let verticalScrollStepMultiplier: CGFloat = 1.15
190197

191198
private let terminalViewport = UIView()
192199
private let inputField = TerminalInputField()
193200
private let focusTapGesture = UITapGestureRecognizer()
194201
private let scrollPanGesture = UIPanGestureRecognizer()
202+
private let pasteMenuGesture = UILongPressGestureRecognizer()
203+
private lazy var pasteMenuInteraction = UIEditMenuInteraction(delegate: self)
195204
private var lastViewportSize: CGSize = .zero
196205
private var lastContentScale: CGFloat = 0
197206
private var lastReportedGrid: (cols: Int, rows: Int)?
@@ -208,6 +217,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
208217
let onInput = EventDispatcher()
209218
let onResize = EventDispatcher()
210219
let onCapture = EventDispatcher()
220+
let onPaste = EventDispatcher()
211221
var captureRequest: Double = 0 {
212222
didSet {
213223
guard captureRequest > 0, captureRequest != oldValue else { return }
@@ -348,13 +358,19 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
348358
inputField.onInsert = { [weak self] data in
349359
self?.emitInput(data)
350360
}
361+
inputField.onPaste = { [weak self] in
362+
self?.emitPaste()
363+
}
351364

352365
focusTapGesture.addTarget(self, action: #selector(handleViewportTap))
353366
terminalViewport.addGestureRecognizer(focusTapGesture)
354367
scrollPanGesture.addTarget(self, action: #selector(handleViewportPan(_:)))
355368
scrollPanGesture.maximumNumberOfTouches = 1
356369
scrollPanGesture.cancelsTouchesInView = false
357370
terminalViewport.addGestureRecognizer(scrollPanGesture)
371+
pasteMenuGesture.addTarget(self, action: #selector(handleViewportLongPress(_:)))
372+
terminalViewport.addGestureRecognizer(pasteMenuGesture)
373+
terminalViewport.addInteraction(pasteMenuInteraction)
358374

359375
addSubview(terminalViewport)
360376
addSubview(inputField)
@@ -466,6 +482,52 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
466482
}
467483
}
468484

485+
@objc
486+
private func handleViewportLongPress(_ gesture: UILongPressGestureRecognizer) {
487+
guard gesture.state == .began, canPaste else { return }
488+
pasteMenuInteraction.presentEditMenu(
489+
with: UIEditMenuConfiguration(identifier: nil, sourcePoint: gesture.location(in: terminalViewport))
490+
)
491+
}
492+
493+
public func editMenuInteraction(
494+
_ interaction: UIEditMenuInteraction,
495+
menuFor configuration: UIEditMenuConfiguration,
496+
suggestedActions: [UIMenuElement]
497+
) -> UIMenu? {
498+
// The terminal has no text selection, so keep only Paste. The hidden input field
499+
// would otherwise contribute actions such as Scan Text or Writing Tools.
500+
UIMenu(children: Self.pasteCommand(in: suggestedActions).map { [$0] } ?? [])
501+
}
502+
503+
private static func pasteCommand(in elements: [UIMenuElement]) -> UIMenuElement? {
504+
for element in elements {
505+
if let command = element as? UICommand,
506+
command.action == #selector(UIResponderStandardEditActions.paste(_:)) {
507+
return command
508+
}
509+
if let menu = element as? UIMenu, let command = pasteCommand(in: menu.children) {
510+
return command
511+
}
512+
}
513+
return nil
514+
}
515+
516+
public override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
517+
if action == #selector(UIResponderStandardEditActions.paste(_:)) {
518+
return canPaste
519+
}
520+
return super.canPerformAction(action, withSender: sender)
521+
}
522+
523+
public override func paste(_ sender: Any?) {
524+
emitPaste()
525+
}
526+
527+
private var canPaste: Bool {
528+
!readOnly && UIPasteboard.general.hasStrings
529+
}
530+
469531
@objc
470532
private func handleInputEditingDidBegin() {
471533
textInputModeDidChange()
@@ -716,6 +778,13 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
716778
onInput(["data": data])
717779
}
718780

781+
/// Reads the clipboard inside the user's paste action, so iOS does not ask for paste
782+
/// permission. JS sends the text through the same paste path as the toolbar button.
783+
private func emitPaste() {
784+
guard !readOnly, let text = UIPasteboard.general.string, !text.isEmpty else { return }
785+
onPaste(["text": text])
786+
}
787+
719788
private func textInputModeDidChange() {
720789
guard let app else { return }
721790
ghostty_app_keyboard_changed(app)

‎apps/mobile/src/features/terminal/NativeTerminalSurface.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ interface TerminalSurfaceProps extends ViewProps {
6565
readonly onCapture?: (text: string) => void;
6666
readonly theme?: TerminalTheme;
6767
readonly onInput: (data: string) => void;
68+
readonly onPaste?: (text: string) => void;
6869
readonly onResize: (size: { readonly cols: number; readonly rows: number }) => void;
6970
}
7071

@@ -290,6 +291,9 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm
290291
style={{ flex: 1 }}
291292
themeConfig={buildGhosttyThemeConfig(theme)}
292293
onInput={handleNativeInput}
294+
onPaste={(event) => {
295+
if (props.isRunning && !props.readOnly) props.onPaste?.(event.nativeEvent.text);
296+
}}
293297
onResize={handleNativeResize}
294298
captureRequest={props.captureRequest}
295299
onCapture={(event) => props.onCapture?.(event.nativeEvent.text)}

‎apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,9 @@ function ThreadTerminal(props: {
658658
return result._tag === "Success";
659659
};
660660

661-
const pasteFromClipboard = async () => {
661+
const pasteFromClipboard = async (readText: () => Promise<string> = Clipboard.getStringAsync) => {
662662
await pasteSession.paste({
663-
readText: Clipboard.getStringAsync,
663+
readText,
664664
write: writeInput,
665665
onReadError: (cause) => {
666666
console.error(new TerminalClipboardReadError({ terminalId, cause }));
@@ -695,6 +695,11 @@ function ThreadTerminal(props: {
695695
writeModifiedInput(data);
696696
};
697697

698+
const handleNativePaste = (text: string) => {
699+
setPendingModifier(null);
700+
void pasteFromClipboard(async () => text);
701+
};
702+
698703
const handleResize = (size: { readonly cols: number; readonly rows: number }) => {
699704
terminalDebugLog("native:onResize", {
700705
cols: size.cols,
@@ -971,6 +976,7 @@ function ThreadTerminal(props: {
971976
Dialog.alert("No terminal output", "There is no visible output to attach.");
972977
}}
973978
onInput={handleInput}
979+
onPaste={handleNativePaste}
974980
onResize={handleResize}
975981
style={{ flex: 1 }}
976982
terminalKey={terminalKey}

‎apps/mobile/src/features/terminal/nativeTerminalModule.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ interface TerminalInputEvent {
1616
readonly data: string;
1717
}
1818

19+
interface TerminalPasteEvent {
20+
readonly text: string;
21+
}
22+
1923
interface TerminalResizeEvent {
2024
readonly cols: number;
2125
readonly rows: number;
@@ -37,6 +41,8 @@ export interface NativeTerminalSurfaceProps extends ViewProps {
3741
readonly initialBuffer: string;
3842
readonly fontSize: number;
3943
readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void;
44+
/** iOS only: clipboard text from a system paste (edit menu, ⌘V, shortcut bar). */
45+
readonly onPaste?: (event: NativeSyntheticEvent<TerminalPasteEvent>) => void;
4046
readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void;
4147
}
4248

0 commit comments

Comments
 (0)