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

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

Issue 1972013003: Add ContextMenuCoordinator. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename ContextMenuWrangler to coordinator. 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_coordinator.mm
diff --git a/ios/chrome/browser/ui/context_menu/context_menu_coordinator.mm b/ios/chrome/browser/ui/context_menu/context_menu_coordinator.mm
new file mode 100644
index 0000000000000000000000000000000000000000..46065782b39bdd68a189bfb14c68a4485dc0143e
--- /dev/null
+++ b/ios/chrome/browser/ui/context_menu/context_menu_coordinator.mm
@@ -0,0 +1,114 @@
+// 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_coordinator.h"
+
+#include "base/ios/weak_nsobject.h"
+#include "base/mac/scoped_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 ContextMenuCoordinator () {
+ // Underlying system alert.
+ base::scoped_nsobject<UIAlertController> _alertController;
+ // View controller which will be used to present the |_alertController|.
+ base::WeakNSObject<UIViewController> _presentingViewController;
+ // 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
+
+@implementation ContextMenuCoordinator
+@synthesize visible = _visible;
+
+- (instancetype)initWithViewController:(UIViewController*)viewController
+ params:(const web::ContextMenuParams&)params {
+ self = [super init];
+ if (self) {
+ _params = params;
+ _presentingViewController.reset(viewController);
+ }
+ return self;
+}
+
+#pragma mark - Object Lifecycle
+- (void)dealloc {
+ [self stop];
+ [super dealloc];
+}
+
+#pragma mark - Public Methods
+- (void)addItemWithTitle:(NSString*)title action:(ProceduralBlock)actionBlock {
+ base::WeakNSObject<ContextMenuCoordinator> weakSelf(self);
+ void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) {
+ [weakSelf alertDismissed];
+ actionBlock();
+ };
+ [self.alertController
+ addAction:[UIAlertAction actionWithTitle:title
+ style:UIAlertActionStyleDefault
+ handler:actionHandler]];
+}
+
+- (void)start {
+ // 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]]) {
+ return;
+ }
+
+ [_presentingViewController presentViewController:self.alertController
+ animated:YES
+ completion:nil];
+ self.visible = YES;
+}
+
+- (void)stop {
+ [_alertController dismissViewControllerAnimated:NO completion:nil];
+ [self setVisible:NO];
+ _alertController.reset();
+}
+
+#pragma mark - Property Implementation
+- (UIAlertController*)alertController {
+ if (!_alertController) {
+ DCHECK([_params.view isDescendantOfView:[_presentingViewController view]]);
+ 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);
+
+ base::WeakNSObject<ContextMenuCoordinator> weakSelf(self);
+ UIAlertAction* cancelAction =
+ [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL)
+ style:UIAlertActionStyleCancel
+ handler:^(UIAlertAction*) {
+ [weakSelf alertDismissed];
+ }];
+ [alert addAction:cancelAction];
+
+ _alertController.reset([alert retain]);
+ }
+ return _alertController;
+}
+
+#pragma mark - Private Methods
+- (void)alertDismissed {
+ self.visible = NO;
+ _alertController.reset();
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698