Skip to content

Commit 0a4b3ea

Browse files
committed
Make annotations less ugly
1 parent 1c9c681 commit 0a4b3ea

File tree

3 files changed

+94
-40
lines changed

3 files changed

+94
-40
lines changed

sources/PTYNoteView.m

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
//
88

99
#import "PTYNoteView.h"
10+
#import "NSAppearance+iTerm.h"
1011
#import "SFSymbolEnum/SFSymbolEnum.h"
1112
#import "iTermMouseCursor.h"
1213
#import "NSImage+iTerm.h"
1314

1415
static const CGFloat kMinWidth = 50;
15-
static const CGFloat kMinHeight = 33;
16+
static const CGFloat kMinHeight = 39;
1617

17-
static const CGFloat kLeftMargin = 5;
18-
static const CGFloat kRightMargin = 14;
19-
static const CGFloat kTopMargin = 4;
20-
static const CGFloat kBottomMargin = 0;
21-
static const CGFloat kKillButtonTopMargin = 2;
18+
static const CGFloat kLeftMargin = 8;
19+
static const CGFloat kRightMargin = 20;
20+
static const CGFloat kTopMargin = 6;
21+
static const CGFloat kBottomMargin = 2;
2222

2323
static const CGFloat kInset = 5;
2424
static const CGFloat kRadius = 5;
@@ -41,46 +41,63 @@ - (instancetype)initWithFrame:(NSRect)frame {
4141
self = [super initWithFrame:frame];
4242
if (self) {
4343
backgroundColor_ = [[self defaultBackgroundColor] retain];
44-
NSImage *closeImage;
45-
if (@available(macOS 11.0, *)) {
46-
closeImage = [NSImage imageWithSystemSymbolName:SFSymbolGetString(SFSymbolTrash)
47-
accessibilityDescription:@"Delete annotation"];
48-
closeImage = [closeImage it_imageWithTintColor:[NSColor blackColor]];
49-
} else {
50-
closeImage = [NSImage it_imageNamed:@"closebutton" forClass:self.class];
51-
}
5244
killButton_ = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, kButtonSize, kButtonSize)];
5345
[killButton_ setButtonType:NSButtonTypeMomentaryPushIn];
54-
[killButton_ setImage:closeImage];
5546
[killButton_ setTarget:self];
5647
[killButton_ setAction:@selector(kill:)];
5748
[killButton_ setBordered:NO];
5849
[[killButton_ cell] setHighlightsBy:NSContentsCellMask];
5950
[killButton_ setTitle:@""];
6051
[self addSubview:killButton_];
52+
[self updateKillButtonImage];
6153
[killButton_ release];
6254
}
6355
return self;
6456
}
6557

58+
- (void)updateKillButtonImage {
59+
NSImage *closeImage;
60+
if (@available(macOS 11.0, *)) {
61+
closeImage = [NSImage imageWithSystemSymbolName:SFSymbolGetString(SFSymbolTrash)
62+
accessibilityDescription:@"Delete annotation"];
63+
NSColor *tintColor = self.effectiveAppearance.it_isDark ? [NSColor whiteColor] : [NSColor blackColor];
64+
closeImage = [closeImage it_imageWithTintColor:tintColor];
65+
} else {
66+
closeImage = [NSImage it_imageNamed:@"closebutton" forClass:self.class];
67+
}
68+
[killButton_ setImage:closeImage];
69+
}
70+
6671
- (void)dealloc {
6772
[contentView_ release];
6873
[backgroundColor_ release];
6974
[super dealloc];
7075
}
7176

7277
- (NSColor *)defaultBackgroundColor {
73-
return [NSColor colorWithCalibratedRed:252.0/255.0
78+
if (self.effectiveAppearance.it_isDark) {
79+
return [NSColor colorWithCalibratedRed:50.0/255.0
80+
green:50.0/255.0
81+
blue:52.0/255.0
82+
alpha:1];
83+
}
84+
return [NSColor colorWithCalibratedRed:250.0/255.0
7485
green:250.0/255.0
75-
blue:198.0/255.0
86+
blue:250.0/255.0
7687
alpha:1];
7788
}
7889

7990
- (NSColor *)borderColor {
80-
return [NSColor colorWithCalibratedRed:255.0/255.0
81-
green:229.0/255.0
82-
blue:114.0/255.0
83-
alpha:0.95];
91+
if (self.effectiveAppearance.it_isDark) {
92+
return [NSColor colorWithCalibratedRed:1
93+
green:1
94+
blue:1
95+
alpha:0.4];
96+
}
97+
return [NSColor colorWithCalibratedRed:0
98+
green:0
99+
blue:0
100+
alpha:0.1];
84101
}
85102

86103
static NSPoint FlipPoint(NSPoint p, CGFloat height) {
@@ -312,11 +329,21 @@ - (NSBezierPath *)roundedRectangleWithPointerInRect:(NSRect)frame
312329
- (void)viewDidMoveToSuperview {
313330
[super viewDidMoveToSuperview];
314331

332+
[self updateKillButtonImage];
333+
315334
NSClickGestureRecognizer *clickRecognizer = [[NSClickGestureRecognizer alloc] initWithTarget:self action:@selector(handleClick:)];
316335
clickRecognizer.delegate = self;
317336
[self addGestureRecognizer:clickRecognizer];
318337
}
319338

339+
- (void)viewDidChangeEffectiveAppearance {
340+
[super viewDidChangeEffectiveAppearance];
341+
[backgroundColor_ release];
342+
backgroundColor_ = [[self defaultBackgroundColor] retain];
343+
[self updateKillButtonImage];
344+
[self setNeedsDisplay:YES];
345+
}
346+
320347
- (void)handleClick:(NSClickGestureRecognizer *)recognizer {
321348
if (recognizer.state == NSGestureRecognizerStateRecognized) {
322349
[self reorderAboveSiblings];
@@ -487,28 +514,39 @@ - (void)layoutSubviews {
487514
bubbleFrame.size.width - kLeftMargin - kRightMargin,
488515
bubbleFrame.size.height - kTopMargin - kBottomMargin),
489516
self.frame.size.height);
517+
// Calculate what the centered position would be at minimum height to avoid jumps
518+
CGFloat minBubbleHeight = kMinHeight - kPointerLength - kInset;
519+
CGFloat centeredMarginAtMinHeight = (minBubbleHeight - kButtonSize) / 2.0;
520+
CGFloat killButtonY = NSMinY(bubbleFrame) + centeredMarginAtMinHeight;
490521
killButton_.frame = FlipRect(NSMakeRect(NSMaxX(bubbleFrame) - kButtonSize,
491-
NSMinY(bubbleFrame) + kKillButtonTopMargin,
522+
killButtonY,
492523
kButtonSize,
493524
kButtonSize),
494525
self.frame.size.height);
495526
[self.window invalidateCursorRectsForView:self];
496527
}
497528

498529
- (NSSize)sizeThatFitsContentView {
530+
CGFloat width;
531+
CGFloat height;
499532
switch (tipEdge_) {
500533
case kPTYNoteViewTipEdgeBottom:
501534
case kPTYNoteViewTipEdgeTop:
502-
return NSMakeSize(contentView_.frame.size.width + kLeftMargin + kRightMargin + kInset,
503-
contentView_.frame.size.height + kBottomMargin + kTopMargin + kPointerLength + kInset);
535+
width = contentView_.frame.size.width + kLeftMargin + kRightMargin + kInset;
536+
height = contentView_.frame.size.height + kBottomMargin + kTopMargin + kPointerLength + kInset;
537+
break;
504538

505539
case kPTYNoteViewTipEdgeLeft:
506540
case kPTYNoteViewTipEdgeRight:
507-
return NSMakeSize(contentView_.frame.size.width + kLeftMargin + kRightMargin + kPointerLength + kInset,
508-
contentView_.frame.size.height + kBottomMargin + kTopMargin + kInset);
541+
width = contentView_.frame.size.width + kLeftMargin + kRightMargin + kPointerLength + kInset;
542+
height = contentView_.frame.size.height + kBottomMargin + kTopMargin + kInset;
543+
break;
544+
545+
default:
546+
assert(false);
547+
return NSMakeSize(0, 0);
509548
}
510-
assert(false);
511-
return NSMakeSize(0, 0);
549+
return NSMakeSize(MAX(width, kMinWidth), MAX(height, kMinHeight));
512550
}
513551

514552
- (void)kill:(id)sender {

sources/PTYNoteViewController.m

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ - (void)loadView {
8686
self.noteView.autoresizesSubviews = YES;
8787
self.noteView.delegate = self;
8888
NSShadow *shadow = [[NSShadow alloc] init];
89-
shadow.shadowColor = [[NSColor blackColor] colorWithAlphaComponent:0.5];
90-
shadow.shadowOffset = NSMakeSize(1, -1);
91-
shadow.shadowBlurRadius = 1.0;
89+
shadow.shadowColor = [[NSColor blackColor] colorWithAlphaComponent:0.15];
90+
shadow.shadowOffset = NSMakeSize(0, -2);
91+
shadow.shadowBlurRadius = 8.0;
9292
self.noteView.wantsLayer = YES;
9393
self.noteView.shadow = shadow;
9494

@@ -117,7 +117,6 @@ - (void)loadView {
117117
textView_.textContainer.containerSize = NSMakeSize(scrollView_.frame.size.width, FLT_MAX);
118118
textView_.textContainer.widthTracksTextView = YES;
119119
textView_.delegate = self;
120-
textView_.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
121120
scrollView_.documentView = textView_;
122121

123122
// Put the scrollview in a wrapper so we can have a few pixels of padding
@@ -142,12 +141,31 @@ - (void)setAnchor:(NSPoint)anchor {
142141
anchor_ = anchor;
143142

144143
NSRect superViewFrame = noteView_.superview.frame;
145-
CGFloat xOffset = 0;
146-
if (anchor_.x + noteView_.frame.size.width > superViewFrame.size.width) {
147-
xOffset = anchor_.x + noteView_.frame.size.width - superViewFrame.size.width;
144+
NSSize size = [noteView_ sizeThatFitsContentView];
145+
146+
// Position the bubble so the pointer is roughly centered, making it more vertical.
147+
// xOffset is how far the pointer tip is from the left edge of the bubble.
148+
const CGFloat pointerInset = 40;
149+
CGFloat xOffset;
150+
151+
// Ideal bubble position: pointer at pointerInset from left edge.
152+
CGFloat idealBubbleX = anchor_.x - pointerInset;
153+
154+
if (idealBubbleX < 0) {
155+
// Bubble would go off left edge - align to left edge.
156+
xOffset = anchor_.x;
157+
} else if (idealBubbleX + size.width > superViewFrame.size.width) {
158+
// Bubble would go off right edge - align to right edge.
159+
xOffset = anchor_.x - (superViewFrame.size.width - size.width);
160+
} else {
161+
// Bubble fits with ideal positioning.
162+
xOffset = pointerInset;
148163
}
164+
165+
// Clamp xOffset to valid range (pointer must be within bubble).
166+
xOffset = MAX(0, MIN(xOffset, size.width));
167+
149168
noteView_.tipEdge = kPTYNoteViewTipEdgeTop;
150-
NSSize size = [noteView_ sizeThatFitsContentView];
151169
noteView_.point = NSMakePoint(xOffset, 0);
152170
noteView_.frame = NSMakeRect(anchor_.x - xOffset,
153171
anchor_.y,
@@ -333,7 +351,7 @@ - (void)updateBackgroundColor {
333351
alpha = alpha * alpha;
334352

335353
NSColor *defaultBg = [self.noteView defaultBackgroundColor];
336-
CGFloat highlightComponents[] = { 0.9, 0.8, 0 };
354+
CGFloat highlightComponents[] = { 0.85, 0.92, 1.0 };
337355
CGFloat components[3] = {
338356
[defaultBg redComponent] * alpha + (1 - alpha) * highlightComponents[0],
339357
[defaultBg greenComponent] * alpha + (1 - alpha) * highlightComponents[1],

sources/iTermTextDrawingHelper.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,9 +1395,7 @@ - (void)drawNoteRangesOnLine:(int)line
13951395

13961396
CGFloat maxX = MIN(_frame.size.width - [iTermPreferences sideMargins], range.length * _cellSize.width + x);
13971397
CGFloat w = maxX - x;
1398-
iTermRectFill(NSMakeRect(x, y + _cellSize.height - 1.5, w, 1), virtualOffset);
1399-
[[NSColor orangeColor] set];
1400-
iTermRectFill(NSMakeRect(x, y + _cellSize.height - 1, w, 1), virtualOffset);
1398+
iTermRectFill(NSMakeRect(x, y + _cellSize.height - 2, w, 1), virtualOffset);
14011399
}
14021400

14031401
}

0 commit comments

Comments
 (0)