Chromium Code Reviews| 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/context_menu/context_menu_wrangler.h" | |
| 6 | |
| 7 #include "base/ios/weak_nsobject.h" | |
| 8 #include "base/strings/sys_string_conversions.h" | |
| 9 #import "ios/web/public/web_state/context_menu_params.h" | |
| 10 #include "ui/base/l10n/l10n_util.h" | |
| 11 #include "ui/strings/grit/ui_strings.h" | |
| 12 | |
| 13 @interface ContextMenuWrangler () { | |
| 14 // Underlying system alert. | |
| 15 base::WeakNSObject<UIAlertController> _alertController; | |
|
marq (ping after 24h)
2016/05/18 10:38:10
Should this really be weak?
michaeldo
2016/05/18 15:22:04
This should NOT be weak, since it may be deallocat
| |
| 16 // View controller which will be used to present the |_alertController|. | |
| 17 base::WeakNSObject<UIViewController> _presentingViewController; | |
| 18 // Parameters that define what is shown in the context menu. | |
| 19 web::ContextMenuParams _params; | |
| 20 } | |
| 21 // Redefined to readwrite. | |
| 22 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | |
| 23 // Lazy initializer to create the |_alert|. | |
| 24 @property(nonatomic, readonly) UIAlertController* alertController; | |
| 25 // Called when the alert is dismissed to perform cleanup. | |
| 26 - (void)alertDismissed; | |
| 27 @end | |
| 28 | |
| 29 @implementation ContextMenuWrangler | |
| 30 @synthesize visible = _visible; | |
|
marq (ping after 24h)
2016/05/18 10:38:10
Doesn't this property end up just being YES if _al
michaeldo
2016/05/18 15:22:04
It doesn't quite follow _alertController. This is
| |
| 31 | |
| 32 - (instancetype)initWithViewController:(UIViewController*)viewController | |
| 33 params:(const web::ContextMenuParams&)params { | |
| 34 self = [super init]; | |
| 35 if (self) { | |
| 36 _params = params; | |
| 37 _presentingViewController.reset(viewController); | |
| 38 } | |
| 39 return self; | |
| 40 } | |
| 41 | |
|
marq (ping after 24h)
2016/05/18 10:38:10
#pragma mark - Object lifecycle
michaeldo
2016/05/18 15:22:05
Done.
| |
| 42 - (void)dealloc { | |
| 43 [_alertController dismissViewControllerAnimated:NO completion:nil]; | |
|
marq (ping after 24h)
2016/05/18 10:38:10
I'd prefer to have this call [self stop] instead o
michaeldo
2016/05/18 15:22:04
Done.
| |
| 44 [super dealloc]; | |
| 45 } | |
| 46 | |
| 47 - (void)addItemWithTitle:(NSString*)title action:(ProceduralBlock)actionBlock { | |
|
marq (ping after 24h)
2016/05/18 10:38:09
#pragma mark - Public methods
michaeldo
2016/05/18 15:22:05
Done.
| |
| 48 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
| 49 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) { | |
| 50 [weakSelf alertDismissed]; | |
| 51 actionBlock(); | |
| 52 }; | |
| 53 [self.alertController | |
| 54 addAction:[UIAlertAction actionWithTitle:title | |
| 55 style:UIAlertActionStyleDefault | |
| 56 handler:actionHandler]]; | |
| 57 } | |
| 58 | |
| 59 - (void)start { | |
| 60 // Check that the view is still visible on screen, otherwise just return and | |
| 61 // don't show the context menu. | |
| 62 if (![_params.view window] && | |
| 63 ![_params.view isKindOfClass:[UIWindow class]]) { | |
| 64 _alertController.reset(); | |
|
marq (ping after 24h)
2016/05/18 10:38:10
This case destroys all of the config information c
michaeldo
2016/05/18 15:22:04
I agree, but maybe it isn't really necessary to de
| |
| 65 return; | |
| 66 } | |
| 67 | |
| 68 [_presentingViewController presentViewController:_alertController | |
|
marq (ping after 24h)
2016/05/18 10:38:09
_alertController or self.alertController?
michaeldo
2016/05/18 15:22:04
I changed this to self.alertController. I think it
| |
| 69 animated:YES | |
| 70 completion:nil]; | |
| 71 self.visible = YES; | |
| 72 } | |
| 73 | |
| 74 - (void)stop { | |
| 75 [_alertController dismissViewControllerAnimated:NO completion:nil]; | |
| 76 [self setVisible:NO]; | |
| 77 _alertController.reset(); | |
| 78 } | |
| 79 | |
| 80 - (UIAlertController*)alertController { | |
|
marq (ping after 24h)
2016/05/18 10:38:10
#pragma mark - Property implementation
michaeldo
2016/05/18 15:22:04
Done.
| |
| 81 if (!_alertController) { | |
| 82 DCHECK([_params.view isDescendantOfView:[_presentingViewController view]]); | |
| 83 UIAlertController* alert = [UIAlertController | |
| 84 alertControllerWithTitle:_params.menu_title | |
| 85 message:nil | |
| 86 preferredStyle:UIAlertControllerStyleActionSheet]; | |
| 87 alert.popoverPresentationController.sourceView = _params.view; | |
| 88 alert.popoverPresentationController.sourceRect = | |
| 89 CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0); | |
| 90 | |
| 91 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
| 92 UIAlertAction* cancelAction = | |
| 93 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) | |
| 94 style:UIAlertActionStyleCancel | |
| 95 handler:^(UIAlertAction*) { | |
| 96 [weakSelf alertDismissed]; | |
| 97 }]; | |
| 98 [alert addAction:cancelAction]; | |
| 99 | |
| 100 _alertController.reset([alert retain]); | |
| 101 } | |
| 102 return _alertController; | |
| 103 } | |
| 104 | |
| 105 - (void)alertDismissed { | |
|
marq (ping after 24h)
2016/05/18 10:38:10
#pragma mark - Private methods
michaeldo
2016/05/18 15:22:04
Done.
| |
| 106 [self setVisible:NO]; | |
|
marq (ping after 24h)
2016/05/18 10:38:09
Please be consistent with how you assign to this p
michaeldo
2016/05/18 15:22:04
agreed! Done.
| |
| 107 _alertController.reset(); | |
| 108 } | |
| 109 | |
| 110 @end | |
| OLD | NEW |