Advertisement
Guest User

FindHighlightLayer - Scintilla

a guest
Apr 22nd, 2012
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3.  * Class to display the animated gold roundrect used on OS X for matches.
  4.  */
  5. @interface FindHighlightLayer : CAGradientLayer
  6. {
  7. @private
  8.     NSString *sFind;
  9.     int positionFind;
  10.     BOOL retaining;
  11.     CGFloat widthText;
  12.     CGFloat heightLine;
  13.     NSString *sFont;
  14.     CGFloat fontSize;
  15. }
  16.  
  17. @property (copy) NSString *sFind;
  18. @property (assign) int positionFind;
  19. @property (assign) BOOL retaining;
  20. @property (assign) CGFloat widthText;
  21. @property (assign) CGFloat heightLine;
  22. @property (copy) NSString *sFont;
  23. @property (assign) CGFloat fontSize;
  24.  
  25. - (void) animateMatch: (CGPoint)ptText bounce:(BOOL)bounce;
  26. - (void) hideMatch;
  27.  
  28. @end
  29.  
  30. //--------------------------------------------------------------------------------------------------
  31.  
  32. @implementation FindHighlightLayer
  33.  
  34. @synthesize sFind, positionFind, retaining, widthText, heightLine, sFont, fontSize;
  35.  
  36. -(id) init {
  37.     if (self = [super init]) {
  38.         [self setNeedsDisplayOnBoundsChange: YES];
  39.         // A gold to slightly redder gradient to match other applications
  40.         CGColorRef colGold = CGColorCreateGenericRGB(1.0, 1.0, 0, 1.0);
  41.         CGColorRef colGoldRed = CGColorCreateGenericRGB(1.0, 0.8, 0, 1.0);
  42.         self.colors = [NSArray arrayWithObjects:(id)colGoldRed, (id)colGold, nil];
  43.         CGColorRelease(colGoldRed);
  44.         CGColorRelease(colGold);
  45.        
  46.         CGColorRef colGreyBorder = CGColorCreateGenericGray(0.756f, 0.5f);
  47.         self.borderColor = colGreyBorder;
  48.         CGColorRelease(colGreyBorder);
  49.        
  50.         self.borderWidth = 1.0;
  51.         self.cornerRadius = 5.0f;
  52.         self.shadowRadius = 1.0f;
  53.         self.shadowOpacity = 0.9f;
  54.         self.shadowOffset = CGSizeMake(0.0f, -2.0f);
  55.         self.anchorPoint = CGPointMake(0.5, 0.5);
  56.     }
  57.     return self;
  58.    
  59. }
  60.  
  61. const CGFloat paddingHighlightX = 4;
  62. const CGFloat paddingHighlightY = 2;
  63.  
  64. -(void) drawInContext:(CGContextRef)context {
  65.     if (!sFind || !sFont)
  66.         return;
  67.    
  68.     CFStringRef str = CFStringRef(sFind);
  69.    
  70.     CFMutableDictionaryRef styleDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 2,
  71.                                                                  &kCFTypeDictionaryKeyCallBacks,
  72.                                                                  &kCFTypeDictionaryValueCallBacks);
  73.     CGColorRef color = CGColorCreateGenericRGB(0.0, 0.0, 0.0, 1.0);
  74.     CFDictionarySetValue(styleDict, kCTForegroundColorAttributeName, color);
  75.     CTFontRef fontRef = ::CTFontCreateWithName((CFStringRef)sFont, fontSize, NULL);
  76.     CFDictionaryAddValue(styleDict, kCTFontAttributeName, fontRef);
  77.    
  78.     CFAttributedStringRef attrString = ::CFAttributedStringCreate(NULL, str, styleDict);
  79.     CTLineRef textLine = ::CTLineCreateWithAttributedString(attrString);
  80.     // Indent from corner of bounds
  81.     CGContextSetTextPosition(context, paddingHighlightX, 3 + paddingHighlightY);
  82.     CTLineDraw(textLine, context);
  83.    
  84.     CFRelease(textLine);
  85.     CFRelease(attrString);
  86.     CFRelease(fontRef);
  87.     CGColorRelease(color);
  88.     CFRelease(styleDict);
  89. }
  90.  
  91. - (void) animateMatch: (CGPoint)ptText bounce:(BOOL)bounce {
  92.     if (!self.sFind || ![self.sFind length])
  93.         return;
  94.    
  95.     CGFloat width = self.widthText + paddingHighlightX * 2;
  96.     CGFloat height = self.heightLine + paddingHighlightY * 2;
  97.    
  98.     // Adjust for padding
  99.     ptText.x -= paddingHighlightX;
  100.     ptText.y += paddingHighlightY;
  101.    
  102.     // Shift point to centre as expanding about centre
  103.     ptText.x += width / 2.0;
  104.     ptText.y -= height / 2.0;
  105.    
  106.     [CATransaction begin];
  107.     [CATransaction setValue:[NSNumber numberWithFloat:0.0] forKey:kCATransactionAnimationDuration];
  108.     self.bounds = CGRectMake(0,0, width, height);
  109.     self.position = ptText;
  110.     if (bounce) {
  111.         // Do not reset visibility when just moving
  112.         self.hidden = NO;
  113.         self.opacity = 1.0;
  114.     }
  115.     [self setNeedsDisplay];
  116.     [CATransaction commit];
  117.    
  118.     if (bounce) {
  119.         CABasicAnimation *animBounce = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  120.         animBounce.duration = 0.15;
  121.         animBounce.autoreverses = YES;
  122.         animBounce.removedOnCompletion = NO;
  123.         animBounce.fromValue = [NSNumber numberWithFloat: 1.0];
  124.         animBounce.toValue = [NSNumber numberWithFloat: 1.25];
  125.        
  126.         if (self.retaining) {
  127.            
  128.             [self addAnimation: animBounce forKey:@"animateFound"];
  129.            
  130.         } else {
  131.            
  132.             CABasicAnimation *animFade = [CABasicAnimation animationWithKeyPath:@"opacity"];
  133.             animFade.duration = 0.1;
  134.             animFade.beginTime = 0.4;
  135.             animFade.removedOnCompletion = NO;
  136.             animFade.fromValue = [NSNumber numberWithFloat: 1.0];
  137.             animFade.toValue = [NSNumber numberWithFloat: 0.0];
  138.            
  139.             CAAnimationGroup *group = [CAAnimationGroup animation];
  140.             [group setDuration:0.5];
  141.             group.removedOnCompletion = NO;
  142.             group.fillMode = kCAFillModeForwards;
  143.             [group setAnimations:[NSArray arrayWithObjects:animBounce, animFade, nil]];
  144.            
  145.             [self addAnimation:group forKey:@"animateFound"];
  146.         }
  147.     }
  148. }
  149.  
  150. - (void) hideMatch {
  151.     self.sFind = @"";
  152.     self.positionFind = INVALID_POSITION;
  153.     self.hidden = YES;
  154. }
  155.  
  156. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement