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

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

Issue 2743633005: [Mac] Add Home button to Default Touch Bar (Closed)
Patch Set: Add comments Created 3 years, 9 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
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>
8
7 #include "base/mac/mac_util.h" 9 #include "base/mac/mac_util.h"
8 #import "base/mac/scoped_nsobject.h" 10 #import "base/mac/scoped_nsobject.h"
9 #import "base/mac/sdk_forward_declarations.h" 11 #import "base/mac/sdk_forward_declarations.h"
10 #include "base/strings/sys_string_conversions.h" 12 #include "base/strings/sys_string_conversions.h"
11 #include "chrome/app/chrome_command_ids.h" 13 #include "chrome/app/chrome_command_ids.h"
12 #include "chrome/app/vector_icons/vector_icons.h" 14 #include "chrome/app/vector_icons/vector_icons.h"
13 #include "chrome/browser/command_updater.h" 15 #include "chrome/browser/command_updater.h"
16 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/search_engines/template_url_service_factory.h" 17 #include "chrome/browser/search_engines/template_url_service_factory.h"
15 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_command_controller.h" 19 #include "chrome/browser/ui/browser_command_controller.h"
20 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
17 #include "chrome/common/chrome_features.h" 21 #include "chrome/common/chrome_features.h"
22 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/generated_resources.h" 23 #include "chrome/grit/generated_resources.h"
19 #include "components/omnibox/browser/vector_icons.h" 24 #include "components/omnibox/browser/vector_icons.h"
25 #include "components/prefs/pref_member.h"
20 #include "components/search_engines/util.h" 26 #include "components/search_engines/util.h"
21 #include "components/toolbar/vector_icons.h" 27 #include "components/toolbar/vector_icons.h"
22 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/color_palette.h" 29 #include "ui/gfx/color_palette.h"
24 #include "ui/gfx/color_utils.h" 30 #include "ui/gfx/color_utils.h"
25 #include "ui/gfx/image/image.h" 31 #include "ui/gfx/image/image.h"
26 #include "ui/gfx/image/image_skia_util_mac.h" 32 #include "ui/gfx/image/image_skia_util_mac.h"
27 #include "ui/gfx/paint_vector_icon.h" 33 #include "ui/gfx/paint_vector_icon.h"
28 #include "ui/gfx/vector_icons_public.h" 34 #include "ui/gfx/vector_icons_public.h"
29 #include "ui/vector_icons/vector_icons.h" 35 #include "ui/vector_icons/vector_icons.h"
30 36
31 namespace { 37 namespace {
32 38
33 // The touch bar's identifier. 39 // The touch bar's identifier.
34 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = 40 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId =
35 @"BrowserWindowTouchBarId"; 41 @"BrowserWindowTouchBarId";
36 42
37 // Touch bar items identifiers. 43 // Touch bar items identifiers.
38 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; 44 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId";
39 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; 45 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId";
46 const NSTouchBarItemIdentifier kHomeTouchId = @"HomeTouchId";
40 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; 47 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId";
41 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; 48 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId";
42 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; 49 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId";
43 50
44 // The button indexes in the back and forward segment control. 51 // The button indexes in the back and forward segment control.
45 const int kBackSegmentIndex = 0; 52 const int kBackSegmentIndex = 0;
46 const int kForwardSegmentIndex = 1; 53 const int kForwardSegmentIndex = 1;
47 54
48 // Touch bar icon colors values. 55 // Touch bar icon colors values.
49 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; 56 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE;
50 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500; 57 const SkColor kTouchBarStarActiveColor = gfx::kGoogleBlue500;
51 58
52 // The size of the touch bar icons. 59 // The size of the touch bar icons.
53 const int kTouchBarIconSize = 16; 60 const int kTouchBarIconSize = 16;
54 61
55 // The width of the search button in the touch bar. 62 // The width of the search button in the touch bar.
56 const int kTouchBarSearchButtonWidth = 280; 63 const int kSearchBtnWidthWithHomeBtn = 205;
64 const int kSearchBtnWidthWithoutHomeBtn = 280;
57 65
58 // Creates an NSImage from the given VectorIcon. 66 // Creates an NSImage from the given VectorIcon.
59 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon, 67 NSImage* CreateNSImageFromIcon(const gfx::VectorIcon& icon,
60 SkColor color = kTouchBarDefaultIconColor) { 68 SkColor color = kTouchBarDefaultIconColor) {
61 return NSImageFromImageSkiaWithColorSpace( 69 return NSImageFromImageSkiaWithColorSpace(
62 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color), 70 gfx::CreateVectorIcon(icon, kTouchBarIconSize, color),
63 base::mac::GetSRGBColorSpace()); 71 base::mac::GetSRGBColorSpace());
64 } 72 }
65 73
66 // Creates a NSButton for the touch bar. 74 // Creates a NSButton for the touch bar.
67 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon, 75 NSButton* CreateTouchBarButton(const gfx::VectorIcon& icon,
68 BrowserWindowTouchBar* owner, 76 BrowserWindowTouchBar* owner,
69 int command, 77 int command,
70 SkColor color = kTouchBarDefaultIconColor) { 78 SkColor color = kTouchBarDefaultIconColor) {
71 NSButton* button = 79 NSButton* button =
72 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color) 80 [NSButton buttonWithImage:CreateNSImageFromIcon(icon, color)
73 target:owner 81 target:owner
74 action:@selector(executeCommand:)]; 82 action:@selector(executeCommand:)];
75 button.tag = command; 83 button.tag = command;
76 return button; 84 return button;
77 } 85 }
78 86
87 // A class registered for C++ notifications. This is used to detect changes in
88 // the home button preferences and update the Touch Bar.
89 class HomePrefNotificationBridge {
90 public:
91 explicit HomePrefNotificationBridge(BrowserWindowController* bwc)
92 : bwc_(bwc) {}
93
94 ~HomePrefNotificationBridge() {}
95
96 void UpdateTouchBar() { [bwc_ refreshTouchBar]; }
Robert Sesek 2017/03/10 17:39:56 I don't understand why refreshTouchBar needs to ex
spqchan 2017/03/10 18:04:27 In order to update the touch bar, the first respon
Robert Sesek 2017/03/10 21:23:58 You're right, that's what the docs say to do. That
spqchan 2017/03/10 22:50:12 I was surprised too when I found out. It's weird
97
98 private:
99 BrowserWindowController* bwc_; // Weak.
100
101 DISALLOW_COPY_AND_ASSIGN(HomePrefNotificationBridge);
102 };
103
79 } // namespace 104 } // namespace
80 105
81 @interface BrowserWindowTouchBar () { 106 @interface BrowserWindowTouchBar () {
82 // Used to execute commands such as navigating back and forward. 107 // Used to execute commands such as navigating back and forward.
83 CommandUpdater* commandUpdater_; // Weak, owned by Browser. 108 CommandUpdater* commandUpdater_; // Weak, owned by Browser.
84 109
85 // The browser associated with the touch bar. 110 // The browser associated with the touch bar.
86 Browser* browser_; // Weak. 111 Browser* browser_; // Weak.
112
113 BrowserWindowController* bwc_; // Weak, own us.
114
115 // Used to monitor the optional home button pref.
116 BooleanPrefMember showHomeButton_;
117
118 // Used to receive and handle notifications for the home button pref.
119 std::unique_ptr<HomePrefNotificationBridge> notificationBridge_;
87 } 120 }
88 121
89 // Creates and return the back and forward segmented buttons. 122 // Creates and return the back and forward segmented buttons.
90 - (NSView*)backOrForwardTouchBarView; 123 - (NSView*)backOrForwardTouchBarView;
91 124
92 // Creates and returns the search button. 125 // Creates and returns the search button.
93 - (NSView*)searchTouchBarView; 126 - (NSView*)searchTouchBarView;
94 @end 127 @end
95 128
96 @implementation BrowserWindowTouchBar 129 @implementation BrowserWindowTouchBar
97 130
98 @synthesize isPageLoading = isPageLoading_; 131 @synthesize isPageLoading = isPageLoading_;
99 @synthesize isStarred = isStarred_; 132 @synthesize isStarred = isStarred_;
100 133
101 - (instancetype)initWithBrowser:(Browser*)browser { 134 - (instancetype)initWithBrowser:(Browser*)browser
135 browserWindowController:(BrowserWindowController*)bwc {
102 if ((self = [self init])) { 136 if ((self = [self init])) {
103 DCHECK(browser); 137 DCHECK(browser);
104 commandUpdater_ = browser->command_controller()->command_updater(); 138 commandUpdater_ = browser->command_controller()->command_updater();
105 browser_ = browser; 139 browser_ = browser;
140 bwc_ = bwc;
141
142 notificationBridge_.reset(new HomePrefNotificationBridge(bwc_));
143 PrefService* prefs = browser->profile()->GetPrefs();
144 showHomeButton_.Init(
145 prefs::kShowHomeButton, prefs,
146 base::Bind(&HomePrefNotificationBridge::UpdateTouchBar,
147 base::Unretained(notificationBridge_.get())));
106 } 148 }
107 149
108 return self; 150 return self;
109 } 151 }
110 152
111 - (NSTouchBar*)makeTouchBar { 153 - (NSTouchBar*)makeTouchBar {
112 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) 154 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar))
113 return nil; 155 return nil;
114 156
115 base::scoped_nsobject<NSTouchBar> touchBar( 157 base::scoped_nsobject<NSTouchBar> touchBar(
116 [[NSClassFromString(@"NSTouchBar") alloc] init]); 158 [[NSClassFromString(@"NSTouchBar") alloc] init]);
117 NSArray* touchBarItemIdentifiers = @[ 159 NSArray* touchBarItemIdentifiers;
118 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId, 160 if (showHomeButton_.GetValue()) {
119 kNewTabTouchId 161 touchBarItemIdentifiers = @[
120 ]; 162 kBackForwardTouchId, kReloadOrStopTouchId, kHomeTouchId, kSearchTouchId,
163 kStarTouchId, kNewTabTouchId
164 ];
165 } else {
166 touchBarItemIdentifiers = @[
167 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kStarTouchId,
168 kNewTabTouchId
169 ];
170 }
171
121 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; 172 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId];
122 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; 173 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers];
123 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; 174 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers];
124 [touchBar setDelegate:self]; 175 [touchBar setDelegate:self];
125 176
126 return touchBar.autorelease(); 177 return touchBar.autorelease();
127 } 178 }
128 179
129 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar 180 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar
130 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { 181 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier {
131 if (!touchBar) 182 if (!touchBar)
132 return nil; 183 return nil;
133 184
134 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( 185 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString(
135 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); 186 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]);
136 if ([identifier isEqualTo:kBackForwardTouchId]) { 187 if ([identifier isEqualTo:kBackForwardTouchId]) {
137 [touchBarItem setView:[self backOrForwardTouchBarView]]; 188 [touchBarItem setView:[self backOrForwardTouchBarView]];
138 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { 189 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) {
139 const gfx::VectorIcon& icon = 190 const gfx::VectorIcon& icon =
140 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon; 191 isPageLoading_ ? kNavigateStopIcon : kNavigateReloadIcon;
141 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; 192 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD;
142 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; 193 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)];
194 } else if ([identifier isEqualTo:kHomeTouchId]) {
195 [touchBarItem
196 setView:CreateTouchBarButton(kNavigateHomeIcon, self, IDC_HOME)];
143 } else if ([identifier isEqualTo:kNewTabTouchId]) { 197 } else if ([identifier isEqualTo:kNewTabTouchId]) {
144 [touchBarItem setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self, 198 [touchBarItem setView:CreateTouchBarButton(kNewTabMacTouchbarIcon, self,
145 IDC_NEW_TAB)]; 199 IDC_NEW_TAB)];
146 } else if ([identifier isEqualTo:kStarTouchId]) { 200 } else if ([identifier isEqualTo:kStarTouchId]) {
147 const gfx::VectorIcon& icon = 201 const gfx::VectorIcon& icon =
148 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon; 202 isStarred_ ? toolbar::kStarActiveIcon : toolbar::kStarIcon;
149 SkColor iconColor = 203 SkColor iconColor =
150 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor; 204 isStarred_ ? kTouchBarStarActiveColor : kTouchBarDefaultIconColor;
151 [touchBarItem 205 [touchBarItem
152 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; 206 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)];
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 image = CreateNSImageFromIcon(omnibox::kSearchIcon); 254 image = CreateNSImageFromIcon(omnibox::kSearchIcon);
201 } 255 }
202 256
203 NSButton* searchButton = 257 NSButton* searchButton =
204 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) 258 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title)
205 image:image 259 image:image
206 target:self 260 target:self
207 action:@selector(executeCommand:)]; 261 action:@selector(executeCommand:)];
208 searchButton.imageHugsTitle = YES; 262 searchButton.imageHugsTitle = YES;
209 searchButton.tag = IDC_FOCUS_LOCATION; 263 searchButton.tag = IDC_FOCUS_LOCATION;
210 [searchButton.widthAnchor 264 int width = showHomeButton_.GetValue() ? kSearchBtnWidthWithHomeBtn
211 constraintEqualToConstant:kTouchBarSearchButtonWidth] 265 : kSearchBtnWidthWithoutHomeBtn;
212 .active = YES; 266 [searchButton.widthAnchor constraintEqualToConstant:width].active = YES;
213 return searchButton; 267 return searchButton;
214 } 268 }
215 269
216 - (void)backOrForward:(id)sender { 270 - (void)backOrForward:(id)sender {
217 NSSegmentedControl* control = sender; 271 NSSegmentedControl* control = sender;
218 if ([control selectedSegment] == kBackSegmentIndex) 272 if ([control selectedSegment] == kBackSegmentIndex)
219 commandUpdater_->ExecuteCommand(IDC_BACK); 273 commandUpdater_->ExecuteCommand(IDC_BACK);
220 else 274 else
221 commandUpdater_->ExecuteCommand(IDC_FORWARD); 275 commandUpdater_->ExecuteCommand(IDC_FORWARD);
222 } 276 }
223 277
224 - (void)executeCommand:(id)sender { 278 - (void)executeCommand:(id)sender {
225 commandUpdater_->ExecuteCommand([sender tag]); 279 commandUpdater_->ExecuteCommand([sender tag]);
226 } 280 }
227 281
228 @end 282 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698