Fixes for copying on Safari & Firefox #94
+96
−15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When selecting text in the terminal and pressing Cmd+C (or releasing the mouse after a selection), the clipboard copy operation fails silently in Safari. The same operations work correctly in Chrome.
Root Cause
Safari has stricter security requirements for clipboard operations than Chrome:
User Gesture Requirement: Safari requires clipboard operations to happen synchronously within a user gesture (click, keypress, etc.)
Async Context Invalidation: The original
copyToClipboardmethod wasasyncand usedawait navigator.clipboard.writeText(). By the time theawaitresolves, Safari has already invalidated the user gesture context, causing the operation to fail silently.Canvas-based Selection: When pressing Cmd+C, the input handler was letting the event pass through to the browser. However, since ghostty-web uses a
<canvas>for rendering (not actual DOM text), there's no native text selection for the browser to copy.Solution
1. Use Modern Clipboard APIs with Safari Compatibility (
selection-manager.ts)Rewrote the clipboard copy strategy to prioritize modern APIs while maintaining Safari compatibility:
First try:
ClipboardItemAPIClipboardItemto be created synchronously within the user gestureSecond try:
navigator.clipboard.writeText()Third try: Legacy
document.execCommand('copy')Helper methods extracted for cleaner fallback chain:
copyWithWriteText()- wraps writeText with execCommand fallbackcopyWithExecCommand()- legacy copy using textarea2. Add Public
copySelection()Method (selection-manager.ts,terminal.ts)Added a public method to programmatically trigger clipboard copy:
3. Handle Cmd+C Keyboard Shortcut (
input-handler.ts,terminal.ts)Added an
onCopycallback to theInputHandlerthat gets called when Cmd+C is pressed:Files Changed
lib/selection-manager.tscopyToClipboard()to try modern APIs first (ClipboardItem → writeText → execCommand); extracted helper methods; added publiccopySelection()methodlib/input-handler.tsonCopycallback parameter; updated Cmd+C handling to use callbacklib/terminal.tscopySelection()method; pass copy callback to InputHandlerBrowser Compatibility
*Firefox stable doesn't support
ClipboardItem, so it falls through towriteTextwhich works well.Testing
To verify the fix works:
References