OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <MobileCoreServices/MobileCoreServices.h> | 5 #import <MobileCoreServices/MobileCoreServices.h> |
6 | 6 |
7 #import "ios/chrome/share_extension/share_view_controller.h" | 7 #import "ios/chrome/share_extension/share_view_controller.h" |
8 | 8 |
9 #import "base/ios/block_types.h" | 9 #import "base/ios/block_types.h" |
10 #import "base/mac/foundation_util.h" | 10 #import "base/mac/foundation_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 // If |cancel| is true, |actionType| is ignored. | 52 // If |cancel| is true, |actionType| is ignored. |
53 - (void)queueActionItemURL:(NSURL*)URL | 53 - (void)queueActionItemURL:(NSURL*)URL |
54 title:(NSString*)title | 54 title:(NSString*)title |
55 action:(app_group::ShareExtensionItemType)actionType | 55 action:(app_group::ShareExtensionItemType)actionType |
56 cancel:(BOOL)cancel | 56 cancel:(BOOL)cancel |
57 completion:(ProceduralBlock)completion; | 57 completion:(ProceduralBlock)completion; |
58 | 58 |
59 // Loads all the shared elements from the extension context and update the UI. | 59 // Loads all the shared elements from the extension context and update the UI. |
60 - (void)loadElementsFromContext; | 60 - (void)loadElementsFromContext; |
61 | 61 |
62 // Performs a fade in animation for the whole widget. | |
63 - (void)fadeIn; | |
64 | |
65 // Sets constaints to the widget so that margin are at least | 62 // Sets constaints to the widget so that margin are at least |
66 // kShareExtensionMargin points and widget width is closest up to | 63 // kShareExtensionMargin points and widget width is closest up to |
67 // kShareExtensionMaxWidth points. | 64 // kShareExtensionMaxWidth points. |
68 - (void)constrainWidgetWidth; | 65 - (void)constrainWidgetWidth; |
69 | 66 |
70 @end | 67 @end |
71 | 68 |
72 @implementation ShareViewController | 69 @implementation ShareViewController |
73 | 70 |
74 @synthesize maskView = _maskView; | 71 @synthesize maskView = _maskView; |
75 @synthesize shareView = _shareView; | 72 @synthesize shareView = _shareView; |
76 @synthesize itemType = _itemType; | 73 @synthesize itemType = _itemType; |
77 | 74 |
75 #pragma mark - UIViewController | |
76 | |
78 - (void)viewDidLoad { | 77 - (void)viewDidLoad { |
79 [super viewDidLoad]; | 78 [super viewDidLoad]; |
80 | 79 |
81 // This view shadows the screen under the share extension. | 80 // This view shadows the screen under the share extension. |
82 UIView* maskView = [[UIView alloc] initWithFrame:CGRectZero]; | 81 UIView* maskView = [[UIView alloc] initWithFrame:CGRectZero]; |
83 [self setMaskView:maskView]; | 82 [self setMaskView:maskView]; |
84 [self.maskView | 83 [self.maskView |
85 setBackgroundColor:[UIColor colorWithWhite:0 alpha:kMediumAlpha]]; | 84 setBackgroundColor:[UIColor colorWithWhite:0 alpha:kMediumAlpha]]; |
86 [self.view addSubview:self.maskView]; | 85 [self.view addSubview:self.maskView]; |
87 ui_util::ConstrainAllSidesOfViewToView(self.view, self.maskView); | 86 ui_util::ConstrainAllSidesOfViewToView(self.view, self.maskView); |
88 | 87 |
89 // This view is the main view of the share extension. | 88 // This view is the main view of the share extension. |
90 ShareExtensionView* shareView = | 89 ShareExtensionView* shareView = |
91 [[ShareExtensionView alloc] initWithActionTarget:self]; | 90 [[ShareExtensionView alloc] initWithActionTarget:self]; |
92 [self setShareView:shareView]; | 91 [self setShareView:shareView]; |
93 [self.view addSubview:self.shareView]; | 92 [self.view addSubview:self.shareView]; |
94 | 93 |
95 [self constrainWidgetWidth]; | 94 [self constrainWidgetWidth]; |
96 | 95 |
97 // Center the widget in the screen. | 96 // Position the widget below the screen. It will be slided up with an |
97 // animation. | |
98 _centerYConstraint = [[shareView centerYAnchor] | 98 _centerYConstraint = [[shareView centerYAnchor] |
99 constraintEqualToAnchor:[self.view centerYAnchor]]; | 99 constraintEqualToAnchor:[self.view centerYAnchor]]; |
100 [_centerYConstraint setConstant:(self.view.frame.size.height + | |
101 self.shareView.frame.size.height) / | |
102 2]; | |
100 [_centerYConstraint setActive:YES]; | 103 [_centerYConstraint setActive:YES]; |
101 [[[shareView centerXAnchor] constraintEqualToAnchor:[self.view centerXAnchor]] | 104 [[[shareView centerXAnchor] constraintEqualToAnchor:[self.view centerXAnchor]] |
102 setActive:YES]; | 105 setActive:YES]; |
103 | 106 |
104 [self.maskView setTranslatesAutoresizingMaskIntoConstraints:NO]; | 107 [self.maskView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
105 [self.shareView setTranslatesAutoresizingMaskIntoConstraints:NO]; | 108 [self.shareView setTranslatesAutoresizingMaskIntoConstraints:NO]; |
106 | 109 |
107 [self loadElementsFromContext]; | 110 [self loadElementsFromContext]; |
108 [self fadeIn]; | |
109 } | 111 } |
110 | 112 |
113 - (void)viewWillAppear:(BOOL)animated { | |
114 [super viewWillAppear:animated]; | |
115 | |
116 // Center the widget. | |
117 [_centerYConstraint setConstant:0]; | |
118 [self.maskView setAlpha:0]; | |
119 [UIView animateWithDuration:kAnimationDuration | |
120 animations:^{ | |
121 [self.maskView setAlpha:1]; | |
Olivier
2016/12/20 16:31:44
Is this needed?
Will this not also fade in ?
gambard
2016/12/20 16:33:49
The dark background view will fade in and the exte
| |
122 [self.view layoutIfNeeded]; | |
123 }]; | |
124 } | |
125 | |
126 #pragma mark - Private methods | |
127 | |
111 - (void)constrainWidgetWidth { | 128 - (void)constrainWidgetWidth { |
112 // Setting the constraints. | 129 // Setting the constraints. |
113 NSDictionary* views = @{ @"share" : self.shareView }; | 130 NSDictionary* views = @{ @"share" : self.shareView }; |
114 | 131 |
115 NSDictionary* metrics = @{ | 132 NSDictionary* metrics = @{ |
116 @"margin" : @(kShareExtensionMargin), | 133 @"margin" : @(kShareExtensionMargin), |
117 @"maxWidth" : @(kShareExtensionMaxWidth), | 134 @"maxWidth" : @(kShareExtensionMaxWidth), |
118 }; | 135 }; |
119 | 136 |
120 NSArray* constraints = @[ | 137 NSArray* constraints = @[ |
(...skipping 12 matching lines...) Expand all Loading... | |
133 views:views]]; | 150 views:views]]; |
134 } | 151 } |
135 | 152 |
136 // |self.shareView| must be as large as possible and in the center of the | 153 // |self.shareView| must be as large as possible and in the center of the |
137 // screen. | 154 // screen. |
138 [self.shareView | 155 [self.shareView |
139 setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh | 156 setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh |
140 forAxis:UILayoutConstraintAxisHorizontal]; | 157 forAxis:UILayoutConstraintAxisHorizontal]; |
141 } | 158 } |
142 | 159 |
143 - (void)fadeIn { | |
144 // Fade in animation. | |
145 [self.maskView setAlpha:0]; | |
146 [self.shareView setAlpha:0]; | |
147 [UIView animateWithDuration:kAnimationDuration | |
148 animations:^{ | |
149 [self.maskView setAlpha:kMediumAlpha]; | |
150 [self.shareView setAlpha:1]; | |
151 }]; | |
152 } | |
153 | |
154 - (void)loadElementsFromContext { | 160 - (void)loadElementsFromContext { |
155 NSString* typeURL = static_cast<NSString*>(kUTTypeURL); | 161 NSString* typeURL = static_cast<NSString*>(kUTTypeURL); |
156 for (NSExtensionItem* item in self.extensionContext.inputItems) { | 162 for (NSExtensionItem* item in self.extensionContext.inputItems) { |
157 for (NSItemProvider* itemProvider in item.attachments) { | 163 for (NSItemProvider* itemProvider in item.attachments) { |
158 if ([itemProvider hasItemConformingToTypeIdentifier:typeURL]) { | 164 if ([itemProvider hasItemConformingToTypeIdentifier:typeURL]) { |
159 ItemBlock URLCompletion = ^(id idURL, NSError* error) { | 165 ItemBlock URLCompletion = ^(id idURL, NSError* error) { |
160 NSURL* URL = static_cast<NSURL*>(idURL); | 166 NSURL* URL = static_cast<NSURL*>(idURL); |
161 dispatch_async(dispatch_get_main_queue(), ^{ | 167 dispatch_async(dispatch_get_main_queue(), ^{ |
162 _shareItem = [item copy]; | 168 _shareItem = [item copy]; |
163 _shareURL = [URL copy]; | 169 _shareURL = [URL copy]; |
(...skipping 24 matching lines...) Expand all Loading... | |
188 | 194 |
189 }; | 195 }; |
190 [itemProvider loadPreviewImageWithOptions:imageOptions | 196 [itemProvider loadPreviewImageWithOptions:imageOptions |
191 completionHandler:ImageCompletion]; | 197 completionHandler:ImageCompletion]; |
192 } | 198 } |
193 } | 199 } |
194 } | 200 } |
195 } | 201 } |
196 | 202 |
197 - (void)dismissAndReturnItem:(NSExtensionItem*)item { | 203 - (void)dismissAndReturnItem:(NSExtensionItem*)item { |
198 // Set the Y center constraints so the whole extension slides up out of the | 204 // Set the Y center constraints so the whole extension slides out of the |
199 // screen. Constant is relative to the center of the screen. | 205 // screen. Constant is relative to the center of the screen. |
200 [_centerYConstraint setConstant:-(self.view.frame.size.height + | 206 // The direction (up or down) is relative to the output (cancel or submit). |
201 self.shareView.frame.size.height) / | 207 NSInteger direction = item ? -1 : 1; |
208 [_centerYConstraint setConstant:direction * | |
209 (self.view.frame.size.height + | |
210 self.shareView.frame.size.height) / | |
202 2]; | 211 2]; |
203 [UIView animateWithDuration:kAnimationDuration | 212 [UIView animateWithDuration:kAnimationDuration |
204 animations:^{ | 213 animations:^{ |
205 [self.maskView setAlpha:0]; | 214 [self.maskView setAlpha:0]; |
206 [self.view layoutIfNeeded]; | 215 [self.view layoutIfNeeded]; |
207 } | 216 } |
208 completion:^(BOOL finished) { | 217 completion:^(BOOL finished) { |
209 NSArray* returnItem = item ? @[ item ] : @[]; | 218 NSArray* returnItem = item ? @[ item ] : @[]; |
210 [self.extensionContext completeRequestReturningItems:returnItem | 219 [self.extensionContext completeRequestReturningItems:returnItem |
211 completionHandler:nil]; | 220 completionHandler:nil]; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 [self dismissAndReturnItem:_shareItem]; | 298 [self dismissAndReturnItem:_shareItem]; |
290 }]; | 299 }]; |
291 } | 300 } |
292 | 301 |
293 - (void)shareExtensionView:(id)sender | 302 - (void)shareExtensionView:(id)sender |
294 typeChanged:(app_group::ShareExtensionItemType)type { | 303 typeChanged:(app_group::ShareExtensionItemType)type { |
295 [self setItemType:type]; | 304 [self setItemType:type]; |
296 } | 305 } |
297 | 306 |
298 @end | 307 @end |
OLD | NEW |