OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "chrome/browser/ui/cocoa/browser_window_touch_bar.h" | |
6 | |
7 #include "base/mac/mac_util.h" | |
8 #import "base/mac/scoped_nsobject.h" | |
9 #import "base/mac/sdk_forward_declarations.h" | |
10 #include "base/strings/sys_string_conversions.h" | |
11 #include "chrome/app/chrome_command_ids.h" | |
12 #include "chrome/app/vector_icons/vector_icons.h" | |
13 #include "chrome/browser/command_updater.h" | |
14 #include "chrome/browser/search_engines/template_url_service_factory.h" | |
15 #include "chrome/browser/ui/browser.h" | |
16 #include "chrome/browser/ui/browser_command_controller.h" | |
17 #include "chrome/common/chrome_features.h" | |
18 #include "chrome/grit/generated_resources.h" | |
19 #include "components/search_engines/util.h" | |
20 #include "components/toolbar/vector_icons.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/gfx/color_palette.h" | |
23 #include "ui/gfx/color_utils.h" | |
24 #include "ui/gfx/image/image.h" | |
25 #include "ui/gfx/image/image_skia_util_mac.h" | |
26 #include "ui/gfx/paint_vector_icon.h" | |
27 #include "ui/gfx/vector_icons_public.h" | |
28 #include "ui/vector_icons/vector_icons.h" | |
29 | |
30 namespace { | |
31 | |
32 // The touch bar's identifier. | |
33 const NSTouchBarCustomizationIdentifier kBrowserWindowTouchBarId = | |
34 @"BrowserWindowTouchBarId"; | |
35 | |
36 // Touch bar items identifiers. | |
37 const NSTouchBarItemIdentifier kBackForwardTouchId = @"BackForwardTouchId"; | |
38 const NSTouchBarItemIdentifier kReloadOrStopTouchId = @"ReloadOrStopTouchId"; | |
39 const NSTouchBarItemIdentifier kSearchTouchId = @"SearchTouchId"; | |
40 const NSTouchBarItemIdentifier kNewTabTouchId = @"NewTabTouchId"; | |
41 const NSTouchBarItemIdentifier kStarTouchId = @"StarTouchId"; | |
42 | |
43 // The button indexes in the back and forward segment control. | |
44 const int kBackSegmentIndex = 0; | |
45 const int kForwardSegmentIndex = 1; | |
46 | |
47 // Touch bar icon colors values. | |
48 const SkColor kTouchBarDefaultIconColor = SK_ColorWHITE; | |
49 const SkColor kTouchBarStarIconColor = gfx::kGoogleBlue500; | |
50 | |
51 // The size of the touch bar icons. | |
52 const int kTouchBarIconSize = 16; | |
53 | |
54 // The width of the search button in the touch bar. | |
55 const int kTouchBarSearchButtonWidth = 280; | |
56 | |
57 // Creates an NSImage from the given VectorIcon. | |
58 NSImage* CreateNSImageFromId(const gfx::VectorIcon* icon, | |
59 SkColor color = kTouchBarDefaultIconColor) { | |
60 return NSImageFromImageSkiaWithColorSpace( | |
61 gfx::CreateVectorIcon(*icon, kTouchBarIconSize, color), | |
62 base::mac::GetSRGBColorSpace()); | |
63 } | |
64 | |
65 // Creates a NSButton for the touch bar. | |
66 NSButton* CreateTouchBarButton(const gfx::VectorIcon* icon, | |
67 BrowserWindowTouchBar* owner, | |
68 int command, | |
69 SkColor color = kTouchBarDefaultIconColor) { | |
70 NSButton* button = [NSButton buttonWithImage:CreateNSImageFromId(icon, color) | |
71 target:owner | |
72 action:@selector(executeCommand:)]; | |
73 button.tag = command; | |
74 return button; | |
75 } | |
76 | |
77 } // namespace | |
78 | |
79 @interface BrowserWindowTouchBar () { | |
80 // Used to execute commands such as navigating back and forward. | |
81 CommandUpdater* commandUpdater_; // Weak, owned by Browser. | |
82 | |
83 // The browser associated with the touch bar. | |
84 Browser* browser_; // Weak. | |
85 } | |
86 | |
87 // Creates and return the back and forward segmented buttons. | |
88 - (NSView*)backOrForwardTouchBarView; | |
89 | |
90 // Creates and returns the search button. | |
91 - (NSView*)searchTouchBarView; | |
92 @end | |
93 | |
94 @implementation BrowserWindowTouchBar | |
95 | |
96 @synthesize isPageLoading = isPageLoading_; | |
97 @synthesize isStarred = isStarred_; | |
98 | |
99 - (instancetype)initWithBrowser:(Browser*)browser { | |
100 if ((self = [self init])) { | |
101 DCHECK(browser); | |
102 commandUpdater_ = browser->command_controller()->command_updater(); | |
103 browser_ = browser; | |
104 } | |
105 | |
106 return self; | |
107 } | |
108 | |
109 - (NSTouchBar*)makeTouchBar { | |
110 if (!base::FeatureList::IsEnabled(features::kBrowserTouchBar)) | |
111 return nil; | |
112 | |
113 base::scoped_nsobject<NSTouchBar> touchBar( | |
114 [[NSClassFromString(@"NSTouchBar") alloc] init]); | |
115 NSArray* touchBarItemIdentifiers = @[ | |
116 kBackForwardTouchId, kReloadOrStopTouchId, kSearchTouchId, kNewTabTouchId, | |
117 kStarTouchId | |
118 ]; | |
119 [touchBar setCustomizationIdentifier:kBrowserWindowTouchBarId]; | |
120 [touchBar setDefaultItemIdentifiers:touchBarItemIdentifiers]; | |
121 [touchBar setCustomizationAllowedItemIdentifiers:touchBarItemIdentifiers]; | |
122 [touchBar setDelegate:self]; | |
123 | |
124 return touchBar.autorelease(); | |
125 } | |
126 | |
127 - (NSTouchBarItem*)touchBar:(NSTouchBar*)touchBar | |
128 makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier { | |
129 if (!touchBar) | |
130 return nil; | |
131 | |
132 base::scoped_nsobject<NSCustomTouchBarItem> touchBarItem([[NSClassFromString( | |
133 @"NSCustomTouchBarItem") alloc] initWithIdentifier:identifier]); | |
134 if ([identifier isEqualTo:kBackForwardTouchId]) { | |
135 [touchBarItem setView:[self backOrForwardTouchBarView]]; | |
136 } else if ([identifier isEqualTo:kReloadOrStopTouchId]) { | |
137 const gfx::VectorIcon* icon = | |
138 isPageLoading_ ? &kNavigateStopIcon : &kNavigateReloadIcon; | |
139 int command_id = isPageLoading_ ? IDC_STOP : IDC_RELOAD; | |
140 [touchBarItem setView:CreateTouchBarButton(icon, self, command_id)]; | |
141 } else if ([identifier isEqualTo:kNewTabTouchId]) { | |
142 [touchBarItem setView:CreateTouchBarButton(&kNewTabMacTouchbarIcon, self, | |
143 IDC_NEW_TAB)]; | |
144 } else if ([identifier isEqualTo:kStarTouchId]) { | |
145 const gfx::VectorIcon* icon = | |
146 isStarred_ ? &toolbar::kStarActiveIcon : &toolbar::kStarIcon; | |
147 SkColor iconColor = | |
148 isStarred_ ? kTouchBarStarIconColor : kTouchBarDefaultIconColor; | |
149 [touchBarItem | |
150 setView:CreateTouchBarButton(icon, self, IDC_BOOKMARK_PAGE, iconColor)]; | |
151 } else if ([identifier isEqualTo:kSearchTouchId]) { | |
152 [touchBarItem setView:[self searchTouchBarView]]; | |
153 } | |
154 | |
155 return touchBarItem.autorelease(); | |
156 } | |
157 | |
158 - (NSView*)backOrForwardTouchBarView { | |
159 NSArray* images = @[ | |
160 CreateNSImageFromId(&ui::kBackArrowIcon), | |
161 CreateNSImageFromId(&ui::kForwardArrowIcon) | |
162 ]; | |
163 | |
164 NSSegmentedControl* control = [NSSegmentedControl | |
165 segmentedControlWithImages:images | |
166 trackingMode:NSSegmentSwitchTrackingMomentary | |
167 target:self | |
168 action:@selector(backOrForward:)]; | |
169 control.segmentStyle = NSSegmentStyleSeparated; | |
170 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_BACK) | |
171 forSegment:kBackSegmentIndex]; | |
172 [control setEnabled:commandUpdater_->IsCommandEnabled(IDC_FORWARD) | |
173 forSegment:kForwardSegmentIndex]; | |
174 return control; | |
175 } | |
176 | |
177 - (NSView*)searchTouchBarView { | |
178 TemplateURLService* templateUrlService = | |
179 TemplateURLServiceFactory::GetForProfile(browser_->profile()); | |
180 const TemplateURL* default_provider = | |
181 templateUrlService->GetDefaultSearchProvider(); | |
182 BOOL isGoogle = | |
183 default_provider->GetEngineType( | |
184 templateUrlService->search_terms_data()) == SEARCH_ENGINE_GOOGLE; | |
185 | |
186 base::string16 title = l10n_util::GetStringUTF16(IDS_TOUCH_BAR_SEARCH); | |
187 | |
188 gfx::VectorIconId iconId = isGoogle | |
189 ? gfx::VectorIconId::GOOGLE_SEARCH_MAC_TOUCHBAR | |
190 : gfx::VectorIconId::OMNIBOX_SEARCH; | |
Evan Stade
2017/02/17 20:11:39
please use omnibox::kSearchIcon for this (it's the
spqchan
2017/02/17 20:26:10
Done.
| |
191 NSImage* image = NSImageFromImageSkiaWithColorSpace( | |
192 gfx::CreateVectorIcon(iconId, kTouchBarIconSize, | |
193 kTouchBarDefaultIconColor), | |
194 base::mac::GetSRGBColorSpace()); | |
195 | |
196 NSButton* searchButton = | |
197 [NSButton buttonWithTitle:base::SysUTF16ToNSString(title) | |
198 image:image | |
199 target:self | |
200 action:@selector(executeCommand:)]; | |
201 searchButton.imageHugsTitle = YES; | |
202 searchButton.tag = IDC_FOCUS_LOCATION; | |
203 [searchButton.widthAnchor | |
204 constraintEqualToConstant:kTouchBarSearchButtonWidth] | |
205 .active = YES; | |
206 return searchButton; | |
207 } | |
208 | |
209 - (void)backOrForward:(id)sender { | |
210 NSSegmentedControl* control = sender; | |
211 if ([control selectedSegment] == kBackSegmentIndex) | |
212 commandUpdater_->ExecuteCommand(IDC_BACK); | |
213 else | |
214 commandUpdater_->ExecuteCommand(IDC_FORWARD); | |
215 } | |
216 | |
217 - (void)executeCommand:(id)sender { | |
218 commandUpdater_->ExecuteCommand([sender tag]); | |
219 } | |
220 | |
221 @end | |
OLD | NEW |