public
Last active

A little rounded black tooltip you can easily point at stuff

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/*
* CPTooltip.j
* Makes a little black rounded-rect gradient tooltip with some text (can have multiple lines with \n) which points at something.
* Usage example:
* tooltip = [[CPTooltip alloc] initWithText:@"Hello There" atPoint:CPPointMake(100,100)];
* [self addSubview:tooltip];
*
* The above will make a tooltip that says "Hello There" which is pointing at this view's 100x100 point.
* If the tooltip is too close to its superview's edges, it will adjust itself to maintain readability.
*
* Created by Justin Graves
* Copyright 2009, Infegy, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
@import <AppKit/CPView.j>
 
@implementation CPTooltip : CPView
{
CPLabel _text;
CPPoint _target;
CPArray _clipPoints;
var _horizontalPadding;
var _verticalPadding;
CPPoint _arrowOffset;
BOOL _flippedArrow;
}
 
- (id)initWithText:(CPString)text atPoint:(CPPoint)target
{
if (self = [super initWithFrame:CGRectMake(0,0,26,26)])
{
if(!_text)
{
_text = [[CPTextField alloc] initWithFrame:CGRectMakeZero()];
[_text setFont:[CPFont boldSystemFontOfSize:11.0]];
[_text setTextColor:[CPColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0]];
[_text setAlignment:CPCenterTextAlignment];
}
_target = target;
_arrowOffset = CPPointMakeZero();
_flippedArrow = NO;
[self setText:text];
[self addSubview:_text];
}
return self;
}
 
- (id)init
{
return [self initWithFrame:CGRectMakeZero()];
}
 
- (id)initWithFrame:(CGRect)aFrame
{
return [self initWithText:@"" atPoint:CPPointMakeZero()];
}
 
- (void)setText:(CPString)text
{
[self setText:text atPoint:_target];
}
 
- (void)setText:(CPString)text atPoint:(CPPoint)target
{
[_text setStringValue:text];
[_text sizeToFit];
// Make size around the requested point
var textSize = [_text frame].size;
// Padded height
_verticalPadding = 2;
textSize.height += (_verticalPadding * 2); // 20% padding top/bottom
// Padded width
_horizontalPadding = 5;
textSize.width += (_horizontalPadding * 2); // Add caps at the ends, each half of height
textSize.height += 6; // Add 6 pixel pointer
var lowerSegment = (textSize.width - 12 - (_horizontalPadding * 2)) / 2;
_clipPoints = Array(_horizontalPadding, _verticalPadding, lowerSegment);
[self setTarget:target withSize:textSize];
var textOrigin;
if(_flippedArrow)
textOrigin = CPPointMake(textSize.width/2, ((textSize.height - 6)/2) + 6);
else
textOrigin = CPPointMake(textSize.width/2, (textSize.height - 6)/2);
[_text setCenter:textOrigin];
}
 
- (void)setTarget:(CPPoint)target withSize:(CPSize)size
{
var bounds = [self frame];
var sizesMatch = bounds.size.width == size.width && bounds.size.height == size.height;
if(target.x == _target.x && target.y == _target.y && sizesMatch)
return;
_target.x = target.x;
_target.y = target.y;
// Find origin based on target point.
bounds.origin.y = target.y - size.height;
bounds.origin.x = target.x - size.width/2;
bounds.size.width = size.width;
bounds.size.height = size.height;
// Check if the tooltip is too close to the superview's edges
// If it is, move it around so the text is still clearly visibile
var oldOffset = CPPointMake(_arrowOffset.x, _arrowOffset.y);
var wasFlipped = _flippedArrow;
_arrowOffset.x = 0; _arrowOffset.y = 0;
_flippedArrow = NO;
if(_superview)
{
// Handle getting pushed to superview's edges
if(bounds.origin.x + _horizontalPadding < 0)
{
bounds.origin.x = -_horizontalPadding;
_arrowOffset.x = target.x - (bounds.origin.x + (size.width/2));
}
else if(bounds.origin.x + size.width - _horizontalPadding > [_superview bounds].size.width)
{
bounds.origin.x -= (bounds.origin.x + size.width - _horizontalPadding) - [_superview bounds].size.width;
_arrowOffset.x = target.x - (bounds.origin.x + (size.width/2));
}
// Handle moving too high up
if(bounds.origin.y < 0)
{
_flippedArrow = YES;
bounds.origin.y = target.y;
}
}
// If the arrow offset changed and sizes match, we need to redraw and setFrame didn't do it
// If sizes don't match, setFrame (just below) will redraw for us
needsDraw = NO;
if((oldOffset.x != _arrowOffset.x || oldOffset.y != _arrowOffset.y || wasFlipped != _flippedArrow) && sizesMatch)
needsDraw = YES;
if(needsDraw)
{
[self setFrameOrigin:bounds.origin];
[self setNeedsLayout];
[self setNeedsDisplay:YES];
}
else
[self setFrame:bounds];
}
 
- (void)drawRect:(CPRect)aRect
{
var context = [[CPGraphicsContext currentContext] graphicsPort];
CGContextBeginPath(context);
CGContextSaveGState(context);
// Prepare gradient for background
var locations = new Array(0.0, 1.5/_bounds.size.height, 1.0);
var components = new Array( 0.9, 0.9, 0.9, 1.0, // Start (top) color
0.3, 0.3, 0.3, 0.80,
0.0, 0.0, 0.0, 0.80); // End (bottom) color
var myGradient = CGGradientCreateWithColorComponents(CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB), components, locations, 3);
var myStartPoint = CPPointMake(0,0);
var myEndPoint = CPPointMake(0,_bounds.size.height);
// Draw the path for the main body: a rounded rect
var radius = 6;
var rect = CPRectMake(_bounds.origin.x, _bounds.origin.y, _bounds.size.width, _bounds.size.height);
rect.size.height -= 6;
// Draws the rect by setting up scaled four arcs
CGContextSaveGState(context);
if(_flippedArrow)
CGContextTranslateCTM(context, 0, 6);
CGContextScaleCTM (context, radius, radius);
fw = CGRectGetWidth (rect) / radius;
fh = CGRectGetHeight (rect) / radius;
CGContextMoveToPoint(context, fw, fh/2);
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
CGContextClosePath(context);
CGContextRestoreGState(context);
// Arrow pointing to target
// If the tooltip is too close to the top of its containing view, the arrow points up as the tooltip is moved down
if(_flippedArrow)
{
CGContextMoveToPoint(context, _clipPoints[0] + _clipPoints[2] + _arrowOffset.x, 6);
CGContextAddLineToPoint(context, _clipPoints[0] + _clipPoints[2] + 12 + _arrowOffset.x, 6);
CGContextAddLineToPoint(context, _clipPoints[0] + _clipPoints[2] + 6 + _arrowOffset.x, 0);
}
// Otherwise, the arrow points down
else
{
CGContextMoveToPoint(context, _clipPoints[0] + _clipPoints[2] + _arrowOffset.x, rect.size.height);
CGContextAddLineToPoint(context, _clipPoints[0] + _clipPoints[2] + 12 + _arrowOffset.x, rect.size.height);
CGContextAddLineToPoint(context, _clipPoints[0] + _clipPoints[2] + 6 + _arrowOffset.x, rect.size.height + 6);
}
CGContextClosePath(context);
// Actual gradient fill operation
CGContextClip(context);
CGContextDrawLinearGradient(context, myGradient, myStartPoint, myEndPoint, 0);
CGContextRestoreGState(context);
[super drawRect:aRect];
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.