OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/reader_mode/reader_mode_view.h" |
| 6 |
| 7 #include "base/mac/objc_property_releaser.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "ios/chrome/browser/dom_distiller/distiller_viewer.h" |
| 10 #import "ios/chrome/browser/ui/material_components/activity_indicator.h" |
| 11 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato
r/src/MaterialActivityIndicator.h" |
| 12 |
| 13 namespace { |
| 14 const CGFloat kCloseButtonSize = 40; |
| 15 const CGFloat kCloseButtonMargin = 10; |
| 16 const CGFloat kMinWidth = kCloseButtonSize + kCloseButtonMargin * 2; |
| 17 const CGFloat kMinHeight = kMinWidth; |
| 18 } // namespace |
| 19 |
| 20 @interface ReaderModeView ()<MDCActivityIndicatorDelegate> { |
| 21 std::unique_ptr<dom_distiller::DistillerViewer> _viewer; |
| 22 base::mac::ObjCPropertyReleaser _propertyReleaser_ReaderModeView; |
| 23 } |
| 24 @property(nonatomic, retain) MDCActivityIndicator* activityIndicator; |
| 25 @property(nonatomic, copy) ProceduralBlock animateOutCompletionBlock; |
| 26 @property(nonatomic, retain) UIButton* closeButton; |
| 27 |
| 28 @end |
| 29 |
| 30 @implementation ReaderModeView |
| 31 @synthesize activityIndicator = _activityIndicator; |
| 32 @synthesize animateOutCompletionBlock = _animateOutCompletionBlock; |
| 33 @synthesize closeButton = _closeButton; |
| 34 @synthesize delegate = _delegate; |
| 35 |
| 36 - (instancetype)initWithFrame:(CGRect)frame |
| 37 delegate:(id<ReaderModeViewDelegate>)delegate { |
| 38 self = [super initWithFrame:frame]; |
| 39 if (self) { |
| 40 _propertyReleaser_ReaderModeView.Init(self, [ReaderModeView class]); |
| 41 _delegate = delegate; |
| 42 |
| 43 self.backgroundColor = [UIColor whiteColor]; |
| 44 |
| 45 self.autoresizingMask = |
| 46 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; |
| 47 |
| 48 _closeButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; |
| 49 [_closeButton addTarget:self |
| 50 action:@selector(close) |
| 51 forControlEvents:UIControlEventTouchUpInside]; |
| 52 [_closeButton setTitle:@"X" forState:UIControlStateNormal]; |
| 53 _closeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | |
| 54 UIViewAutoresizingFlexibleTopMargin; |
| 55 |
| 56 _activityIndicator = |
| 57 [[MDCActivityIndicator alloc] initWithFrame:CGRectMake(0, 0, 24, 24)]; |
| 58 _activityIndicator.delegate = self; |
| 59 _activityIndicator.cycleColors = ActivityIndicatorBrandedCycleColors(); |
| 60 _activityIndicator.autoresizingMask = |
| 61 UIViewAutoresizingFlexibleLeftMargin | |
| 62 UIViewAutoresizingFlexibleTopMargin | |
| 63 UIViewAutoresizingFlexibleRightMargin | |
| 64 UIViewAutoresizingFlexibleBottomMargin; |
| 65 |
| 66 [self addSubview:_closeButton]; |
| 67 [self addSubview:_activityIndicator]; |
| 68 [self sizeToFit]; |
| 69 } |
| 70 return self; |
| 71 } |
| 72 |
| 73 - (instancetype)initWithFrame:(CGRect)frame { |
| 74 NOTREACHED(); |
| 75 return nil; |
| 76 } |
| 77 |
| 78 - (instancetype)initWithCoder:(NSCoder*)aDecoder { |
| 79 NOTREACHED(); |
| 80 return nil; |
| 81 } |
| 82 |
| 83 - (void)start { |
| 84 [self.activityIndicator startAnimating]; |
| 85 } |
| 86 |
| 87 - (void)stopWaitingWithCompletion:(ProceduralBlock)completion { |
| 88 if (self.activityIndicator.isAnimating) { |
| 89 self.animateOutCompletionBlock = completion; |
| 90 [self.activityIndicator stopAnimating]; |
| 91 } else if (completion) { |
| 92 completion(); |
| 93 } |
| 94 } |
| 95 |
| 96 - (void)close { |
| 97 [self.delegate exitReaderMode]; |
| 98 } |
| 99 |
| 100 - (void)assignDistillerViewer: |
| 101 (std::unique_ptr<dom_distiller::DistillerViewer>)viewer { |
| 102 _viewer.reset(viewer.release()); |
| 103 } |
| 104 |
| 105 - (void)layoutSubviews { |
| 106 [super layoutSubviews]; |
| 107 |
| 108 self.closeButton.frame = CGRectMake( |
| 109 self.bounds.size.width - kCloseButtonSize - kCloseButtonMargin, |
| 110 self.bounds.size.height - kCloseButtonSize - kCloseButtonMargin, |
| 111 kCloseButtonSize, kCloseButtonSize); |
| 112 self.activityIndicator.center = CGPointMake(CGRectGetWidth(self.bounds) / 2, |
| 113 CGRectGetHeight(self.bounds) / 2); |
| 114 } |
| 115 |
| 116 - (CGSize)sizeThatFits:(CGSize)size { |
| 117 CGSize newSize = [super sizeThatFits:size]; |
| 118 if (newSize.width < kMinWidth) |
| 119 newSize.width = kMinWidth; |
| 120 if (newSize.height < kMinHeight) |
| 121 newSize.height = kMinHeight; |
| 122 |
| 123 return newSize; |
| 124 } |
| 125 |
| 126 #pragma mark - MDCActivityIndicatorDelegate |
| 127 |
| 128 - (void)activityIndicatorAnimationDidFinish: |
| 129 (MDCActivityIndicator*)activityIndicator { |
| 130 [self.activityIndicator removeFromSuperview]; |
| 131 self.activityIndicator = nil; |
| 132 if (self.animateOutCompletionBlock) |
| 133 self.animateOutCompletionBlock(); |
| 134 self.animateOutCompletionBlock = nil; |
| 135 } |
| 136 |
| 137 @end |
OLD | NEW |