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/website_settings/website_settings_ui.h" | 8 #include "chrome/browser/ui/website_settings/website_settings_ui.h" |
9 #include "chrome/browser/ui/website_settings/website_settings_utils.h" | 9 #include "chrome/browser/ui/website_settings/website_settings_utils.h" |
10 #import "ui/base/cocoa/menu_controller.h" | 10 #import "ui/base/cocoa/menu_controller.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
37 permissionInfo.setting, | 37 permissionInfo.setting, |
38 permissionInfo.default_setting, | 38 permissionInfo.default_setting, |
39 permissionInfo.source); | 39 permissionInfo.source); |
40 [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)]; | 40 [titleItem setTitle:base::SysUTF16ToNSString(buttonTitle)]; |
41 [[self cell] setUsesItemFromMenu:NO]; | 41 [[self cell] setUsesItemFromMenu:NO]; |
42 [[self cell] setMenuItem:titleItem.get()]; | 42 [[self cell] setMenuItem:titleItem.get()]; |
43 // Although the frame is reset, below, this sizes the cell properly. | 43 // Although the frame is reset, below, this sizes the cell properly. |
44 [self sizeToFit]; | 44 [self sizeToFit]; |
45 | 45 |
46 // Size the button to just fit the visible title - not all of its items. | 46 // Size the button to just fit the visible title - not all of its items. |
47 [self setFrameSize:[self sizeForTitle:[self title]]]; | 47 [self setFrameSize:[PermissionSelectorButton sizeForTitle:[self title] |
48 forButton:self]]; | |
48 } | 49 } |
49 return self; | 50 return self; |
50 } | 51 } |
51 | 52 |
52 // Determine the size of a popup button with the given title. | 53 + (NSSize)sizeForTitle:(NSString*)title forButton:(NSButton*)button { |
groby-ooo-7-16
2014/04/18 18:18:18
If you can at all, I think I'd move that onto the
leng
2014/04/18 22:48:46
I'll move this to a shared location in a subsequen
| |
53 - (NSSize)sizeForTitle:(NSString*)title { | 54 NSDictionary* textAttributes = @{NSFontAttributeName : [button font]}; |
54 NSDictionary* textAttributes = @{NSFontAttributeName : [self font]}; | |
55 NSSize titleSize = [title sizeWithAttributes:textAttributes]; | 55 NSSize titleSize = [title sizeWithAttributes:textAttributes]; |
56 | 56 |
57 NSRect frame = [self frame]; | 57 NSRect frame = [button frame]; |
58 NSRect titleRect = [[self cell] titleRectForBounds:frame]; | 58 NSRect titleRect = [[button cell] titleRectForBounds:frame]; |
59 CGFloat width = titleSize.width + NSWidth(frame) - NSWidth(titleRect); | 59 CGFloat width = titleSize.width + NSWidth(frame) - NSWidth(titleRect); |
60 | 60 |
61 return NSMakeSize(width + kPermissionButtonTitleRightPadding, | 61 return NSMakeSize(width + kPermissionButtonTitleRightPadding, |
62 NSHeight(frame)); | 62 NSHeight(frame)); |
63 } | 63 } |
64 | 64 |
65 - (CGFloat)maxTitleWidthWithDefaultSetting:(ContentSetting)defaultSetting { | 65 - (CGFloat)maxTitleWidthWithDefaultSetting:(ContentSetting)defaultSetting { |
66 // Determine the largest possible size for this button. | 66 // Determine the largest possible size for this button. |
67 CGFloat maxTitleWidth = 0; | 67 CGFloat maxTitleWidth = 0; |
68 for (NSMenuItem* item in [self itemArray]) { | 68 for (NSMenuItem* item in [self itemArray]) { |
69 base::string16 title = WebsiteSettingsUI::PermissionActionToUIString( | 69 base::string16 title = WebsiteSettingsUI::PermissionActionToUIString( |
70 static_cast<ContentSetting>([item tag]), | 70 static_cast<ContentSetting>([item tag]), |
71 defaultSetting, | 71 defaultSetting, |
72 content_settings::SETTING_SOURCE_USER); | 72 content_settings::SETTING_SOURCE_USER); |
73 NSSize size = [self sizeForTitle:base::SysUTF16ToNSString(title)]; | 73 NSSize size = |
74 [PermissionSelectorButton sizeForTitle:base::SysUTF16ToNSString(title) | |
75 forButton:self]; | |
74 maxTitleWidth = std::max(maxTitleWidth, size.width); | 76 maxTitleWidth = std::max(maxTitleWidth, size.width); |
75 } | 77 } |
76 return maxTitleWidth; | 78 return maxTitleWidth; |
77 } | 79 } |
78 | 80 |
79 // Accessor function for testing only. | 81 // Accessor function for testing only. |
80 - (NSMenu*)permissionMenu { | 82 - (NSMenu*)permissionMenu { |
81 return [menuController_ menu]; | 83 return [menuController_ menu]; |
82 } | 84 } |
83 | 85 |
84 @end | 86 @end |
OLD | NEW |