@@ -201,6 +201,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
201201 private let inputField = TerminalInputField ( )
202202 private let focusTapGesture = UITapGestureRecognizer ( )
203203 private let scrollPanGesture = UIPanGestureRecognizer ( )
204+ private let selectTextGesture = UILongPressGestureRecognizer ( )
204205 private var lastViewportSize : CGSize = . zero
205206 private var lastContentScale : CGFloat = 0
206207 private var lastReportedGrid : ( cols : Int , rows : Int ) ?
@@ -219,15 +220,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
219220 var captureRequest : Double = 0 {
220221 didSet {
221222 guard captureRequest > 0 , captureRequest != oldValue else { return }
222- guard let surface else { onCapture ( [ " text " : " " ] ) ; return }
223- let selection = ghostty_selection_s (
224- top_left : ghostty_point_s ( tag : GHOSTTY_POINT_VIEWPORT , coord : GHOSTTY_POINT_COORD_TOP_LEFT , x : 0 , y : 0 ) ,
225- bottom_right : ghostty_point_s ( tag : GHOSTTY_POINT_VIEWPORT , coord : GHOSTTY_POINT_COORD_BOTTOM_RIGHT , x : 0 , y : 0 ) ,
226- rectangle : false )
227- var captured = ghostty_text_s ( )
228- guard ghostty_surface_read_text ( surface , selection , & captured ) else { onCapture ( [ " text " : " " ] ) ; return }
229- defer { ghostty_surface_free_text ( surface , & captured ) }
230- let text = captured . text . flatMap { String ( bytes : UnsafeBufferPointer ( start : UnsafeRawPointer ( $0 ) . assumingMemoryBound ( to : UInt8 . self ) , count : Int ( captured . text_len ) ) , encoding : . utf8 ) } ?? " "
223+ let text = readViewportText ( ) ?? " "
231224 // Android joins snapshot rows with "\n" and trims each row's trailing whitespace, so do
232225 // the same here: identical terminal content must capture identically on both platforms.
233226 let normalized = text . split ( separator : " \n " , omittingEmptySubsequences : false )
@@ -355,6 +348,9 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
355348 scrollPanGesture . maximumNumberOfTouches = 1
356349 scrollPanGesture . cancelsTouchesInView = false
357350 terminalViewport . addGestureRecognizer ( scrollPanGesture )
351+ selectTextGesture . addTarget ( self , action : #selector( handleSelectText ( _ : ) ) )
352+ terminalViewport . addGestureRecognizer ( selectTextGesture )
353+ focusTapGesture . require ( toFail : selectTextGesture )
358354
359355 addSubview ( terminalViewport )
360356 addSubview ( inputField )
@@ -426,6 +422,40 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
426422 requestKeyboardFocus ( )
427423 }
428424
425+ @ objc
426+ private func handleSelectText ( _ gesture : UILongPressGestureRecognizer ) {
427+ guard gesture . state == . began ,
428+ let presenter = appContext ? . utilities ? . currentViewController ( ) ,
429+ let text = readViewportText ( ) ,
430+ ! text . trimmingCharacters ( in : . whitespacesAndNewlines ) . isEmpty
431+ else { return }
432+
433+ // Capture before dismissing the keyboard: doing so can resize the remote PTY.
434+ // UIKit owns selection in this snapshot, so live output cannot move its endpoints.
435+ inputField . resignFirstResponder ( )
436+ let controller = TerminalTextSelectionViewController ( text : text , fontSize : fontSize )
437+ let navigation = UINavigationController ( rootViewController : controller )
438+ navigation . modalPresentationStyle = . pageSheet
439+ presenter . present ( navigation , animated : ! UIAccessibility . isReduceMotionEnabled )
440+ }
441+
442+ private func readViewportText ( ) -> String ? {
443+ guard let surface else { return nil }
444+ let selection = ghostty_selection_s (
445+ top_left : ghostty_point_s ( tag : GHOSTTY_POINT_VIEWPORT , coord : GHOSTTY_POINT_COORD_TOP_LEFT , x : 0 , y : 0 ) ,
446+ bottom_right : ghostty_point_s ( tag : GHOSTTY_POINT_VIEWPORT , coord : GHOSTTY_POINT_COORD_BOTTOM_RIGHT , x : 0 , y : 0 ) ,
447+ rectangle : false )
448+ var captured = ghostty_text_s ( )
449+ guard ghostty_surface_read_text ( surface , selection , & captured ) else { return nil }
450+ defer { ghostty_surface_free_text ( surface , & captured ) }
451+ guard let text = captured . text else { return nil }
452+ return String (
453+ bytes : UnsafeBufferPointer (
454+ start : UnsafeRawPointer ( text ) . assumingMemoryBound ( to : UInt8 . self ) ,
455+ count : Int ( captured . text_len ) ) ,
456+ encoding : . utf8 )
457+ }
458+
429459 @ objc
430460 private func handleViewportPan ( _ gesture : UIPanGestureRecognizer ) {
431461 guard let surface else { return }
0 commit comments