Skip to content

Commit f7c216b

Browse files
authored
Add "Copy or Paste" pointer action (#624)
This action allows the user to copy and paste with the same pointer binding, e.g. right-click. When the bound event occurs: - If the window contains selected text, that text is copied to the clipboard and the selection is cleared. - If the window does not contain selected text, the action will paste from the clipboard
1 parent d35a24c commit f7c216b

File tree

6 files changed

+27
-0
lines changed

6 files changed

+27
-0
lines changed

sources/Browser/Core/iTermBrowserPointerActionPerformer.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,14 @@ class iTermBrowserPointerActionPerformer: NSObject, PointerControllerDelegate {
161161
func copyLinkAddress(with event: NSEvent) {
162162
delegate?.actionPerformingCopyLinkAddress(atPointInWindow: event.locationInWindow)
163163
}
164+
165+
func copyOrPaste(with event: NSEvent) {
166+
Task {
167+
if await delegate?.actionPerformingHasSelection() == true {
168+
delegate?.actionPerformingCopyToClipboard()
169+
} else if NSString.fromPasteboard()?.isEmpty == false {
170+
delegate?.actionPerformingPasteFromClipboard()
171+
}
172+
}
173+
}
164174
}

sources/PTYTextView.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7656,6 +7656,15 @@ - (void)copyLinkAddressWithEvent:(NSEvent *)event {
76567656
}
76577657
}
76587658

7659+
- (void)copyOrPasteWithEvent:(NSEvent *)event {
7660+
if ([self canCopy]) {
7661+
[self copySelectionAccordingToUserPreferences];
7662+
[self deselect];
7663+
} else {
7664+
[self paste:nil];
7665+
}
7666+
}
7667+
76597668
- (void)nextTabWithEvent:(NSEvent *)event {
76607669
[_delegate textViewSelectNextTab];
76617670
}

sources/PointerController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ - (void)performAction:(NSString *)action
109109
NSString *title = parts[1];
110110
[delegate_ selectMenuItemWithIdentifier:identifier title:title event:event];
111111
}
112+
} else if ([action isEqualToString:kCopyOrPastePointerAction]) {
113+
[delegate_ copyOrPasteWithEvent:event];
112114
} else if ([action isEqualToString:kIgnoreAction]) {
113115
// Do nothing
114116
}

sources/PointerControllerDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,7 @@ protocol PointerControllerDelegate: AnyObject {
114114

115115
@objc(copyLinkAddressWithEvent:)
116116
func copyLinkAddress(with event: NSEvent)
117+
118+
@objc(copyOrPasteWithEvent:)
119+
func copyOrPaste(with event: NSEvent)
117120
}

sources/PointerPrefsController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extern NSString *kQuickLookAction;
4040
extern NSString *kIgnoreAction;
4141
extern NSString *kSelectMenuItemPointerAction;
4242
extern NSString *kCopyLinkAddressPointerAction;
43+
extern NSString *kCopyOrPastePointerAction;
4344

4445
extern NSString *kThreeFingerClickGesture;
4546
extern NSString *kThreeFingerSwipeRight;

sources/PointerPrefsController.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
NSString *kIgnoreAction = @"kIgnoreAction";
8383
NSString *kSelectMenuItemPointerAction = @"kSelectMenuItemPointerAction";
8484
NSString *kCopyLinkAddressPointerAction = @"kCopyLinkAddressPointerAction";
85+
NSString *kCopyOrPastePointerAction = @"kCopyOrPastePointerAction";
8586

8687
typedef enum {
8788
kNoArg,
@@ -437,6 +438,7 @@ + (NSDictionary *)localizedActionMap
437438
@"Copy Link Address", kCopyLinkAddressPointerAction,
438439
@"Select Next Pane", kSelectNextPanePointerAction,
439440
@"Select Previous Pane", kSelectPreviousPanePointerAction,
441+
@"Copy or Paste", kCopyOrPastePointerAction,
440442
nil];
441443
return names;
442444
}

0 commit comments

Comments
 (0)