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; | |
| 16 // Parameters that define what is shown in the context menu. | |
| 17 web::ContextMenuParams _params; | |
| 18 } | |
| 19 // Redefined to readwrite. | |
| 20 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | |
| 21 // Lazy initializer to create the |_alert|. | |
| 22 @property(nonatomic, readonly) UIAlertController* alertController; | |
| 23 // Called when the alert is dismissed to perform cleanup. | |
| 24 - (void)alertDismissed; | |
| 25 @end | |
| 26 | |
| 27 // Displays a context menu. | |
|
Eugene But (OOO till 7-30)
2016/05/13 01:23:55
Drop this comment. Interface already has documenta
michaeldo
2016/05/17 18:03:03
Done.
| |
| 28 @implementation ContextMenuWrangler | |
| 29 @synthesize visible = _visible; | |
| 30 | |
| 31 - (instancetype)initWithContextMenuParams: | |
| 32 (const web::ContextMenuParams&)params { | |
| 33 self = [super init]; | |
| 34 if (self) { | |
| 35 _params = params; | |
| 36 } | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (void)dealloc { | |
| 41 [_alertController dismissViewControllerAnimated:NO completion:nil]; | |
| 42 [super dealloc]; | |
| 43 } | |
| 44 | |
| 45 - (UIAlertController*)alertController { | |
|
Eugene But (OOO till 7-30)
2016/05/13 01:23:55
Move to the bottom. Before |alertDismissed|.
michaeldo
2016/05/17 18:03:03
Done.
| |
| 46 if (!_alertController) { | |
| 47 UIAlertController* alert = [UIAlertController | |
| 48 alertControllerWithTitle:_params.menu_title | |
| 49 message:nil | |
| 50 preferredStyle:UIAlertControllerStyleActionSheet]; | |
| 51 alert.popoverPresentationController.sourceView = _params.view; | |
|
marq (ping after 24h)
2016/05/13 14:51:29
DCHECK at some stage that _params.view is a subvie
michaeldo
2016/05/17 18:03:03
I agree, good idea. Done.
| |
| 52 alert.popoverPresentationController.sourceRect = | |
| 53 CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0); | |
| 54 | |
| 55 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
| 56 UIAlertAction* cancel_action = | |
|
Eugene But (OOO till 7-30)
2016/05/13 01:23:55
s/cancel_action/cancelAction
michaeldo
2016/05/17 18:03:03
Done.
| |
| 57 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) | |
| 58 style:UIAlertActionStyleCancel | |
| 59 handler:^(UIAlertAction*) { | |
| 60 [weakSelf alertDismissed]; | |
| 61 }]; | |
| 62 [alert addAction:cancel_action]; | |
| 63 | |
| 64 _alertController.reset([alert retain]); | |
| 65 } | |
| 66 return _alertController; | |
| 67 } | |
| 68 | |
| 69 - (void)addItemWithTitle:(NSString*)title action:(ProceduralBlock)actionBlock { | |
|
marq (ping after 24h)
2016/05/13 14:51:29
It feels like everything that's added here should
michaeldo
2016/05/17 18:03:03
This would go against what chromium is doing, but
Eugene But (OOO till 7-30)
2016/05/17 19:37:00
Web can not be responsible for deciding about the
| |
| 70 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
| 71 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) { | |
| 72 [weakSelf alertDismissed]; | |
|
marq (ping after 24h)
2016/05/13 14:51:29
Annoying that there's no callback or delegate to l
michaeldo
2016/05/17 18:03:03
This is in the plans, once migrated to this new cl
| |
| 73 actionBlock(); | |
| 74 }; | |
| 75 [self.alertController | |
| 76 addAction:[UIAlertAction actionWithTitle:title | |
| 77 style:UIAlertActionStyleDefault | |
| 78 handler:actionHandler]]; | |
| 79 } | |
| 80 | |
| 81 - (void)present { | |
| 82 // Check that the view is still visible on screen, otherwise just return and | |
| 83 // don't show the context menu. | |
| 84 if (![_params.view window] && | |
| 85 ![_params.view isKindOfClass:[UIWindow class]]) { | |
| 86 _alertController.reset(); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 // Present sheet/popover using controller that is added to view hierarchy. | |
| 91 UIViewController* topController = [_params.view window].rootViewController; | |
| 92 while (topController.presentedViewController) | |
| 93 topController = topController.presentedViewController; | |
|
marq (ping after 24h)
2016/05/13 14:51:29
We do this a lot and I regard it as a fairly perni
michaeldo
2016/05/17 18:03:03
I agree, will update to pass in the VC.
| |
| 94 [topController presentViewController:_alertController | |
| 95 animated:YES | |
| 96 completion:nil]; | |
| 97 self.visible = YES; | |
| 98 } | |
| 99 | |
| 100 - (void)dismissAnimated:(BOOL)animated | |
| 101 completionHandler:(ProceduralBlock)completionHandler { | |
| 102 [_alertController dismissViewControllerAnimated:animated | |
| 103 completion:completionHandler]; | |
| 104 [self setVisible:NO]; | |
| 105 _alertController.reset(); | |
| 106 } | |
| 107 | |
| 108 - (void)alertDismissed { | |
| 109 [self setVisible:NO]; | |
| 110 _alertController.reset(); | |
| 111 } | |
| 112 | |
| 113 @end | |
| OLD | NEW |