Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: chrome/browser/ui/cocoa/browser_window_touch_bar.mm

Issue 2814683005: [Mac] Support for Touch Bar Customization (Closed)
Patch Set: Use BaseBundleId() Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/browser_window_touch_bar.h" 5 #import "chrome/browser/ui/cocoa/browser_window_touch_bar.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/mac/foundation_util.h"
9 #include "base/mac/mac_util.h" 10 #include "base/mac/mac_util.h"
10 #import "base/mac/scoped_nsobject.h" 11 #import "base/mac/scoped_nsobject.h"
11 #import "base/mac/sdk_forward_declarations.h" 12 #import "base/mac/sdk_forward_declarations.h"
12 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
13 #include "base/strings/sys_string_conversions.h" 14 #include "base/strings/sys_string_conversions.h"
14 #include "chrome/app/chrome_command_ids.h" 15 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/app/vector_icons/vector_icons.h" 16 #include "chrome/app/vector_icons/vector_icons.h"
16 #include "chrome/browser/command_updater.h" 17 #include "chrome/browser/command_updater.h"
17 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h" 19 #include "chrome/browser/search_engines/template_url_service_factory.h"
(...skipping 27 matching lines...) Expand all
46 STOP, 47 STOP,
47 RELOAD, 48 RELOAD,
48 HOME, 49 HOME,
49 SEARCH, 50 SEARCH,
50 STAR, 51 STAR,
51 NEW_TAB, 52 NEW_TAB,
52 TOUCH_BAR_ACTION_COUNT 53 TOUCH_BAR_ACTION_COUNT
53 }; 54 };
54 55
55 // The touch bar's identifier. 56 // The touch bar's identifier.
56 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = 57 NSString* const kBrowserWindowTouchBarId = @"browser-window";
57 @"BrowserWindowTouchBarId";
58 58
59 // Touch bar items identifiers. 59 // Touch bar items identifiers.
60 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; 60 NSString* const kBackForwardTouchId = @"BACK-FWD";
Robert Sesek 2017/04/17 16:56:23 CamelCase instead of all upper?
spqchan 2017/04/17 17:56:37 It's supposed to be upper, since the format is “co
61 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; 61 NSString* const kReloadOrStopTouchId = @"RELOAD-STOP";
62 const NSTouchBarItemIdentifier kHomeTouchId = @"HomeTouchId"; 62 NSString* const kHomeTouchId = @"HOME";
63 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; 63 NSString* const kSearchTouchId = @"SEARCH";
64 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; 64 NSString* const kStarTouchId = @"BOOKMARK";
65 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; 65 NSString* const kNewTabTouchId = @"NEW-TAB";
66 66
67 // The button indexes in the back and forward segment control. 67 // The button indexes in the back and forward segment control.
68 const int kBackSegmentIndex = 0; 68 const int kBackSegmentIndex = 0;
69 const int kForwardSegmentIndex = 1; 69 const int kForwardSegmentIndex = 1;
70 70
71 // Touch bar icon colors values. 71 // Touch bar icon colors values.
72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; 72 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE;
73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; 73 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500;
74 74
75 // The size of the touch bar icons. 75 // The size of the touch bar icons.
76 const int kTouchBarIconSize = 16; 76 const int kTouchBarIconSize = 16;
77 77
78 // The width of the search button in the touch bar. 78 // The min width of the search button in the touch bar.
79 const int kSearchBtnWidthWithHomeBtn = 205; 79 const int kSearchBtnMinWidth = 205;
80 const int kSearchBtnWidthWithoutHomeBtn = 280;
81 80
82 // Creates an NSImage from the given VectorIcon. 81 // Creates an NSImage from the given VectorIcon.
83 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, 82 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon,
84 SkColor color = kTouchBarDefaultIconColor) { 83 SkColor color = kTouchBarDefaultIconColor) {
85 return NSImageFromImageSkiaWithColorSpace( 84 return NSImageFromImageSkiaWithColorSpace(
86 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), 85 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color),
87 base::mac::GetSRGBColorSpace()); 86 base::mac::GetSRGBColorSpace());
88 } 87 }
89 88
90 // Creates a NSButton for the touch bar. 89 // Creates a NSButton for the touch bar.
91 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, 90 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon,
92 BrowserWindowTouchBar* owner, 91 BrowserWindowTouchBar* owner,
93 int command, 92 int command,
94 int tooltip_id, 93 int tooltip_id,
95 SkColor color = kTouchBarDefaultIconColor) { 94 SkColor color = kTouchBarDefaultIconColor) {
96 NSButton* button = 95 NSButton* button =
97 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) 96 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color)
98 target:owner 97 target:owner
99 action:@selector(executeCommand:)]; 98 action:@selector(executeCommand:)];
100 button.tag = command; 99 button.tag = command;
101 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; 100 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)];
102 return button; 101 return button;
103 } 102 }
104 103
104 NSString* CreateTouchBarId(NSString* const touch_bar_id) {
spqchan 2017/04/14 22:46:57 I'm putting these methods here for now, but I thin
Robert Sesek 2017/04/17 16:56:23 Keeping them local to where they're used is defini
Robert Sesek 2017/04/17 16:56:23 Naming nit: The word "Create" implies strong owner
spqchan 2017/04/17 17:56:37 Done.
spqchan 2017/04/17 17:56:37 I was planning on using it in other files in the f
105 NSString* chrome_bundle_id =
106 base::SysUTF8ToNSString(base::mac::BaseBundleID());
107 return [NSString stringWithFormat:@"%@-%@", chrome_bundle_id, touch_bar_id];
Robert Sesek 2017/04/17 16:56:23 Use dots instead of dashes? And on line 113.
spqchan 2017/04/17 17:56:37 The format is “com.company-name.app-name.alphanume
Robert Sesek 2017/04/17 17:57:54 Is there a dot between the base bundle ID and the
108 }
109
110 NSString* CreateTouchBarItemId(NSString* const touch_bar_id,
111 NSString* const item_id) {
112 return [NSString
113 stringWithFormat:@"%@-%@", CreateTouchBarId(touch_bar_id), item_id];
114 }
115
105 TouchBarAction TouchBarActionFromCommand(int command) { 116 TouchBarAction TouchBarActionFromCommand(int command) {
106 switch (command) { 117 switch (command) {
107 case IDC_BACK: 118 case IDC_BACK:
108 return TouchBarAction::BACK; 119 return TouchBarAction::BACK;
109 case IDC_FORWARD: 120 case IDC_FORWARD:
110 return TouchBarAction::FORWARD; 121 return TouchBarAction::FORWARD;
111 case IDC_STOP: 122 case IDC_STOP:
112 return TouchBarAction::STOP; 123 return TouchBarAction::STOP;
113 case IDC_RELOAD: 124 case IDC_RELOAD:
114 return TouchBarAction::RELOAD; 125 return TouchBarAction::RELOAD;
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 209
199 return self; 210 return self;
200 } 211 }
201 212
202 - (NSTouchBar*)makeTouchBar { 213 - (NSTouchBar*)makeTouchBar {
203 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) 214 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar))
204 return nil; 215 return nil;
205 216
206 base::scoped_nsobject<NSTouchBar> touchBar( 217 base::scoped_nsobject<NSTouchBar> touchBar(
207 [[NSClassFromString(@"NSTouchBar") alloc] init]); 218 [[NSClassFromString(@"NSTouchBar") alloc] init]);
208 NSArray* touchBarItemIdentifiers; 219 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7];
209 if (showHomeButton_.GetValue()) { 220 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6];
210 touchBarItemIdentifiers = @[ 221
211 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, 222 NSArray* touchBarItems = @[
212 kStarTouchId, kNewTabTouchId 223 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId,
213 ]; 224 kStarTouchId, kNewTabTouchId
214 } else { 225 ];
215 touchBarItemIdentifiers = @[ 226
216 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, 227 for (NSString* item in touchBarItems) {
217 kNewTabTouchId 228 NSString* itemIdentifier =
218 ]; 229 CreateTouchBarItemId(kBrowserWindowTouchBarId, item);
230 [customIdentifiers addObject:itemIdentifier];
231
232 // Don't add the home button if it's not shown in the toolbar.
233 if (showHomeButton_.GetValue() || ![item isEqualTo:kHomeTouchId])
234 [defaultIdentifiers addObject:itemIdentifier];
219 } 235 }
220 236
221 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; 237 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace];
222 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; 238
223 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; 239 [touchBar
240 setCustomizationIdentifier:CreateTouchBarId(kBrowserWindowTouchBarId)];
241 [touchBar setDefaultItemIdentifiers:defaultIdentifiers];
242 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers];
224 [touchBar setDelegate:self]; 243 [touchBar setDelegate:self];
225 244
226 return touchBar.autorelease(); 245 return touchBar.autorelease();
227 } 246 }
228 247
229 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar 248 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
230 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { 249 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
231 if (!touchBar) 250 if (!touchBar)
232 return nil; 251 return nil;
233 252
234 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( 253 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString(
235 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); 254 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
236 if ([identifier isEqualTo:kBackForwardTouchId]) { 255 if ([identifier hasSuffix:kBackForwardTouchId]) {
237 [touchBarItem setView:[self backOrForwardTouchBarView]]; 256 [touchBarItem setView:[self backOrForwardTouchBarView]];
238 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { 257 [touchBarItem setCustomizationLabel:
258 l10n_util::GetNSString(
259 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)];
260 } else if ([identifier hasSuffix:kReloadOrStopTouchId]) {
239 const gfx::VectorIcon& icon = 261 const gfx::VectorIcon& icon =
240 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; 262 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon;
241 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; 263 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD;
242 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; 264 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD;
243 [touchBarItem 265 [touchBarItem
244 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; 266 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)];
245 } else if ([identifier isEqualTo:kHomeTouchId]) { 267 [touchBarItem setCustomizationLabel:
268 l10n_util::GetNSString(
269 IDS_TOUCH_BAR_STOP_RELOAD_CUSTOMIZATION_LABEL)];
270 } else if ([identifier hasSuffix:kHomeTouchId]) {
246 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, 271 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self,
247 IDC_HOME, IDS_TOOLTIP_HOME)]; 272 IDC_HOME, IDS_TOOLTIP_HOME)];
248 } else if ([identifier isEqualTo:kNewTabTouchId]) { 273 [touchBarItem
274 setCustomizationLabel:l10n_util::GetNSString(
275 IDS_TOUCH_BAR_HOME_CUSTOMIZATION_LABEL)];
276 } else if ([identifier hasSuffix:kNewTabTouchId]) {
249 [touchBarItem 277 [touchBarItem
250 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, 278 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB,
251 IDS_TOOLTIP_NEW_TAB)]; 279 IDS_TOOLTIP_NEW_TAB)];
252 } else if ([identifier isEqualTo:kStarTouchId]) { 280 [touchBarItem
281 setCustomizationLabel:l10n_util::GetNSString(
282 IDS_TOUCH_BAR_NEW_TAB_CUSTOMIZATION_LABEL)];
283 } else if ([identifier hasSuffix:kStarTouchId]) {
253 const gfx::VectorIcon& icon = 284 const gfx::VectorIcon& icon =
254 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; 285 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon;
255 SkColor iconColor = 286 SkColor iconColor =
256 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; 287 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor;
257 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; 288 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR;
258 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, 289 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE,
259 tooltipId, iconColor)]; 290 tooltipId, iconColor)];
260 } else if ([identifier isEqualTo:kSearchTouchId]) { 291 [touchBarItem
292 setCustomizationLabel:l10n_util::GetNSString(
293 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)];
294 } else if ([identifier hasSuffix:kSearchTouchId]) {
261 [touchBarItem setView:[self searchTouchBarView]]; 295 [touchBarItem setView:[self searchTouchBarView]];
296 [touchBarItem setCustomizationLabel:l10n_util::GetNSString(
297 IDS_TOUCH_BAR_GOOGLE_SEARCH)];
262 } 298 }
263 299
264 return touchBarItem.autorelease(); 300 return touchBarItem.autorelease();
265 } 301 }
266 302
267 - (NSView*)backOrForwardTouchBarView { 303 - (NSView*)backOrForwardTouchBarView {
268 NSArray* images = @[ 304 NSArray* images = @[
269 CreateNSImageFromIcon(ui::kBackArrowIcon), 305 CreateNSImageFromIcon(ui::kBackArrowIcon),
270 CreateNSImageFromIcon(ui::kForwardArrowIcon) 306 CreateNSImageFromIcon(ui::kForwardArrowIcon)
271 ]; 307 ];
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 image = CreateNSImageFromIcon(ui::kSearchIcon); 358 image = CreateNSImageFromIcon(ui::kSearchIcon);
323 } 359 }
324 360
325 NSButton* searchButton = 361 NSButton* searchButton =
326 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) 362 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title)
327 image:image 363 image:image
328 target:self 364 target:self
329 action:@selector(executeCommand:)]; 365 action:@selector(executeCommand:)];
330 searchButton.imageHugsTitle = YES; 366 searchButton.imageHugsTitle = YES;
331 searchButton.tag = IDC_FOCUS_LOCATION; 367 searchButton.tag = IDC_FOCUS_LOCATION;
332 int width = showHomeButton_.GetValue() ? kSearchBtnWidthWithHomeBtn 368 [searchButton.widthAnchor
333 : kSearchBtnWidthWithoutHomeBtn; 369 constraintGreaterThanOrEqualToConstant:kSearchBtnMinWidth]
334 [searchButton.widthAnchor constraintEqualToConstant:width].active = YES; 370 .active = YES;
371 [searchButton
372 setContentHuggingPriority:1.0
373 forOrientation:NSLayoutConstraintOrientationHorizontal];
335 return searchButton; 374 return searchButton;
336 } 375 }
337 376
338 - (void)backOrForward:(id)sender { 377 - (void)backOrForward:(id)sender {
339 NSSegmentedControl* control = sender; 378 NSSegmentedControl* control = sender;
340 int command = 379 int command =
341 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; 380 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD;
342 LogTouchBarUMA(command); 381 LogTouchBarUMA(command);
343 commandUpdater_->ExecuteCommand(command); 382 commandUpdater_->ExecuteCommand(command);
344 } 383 }
345 384
346 - (void)executeCommand:(id)sender { 385 - (void)executeCommand:(id)sender {
347 int command = [sender tag]; 386 int command = [sender tag];
348 LogTouchBarUMA(command); 387 LogTouchBarUMA(command);
349 commandUpdater_->ExecuteCommand(command); 388 commandUpdater_->ExecuteCommand(command);
350 } 389 }
351 390
352 @end 391 @end
OLDNEW
« no previous file with comments | « chrome/browser/app_controller_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698