| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/page_info/permission_selector_button.h" | 5 #import "chrome/browser/ui/cocoa/page_info/permission_selector_button.h" |
| 6 | 6 |
| 7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "chrome/browser/ui/cocoa/page_info/website_settings_utils_cocoa.h" | 8 #include "chrome/browser/ui/cocoa/page_info/page_info_utils_cocoa.h" |
| 9 #include "chrome/browser/ui/page_info/website_settings_ui.h" | 9 #include "chrome/browser/ui/page_info/page_info_ui.h" |
| 10 #import "ui/base/cocoa/menu_controller.h" | 10 #import "ui/base/cocoa/menu_controller.h" |
| 11 | 11 |
| 12 @implementation PermissionSelectorButton | 12 @implementation PermissionSelectorButton |
| 13 | 13 |
| 14 - (id)initWithPermissionInfo: | 14 - (id)initWithPermissionInfo:(const PageInfoUI::PermissionInfo&)permissionInfo |
| 15 (const WebsiteSettingsUI::PermissionInfo&)permissionInfo | |
| 16 forURL:(const GURL&)url | 15 forURL:(const GURL&)url |
| 17 withCallback:(PermissionMenuModel::ChangeCallback)callback | 16 withCallback:(PermissionMenuModel::ChangeCallback)callback |
| 18 profile:(Profile*)profile { | 17 profile:(Profile*)profile { |
| 19 if (self = [super initWithFrame:NSMakeRect(0, 0, 1, 1) pullsDown:NO]) { | 18 if (self = [super initWithFrame:NSMakeRect(0, 0, 1, 1) pullsDown:NO]) { |
| 20 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; | 19 [self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]]; |
| 21 [self setBordered:NO]; | 20 [self setBordered:NO]; |
| 22 [[self cell] setControlSize:NSSmallControlSize]; | 21 [[self cell] setControlSize:NSSmallControlSize]; |
| 23 | 22 |
| 24 menuModel_.reset( | 23 menuModel_.reset( |
| 25 new PermissionMenuModel(profile, url, permissionInfo, callback)); | 24 new PermissionMenuModel(profile, url, permissionInfo, callback)); |
| 26 | 25 |
| 27 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() | 26 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() |
| 28 useWithPopUpButtonCell:NO]); | 27 useWithPopUpButtonCell:NO]); |
| 29 [self setMenu:[menuController_ menu]]; | 28 [self setMenu:[menuController_ menu]]; |
| 30 [self selectItemWithTag:permissionInfo.setting]; | 29 [self selectItemWithTag:permissionInfo.setting]; |
| 31 | 30 |
| 32 // Set the button title. | 31 // Set the button title. |
| 33 base::scoped_nsobject<NSMenuItem> titleItem([[NSMenuItem alloc] init]); | 32 base::scoped_nsobject<NSMenuItem> titleItem([[NSMenuItem alloc] init]); |
| 34 base::string16 buttonTitle = WebsiteSettingsUI::PermissionActionToUIString( | 33 base::string16 buttonTitle = PageInfoUI::PermissionActionToUIString( |
| 35 profile, permissionInfo.type, permissionInfo.setting, | 34 profile, permissionInfo.type, permissionInfo.setting, |
| 36 permissionInfo.default_setting, permissionInfo.source); | 35 permissionInfo.default_setting, permissionInfo.source); |
| 37 [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)]; | 36 [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)]; |
| 38 [[self cell] setUsesItemFromMenu:NO]; | 37 [[self cell] setUsesItemFromMenu:NO]; |
| 39 [[self cell] setMenuItem:titleItem.get()]; | 38 [[self cell] setMenuItem:titleItem.get()]; |
| 40 // Although the frame is reset, below, this sizes the cell properly. | 39 // Although the frame is reset, below, this sizes the cell properly. |
| 41 [self sizeToFit]; | 40 [self sizeToFit]; |
| 42 | 41 |
| 43 // Size the button to just fit the visible title - not all of its items. | 42 // Size the button to just fit the visible title - not all of its items. |
| 44 [self setFrameSize:SizeForWebsiteSettingsButtonTitle(self, [self title])]; | 43 [self setFrameSize:SizeForPageInfoButtonTitle(self, [self title])]; |
| 45 } | 44 } |
| 46 return self; | 45 return self; |
| 47 } | 46 } |
| 48 | 47 |
| 49 - (CGFloat)maxTitleWidthForContentSettingsType:(ContentSettingsType)type | 48 - (CGFloat)maxTitleWidthForContentSettingsType:(ContentSettingsType)type |
| 50 withDefaultSetting:(ContentSetting)defaultSetting | 49 withDefaultSetting:(ContentSetting)defaultSetting |
| 51 profile:(Profile*)profile { | 50 profile:(Profile*)profile { |
| 52 // Determine the largest possible size for this button. | 51 // Determine the largest possible size for this button. |
| 53 CGFloat maxTitleWidth = 0; | 52 CGFloat maxTitleWidth = 0; |
| 54 for (NSMenuItem* item in [self itemArray]) { | 53 for (NSMenuItem* item in [self itemArray]) { |
| 55 NSString* title = | 54 NSString* title = |
| 56 base::SysUTF16ToNSString(WebsiteSettingsUI::PermissionActionToUIString( | 55 base::SysUTF16ToNSString(PageInfoUI::PermissionActionToUIString( |
| 57 profile, type, static_cast<ContentSetting>([item tag]), | 56 profile, type, static_cast<ContentSetting>([item tag]), |
| 58 defaultSetting, content_settings::SETTING_SOURCE_USER)); | 57 defaultSetting, content_settings::SETTING_SOURCE_USER)); |
| 59 NSSize size = SizeForWebsiteSettingsButtonTitle(self, title); | 58 NSSize size = SizeForPageInfoButtonTitle(self, title); |
| 60 maxTitleWidth = std::max(maxTitleWidth, size.width); | 59 maxTitleWidth = std::max(maxTitleWidth, size.width); |
| 61 } | 60 } |
| 62 return maxTitleWidth; | 61 return maxTitleWidth; |
| 63 } | 62 } |
| 64 | 63 |
| 65 // Accessor function for testing only. | 64 // Accessor function for testing only. |
| 66 - (NSMenu*)permissionMenu { | 65 - (NSMenu*)permissionMenu { |
| 67 return [menuController_ menu]; | 66 return [menuController_ menu]; |
| 68 } | 67 } |
| 69 | 68 |
| 70 @end | 69 @end |
| OLD | NEW |