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