Commit 1a1e1637 authored by vandebo@chromium.org's avatar vandebo@chromium.org

Refactor media galleries dialogs on cocoa to extract common code

BUG=NONE

Review URL: https://codereview.chromium.org/231873003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@267021 0039d316-1c4b-4281-b951-d872f2087c98
parent bbd606fd
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/gtest_prod_util.h" #include "base/gtest_prod_util.h"
#include "chrome/browser/media_galleries/media_galleries_dialog_controller.h" #include "chrome/browser/media_galleries/media_galleries_dialog_controller.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#import "chrome/browser/ui/cocoa/extensions/media_gallery_list_entry_view.h"
@class ConstrainedWindowAlert; @class ConstrainedWindowAlert;
@class MediaGalleriesCocoaController; @class MediaGalleriesCocoaController;
...@@ -24,7 +25,8 @@ class MenuModel; ...@@ -24,7 +25,8 @@ class MenuModel;
// This class displays an alert that can be used to grant permission for // This class displays an alert that can be used to grant permission for
// extensions to access a gallery (media folders). // extensions to access a gallery (media folders).
class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate, class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate,
public MediaGalleriesDialog { public MediaGalleriesDialog,
public MediaGalleryListEntryController {
public: public:
MediaGalleriesDialogCocoa( MediaGalleriesDialogCocoa(
MediaGalleriesDialogController* controller, MediaGalleriesDialogController* controller,
...@@ -37,8 +39,6 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate, ...@@ -37,8 +39,6 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate,
void OnCancelClicked(); void OnCancelClicked();
// Called when the user clicks the Add Gallery button. // Called when the user clicks the Add Gallery button.
void OnAddFolderClicked(); void OnAddFolderClicked();
// Called when the user toggles a gallery checkbox.
void OnCheckboxToggled(NSButton* checkbox);
// MediaGalleriesDialog implementation: // MediaGalleriesDialog implementation:
virtual void UpdateGalleries() OVERRIDE; virtual void UpdateGalleries() OVERRIDE;
...@@ -47,7 +47,10 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate, ...@@ -47,7 +47,10 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate,
virtual void OnConstrainedWindowClosed( virtual void OnConstrainedWindowClosed(
ConstrainedWindowMac* window) OVERRIDE; ConstrainedWindowMac* window) OVERRIDE;
ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id); // MediaGalleryListEntryController implementation.
virtual void OnCheckboxToggled(GalleryDialogId gallery_id,
bool checked) OVERRIDE;
virtual ui::MenuModel* GetContextMenu(GalleryDialogId gallery_id) OVERRIDE;
private: private:
FRIEND_TEST_ALL_PREFIXES(MediaGalleriesDialogBrowserTest, Close); FRIEND_TEST_ALL_PREFIXES(MediaGalleriesDialogBrowserTest, Close);
...@@ -77,9 +80,6 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate, ...@@ -77,9 +80,6 @@ class MediaGalleriesDialogCocoa : public ConstrainedWindowMacDelegate,
// True if the user has pressed accept. // True if the user has pressed accept.
bool accepted_; bool accepted_;
// List of checkboxes ordered from bottom to top.
base::scoped_nsobject<NSMutableArray> checkboxes_;
// Container view for checkboxes. // Container view for checkboxes.
base::scoped_nsobject<NSView> checkbox_container_; base::scoped_nsobject<NSView> checkbox_container_;
......
...@@ -28,6 +28,10 @@ ...@@ -28,6 +28,10 @@
@property(assign, nonatomic) MediaGalleriesDialogCocoa* dialog; @property(assign, nonatomic) MediaGalleriesDialogCocoa* dialog;
- (void)onAcceptButton:(id)sender;
- (void)onCancelButton:(id)sender;
- (void)onAddFolderClicked:(id)sender;
@end @end
@implementation MediaGalleriesCocoaController @implementation MediaGalleriesCocoaController
...@@ -47,47 +51,6 @@ ...@@ -47,47 +51,6 @@
dialog_->OnAddFolderClicked(); dialog_->OnAddFolderClicked();
} }
- (void)onCheckboxToggled:(id)sender {
DCHECK(dialog_);
dialog_->OnCheckboxToggled(sender);
}
@end
@interface MediaGalleriesCheckbox : NSButton {
@private
MediaGalleriesDialogCocoa* dialog_; // |dialog_| owns |this|.
GalleryDialogId galleryId_;
base::scoped_nsobject<MenuController> menuController_;
}
- (id)initWithFrame:(NSRect)frameRect
dialog:(MediaGalleriesDialogCocoa*)dialog
galleryId:(GalleryDialogId)galleryId;
- (NSMenu*)menuForEvent:(NSEvent*)theEvent;
@end
@implementation MediaGalleriesCheckbox
- (id)initWithFrame:(NSRect)frameRect
dialog:(MediaGalleriesDialogCocoa*)dialog
galleryId:(GalleryDialogId)galleryId {
if ((self = [super initWithFrame:frameRect])) {
dialog_ = dialog;
galleryId_ = galleryId;
}
return self;
}
- (NSMenu*)menuForEvent:(NSEvent*)theEvent {
menuController_.reset(
[[MenuController alloc] initWithModel:dialog_->GetContextMenu(galleryId_)
useWithPopUpButtonCell:NO]);
return [menuController_ menu];
}
@end @end
namespace { namespace {
...@@ -112,7 +75,7 @@ MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa( ...@@ -112,7 +75,7 @@ MediaGalleriesDialogCocoa::MediaGalleriesDialogCocoa(
[alert_ setInformativeText: [alert_ setInformativeText:
base::SysUTF16ToNSString(controller_->GetSubtext())]; base::SysUTF16ToNSString(controller_->GetSubtext())];
[alert_ addButtonWithTitle: [alert_ addButtonWithTitle:
l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM) l10n_util::GetNSString(IDS_MEDIA_GALLERIES_DIALOG_CONFIRM)
keyEquivalent:kKeyEquivalentReturn keyEquivalent:kKeyEquivalentReturn
target:cocoa_controller_ target:cocoa_controller_
action:@selector(onAcceptButton:)]; action:@selector(onAcceptButton:)];
...@@ -168,28 +131,30 @@ void MediaGalleriesDialogCocoa::InitDialogControls() { ...@@ -168,28 +131,30 @@ void MediaGalleriesDialogCocoa::InitDialogControls() {
// Add gallery permission checkboxes inside the scrolling view. // Add gallery permission checkboxes inside the scrolling view.
checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]); checkbox_container_.reset([[FlippedView alloc] initWithFrame:NSZeroRect]);
checkboxes_.reset([[NSMutableArray alloc] init]);
[scroll_view setDocumentView:checkbox_container_];
CGFloat y_pos = 0;
y_pos = CreateCheckboxes(y_pos, controller_->AttachedPermissions()); CGFloat y_pos = CreateCheckboxes(0, controller_->AttachedPermissions());
if (!controller_->UnattachedPermissions().empty()) { if (!controller_->UnattachedPermissions().empty()) {
y_pos = CreateCheckboxSeparator(y_pos); y_pos = CreateCheckboxSeparator(y_pos);
y_pos = CreateCheckboxes(y_pos, controller_->UnattachedPermissions()); y_pos = CreateCheckboxes(y_pos, controller_->UnattachedPermissions());
} }
[checkbox_container_ setFrame:NSMakeRect(0, 0, kCheckboxMaxWidth, y_pos + 2)]; // Give the container a reasonable initial size so that the scroll_view can
// figure out the content size.
[checkbox_container_ setFrameSize:NSMakeSize(kCheckboxMaxWidth, y_pos)];
[scroll_view setDocumentView:checkbox_container_];
[checkbox_container_ setFrameSize:NSMakeSize([scroll_view contentSize].width,
y_pos)];
// Resize to pack the scroll view if possible. // Resize to pack the scroll view if possible.
NSRect scroll_frame = [scroll_view frame]; NSRect scroll_frame = [scroll_view frame];
if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) { if (NSHeight(scroll_frame) > NSHeight([checkbox_container_ frame])) {
scroll_frame.size.height = NSHeight([checkbox_container_ frame]); scroll_frame.size.height = NSHeight([checkbox_container_ frame]);
[scroll_view setFrame:scroll_frame]; [scroll_view setFrameSize:scroll_frame.size];
} }
[main_container_ setFrameFromContentFrame:scroll_frame]; [main_container_ setFrameFromContentFrame:scroll_frame];
[main_container_ setFrameOrigin:NSZeroPoint];
[alert_ setAccessoryView:main_container_]; [alert_ setAccessoryView:main_container_];
// As a safeguard against the user skipping reading over the dialog and just // As a safeguard against the user skipping reading over the dialog and just
...@@ -205,14 +170,26 @@ CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes( ...@@ -205,14 +170,26 @@ CGFloat MediaGalleriesDialogCocoa::CreateCheckboxes(
CGFloat y_pos, CGFloat y_pos,
const MediaGalleriesDialogController::GalleryPermissionsVector& const MediaGalleriesDialogController::GalleryPermissionsVector&
permissions) { permissions) {
y_pos += kCheckboxMargin;
for (MediaGalleriesDialogController::GalleryPermissionsVector:: for (MediaGalleriesDialogController::GalleryPermissionsVector::
const_iterator iter = permissions.begin(); const_iterator iter = permissions.begin();
iter != permissions.end(); iter++) { iter != permissions.end(); iter++) {
const MediaGalleriesDialogController::GalleryPermission& permission = *iter; const MediaGalleriesDialogController::GalleryPermission& permission = *iter;
UpdateGalleryCheckbox(permission, y_pos); base::scoped_nsobject<MediaGalleryListEntry> checkbox_entry(
y_pos = NSMaxY([[checkboxes_ lastObject] frame]) + kCheckboxMargin; [[MediaGalleryListEntry alloc]
initWithFrame:NSZeroRect
controller:this
prefId:permission.gallery_id
galleryName:permission.pref_info.GetGalleryDisplayName()
subscript:permission.pref_info.GetGalleryAdditionalDetails()
tooltip:permission.pref_info.GetGalleryTooltip()
showFolderViewer:false]);
[checkbox_entry setState:permission.allowed];
[checkbox_entry setFrameOrigin:NSMakePoint(0, y_pos)];
y_pos = NSMaxY([checkbox_entry frame]) + kCheckboxMargin;
[checkbox_container_ addSubview:checkbox_entry];
} }
return y_pos; return y_pos;
...@@ -268,76 +245,6 @@ void MediaGalleriesDialogCocoa::OnAddFolderClicked() { ...@@ -268,76 +245,6 @@ void MediaGalleriesDialogCocoa::OnAddFolderClicked() {
controller_->OnAddFolderClicked(); controller_->OnAddFolderClicked();
} }
void MediaGalleriesDialogCocoa::OnCheckboxToggled(NSButton* checkbox) {
GalleryDialogId gallery_id =
[[[checkbox cell] representedObject] longLongValue];
controller_->DidToggleGallery(gallery_id, [checkbox state] == NSOnState);
[[[alert_ buttons] objectAtIndex:0] setEnabled:
controller_->IsAcceptAllowed()];
}
void MediaGalleriesDialogCocoa::UpdateGalleryCheckbox(
const MediaGalleriesDialogController::GalleryPermission& gallery,
CGFloat y_pos) {
// Checkbox.
base::scoped_nsobject<MediaGalleriesCheckbox> checkbox(
[[MediaGalleriesCheckbox alloc] initWithFrame:NSZeroRect
dialog:this
galleryId:gallery.gallery_id]);
NSNumber* gallery_id_object =
[NSNumber numberWithLongLong:gallery.gallery_id];
[[checkbox cell] setRepresentedObject:gallery_id_object];
[[checkbox cell] setLineBreakMode:NSLineBreakByTruncatingMiddle];
[checkbox setButtonType:NSSwitchButton];
[checkbox setTarget:cocoa_controller_];
[checkbox setAction:@selector(onCheckboxToggled:)];
[checkboxes_ addObject:checkbox];
[checkbox setTitle:base::SysUTF16ToNSString(
gallery.pref_info.GetGalleryDisplayName())];
[checkbox setToolTip:base::SysUTF16ToNSString(
gallery.pref_info.GetGalleryTooltip())];
[checkbox setState:gallery.allowed ? NSOnState : NSOffState];
[checkbox sizeToFit];
NSRect rect = [checkbox bounds];
// Detail text.
base::scoped_nsobject<NSTextField> details(
[[NSTextField alloc] initWithFrame:NSZeroRect]);
[details setEditable:NO];
[details setSelectable:NO];
[details setBezeled:NO];
[details setAttributedStringValue:
constrained_window::GetAttributedLabelString(
base::SysUTF16ToNSString(
gallery.pref_info.GetGalleryAdditionalDetails()),
chrome_style::kTextFontStyle,
NSNaturalTextAlignment,
NSLineBreakByClipping
)];
[details setTextColor:[NSColor colorWithCalibratedRed:0.625
green:0.625
blue:0.625
alpha:1.0]];
[details sizeToFit];
NSRect details_rect = [details bounds];
// The checkbox will elide so reduce its size so it will all fit.
rect.size.width =
std::min(NSWidth(rect),
kCheckboxMaxWidth - 3 * kCheckboxMargin - NSWidth(details_rect));
rect.origin = NSMakePoint(kCheckboxMargin, y_pos);
[checkbox setFrame:rect];
[checkbox_container_ addSubview:checkbox];
details_rect.origin = NSMakePoint(NSMaxX(rect) + kCheckboxMargin, y_pos - 1);
[details setFrame:details_rect];
[checkbox_container_ addSubview:details];
}
void MediaGalleriesDialogCocoa::UpdateGalleries() { void MediaGalleriesDialogCocoa::UpdateGalleries() {
InitDialogControls(); InitDialogControls();
} }
...@@ -347,6 +254,14 @@ void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed( ...@@ -347,6 +254,14 @@ void MediaGalleriesDialogCocoa::OnConstrainedWindowClosed(
controller_->DialogFinished(accepted_); controller_->DialogFinished(accepted_);
} }
void MediaGalleriesDialogCocoa::OnCheckboxToggled(GalleryDialogId gallery_id,
bool checked) {
controller_->DidToggleGallery(gallery_id, checked);
[[[alert_ buttons] objectAtIndex:0] setEnabled:
controller_->IsAcceptAllowed()];
}
ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu( ui::MenuModel* MediaGalleriesDialogCocoa::GetContextMenu(
GalleryDialogId gallery_id) { GalleryDialogId gallery_id) {
return controller_->GetContextMenu(gallery_id); return controller_->GetContextMenu(gallery_id);
......
...@@ -17,6 +17,24 @@ using ::testing::Return; ...@@ -17,6 +17,24 @@ using ::testing::Return;
using ::testing::ReturnPointee; using ::testing::ReturnPointee;
using ::testing::ReturnRef; using ::testing::ReturnRef;
@interface MediaGalleryListEntry (testing)
- (NSInteger)state;
- (void)performClick:(id)sender;
@end
@implementation MediaGalleryListEntry (testing)
- (NSInteger)state {
return [checkbox_ state];
}
- (void)performClick:(id)sender {
[checkbox_ performClick:sender];
}
@end
MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId pref_id) { MediaGalleryPrefInfo MakePrefInfoForTesting(MediaGalleryPrefId pref_id) {
MediaGalleryPrefInfo gallery; MediaGalleryPrefInfo gallery;
gallery.pref_id = pref_id; gallery.pref_id = pref_id;
...@@ -75,12 +93,14 @@ TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) { ...@@ -75,12 +93,14 @@ TEST_F(MediaGalleriesDialogTest, InitializeCheckboxes) {
scoped_ptr<MediaGalleriesDialogCocoa> dialog( scoped_ptr<MediaGalleriesDialogCocoa> dialog(
static_cast<MediaGalleriesDialogCocoa*>( static_cast<MediaGalleriesDialogCocoa*>(
MediaGalleriesDialog::Create(&controller))); MediaGalleriesDialog::Create(&controller)));
EXPECT_EQ(2U, [dialog->checkboxes_ count]); EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
NSButton* checkbox1 = [dialog->checkboxes_ objectAtIndex:0]; NSButton* checkbox1 =
[[dialog->checkbox_container_ subviews] objectAtIndex:0];
EXPECT_EQ([checkbox1 state], NSOnState); EXPECT_EQ([checkbox1 state], NSOnState);
NSButton* checkbox2 = [dialog->checkboxes_ objectAtIndex:1]; NSButton* checkbox2 =
[[dialog->checkbox_container_ subviews] objectAtIndex:1];
EXPECT_EQ([checkbox2 state], NSOffState); EXPECT_EQ([checkbox2 state], NSOffState);
} }
...@@ -103,9 +123,9 @@ TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) { ...@@ -103,9 +123,9 @@ TEST_F(MediaGalleriesDialogTest, ToggleCheckboxes) {
scoped_ptr<MediaGalleriesDialogCocoa> dialog( scoped_ptr<MediaGalleriesDialogCocoa> dialog(
static_cast<MediaGalleriesDialogCocoa*>( static_cast<MediaGalleriesDialogCocoa*>(
MediaGalleriesDialog::Create(&controller))); MediaGalleriesDialog::Create(&controller)));
EXPECT_EQ(1U, [dialog->checkboxes_ count]); EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
NSButton* checkbox = [dialog->checkboxes_ objectAtIndex:0]; NSButton* checkbox = [[dialog->checkbox_container_ subviews] objectAtIndex:0];
EXPECT_EQ([checkbox state], NSOnState); EXPECT_EQ([checkbox state], NSOnState);
EXPECT_CALL(controller, DidToggleGallery(1, false)); EXPECT_CALL(controller, DidToggleGallery(1, false));
...@@ -135,14 +155,14 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) { ...@@ -135,14 +155,14 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
static_cast<MediaGalleriesDialogCocoa*>( static_cast<MediaGalleriesDialogCocoa*>(
MediaGalleriesDialog::Create(&controller))); MediaGalleriesDialog::Create(&controller)));
EXPECT_EQ(0U, [dialog->checkboxes_ count]); EXPECT_EQ(0U, [[dialog->checkbox_container_ subviews] count]);
CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]); CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
attached_permissions.push_back( attached_permissions.push_back(
MediaGalleriesDialogController::GalleryPermission( MediaGalleriesDialogController::GalleryPermission(
1, MakePrefInfoForTesting(10), true)); 1, MakePrefInfoForTesting(10), true));
dialog->UpdateGalleries(); dialog->UpdateGalleries();
EXPECT_EQ(1U, [dialog->checkboxes_ count]); EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
// The checkbox container should be taller. // The checkbox container should be taller.
CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]); CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
...@@ -153,7 +173,7 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) { ...@@ -153,7 +173,7 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
MediaGalleriesDialogController::GalleryPermission( MediaGalleriesDialogController::GalleryPermission(
2, MakePrefInfoForTesting(20), true)); 2, MakePrefInfoForTesting(20), true));
dialog->UpdateGalleries(); dialog->UpdateGalleries();
EXPECT_EQ(2U, [dialog->checkboxes_ count]); EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
// The checkbox container should be taller. // The checkbox container should be taller.
new_container_height = NSHeight([dialog->checkbox_container_ frame]); new_container_height = NSHeight([dialog->checkbox_container_ frame]);
...@@ -162,7 +182,7 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) { ...@@ -162,7 +182,7 @@ TEST_F(MediaGalleriesDialogTest, UpdateAdds) {
attached_permissions[1].allowed = false; attached_permissions[1].allowed = false;
dialog->UpdateGalleries(); dialog->UpdateGalleries();
EXPECT_EQ(2U, [dialog->checkboxes_ count]); EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
// The checkbox container height should not have changed. // The checkbox container height should not have changed.
new_container_height = NSHeight([dialog->checkbox_container_ frame]); new_container_height = NSHeight([dialog->checkbox_container_ frame]);
...@@ -194,13 +214,13 @@ TEST_F(MediaGalleriesDialogTest, ForgetDeletes) { ...@@ -194,13 +214,13 @@ TEST_F(MediaGalleriesDialogTest, ForgetDeletes) {
MediaGalleriesDialogController::GalleryPermission( MediaGalleriesDialogController::GalleryPermission(
2, MakePrefInfoForTesting(20), true)); 2, MakePrefInfoForTesting(20), true));
dialog->UpdateGalleries(); dialog->UpdateGalleries();
EXPECT_EQ(2U, [dialog->checkboxes_ count]); EXPECT_EQ(2U, [[dialog->checkbox_container_ subviews] count]);
CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]); CGFloat old_container_height = NSHeight([dialog->checkbox_container_ frame]);
// Remove a gallery. // Remove a gallery.
attached_permissions.erase(attached_permissions.begin()); attached_permissions.erase(attached_permissions.begin());
dialog->UpdateGalleries(); dialog->UpdateGalleries();
EXPECT_EQ(1U, [dialog->checkboxes_ count]); EXPECT_EQ(1U, [[dialog->checkbox_container_ subviews] count]);
// The checkbox container should be shorter. // The checkbox container should be shorter.
CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]); CGFloat new_container_height = NSHeight([dialog->checkbox_container_ frame]);
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_controller.h" #include "chrome/browser/media_galleries/media_galleries_scan_result_dialog_controller.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
#import "chrome/browser/ui/cocoa/extensions/media_gallery_list_entry_view.h"
@class ConstrainedWindowAlert; @class ConstrainedWindowAlert;
@class MediaGalleriesScanResultCocoaController; @class MediaGalleriesScanResultCocoaController;
...@@ -22,7 +23,8 @@ class MenuModel; ...@@ -22,7 +23,8 @@ class MenuModel;
// (media folders) the app (extension) should have access to. // (media folders) the app (extension) should have access to.
class MediaGalleriesScanResultDialogCocoa class MediaGalleriesScanResultDialogCocoa
: public ConstrainedWindowMacDelegate, : public ConstrainedWindowMacDelegate,
public MediaGalleriesScanResultDialog { public MediaGalleriesScanResultDialog,
public MediaGalleryListEntryController {
public: public:
MediaGalleriesScanResultDialogCocoa( MediaGalleriesScanResultDialogCocoa(
MediaGalleriesScanResultDialogController* controller, MediaGalleriesScanResultDialogController* controller,
...@@ -33,10 +35,6 @@ class MediaGalleriesScanResultDialogCocoa ...@@ -33,10 +35,6 @@ class MediaGalleriesScanResultDialogCocoa
void OnAcceptClicked(); void OnAcceptClicked();
// Called when the user clicks the cancel button. // Called when the user clicks the cancel button.
void OnCancelClicked(); void OnCancelClicked();
// Called when the user toggles a gallery checkbox.
void OnCheckboxToggled(NSButton* checkbox);
// Called when the user toggles a gallery checkbox.
void OnFolderViewClicked(NSButton* folder_viewer_button);
// MediaGalleriesScanResultDialog implementation: // MediaGalleriesScanResultDialog implementation:
virtual void UpdateResults() OVERRIDE; virtual void UpdateResults() OVERRIDE;
...@@ -45,20 +43,17 @@ class MediaGalleriesScanResultDialogCocoa ...@@ -45,20 +43,17 @@ class MediaGalleriesScanResultDialogCocoa
virtual void OnConstrainedWindowClosed( virtual void OnConstrainedWindowClosed(
ConstrainedWindowMac* window) OVERRIDE; ConstrainedWindowMac* window) OVERRIDE;
ui::MenuModel* GetContextMenu(MediaGalleryPrefId prefid); // MediaGalleryListEntryController implementation.
virtual void OnCheckboxToggled(MediaGalleryPrefId prefId,
bool checked) OVERRIDE;
virtual void OnFolderViewerClicked(MediaGalleryPrefId prefId) OVERRIDE;
virtual ui::MenuModel* GetContextMenu(MediaGalleryPrefId prefid) OVERRIDE;
private: private:
friend class MediaGalleriesScanResultDialogCocoaTest; friend class MediaGalleriesScanResultDialogCocoaTest;
void UpdateScanResultCheckbox(const MediaGalleryPrefInfo& scan_result,
bool checked,
CGFloat y_pos);
void InitDialogControls(); void InitDialogControls();
CGFloat CreateCheckboxes( CGFloat CreateCheckboxes();
CGFloat y_pos,
const MediaGalleriesScanResultDialogController::OrderedScanResults&
scan_results);
// MediaGalleriesScanResultDialog implementation: // MediaGalleriesScanResultDialog implementation:
virtual void AcceptDialogForTesting() OVERRIDE; virtual void AcceptDialogForTesting() OVERRIDE;
...@@ -72,9 +67,6 @@ class MediaGalleriesScanResultDialogCocoa ...@@ -72,9 +67,6 @@ class MediaGalleriesScanResultDialogCocoa
// True if the user has pressed accept. // True if the user has pressed accept.
bool accepted_; bool accepted_;
// List of checkboxes ordered from bottom to top.
base::scoped_nsobject<NSMutableArray> checkboxes_;
// Container view for checkboxes. // Container view for checkboxes.
base::scoped_nsobject<NSView> checkbox_container_; base::scoped_nsobject<NSView> checkbox_container_;
......
// Copyright 2014 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.
#ifndef CHROME_BROWSER_UI_COCOA_EXTENSIONS_MEDIA_GALLERY_LIST_ENTRY_VIEW_H_
#define CHROME_BROWSER_UI_COCOA_EXTENSIONS_MEDIA_GALLERY_LIST_ENTRY_VIEW_H_
#import <Cocoa/Cocoa.h>
#import "base/mac/scoped_nsobject.h"
#include "chrome/browser/media_galleries/media_galleries_preferences.h"
#import "ui/base/models/menu_model.h"
@class MediaGalleryButton;
class MediaGalleryListEntryController {
public:
virtual void OnCheckboxToggled(MediaGalleryPrefId pref_id, bool checked) {}
virtual void OnFolderViewerClicked(MediaGalleryPrefId pref_id) {}
virtual ui::MenuModel* GetContextMenu(MediaGalleryPrefId pref_id);
protected:
virtual ~MediaGalleryListEntryController() {}
};
@interface MediaGalleryListEntry : NSView {
@private
MediaGalleryListEntryController* controller_; // |controller_| owns |this|.
MediaGalleryPrefId prefId_;
base::scoped_nsobject<MediaGalleryButton> checkbox_;
base::scoped_nsobject<MediaGalleryButton> folderViewer_;
base::scoped_nsobject<NSTextField> details_;
}
// Does size to fit if frameRect is empty.
- (id)initWithFrame:(NSRect)frameRect
controller:(MediaGalleryListEntryController*)controller_
prefId:(MediaGalleryPrefId)prefId
galleryName:(base::string16)galleryName
subscript:(base::string16)subscript
tooltip:(base::string16)tooltip
showFolderViewer:(bool)showFolderViewer;
- (void)setState:(bool)selected;
@end
#endif // CHROME_BROWSER_UI_COCOA_EXTENSIONS_MEDIA_GALLERY_LIST_ENTRY_VIEW_H_
// Copyright 2014 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 "chrome/browser/ui/cocoa/extensions/media_gallery_list_entry_view.h"
#include "base/strings/sys_string_conversions.h"
#import "chrome/browser/ui/cocoa/constrained_window/constrained_window_control_utils.h"
#include "chrome/browser/ui/chrome_style.h"
#include "grit/theme_resources.h"
#import "ui/base/cocoa/menu_controller.h"
#include "ui/base/resource/resource_bundle.h"
const CGFloat kCheckboxMargin = 10;
ui::MenuModel* MediaGalleryListEntryController::GetContextMenu(
MediaGalleryPrefId pref_id) {
return NULL;
}
@interface MediaGalleryListEntry ()
- (void)onCheckboxToggled:(id)sender;
- (void)onFolderViewerClicked:(id)sender;
- (ui::MenuModel*)getContextMenu;
- (void)layoutSubViews;
@end
@interface MediaGalleryButton : NSButton {
@private
MediaGalleryListEntry* controller_; // |controller_| owns |self|.
base::scoped_nsobject<MenuController> menuController_;
}
- (id)initWithFrame:(NSRect)frameRect
controller:(MediaGalleryListEntry*)controller;
- (NSMenu*)menuForEvent:(NSEvent*)theEvent;
@end
@implementation MediaGalleryButton
- (id)initWithFrame:(NSRect)frameRect
controller:(MediaGalleryListEntry*)controller {
if ((self = [super initWithFrame:frameRect])) {
controller_ = controller;
}
return self;
}
- (NSMenu*)menuForEvent:(NSEvent*)theEvent {
menuController_.reset(
[[MenuController alloc] initWithModel:[controller_ getContextMenu]
useWithPopUpButtonCell:NO]);
return [menuController_ menu];
}
@end
@implementation MediaGalleryListEntry
- (id)initWithFrame:(NSRect)frameRect
controller:(MediaGalleryListEntryController*)controller
prefId:(MediaGalleryPrefId)prefId
galleryName:(base::string16)galleryName
subscript:(base::string16)subscript
tooltip:(base::string16)tooltip
showFolderViewer:(bool)showFolderViewer {
if ((self = [super initWithFrame:frameRect])) {
controller_ = controller;
prefId_ = prefId;
NSString* nsTooltip = base::SysUTF16ToNSString(tooltip);
// Set a auto resize mask so that -resizeWithOldSuperviewSize: is called.
// It is overridden so the particular mask doesn't matter.
[self setAutoresizingMask:NSViewWidthSizable];
checkbox_.reset(
[[MediaGalleryButton alloc] initWithFrame:NSZeroRect
controller:self]);
[[checkbox_ cell] setLineBreakMode:NSLineBreakByTruncatingMiddle];
[checkbox_ setButtonType:NSSwitchButton];
[checkbox_ setTarget:self];
[checkbox_ setAction:@selector(onCheckboxToggled:)];
[checkbox_ setTitle:base::SysUTF16ToNSString(galleryName)];
[checkbox_ setToolTip:nsTooltip];
[self addSubview:checkbox_];
// Folder viewer button.
if (showFolderViewer) {
folderViewer_.reset(
[[MediaGalleryButton alloc] initWithFrame:NSZeroRect
controller:self]);
[folderViewer_ setButtonType:NSMomentaryChangeButton];
[folderViewer_ setTarget:self];
[folderViewer_ setAction:@selector(onFolderViewerClicked:)];
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
[folderViewer_ setImage:rb.GetNativeImageNamed(
IDR_FILE_FOLDER).ToNSImage()];
[folderViewer_ setTitle:nil];
[folderViewer_ setBordered:false];
[folderViewer_ setToolTip:nsTooltip];
[self addSubview:folderViewer_];
}
// Additional details text.
if (!subscript.empty()) {
details_.reset([[NSTextField alloc] initWithFrame:NSZeroRect]);
[[details_ cell] setLineBreakMode:NSLineBreakByTruncatingHead];
[details_ setEditable:NO];
[details_ setSelectable:NO];
[details_ setBezeled:NO];
[details_ setAttributedStringValue:
constrained_window::GetAttributedLabelString(
base::SysUTF16ToNSString(subscript),
chrome_style::kTextFontStyle,
NSNaturalTextAlignment,
NSLineBreakByClipping
)];
[details_ setTextColor:[NSColor disabledControlTextColor]];
[self addSubview:details_];
}
[self layoutSubViews];
}
return self;
}
- (void)setFrameSize:(NSSize)frameSize {
[super setFrameSize:frameSize];
[self layoutSubViews];
}
- (void)resizeWithOldSuperviewSize:(NSSize)oldBoundsSize {
NSRect frame = [self frame];
frame.size.width = NSWidth([[self superview] frame]);
[self setFrameSize:frame.size];
}
- (void)setState:(bool)selected {
[checkbox_ setState:selected ? NSOnState : NSOffState];
}
- (void)onCheckboxToggled:(id)sender {
controller_->OnCheckboxToggled(prefId_, [sender state] == NSOnState);
}
- (void)onFolderViewerClicked:(id)sender {
controller_->OnFolderViewerClicked(prefId_);
}
- (ui::MenuModel*)getContextMenu {
return controller_->GetContextMenu(prefId_);
}
- (void)layoutSubViews {
NSRect bounds = [self bounds];
// If we have an empty frame, we should auto size, so start with really big
// bounds and then set it to the real size of the contents later.
if (NSIsEmptyRect([self frame]))
bounds.size = NSMakeSize(10000, 10000);
[checkbox_ sizeToFit];
[folderViewer_ sizeToFit];
[details_ sizeToFit];
// Auto size everything and lay it out horizontally.
CGFloat xPos = kCheckboxMargin;
for (NSView* view in [self subviews]) {
NSRect viewFrame = [view frame];
viewFrame.origin.x = xPos;
viewFrame.size.height = std::min(NSHeight(bounds), NSHeight(viewFrame));
[view setFrame:viewFrame];
xPos = NSMaxX([view frame]) + kCheckboxMargin;
}
// Size the views. If all the elements don't naturally fit, the checkbox
// should get squished and will elide in the middle. However, it shouldn't
// squish too much so it gets at least half of the max width and the details
// text should elide as well in that case.
if (xPos > NSWidth(bounds)) {
CGFloat maxRHSContent = NSWidth(bounds) / 2 - kCheckboxMargin;
NSRect detailsFrame = [details_ frame];
NSRect checkboxFrame = [checkbox_ frame];
if (NSMaxX(detailsFrame) - NSMaxX(checkboxFrame) > maxRHSContent) {
detailsFrame.size.width = std::max(
maxRHSContent - (NSMinX(detailsFrame) - NSMaxX(checkboxFrame)),
NSWidth(bounds) - kCheckboxMargin - NSMinX(detailsFrame));
[details_ setFrameSize:detailsFrame.size];
xPos = NSMaxX(detailsFrame) + kCheckboxMargin;
}
CGFloat overflow = xPos - NSWidth(bounds);
if (overflow > 0) {
checkboxFrame.size.width -= overflow;
[checkbox_ setFrameSize:checkboxFrame.size];
if (folderViewer_.get()) {
NSRect folderViewerFrame = [folderViewer_ frame];
folderViewerFrame.origin.x -= overflow;
[folderViewer_ setFrameOrigin:folderViewerFrame.origin];
}
if (details_.get()) {
detailsFrame.origin.x -= overflow;
[details_ setFrameOrigin:detailsFrame.origin];
}
}
}
if (NSIsEmptyRect([self frame])) {
NSRect frame = NSMakeRect(0, 0, 1, 1);
for (NSView* view in [self subviews]) {
frame = NSUnionRect(frame, [view frame]);
}
frame.size.width += kCheckboxMargin;
[super setFrameSize:frame.size];
}
}
@end
...@@ -777,6 +777,8 @@ ...@@ -777,6 +777,8 @@
'browser/ui/cocoa/extensions/extension_view_mac.mm', 'browser/ui/cocoa/extensions/extension_view_mac.mm',
'browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h', 'browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.h',
'browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.mm', 'browser/ui/cocoa/extensions/media_galleries_dialog_cocoa.mm',
'browser/ui/cocoa/extensions/media_gallery_list_entry_view.h',
'browser/ui/cocoa/extensions/media_gallery_list_entry_view.mm',
'browser/ui/cocoa/extensions/media_galleries_scan_result_dialog_cocoa.h', 'browser/ui/cocoa/extensions/media_galleries_scan_result_dialog_cocoa.h',
'browser/ui/cocoa/extensions/media_galleries_scan_result_dialog_cocoa.mm', 'browser/ui/cocoa/extensions/media_galleries_scan_result_dialog_cocoa.mm',
'browser/ui/cocoa/extensions/windowed_install_dialog_controller.h', 'browser/ui/cocoa/extensions/windowed_install_dialog_controller.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment