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

Side by Side 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: Fix unittest when running on iPhone. 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 unified diff | Download patch
OLDNEW
(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_coordinator.h"
6
7 #import "base/ios/weak_nsobject.h"
8 #import "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "ios/web/public/web_state/context_menu_params.h"
11 #include "ui/base/l10n/l10n_util.h"
12 #include "ui/strings/grit/ui_strings.h"
13
14 @interface ContextMenuCoordinator () {
15 // Underlying system alert.
16 base::scoped_nsobject<UIAlertController> _alertController;
17 // View controller which will be used to present the |_alertController|.
18 base::WeakNSObject<UIViewController> _presentingViewController;
19 // Parameters that define what is shown in the context menu.
20 web::ContextMenuParams _params;
21 }
22 // Redefined to readwrite.
23 @property(nonatomic, readwrite, getter=isVisible) BOOL visible;
24 // Lazy initializer to create the |_alert|.
25 @property(nonatomic, readonly) UIAlertController* alertController;
26 // Called when the alert is dismissed to perform cleanup.
27 - (void)alertDismissed;
28 @end
29
30 @implementation ContextMenuCoordinator
31 @synthesize visible = _visible;
32
33 - (instancetype)initWithViewController:(UIViewController*)viewController
34 params:(const web::ContextMenuParams&)params {
35 self = [super init];
36 if (self) {
37 _params = params;
38 _presentingViewController.reset(viewController);
39 }
40 return self;
41 }
42
43 #pragma mark - Object Lifecycle
44
45 - (void)dealloc {
46 [self stop];
47 [super dealloc];
48 }
49
50 #pragma mark - Public Methods
51
52 - (void)addItemWithTitle:(NSString*)title action:(ProceduralBlock)actionBlock {
53 if (self.visible) {
54 return;
55 }
56 base::WeakNSObject<ContextMenuCoordinator> weakSelf(self);
57 [self.alertController
58 addAction:[UIAlertAction actionWithTitle:title
59 style:UIAlertActionStyleDefault
60 handler:^(UIAlertAction*) {
61 [weakSelf alertDismissed];
62 actionBlock();
63 }]];
64 }
65
66 - (void)start {
67 // Check that the view is still visible on screen, otherwise just return and
68 // don't show the context menu.
69 if (![_params.view window] &&
70 ![_params.view isKindOfClass:[UIWindow class]]) {
71 return;
72 }
73
74 [_presentingViewController presentViewController:self.alertController
75 animated:YES
76 completion:nil];
77 self.visible = YES;
78 }
79
80 - (void)stop {
81 [_alertController dismissViewControllerAnimated:NO completion:nil];
82 self.visible = NO;
83 _alertController.reset();
84 }
85
86 #pragma mark - Property Implementation
87
88 - (UIAlertController*)alertController {
89 if (!_alertController) {
90 DCHECK([_params.view isDescendantOfView:[_presentingViewController view]]);
91 UIAlertController* alert = [UIAlertController
92 alertControllerWithTitle:_params.menu_title
93 message:nil
94 preferredStyle:UIAlertControllerStyleActionSheet];
95 alert.popoverPresentationController.sourceView = _params.view;
96 alert.popoverPresentationController.sourceRect =
97 CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0);
98
99 base::WeakNSObject<ContextMenuCoordinator> weakSelf(self);
100 UIAlertAction* cancelAction =
101 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL)
102 style:UIAlertActionStyleCancel
103 handler:^(UIAlertAction*) {
104 [weakSelf alertDismissed];
105 }];
106 [alert addAction:cancelAction];
107
108 _alertController.reset([alert retain]);
109 }
110 return _alertController;
111 }
112
113 #pragma mark - Private Methods
114
115 - (void)alertDismissed {
116 self.visible = NO;
117 _alertController.reset();
118 }
119
120 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698