[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / extensions / media_galleries_scan_result_dialog_cocoa.mm
blob6a1e6769e1c3fd30397fb46097653b81f92f2a07
1 // Copyright 2014 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.
5 #include "chrome/browser/ui/cocoa/extensions/media_galleries_scan_result_dialog_cocoa.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/mac/scoped_nsobject.h"
9 #include "base/strings/sys_string_conversions.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_alert.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_control_utils.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sheet.h"
13 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
14 #include "content/public/browser/web_contents.h"
15 #include "grit/generated_resources.h"
16 #import "ui/base/cocoa/flipped_view.h"
17 #import "ui/base/models/menu_model.h"
18 #include "ui/base/l10n/l10n_util.h"
20 const CGFloat kCheckboxLeading = 10;
21 const CGFloat kCheckboxWidth = 440;
22 const CGFloat kScrollAreaHeight = 220;
24 // Controller for UI events on items in the media galleries dialog.
25 @interface MediaGalleriesScanResultCocoaController : NSObject {
26  @private
27   MediaGalleriesScanResultDialogCocoa* dialog_;
30 @property(assign, nonatomic) MediaGalleriesScanResultDialogCocoa* dialog;
32 - (void)onAcceptButton:(id)sender;
33 - (void)onCancelButton:(id)sender;
35 @end
37 @implementation MediaGalleriesScanResultCocoaController
39 @synthesize dialog = dialog_;
41 - (void)onAcceptButton:(id)sender {
42   dialog_->OnAcceptClicked();
45 - (void)onCancelButton:(id)sender {
46   dialog_->OnCancelClicked();
49 @end
52 MediaGalleriesScanResultDialogCocoa::MediaGalleriesScanResultDialogCocoa(
53     MediaGalleriesScanResultDialogController* controller,
54     MediaGalleriesScanResultCocoaController* cocoa_controller)
55     : controller_(controller),
56       accepted_(false),
57       cocoa_controller_([cocoa_controller retain]) {
58   [cocoa_controller_ setDialog:this];
60   alert_.reset([[ConstrainedWindowAlert alloc] init]);
62   [alert_ setMessageText:base::SysUTF16ToNSString(controller_->GetHeader())];
63   [alert_ setInformativeText:
64       base::SysUTF16ToNSString(controller_->GetSubtext())];
65   [alert_ addButtonWithTitle:
66       l10n_util::GetNSString(IDS_MEDIA_GALLERIES_SCAN_RESULT_DIALOG_CONFIRM)
67                keyEquivalent:kKeyEquivalentReturn
68                       target:cocoa_controller_
69                       action:@selector(onAcceptButton:)];
70   [alert_ addButtonWithTitle:
71       l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CANCEL)
72                keyEquivalent:kKeyEquivalentEscape
73                       target:cocoa_controller_
74                       action:@selector(onCancelButton:)];
75   [[alert_ closeButton] setTarget:cocoa_controller_];
76   [[alert_ closeButton] setAction:@selector(onCancelButton:)];
78   InitDialogControls();
80   // May be NULL during tests.
81   if (controller->web_contents()) {
82     base::scoped_nsobject<CustomConstrainedWindowSheet> sheet(
83         [[CustomConstrainedWindowSheet alloc]
84             initWithCustomWindow:[alert_ window]]);
85     window_.reset(new ConstrainedWindowMac(
86         this, controller->web_contents(), sheet));
87   }
90 MediaGalleriesScanResultDialogCocoa::~MediaGalleriesScanResultDialogCocoa() {
93 void MediaGalleriesScanResultDialogCocoa::InitDialogControls() {
94   main_container_.reset([[NSBox alloc] init]);
95   [main_container_ setBoxType:NSBoxCustom];
96   [main_container_ setBorderType:NSLineBorder];
97   [main_container_ setBorderWidth:1];
98   [main_container_ setCornerRadius:0];
99   [main_container_ setContentViewMargins:NSZeroSize];
100   [main_container_ setTitlePosition:NSNoTitle];
101   [main_container_ setBorderColor:[NSColor disabledControlTextColor]];
103   base::scoped_nsobject<NSScrollView> scroll_view(
104       [[NSScrollView alloc] initWithFrame:
105           NSMakeRect(0, 0, kCheckboxWidth, kScrollAreaHeight)]);
106   [scroll_view setHasVerticalScroller:YES];
107   [scroll_view setHasHorizontalScroller:NO];
108   [scroll_view setBorderType:NSNoBorder];
109   [scroll_view setAutohidesScrollers:YES];
110   [[main_container_ contentView] addSubview:scroll_view];
112   // Add scan results checkboxes inside the scrolling view.
113   checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]);
114   CGFloat height = CreateCheckboxes();
115   // Give the container a reasonable initial size so that the scroll_view can
116   // figure out the content size.
117   [checkbox_container_ setFrameSize:NSMakeSize(kCheckboxWidth, height)];
118   [scroll_view setDocumentView:checkbox_container_];
119   [checkbox_container_ setFrameSize:NSMakeSize([scroll_view contentSize].width,
120                                                height)];
122   // Resize to pack the scroll view if possible.
123   NSRect scroll_frame = [scroll_view frame];
124   if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) {
125     scroll_frame.size.height = NSHeight([checkbox_container_ frame]);
126     [scroll_view setFrameSize:scroll_frame.size];
127   }
129   [main_container_ setFrameFromContentFrame:scroll_frame];
130   [main_container_ setFrameOrigin:NSZeroPoint];
131   [alert_ setAccessoryView:main_container_];
132   [alert_ layout];
135 void MediaGalleriesScanResultDialogCocoa::AcceptDialogForTesting() {
136   OnAcceptClicked();
139 CGFloat MediaGalleriesScanResultDialogCocoa::CreateCheckboxes() {
140   CGFloat y_pos = 0;
141   MediaGalleriesScanResultDialogController::OrderedScanResults scan_results =
142       controller_->GetGalleryList();
143   for (MediaGalleriesScanResultDialogController::OrderedScanResults::
144        const_iterator iter = scan_results.begin();
145        iter != scan_results.end(); iter++) {
146     const MediaGalleriesScanResultDialogController::ScanResult& scan_result =
147         *iter;
148     base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry(
149         [[MediaGalleryListEntry alloc]
150             initWithFrame:NSZeroRect
151                controller:this
152                    prefId:scan_result.pref_info.pref_id
153               galleryName:scan_result.pref_info.GetGalleryDisplayName()
154                 subscript:scan_result.pref_info.GetGalleryAdditionalDetails()
155                   tooltip:scan_result.pref_info.GetGalleryTooltip()
156          showFolderViewer:scan_result.pref_info.IsGalleryAvailable()]);
158     [checkbox_entry setState:scan_result.selected];
160     [checkbox_entry setFrameOrigin:NSMakePoint(0, y_pos)];
161     y_pos = NSMaxY([checkbox_entry frame]) + kCheckboxLeading;
163     [checkbox_container_ addSubview:checkbox_entry];
164   }
166   return y_pos;
169 void MediaGalleriesScanResultDialogCocoa::OnAcceptClicked() {
170   accepted_ = true;
172   if (window_)
173     window_->CloseWebContentsModalDialog();
176 void MediaGalleriesScanResultDialogCocoa::OnCancelClicked() {
177   if (window_)
178     window_->CloseWebContentsModalDialog();
181 void MediaGalleriesScanResultDialogCocoa::UpdateResults() {
182   InitDialogControls();
185 void MediaGalleriesScanResultDialogCocoa::OnConstrainedWindowClosed(
186     ConstrainedWindowMac* window) {
187   controller_->DialogFinished(accepted_);
190 void MediaGalleriesScanResultDialogCocoa::OnCheckboxToggled(
191     MediaGalleryPrefId prefId, bool checked) {
192   controller_->DidToggleGalleryId(prefId, checked);
195 void MediaGalleriesScanResultDialogCocoa::OnFolderViewerClicked(
196     MediaGalleryPrefId prefId) {
197   controller_->DidClickOpenFolderViewer(prefId);
200 ui::MenuModel* MediaGalleriesScanResultDialogCocoa::GetContextMenu(
201     MediaGalleryPrefId pref_id) {
202   return controller_->GetContextMenu(pref_id);
205 // static
206 MediaGalleriesScanResultDialog* MediaGalleriesScanResultDialog::Create(
207     MediaGalleriesScanResultDialogController* controller) {
208   base::scoped_nsobject<MediaGalleriesScanResultCocoaController>
209       cocoa_controller([[MediaGalleriesScanResultCocoaController alloc] init]);
210   return new MediaGalleriesScanResultDialogCocoa(controller, cocoa_controller);