Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Unified Diff: ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm

Issue 1972013003: Add ContextMenuCoordinator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove old files. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..530c7be7c9fe927df9853e109a81359752c4c401
--- /dev/null
+++ b/ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm
@@ -0,0 +1,109 @@
+// 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> _alert;
+ // 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* alert;
Eugene But (OOO till 7-30) 2016/05/12 22:27:48 NIT: s/alert/alertController
michaeldo 2016/05/12 23:32:43 Done.
+// Called when the alert is dismissed to perform cleanup.
+- (void)alertDismissed;
+@end
+
+// Displays a context menu.
+@implementation ContextMenuWrangler
+@synthesize visible = _visible;
+
+- (instancetype)initWithContextMenuParams:
+ (const web::ContextMenuParams&)params {
+ self = [super init];
+ if (self) {
+ _params = params;
+ }
+ return self;
+}
+
+- (void)dealloc {
+ [_alert dismissViewControllerAnimated:NO completion:nil];
+ [super dealloc];
+}
+
+- (UIAlertController*)alert {
+ if (!_alert) {
+ UIAlertController* alert = [UIAlertController
+ alertControllerWithTitle:_params.menu_title
+ message:nil
+ preferredStyle:UIAlertControllerStyleActionSheet];
+ alert.popoverPresentationController.sourceView = _params.view;
+ alert.popoverPresentationController.sourceRect =
+ CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0);
+ _alert.reset([alert retain]);
+ }
+ return _alert;
+}
+
+- (void)appendItemWithTitle:(NSString*)title
+ action:(ProceduralBlock)actionBlock {
+ base::WeakNSObject<ContextMenuWrangler> weakSelf(self);
+ void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) {
+ [weakSelf alertDismissed];
+ actionBlock();
+ };
+ [self.alert 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]]) {
+ _alert.reset();
+ return;
+ }
+
+ base::WeakNSObject<ContextMenuWrangler> weakSelf(self);
Eugene But (OOO till 7-30) 2016/05/12 22:27:48 This should be a part of alert creation, not prese
michaeldo 2016/05/12 23:32:43 I thought it would not end up at the end of the li
+ UIAlertAction* cancel_action =
+ [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL)
+ style:UIAlertActionStyleCancel
+ handler:^(UIAlertAction*) {
+ [weakSelf alertDismissed];
+ }];
+ [self.alert addAction:cancel_action];
+
+ // Present sheet/popover using controller that is added to view hierarchy.
+ UIViewController* topController = [_params.view window].rootViewController;
+ while (topController.presentedViewController)
+ topController = topController.presentedViewController;
+ [topController presentViewController:_alert animated:YES completion:nil];
+ self.visible = YES;
+}
+
+- (void)dismissAnimated:(BOOL)animated
+ completionHandler:(ProceduralBlock)completionHandler {
+ [_alert dismissViewControllerAnimated:animated completion:completionHandler];
+ [self setVisible:NO];
+ _alert.reset();
+}
+
+- (void)alertDismissed {
+ [self setVisible:NO];
+ _alert.reset();
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698