Index: ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm |
diff --git a/ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm b/ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3069ecc4757a67ea19fd7062788a3e67a21824f6 |
--- /dev/null |
+++ b/ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm |
@@ -0,0 +1,113 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#import "ios/chrome/browser/ui/context_menu/context_menu_wrangler.h" |
+ |
+#include "base/ios/weak_nsobject.h" |
+#include "base/strings/sys_string_conversions.h" |
+#import "ios/web/public/web_state/context_menu_params.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/strings/grit/ui_strings.h" |
+ |
+@interface ContextMenuWrangler () { |
+ // Underlying system alert. |
+ base::WeakNSObject<UIAlertController> _alertController; |
+ // Parameters that define what is shown in the context menu. |
+ web::ContextMenuParams _params; |
+} |
+// Redefined to readwrite. |
+@property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
+// Lazy initializer to create the |_alert|. |
+@property(nonatomic, readonly) UIAlertController* alertController; |
+// Called when the alert is dismissed to perform cleanup. |
+- (void)alertDismissed; |
+@end |
+ |
+// 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.
|
+@implementation ContextMenuWrangler |
+@synthesize visible = _visible; |
+ |
+- (instancetype)initWithContextMenuParams: |
+ (const web::ContextMenuParams&)params { |
+ self = [super init]; |
+ if (self) { |
+ _params = params; |
+ } |
+ return self; |
+} |
+ |
+- (void)dealloc { |
+ [_alertController dismissViewControllerAnimated:NO completion:nil]; |
+ [super dealloc]; |
+} |
+ |
+- (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.
|
+ if (!_alertController) { |
+ UIAlertController* alert = [UIAlertController |
+ alertControllerWithTitle:_params.menu_title |
+ message:nil |
+ preferredStyle:UIAlertControllerStyleActionSheet]; |
+ 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.
|
+ alert.popoverPresentationController.sourceRect = |
+ CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0); |
+ |
+ base::WeakNSObject<ContextMenuWrangler> weakSelf(self); |
+ 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.
|
+ [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) |
+ style:UIAlertActionStyleCancel |
+ handler:^(UIAlertAction*) { |
+ [weakSelf alertDismissed]; |
+ }]; |
+ [alert addAction:cancel_action]; |
+ |
+ _alertController.reset([alert retain]); |
+ } |
+ return _alertController; |
+} |
+ |
+- (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
|
+ base::WeakNSObject<ContextMenuWrangler> weakSelf(self); |
+ void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) { |
+ [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
|
+ actionBlock(); |
+ }; |
+ [self.alertController |
+ addAction:[UIAlertAction actionWithTitle:title |
+ style:UIAlertActionStyleDefault |
+ handler:actionHandler]]; |
+} |
+ |
+- (void)present { |
+ // Check that the view is still visible on screen, otherwise just return and |
+ // don't show the context menu. |
+ if (![_params.view window] && |
+ ![_params.view isKindOfClass:[UIWindow class]]) { |
+ _alertController.reset(); |
+ return; |
+ } |
+ |
+ // Present sheet/popover using controller that is added to view hierarchy. |
+ UIViewController* topController = [_params.view window].rootViewController; |
+ while (topController.presentedViewController) |
+ 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.
|
+ [topController presentViewController:_alertController |
+ animated:YES |
+ completion:nil]; |
+ self.visible = YES; |
+} |
+ |
+- (void)dismissAnimated:(BOOL)animated |
+ completionHandler:(ProceduralBlock)completionHandler { |
+ [_alertController dismissViewControllerAnimated:animated |
+ completion:completionHandler]; |
+ [self setVisible:NO]; |
+ _alertController.reset(); |
+} |
+ |
+- (void)alertDismissed { |
+ [self setVisible:NO]; |
+ _alertController.reset(); |
+} |
+ |
+@end |