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

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: Cleanup based on CL comments. 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..6d2760a31f073480edb59bd8a74e307d9187c9ae
--- /dev/null
+++ b/ios/chrome/browser/ui/context_menu/context_menu_wrangler.mm
@@ -0,0 +1,110 @@
+// 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;
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
+ // 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 ContextMenuWrangler
+@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
+
+- (instancetype)initWithViewController:(UIViewController*)viewController
+ params:(const web::ContextMenuParams&)params {
+ self = [super init];
+ if (self) {
+ _params = params;
+ _presentingViewController.reset(viewController);
+ }
+ return self;
+}
+
marq (ping after 24h) 2016/05/18 10:38:10 #pragma mark - Object lifecycle
michaeldo 2016/05/18 15:22:05 Done.
+- (void)dealloc {
+ [_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.
+ [super dealloc];
+}
+
+- (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.
+ base::WeakNSObject<ContextMenuWrangler> 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]]) {
+ _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
+ return;
+ }
+
+ [_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
+ animated:YES
+ completion:nil];
+ self.visible = YES;
+}
+
+- (void)stop {
+ [_alertController dismissViewControllerAnimated:NO completion:nil];
+ [self setVisible:NO];
+ _alertController.reset();
+}
+
+- (UIAlertController*)alertController {
marq (ping after 24h) 2016/05/18 10:38:10 #pragma mark - Property implementation
michaeldo 2016/05/18 15:22:04 Done.
+ 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<ContextMenuWrangler> 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;
+}
+
+- (void)alertDismissed {
marq (ping after 24h) 2016/05/18 10:38:10 #pragma mark - Private methods
michaeldo 2016/05/18 15:22:04 Done.
+ [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.
+ _alertController.reset();
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698