Skip to content

Commit b38468e

Browse files
committed
Add copy to context menu for status bar components. Issue 12777
1 parent e8564b0 commit b38468e

10 files changed

+58
-0
lines changed

sources/iTermStatusBarActionComponent.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ - (nullable NSImage *)statusBarComponentIcon {
114114
return [NSImage it_cacheableImageNamed:@"StatusBarIconAction" forClass:[self class]];
115115
}
116116

117+
- (NSString *)statusBarComponentCopyableString {
118+
return nil;
119+
}
120+
117121
- (BOOL)statusBarComponentHandlesClicks {
118122
return YES;
119123
}
@@ -132,6 +136,10 @@ - (void)statusBarComponentDidClickWithView:(NSView *)view {
132136

133137
@implementation iTermStatusBarActionMenuComponent
134138

139+
- (NSString *)statusBarComponentCopyableString {
140+
return nil;
141+
}
142+
135143
- (nullable NSImage *)statusBarComponentIcon {
136144
return [NSImage it_cacheableImageNamed:@"StatusBarIconAction" forClass:[self class]];
137145
}

sources/iTermStatusBarAttributedTextComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,10 @@ - (NSFont *)font {
290290
return self.advancedConfiguration.font ?: [iTermStatusBarAdvancedConfiguration defaultFont];
291291
}
292292

293+
- (nullable NSString *)statusBarComponentCopyableString {
294+
return [[self longestAttributedStringValue] string];
295+
}
296+
293297
- (CGFloat)statusBarComponentVerticalOffset {
294298
NSFont *font = [self font];
295299
const CGFloat containerHeight = _textField.superview.bounds.size.height;

sources/iTermStatusBarComponent.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ forInvocation:(NSString *)invocation
171171

172172
@optional
173173
- (NSFont *)font;
174+
// Returns a string suitable for copying to the clipboard, or nil if this component
175+
// does not have meaningful copyable text.
176+
- (nullable NSString *)statusBarComponentCopyableString;
174177

175178
@end
176179
NS_ASSUME_NONNULL_END

sources/iTermStatusBarContainerView.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,15 @@ - (BOOL)shouldDragWindowForEvent:(NSEvent *)event {
298298

299299
- (void)showContextMenuForEvent:(NSEvent *)event {
300300
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
301+
if ([_component respondsToSelector:@selector(statusBarComponentCopyableString)]) {
302+
NSString *copyableString = [_component statusBarComponentCopyableString];
303+
if (copyableString.length > 0) {
304+
[menu addItemWithTitle:@"Copy"
305+
action:@selector(copyComponentValue:)
306+
keyEquivalent:@""];
307+
[menu addItem:[NSMenuItem separatorItem]];
308+
}
309+
}
301310
if (![_component statusBarComponentIsInternal]) {
302311
if ([[_component statusBarComponentKnobs] count]) {
303312
[menu addItemWithTitle:[NSString stringWithFormat:@"Configure %@", [self.component statusBarComponentShortDescription]]
@@ -331,6 +340,16 @@ - (void)showContextMenuForEvent:(NSEvent *)event {
331340
[NSMenu popUpContextMenu:menu withEvent:event forView:self];
332341
}
333342

343+
- (void)copyComponentValue:(id)sender {
344+
if ([_component respondsToSelector:@selector(statusBarComponentCopyableString)]) {
345+
NSString *string = [_component statusBarComponentCopyableString];
346+
if (string.length > 0) {
347+
[[NSPasteboard generalPasteboard] clearContents];
348+
[[NSPasteboard generalPasteboard] setString:string forType:NSPasteboardTypeString];
349+
}
350+
}
351+
}
352+
334353
- (void)configureComponent:(id)sender {
335354
[self.delegate statusBarContainerView:self configureComponent:self.component];
336355
}

sources/iTermStatusBarGitComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ - (void)bumpIfLastCommandWasGit {
398398
}
399399
}
400400

401+
- (nullable NSString *)statusBarComponentCopyableString {
402+
return self.currentState.branch;
403+
}
404+
401405
- (BOOL)statusBarComponentHandlesClicks {
402406
return YES;
403407
}

sources/iTermStatusBarPlaceholderComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ - (nullable NSString *)stringValueForCurrentWidth {
4545
return @[ self.stringValue ?: @"" ];
4646
}
4747

48+
- (nullable NSString *)statusBarComponentCopyableString {
49+
return nil;
50+
}
51+
4852
- (BOOL)statusBarComponentHandlesClicks {
4953
return YES;
5054
}

sources/iTermStatusBarSnippetComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ - (nullable NSString *)stringValueForCurrentWidth {
5050
return @[ self.stringValue ];
5151
}
5252

53+
- (NSString *)statusBarComponentCopyableString {
54+
return nil;
55+
}
56+
5357
- (BOOL)statusBarComponentHandlesClicks {
5458
return YES;
5559
}

sources/iTermStatusBarTextComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,10 @@ - (NSColor *)statusBarBackgroundColor {
236236
return [self backgroundColor];
237237
}
238238

239+
- (nullable NSString *)statusBarComponentCopyableString {
240+
return [self longestStringValue];
241+
}
242+
239243
- (CGFloat)statusBarComponentVerticalOffset {
240244
const CGFloat containerHeight = _textField.superview.bounds.size.height;
241245
const CGFloat capHeight = _textField.font.capHeight;

sources/iTermStatusBarTriggersComponent.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ class StatusBarTriggersComponent: iTermStatusBarTextComponent {
6464
return [ stringValue ]
6565
}
6666

67+
override func statusBarComponentCopyableString() -> String? {
68+
return nil
69+
}
70+
6771
override func statusBarComponentHandlesClicks() -> Bool {
6872
return true
6973
}

sources/iTermStatusBarVariableBaseComponent.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ - (nullable NSString *)stringByCompressingString:(NSString *)source {
302302
return [parts componentsJoinedByString:@"/"];
303303
}
304304

305+
- (nullable NSString *)statusBarComponentCopyableString {
306+
return self.fullString;
307+
}
308+
305309
- (BOOL)statusBarComponentHandlesClicks {
306310
return YES;
307311
}

0 commit comments

Comments
 (0)