@@ -128,6 +128,7 @@ private enum TerminalInputSequence {
128128private 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 )
0 commit comments