| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/chooser_content_view.h" | |
| 6 | |
| 7 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" | |
| 8 #include "chrome/grit/generated_resources.h" | |
| 9 #import "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTw
eaker.h" | |
| 10 #import "ui/base/cocoa/controls/hyperlink_button_cell.h" | |
| 11 #include "ui/base/l10n/l10n_util.h" | |
| 12 #include "ui/base/l10n/l10n_util_mac.h" | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 // Chooser width. | |
| 17 const CGFloat kChooserWidth = 320.0f; | |
| 18 | |
| 19 // Chooser height. | |
| 20 const CGFloat kChooserHeight = 280.0f; | |
| 21 | |
| 22 // Distance between the chooser border and the view that is closest to the | |
| 23 // border. | |
| 24 const CGFloat kMarginX = 20.0f; | |
| 25 const CGFloat kMarginY = 20.0f; | |
| 26 | |
| 27 // Distance between two views inside the chooser. | |
| 28 const CGFloat kHorizontalPadding = 10.0f; | |
| 29 const CGFloat kVerticalPadding = 10.0f; | |
| 30 | |
| 31 // Separator alpha value. | |
| 32 const CGFloat kSeparatorAlphaValue = 0.6f; | |
| 33 | |
| 34 // Separator height. | |
| 35 const CGFloat kSeparatorHeight = 1.0f; | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 @implementation ChooserContentView | |
| 40 | |
| 41 - (instancetype)initWithChooserTitle:(NSString*)chooserTitle { | |
| 42 // ------------------------------------ | |
| 43 // | Chooser title | | |
| 44 // | -------------------------------- | | |
| 45 // | | option 0 | | | |
| 46 // | | option 1 | | | |
| 47 // | | option 2 | | | |
| 48 // | | | | | |
| 49 // | | | | | |
| 50 // | | | | | |
| 51 // | -------------------------------- | | |
| 52 // | [ Connect ] [ Cancel ] | | |
| 53 // |----------------------------------| | |
| 54 // | Not seeing your device? Get help | | |
| 55 // ------------------------------------ | |
| 56 | |
| 57 // Determine the dimensions of the chooser. | |
| 58 // Once the height and width are set, the buttons and permission menus can | |
| 59 // be laid out correctly. | |
| 60 NSRect chooserFrame = NSMakeRect(0, 0, kChooserWidth, kChooserHeight); | |
| 61 | |
| 62 if ((self = [super initWithFrame:chooserFrame])) { | |
| 63 // Create the views. | |
| 64 // Title. | |
| 65 titleView_ = [self createChooserTitle:chooserTitle]; | |
| 66 CGFloat titleHeight = NSHeight([titleView_ frame]); | |
| 67 | |
| 68 // Connect button. | |
| 69 connectButton_ = [self createConnectButton]; | |
| 70 CGFloat connectButtonWidth = NSWidth([connectButton_ frame]); | |
| 71 CGFloat connectButtonHeight = NSHeight([connectButton_ frame]); | |
| 72 | |
| 73 // Cancel button. | |
| 74 cancelButton_ = [self createCancelButton]; | |
| 75 CGFloat cancelButtonWidth = NSWidth([cancelButton_ frame]); | |
| 76 | |
| 77 // Separator. | |
| 78 separator_ = [self createSeparator]; | |
| 79 | |
| 80 // Message. | |
| 81 message_ = [self createMessage]; | |
| 82 CGFloat messageWidth = NSWidth([message_ frame]); | |
| 83 CGFloat messageHeight = NSHeight([message_ frame]); | |
| 84 | |
| 85 // Help button. | |
| 86 helpButton_ = [self createHelpButton]; | |
| 87 | |
| 88 // ScollView embedding with TableView. | |
| 89 CGFloat scrollViewWidth = kChooserWidth - 2 * kMarginX; | |
| 90 CGFloat scrollViewHeight = kChooserHeight - 2 * kMarginY - | |
| 91 4 * kVerticalPadding - titleHeight - | |
| 92 connectButtonHeight - messageHeight; | |
| 93 NSRect scrollFrame = NSMakeRect( | |
| 94 kMarginX, | |
| 95 kMarginY + messageHeight + 3 * kVerticalPadding + connectButtonHeight, | |
| 96 scrollViewWidth, scrollViewHeight); | |
| 97 scrollView_.reset([[NSScrollView alloc] initWithFrame:scrollFrame]); | |
| 98 [scrollView_ setBorderType:NSBezelBorder]; | |
| 99 [scrollView_ setHasVerticalScroller:YES]; | |
| 100 [scrollView_ setHasHorizontalScroller:YES]; | |
| 101 [scrollView_ setAutohidesScrollers:YES]; | |
| 102 | |
| 103 // TableView. | |
| 104 tableView_.reset([[NSTableView alloc] initWithFrame:NSZeroRect]); | |
| 105 tableColumn_.reset([[NSTableColumn alloc] initWithIdentifier:@""]); | |
| 106 [tableColumn_ setWidth:(scrollViewWidth - kMarginX)]; | |
| 107 [tableView_ addTableColumn:tableColumn_]; | |
| 108 // Make the column title invisible. | |
| 109 [tableView_ setHeaderView:nil]; | |
| 110 [tableView_ setFocusRingType:NSFocusRingTypeNone]; | |
| 111 | |
| 112 // Lay out the views. | |
| 113 // Title. | |
| 114 CGFloat titleOriginX = kMarginX; | |
| 115 CGFloat titleOriginY = kChooserHeight - kMarginY - titleHeight; | |
| 116 [titleView_ setFrameOrigin:NSMakePoint(titleOriginX, titleOriginY)]; | |
| 117 [self addSubview:titleView_]; | |
| 118 | |
| 119 // ScollView. | |
| 120 [scrollView_ setDocumentView:tableView_]; | |
| 121 [self addSubview:scrollView_]; | |
| 122 | |
| 123 // Connect button. | |
| 124 CGFloat connectButtonOriginX = kChooserWidth - kMarginX - | |
| 125 kHorizontalPadding - connectButtonWidth - | |
| 126 cancelButtonWidth; | |
| 127 CGFloat connectButtonOriginY = | |
| 128 kMarginY + messageHeight + 2 * kVerticalPadding; | |
| 129 [connectButton_ | |
| 130 setFrameOrigin:NSMakePoint(connectButtonOriginX, connectButtonOriginY)]; | |
| 131 [connectButton_ setEnabled:NO]; | |
| 132 [self addSubview:connectButton_]; | |
| 133 | |
| 134 // Cancel button. | |
| 135 CGFloat cancelButtonOriginX = kChooserWidth - kMarginX - cancelButtonWidth; | |
| 136 CGFloat cancelButtonOriginY = connectButtonOriginY; | |
| 137 [cancelButton_ | |
| 138 setFrameOrigin:NSMakePoint(cancelButtonOriginX, cancelButtonOriginY)]; | |
| 139 [self addSubview:cancelButton_]; | |
| 140 | |
| 141 // Separator. | |
| 142 CGFloat separatorOriginX = 0.0f; | |
| 143 CGFloat separatorOriginY = kMarginY + messageHeight + kVerticalPadding; | |
| 144 [separator_ setFrameOrigin:NSMakePoint(separatorOriginX, separatorOriginY)]; | |
| 145 [self addSubview:separator_]; | |
| 146 | |
| 147 // Message. | |
| 148 CGFloat messageOriginX = kMarginX; | |
| 149 CGFloat messageOriginY = kMarginY; | |
| 150 [message_ setFrameOrigin:NSMakePoint(messageOriginX, messageOriginY)]; | |
| 151 [self addSubview:message_]; | |
| 152 | |
| 153 // Help button. | |
| 154 CGFloat helpButtonOriginX = | |
| 155 kMarginX + messageWidth - kHorizontalPadding / 2; | |
| 156 CGFloat helpButtonOriginY = kMarginY; | |
| 157 [helpButton_ | |
| 158 setFrameOrigin:NSMakePoint(helpButtonOriginX, helpButtonOriginY)]; | |
| 159 [self addSubview:helpButton_]; | |
| 160 } | |
| 161 | |
| 162 return self; | |
| 163 } | |
| 164 | |
| 165 - (base::scoped_nsobject<NSTextField>)createChooserTitle:(NSString*)title { | |
| 166 base::scoped_nsobject<NSTextField> titleView( | |
| 167 [[NSTextField alloc] initWithFrame:NSZeroRect]); | |
| 168 [titleView setDrawsBackground:NO]; | |
| 169 [titleView setBezeled:NO]; | |
| 170 [titleView setEditable:NO]; | |
| 171 [titleView setSelectable:NO]; | |
| 172 [titleView setStringValue:title]; | |
| 173 [titleView setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; | |
| 174 // The height is arbitrary as it will be adjusted later. | |
| 175 [titleView setFrameSize:NSMakeSize(kChooserWidth - 2 * kMarginX, 0.0f)]; | |
| 176 [GTMUILocalizerAndLayoutTweaker sizeToFitFixedWidthTextField:titleView]; | |
| 177 return titleView; | |
| 178 } | |
| 179 | |
| 180 - (base::scoped_nsobject<NSButton>)createButtonWithTitle:(NSString*)title { | |
| 181 base::scoped_nsobject<NSButton> button( | |
| 182 [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]); | |
| 183 [button setButtonType:NSMomentaryPushInButton]; | |
| 184 [button setTitle:title]; | |
| 185 [button sizeToFit]; | |
| 186 return button; | |
| 187 } | |
| 188 | |
| 189 - (base::scoped_nsobject<NSButton>)createConnectButton { | |
| 190 NSString* connectTitle = | |
| 191 l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_CONNECT_BUTTON_TEXT); | |
| 192 return [self createButtonWithTitle:connectTitle]; | |
| 193 } | |
| 194 | |
| 195 - (base::scoped_nsobject<NSButton>)createCancelButton { | |
| 196 NSString* cancelTitle = | |
| 197 l10n_util::GetNSString(IDS_CHOOSER_BUBBLE_CANCEL_BUTTON_TEXT); | |
| 198 return [self createButtonWithTitle:cancelTitle]; | |
| 199 } | |
| 200 | |
| 201 - (base::scoped_nsobject<NSBox>)createSeparator { | |
| 202 base::scoped_nsobject<NSBox> spacer([[NSBox alloc] initWithFrame:NSZeroRect]); | |
| 203 [spacer setBoxType:NSBoxSeparator]; | |
| 204 [spacer setBorderType:NSLineBorder]; | |
| 205 [spacer setAlphaValue:kSeparatorAlphaValue]; | |
| 206 [spacer setFrameSize:NSMakeSize(kChooserWidth, kSeparatorHeight)]; | |
| 207 return spacer; | |
| 208 } | |
| 209 | |
| 210 - (base::scoped_nsobject<NSTextField>)createMessage { | |
| 211 base::scoped_nsobject<NSTextField> messageView( | |
| 212 [[NSTextField alloc] initWithFrame:NSZeroRect]); | |
| 213 [messageView setDrawsBackground:NO]; | |
| 214 [messageView setBezeled:NO]; | |
| 215 [messageView setEditable:NO]; | |
| 216 [messageView setSelectable:NO]; | |
| 217 [messageView | |
| 218 setStringValue:l10n_util::GetNSStringF(IDS_CHOOSER_BUBBLE_FOOTNOTE_TEXT, | |
| 219 base::string16())]; | |
| 220 [messageView setFont:[NSFont systemFontOfSize:[NSFont systemFontSize]]]; | |
| 221 [messageView sizeToFit]; | |
| 222 return messageView; | |
| 223 } | |
| 224 | |
| 225 - (base::scoped_nsobject<NSButton>)createHelpButton { | |
| 226 base::scoped_nsobject<NSButton> button( | |
| 227 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 228 base::scoped_nsobject<HyperlinkButtonCell> cell([[HyperlinkButtonCell alloc] | |
| 229 initTextCell:l10n_util::GetNSString( | |
| 230 IDS_CHOOSER_BUBBLE_GET_HELP_LINK_TEXT)]); | |
| 231 [button setCell:cell.get()]; | |
| 232 [button sizeToFit]; | |
| 233 return button; | |
| 234 } | |
| 235 | |
| 236 - (NSTableView*)tableView { | |
| 237 return tableView_.get(); | |
| 238 } | |
| 239 | |
| 240 - (NSButton*)connectButton { | |
| 241 return connectButton_.get(); | |
| 242 } | |
| 243 | |
| 244 - (NSButton*)cancelButton { | |
| 245 return cancelButton_.get(); | |
| 246 } | |
| 247 | |
| 248 - (NSButton*)helpButton { | |
| 249 return helpButton_.get(); | |
| 250 } | |
| 251 | |
| 252 @end | |
| OLD | NEW |