OLD | NEW |
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" | |
10 #include "base/mac/mac_util.h" | 9 #include "base/mac/mac_util.h" |
11 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
12 #import "base/mac/sdk_forward_declarations.h" | 11 #import "base/mac/sdk_forward_declarations.h" |
13 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
14 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
15 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
16 #include "chrome/app/vector_icons/vector_icons.h" | 15 #include "chrome/app/vector_icons/vector_icons.h" |
17 #include "chrome/browser/command_updater.h" | 16 #include "chrome/browser/command_updater.h" |
18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
(...skipping 27 matching lines...) Expand all Loading... |
47 STOP, | 46 STOP, |
48 RELOAD, | 47 RELOAD, |
49 HOME, | 48 HOME, |
50 SEARCH, | 49 SEARCH, |
51 STAR, | 50 STAR, |
52 NEW_TAB, | 51 NEW_TAB, |
53 TOUCH_BAR_ACTION_COUNT | 52 TOUCH_BAR_ACTION_COUNT |
54 }; | 53 }; |
55 | 54 |
56 // The touch bar's identifier. | 55 // The touch bar's identifier. |
57 NSString* const kBrowserWindowTouchBarId = @"browser-window"; | 56 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = |
| 57 @"BrowserWindowTouchBarId"; |
58 | 58 |
59 // Touch bar items identifiers. | 59 // Touch bar items identifiers. |
60 NSString* const kBackForwardTouchId = @"BACK-FWD"; | 60 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; |
61 NSString* const kReloadOrStopTouchId = @"RELOAD-STOP"; | 61 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; |
62 NSString* const kHomeTouchId = @"HOME"; | 62 const NSTouchBarItemIdentifier kHomeTouchId = @"HomeTouchId"; |
63 NSString* const kSearchTouchId = @"SEARCH"; | 63 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; |
64 NSString* const kStarTouchId = @"BOOKMARK"; | 64 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; |
65 NSString* const kNewTabTouchId = @"NEW-TAB"; | 65 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; |
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 min width of the search button in the touch bar. | 78 // The width of the search button in the touch bar. |
79 const int kSearchBtnMinWidth = 205; | 79 const int kSearchBtnWidthWithHomeBtn = 205; |
| 80 const int kSearchBtnWidthWithoutHomeBtn = 280; |
80 | 81 |
81 // Creates an NSImage from the given VectorIcon. | 82 // Creates an NSImage from the given VectorIcon. |
82 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, | 83 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, |
83 SkColor color = kTouchBarDefaultIconColor) { | 84 SkColor color = kTouchBarDefaultIconColor) { |
84 return NSImageFromImageSkiaWithColorSpace( | 85 return NSImageFromImageSkiaWithColorSpace( |
85 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), | 86 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), |
86 base::mac::GetSRGBColorSpace()); | 87 base::mac::GetSRGBColorSpace()); |
87 } | 88 } |
88 | 89 |
89 // Creates a NSButton for the touch bar. | 90 // Creates a NSButton for the touch bar. |
90 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, | 91 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, |
91 BrowserWindowTouchBar* owner, | 92 BrowserWindowTouchBar* owner, |
92 int command, | 93 int command, |
93 int tooltip_id, | 94 int tooltip_id, |
94 SkColor color = kTouchBarDefaultIconColor) { | 95 SkColor color = kTouchBarDefaultIconColor) { |
95 NSButton* button = | 96 NSButton* button = |
96 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) | 97 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) |
97 target:owner | 98 target:owner |
98 action:@selector(executeCommand:)]; | 99 action:@selector(executeCommand:)]; |
99 button.tag = command; | 100 button.tag = command; |
100 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; | 101 [button setAccessibilityLabel:l10n_util::GetNSString(tooltip_id)]; |
101 return button; | 102 return button; |
102 } | 103 } |
103 | 104 |
104 NSString* GetTouchBarId(NSString* const touch_bar_id) { | |
105 NSString* chrome_bundle_id = | |
106 base::SysUTF8ToNSString(base::mac::BaseBundleID()); | |
107 return [NSString stringWithFormat:@"%@.%@", chrome_bundle_id, touch_bar_id]; | |
108 } | |
109 | |
110 NSString* GetTouchBarItemId(NSString* const touch_bar_id, | |
111 NSString* const item_id) { | |
112 return [NSString | |
113 stringWithFormat:@"%@-%@", GetTouchBarId(touch_bar_id), item_id]; | |
114 } | |
115 | |
116 TouchBarAction TouchBarActionFromCommand(int command) { | 105 TouchBarAction TouchBarActionFromCommand(int command) { |
117 switch (command) { | 106 switch (command) { |
118 case IDC_BACK: | 107 case IDC_BACK: |
119 return TouchBarAction::BACK; | 108 return TouchBarAction::BACK; |
120 case IDC_FORWARD: | 109 case IDC_FORWARD: |
121 return TouchBarAction::FORWARD; | 110 return TouchBarAction::FORWARD; |
122 case IDC_STOP: | 111 case IDC_STOP: |
123 return TouchBarAction::STOP; | 112 return TouchBarAction::STOP; |
124 case IDC_RELOAD: | 113 case IDC_RELOAD: |
125 return TouchBarAction::RELOAD; | 114 return TouchBarAction::RELOAD; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
209 | 198 |
210 return self; | 199 return self; |
211 } | 200 } |
212 | 201 |
213 - (NSTouchBar*)makeTouchBar { | 202 - (NSTouchBar*)makeTouchBar { |
214 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | 203 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) |
215 return nil; | 204 return nil; |
216 | 205 |
217 base::scoped_nsobject<NSTouchBar> touchBar( | 206 base::scoped_nsobject<NSTouchBar> touchBar( |
218 [[NSClassFromString(@"NSTouchBar") alloc] init]); | 207 [[NSClassFromString(@"NSTouchBar") alloc] init]); |
219 NSMutableArray* customIdentifiers = [NSMutableArray arrayWithCapacity:7]; | 208 NSArray* touchBarItemIdentifiers; |
220 NSMutableArray* defaultIdentifiers = [NSMutableArray arrayWithCapacity:6]; | 209 if (showHomeButton_.GetValue()) { |
221 | 210 touchBarItemIdentifiers = @[ |
222 NSArray* touchBarItems = @[ | 211 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, |
223 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId, | 212 kStarTouchId, kNewTabTouchId |
224 kStarTouchId, kNewTabTouchId | 213 ]; |
225 ]; | 214 } else { |
226 | 215 touchBarItemIdentifiers = @[ |
227 for (NSString* item in touchBarItems) { | 216 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, |
228 NSString* itemIdentifier = | 217 kNewTabTouchId |
229 GetTouchBarItemId(kBrowserWindowTouchBarId, item); | 218 ]; |
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]; | |
235 } | 219 } |
236 | 220 |
237 [customIdentifiers addObject:NSTouchBarItemIdentifierFlexibleSpace]; | 221 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; |
238 | 222 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; |
239 [touchBar setCustomizationIdentifier:GetTouchBarId(kBrowserWindowTouchBarId)]; | 223 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; |
240 [touchBar setDefaultItemIdentifiers:defaultIdentifiers]; | |
241 [touchBar setCustomizationAllowedItemIdentifiers:customIdentifiers]; | |
242 [touchBar setDelegate:self]; | 224 [touchBar setDelegate:self]; |
243 | 225 |
244 return touchBar.autorelease(); | 226 return touchBar.autorelease(); |
245 } | 227 } |
246 | 228 |
247 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | 229 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar |
248 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | 230 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { |
249 if (!touchBar) | 231 if (!touchBar) |
250 return nil; | 232 return nil; |
251 | 233 |
252 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | 234 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( |
253 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | 235 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); |
254 if ([identifier hasSuffix:kBackForwardTouchId]) { | 236 if ([identifier isEqualTo:kBackForwardTouchId]) { |
255 [touchBarItem setView:[self backOrForwardTouchBarView]]; | 237 [touchBarItem setView:[self backOrForwardTouchBarView]]; |
256 [touchBarItem setCustomizationLabel: | 238 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { |
257 l10n_util::GetNSString( | |
258 IDS_TOUCH_BAR_BACK_FORWARD_CUSTOMIZATION_LABEL)]; | |
259 } else if ([identifier hasSuffix:kReloadOrStopTouchId]) { | |
260 const gfx::VectorIcon& icon = | 239 const gfx::VectorIcon& icon = |
261 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; | 240 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; |
262 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | 241 int commandId = isPageLoading_ ? IDC_STOP : IDC_RELOAD; |
263 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; | 242 int tooltipId = isPageLoading_ ? IDS_TOOLTIP_STOP : IDS_TOOLTIP_RELOAD; |
264 [touchBarItem | 243 [touchBarItem |
265 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; | 244 setView:CreateTouchBarButton(icon, self, commandId, tooltipId)]; |
266 [touchBarItem setCustomizationLabel: | 245 } else if ([identifier isEqualTo:kHomeTouchId]) { |
267 l10n_util::GetNSString( | |
268 IDS_TOUCH_BAR_STOP_RELOAD_CUSTOMIZATION_LABEL)]; | |
269 } else if ([identifier hasSuffix:kHomeTouchId]) { | |
270 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, | 246 [touchBarItem setView:CreateTouchBarButton(kNavigateHomeIcon, self, |
271 IDC_HOME, IDS_TOOLTIP_HOME)]; | 247 IDC_HOME, IDS_TOOLTIP_HOME)]; |
272 [touchBarItem | 248 } else if ([identifier isEqualTo:kNewTabTouchId]) { |
273 setCustomizationLabel:l10n_util::GetNSString( | |
274 IDS_TOUCH_BAR_HOME_CUSTOMIZATION_LABEL)]; | |
275 } else if ([identifier hasSuffix:kNewTabTouchId]) { | |
276 [touchBarItem | 249 [touchBarItem |
277 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, | 250 setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, IDC_NEW_TAB, |
278 IDS_TOOLTIP_NEW_TAB)]; | 251 IDS_TOOLTIP_NEW_TAB)]; |
279 [touchBarItem | 252 } else if ([identifier isEqualTo:kStarTouchId]) { |
280 setCustomizationLabel:l10n_util::GetNSString( | |
281 IDS_TOUCH_BAR_NEW_TAB_CUSTOMIZATION_LABEL)]; | |
282 } else if ([identifier hasSuffix:kStarTouchId]) { | |
283 const gfx::VectorIcon& icon = | 253 const gfx::VectorIcon& icon = |
284 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; | 254 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; |
285 SkColor iconColor = | 255 SkColor iconColor = |
286 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; | 256 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; |
287 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; | 257 int tooltipId = isStarred_ ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR; |
288 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, | 258 [touchBarItem setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, |
289 tooltipId, iconColor)]; | 259 tooltipId, iconColor)]; |
290 [touchBarItem | 260 } else if ([identifier isEqualTo:kSearchTouchId]) { |
291 setCustomizationLabel:l10n_util::GetNSString( | |
292 IDS_TOUCH_BAR_BOOKMARK_CUSTOMIZATION_LABEL)]; | |
293 } else if ([identifier hasSuffix:kSearchTouchId]) { | |
294 [touchBarItem setView:[self searchTouchBarView]]; | 261 [touchBarItem setView:[self searchTouchBarView]]; |
295 [touchBarItem setCustomizationLabel:l10n_util::GetNSString( | |
296 IDS_TOUCH_BAR_GOOGLE_SEARCH)]; | |
297 } | 262 } |
298 | 263 |
299 return touchBarItem.autorelease(); | 264 return touchBarItem.autorelease(); |
300 } | 265 } |
301 | 266 |
302 - (NSView*)backOrForwardTouchBarView { | 267 - (NSView*)backOrForwardTouchBarView { |
303 NSArray* images = @[ | 268 NSArray* images = @[ |
304 CreateNSImageFromIcon(ui::kBackArrowIcon), | 269 CreateNSImageFromIcon(ui::kBackArrowIcon), |
305 CreateNSImageFromIcon(ui::kForwardArrowIcon) | 270 CreateNSImageFromIcon(ui::kForwardArrowIcon) |
306 ]; | 271 ]; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 image = CreateNSImageFromIcon(ui::kSearchIcon); | 322 image = CreateNSImageFromIcon(ui::kSearchIcon); |
358 } | 323 } |
359 | 324 |
360 NSButton* searchButton = | 325 NSButton* searchButton = |
361 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | 326 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) |
362 image:image | 327 image:image |
363 target:self | 328 target:self |
364 action:@selector(executeCommand:)]; | 329 action:@selector(executeCommand:)]; |
365 searchButton.imageHugsTitle = YES; | 330 searchButton.imageHugsTitle = YES; |
366 searchButton.tag = IDC_FOCUS_LOCATION; | 331 searchButton.tag = IDC_FOCUS_LOCATION; |
367 [searchButton.widthAnchor | 332 int width = showHomeButton_.GetValue() ? kSearchBtnWidthWithHomeBtn |
368 constraintGreaterThanOrEqualToConstant:kSearchBtnMinWidth] | 333 : kSearchBtnWidthWithoutHomeBtn; |
369 .active = YES; | 334 [searchButton.widthAnchor constraintEqualToConstant:width].active = YES; |
370 [searchButton | |
371 setContentHuggingPriority:1.0 | |
372 forOrientation:NSLayoutConstraintOrientationHorizontal]; | |
373 return searchButton; | 335 return searchButton; |
374 } | 336 } |
375 | 337 |
376 - (void)backOrForward:(id)sender { | 338 - (void)backOrForward:(id)sender { |
377 NSSegmentedControl* control = sender; | 339 NSSegmentedControl* control = sender; |
378 int command = | 340 int command = |
379 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; | 341 [control selectedSegment] == kBackSegmentIndex ? IDC_BACK : IDC_FORWARD; |
380 LogTouchBarUMA(command); | 342 LogTouchBarUMA(command); |
381 commandUpdater_->ExecuteCommand(command); | 343 commandUpdater_->ExecuteCommand(command); |
382 } | 344 } |
383 | 345 |
384 - (void)executeCommand:(id)sender { | 346 - (void)executeCommand:(id)sender { |
385 int command = [sender tag]; | 347 int command = [sender tag]; |
386 LogTouchBarUMA(command); | 348 LogTouchBarUMA(command); |
387 commandUpdater_->ExecuteCommand(command); | 349 commandUpdater_->ExecuteCommand(command); |
388 } | 350 } |
389 | 351 |
390 @end | 352 @end |
OLD | NEW |