OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/contextual_search/contextual_search_highlighter_v
iew.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/browser/ui/contextual_search/contextual_search_controller.h" |
| 10 |
| 11 @implementation ContextualSearchHighlighterView { |
| 12 // Store size of the container view. If size change, text layout will likely |
| 13 // change and should be updated. |
| 14 CGSize _previousSize; |
| 15 |
| 16 // Store values that were used last time the rects were drawn. |
| 17 CGPoint _scroll; |
| 18 CGFloat _zoom; |
| 19 CGFloat _offset; |
| 20 |
| 21 __unsafe_unretained id<ContextualSearchHighlighterDelegate> _delegate; |
| 22 } |
| 23 |
| 24 - (instancetype)initWithFrame:(CGRect)frame |
| 25 delegate: |
| 26 (id<ContextualSearchHighlighterDelegate>)delegate { |
| 27 self = [super initWithFrame:frame]; |
| 28 if (self) { |
| 29 [self setUserInteractionEnabled:NO]; |
| 30 [self setAutoresizingMask:UIViewAutoresizingFlexibleWidth | |
| 31 UIViewAutoresizingFlexibleHeight]; |
| 32 _previousSize = frame.size; |
| 33 _delegate = delegate; |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 - (instancetype)initWithFrame:(CGRect)frame { |
| 39 NOTREACHED(); |
| 40 return nil; |
| 41 } |
| 42 |
| 43 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 44 NOTREACHED(); |
| 45 return nil; |
| 46 } |
| 47 |
| 48 - (void)layoutSubviews { |
| 49 [super layoutSubviews]; |
| 50 if (!CGSizeEqualToSize(_previousSize, self.frame.size)) { |
| 51 _previousSize = self.frame.size; |
| 52 dispatch_async(dispatch_get_main_queue(), ^{ |
| 53 [_delegate updateHighlight]; |
| 54 }); |
| 55 } |
| 56 } |
| 57 |
| 58 - (void)highlightRects:(NSArray*)rects |
| 59 withOffset:(CGFloat)offset |
| 60 zoom:(CGFloat)zoom |
| 61 scroll:(CGPoint)scroll { |
| 62 for (UIView* view : [self subviews]) { |
| 63 [view removeFromSuperview]; |
| 64 } |
| 65 for (NSValue* value in rects) { |
| 66 CGRect rect = [value CGRectValue]; |
| 67 rect.origin.x *= zoom; |
| 68 rect.origin.y *= zoom; |
| 69 rect.size.width *= zoom; |
| 70 rect.size.height *= zoom; |
| 71 rect.origin.x -= scroll.x; |
| 72 rect.origin.y += offset - scroll.y; |
| 73 UIView* view = [[[UIView alloc] initWithFrame:rect] autorelease]; |
| 74 [self addSubview:view]; |
| 75 view.backgroundColor = |
| 76 [UIColor colorWithRed:0.67 green:0.88 blue:0.96 alpha:0.6]; |
| 77 } |
| 78 [[self superview] bringSubviewToFront:self]; |
| 79 _zoom = zoom; |
| 80 _scroll = scroll; |
| 81 _offset = offset; |
| 82 } |
| 83 |
| 84 - (void)setScroll:(CGPoint)newScroll |
| 85 zoom:(CGFloat)newZoom |
| 86 offset:(CGFloat)newOffset { |
| 87 for (UIView* view : [self subviews]) { |
| 88 CGRect oldFrame = [view frame]; |
| 89 CGRect newFrame; |
| 90 CGFloat scaleAdjust = newZoom / _zoom; |
| 91 newFrame.origin.x = |
| 92 (oldFrame.origin.x + _scroll.x) * scaleAdjust - newScroll.x; |
| 93 newFrame.origin.y = |
| 94 (oldFrame.origin.y + _scroll.y - _offset) * scaleAdjust + newOffset - |
| 95 newScroll.y; |
| 96 newFrame.size.width = oldFrame.size.width * scaleAdjust; |
| 97 newFrame.size.height = oldFrame.size.height * scaleAdjust; |
| 98 [view setFrame:newFrame]; |
| 99 } |
| 100 _scroll = newScroll; |
| 101 } |
| 102 |
| 103 - (CGRect)boundingRect { |
| 104 CGRect bounding = CGRectNull; |
| 105 for (UIView* view : [self subviews]) { |
| 106 bounding = CGRectUnion(bounding, view.frame); |
| 107 } |
| 108 bounding.origin.x += _scroll.x; |
| 109 bounding.origin.y += _scroll.y; |
| 110 return bounding; |
| 111 } |
| 112 |
| 113 @end |
OLD | NEW |