OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "chrome/browser/ui/views/toolbar/app_menu.h" | 5 #include "chrome/browser/ui/views/toolbar/app_menu.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
(...skipping 28 matching lines...) Expand all Loading... | |
39 #include "content/public/browser/notification_source.h" | 39 #include "content/public/browser/notification_source.h" |
40 #include "content/public/browser/notification_types.h" | 40 #include "content/public/browser/notification_types.h" |
41 #include "content/public/browser/user_metrics.h" | 41 #include "content/public/browser/user_metrics.h" |
42 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
43 #include "extensions/common/feature_switch.h" | 43 #include "extensions/common/feature_switch.h" |
44 #include "grit/theme_resources.h" | 44 #include "grit/theme_resources.h" |
45 #include "third_party/skia/include/core/SkCanvas.h" | 45 #include "third_party/skia/include/core/SkCanvas.h" |
46 #include "third_party/skia/include/core/SkPaint.h" | 46 #include "third_party/skia/include/core/SkPaint.h" |
47 #include "ui/base/l10n/l10n_util.h" | 47 #include "ui/base/l10n/l10n_util.h" |
48 #include "ui/base/layout.h" | 48 #include "ui/base/layout.h" |
49 #include "ui/base/material_design/material_design_controller.h" | |
49 #include "ui/base/resource/resource_bundle.h" | 50 #include "ui/base/resource/resource_bundle.h" |
50 #include "ui/gfx/canvas.h" | 51 #include "ui/gfx/canvas.h" |
51 #include "ui/gfx/font_list.h" | 52 #include "ui/gfx/font_list.h" |
52 #include "ui/gfx/image/image.h" | 53 #include "ui/gfx/image/image.h" |
53 #include "ui/gfx/image/image_skia_source.h" | 54 #include "ui/gfx/image/image_skia_source.h" |
54 #include "ui/gfx/skia_util.h" | 55 #include "ui/gfx/skia_util.h" |
55 #include "ui/gfx/text_utils.h" | 56 #include "ui/gfx/text_utils.h" |
56 #include "ui/views/background.h" | 57 #include "ui/views/background.h" |
57 #include "ui/views/controls/button/image_button.h" | 58 #include "ui/views/controls/button/image_button.h" |
58 #include "ui/views/controls/button/label_button.h" | 59 #include "ui/views/controls/button/label_button.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 class InMenuButtonBackground : public views::Background { | 132 class InMenuButtonBackground : public views::Background { |
132 public: | 133 public: |
133 enum ButtonType { | 134 enum ButtonType { |
134 LEFT_BUTTON, | 135 LEFT_BUTTON, |
135 CENTER_BUTTON, | 136 CENTER_BUTTON, |
136 RIGHT_BUTTON, | 137 RIGHT_BUTTON, |
137 SINGLE_BUTTON, | 138 SINGLE_BUTTON, |
138 }; | 139 }; |
139 | 140 |
140 explicit InMenuButtonBackground(ButtonType type) | 141 explicit InMenuButtonBackground(ButtonType type) |
141 : type_(type), left_button_(NULL), right_button_(NULL) {} | 142 : type_(type), |
143 left_button_(NULL), | |
144 right_button_(NULL), | |
145 corner_radius_(ui::MaterialDesignController::IsModeMaterial() ? 2 : 0) { | |
Peter Kasting
2016/02/06 02:51:12
Why is this a member at all instead of simply bein
varkha
2016/02/16 19:59:44
Done.
| |
146 } | |
142 | 147 |
143 // Used when the type is CENTER_BUTTON to determine if the left/right edge | 148 // Used when the type is CENTER_BUTTON to determine if the left/right edge |
144 // needs to be rendered selected. | 149 // needs to be rendered selected. |
145 void SetOtherButtons(const CustomButton* left_button, | 150 void SetOtherButtons(const CustomButton* left_button, |
146 const CustomButton* right_button) { | 151 const CustomButton* right_button) { |
147 if (base::i18n::IsRTL()) { | 152 if (base::i18n::IsRTL()) { |
148 left_button_ = right_button; | 153 left_button_ = right_button; |
149 right_button_ = left_button; | 154 right_button_ = left_button; |
150 } else { | 155 } else { |
151 left_button_ = left_button; | 156 left_button_ = left_button; |
152 right_button_ = right_button; | 157 right_button_ = right_button; |
153 } | 158 } |
154 } | 159 } |
155 | 160 |
156 // Overridden from views::Background. | 161 // Overridden from views::Background. |
157 void Paint(gfx::Canvas* canvas, View* view) const override { | 162 void Paint(gfx::Canvas* canvas, View* view) const override { |
158 CustomButton* button = CustomButton::AsCustomButton(view); | 163 CustomButton* button = CustomButton::AsCustomButton(view); |
159 views::Button::ButtonState state = | 164 views::Button::ButtonState state = |
160 button ? button->state() : views::Button::STATE_NORMAL; | 165 button ? button->state() : views::Button::STATE_NORMAL; |
161 int h = view->height(); | 166 int h = view->height(); |
162 | 167 |
163 // Normal buttons get a border drawn on the right side and the rest gets | 168 // Normal buttons get a border drawn on the right side and the rest gets |
164 // filled in. The left button however does not get a line to combine | 169 // filled in. The left button however does not get a line to combine |
165 // buttons. | 170 // buttons. |
171 gfx::Rect bounds(view->GetLocalBounds()); | |
166 if (type_ != RIGHT_BUTTON) { | 172 if (type_ != RIGHT_BUTTON) { |
167 canvas->FillRect(gfx::Rect(0, 0, 1, h), | 173 canvas->FillRect(gfx::Rect(0, 0, 1, h), |
168 BorderColor(view, views::Button::STATE_NORMAL)); | 174 BorderColor(view, views::Button::STATE_NORMAL)); |
175 bounds.Inset(gfx::Insets(0, 1, 0, 0)); | |
169 } | 176 } |
170 | 177 |
171 gfx::Rect bounds(view->GetLocalBounds()); | |
172 bounds.set_x(view->GetMirroredXForRect(bounds)); | 178 bounds.set_x(view->GetMirroredXForRect(bounds)); |
173 DrawBackground(canvas, view, bounds, state); | 179 DrawBackground(canvas, view, bounds, state); |
174 } | 180 } |
175 | 181 |
176 private: | 182 private: |
177 static SkColor BorderColor(View* view, views::Button::ButtonState state) { | 183 static SkColor BorderColor(View* view, views::Button::ButtonState state) { |
178 ui::NativeTheme* theme = view->GetNativeTheme(); | 184 ui::NativeTheme* theme = view->GetNativeTheme(); |
179 switch (state) { | 185 switch (state) { |
180 case views::Button::STATE_HOVERED: | 186 case views::Button::STATE_HOVERED: |
181 return theme->GetSystemColor( | 187 return theme->GetSystemColor( |
(...skipping 24 matching lines...) Expand all Loading... | |
206 ui::NativeTheme::kColorId_MenuBackgroundColor); | 212 ui::NativeTheme::kColorId_MenuBackgroundColor); |
207 } | 213 } |
208 } | 214 } |
209 | 215 |
210 void DrawBackground(gfx::Canvas* canvas, | 216 void DrawBackground(gfx::Canvas* canvas, |
211 const views::View* view, | 217 const views::View* view, |
212 const gfx::Rect& bounds, | 218 const gfx::Rect& bounds, |
213 views::Button::ButtonState state) const { | 219 views::Button::ButtonState state) const { |
214 if (state == views::Button::STATE_HOVERED || | 220 if (state == views::Button::STATE_HOVERED || |
215 state == views::Button::STATE_PRESSED) { | 221 state == views::Button::STATE_PRESSED) { |
222 ui::NativeTheme::ExtraParams params = ui::NativeTheme::ExtraParams(); | |
223 if (view->GetClassName() == views::MenuButton::kViewClassName) | |
224 params.menu_item.corner_radius = corner_radius_; | |
216 view->GetNativeTheme()->Paint(canvas->sk_canvas(), | 225 view->GetNativeTheme()->Paint(canvas->sk_canvas(), |
217 ui::NativeTheme::kMenuItemBackground, | 226 ui::NativeTheme::kMenuItemBackground, |
218 ui::NativeTheme::kHovered, | 227 ui::NativeTheme::kHovered, bounds, params); |
219 bounds, | |
220 ui::NativeTheme::ExtraParams()); | |
221 } | 228 } |
222 } | 229 } |
223 | 230 |
224 ButtonType TypeAdjustedForRTL() const { | 231 ButtonType TypeAdjustedForRTL() const { |
225 if (!base::i18n::IsRTL()) | 232 if (!base::i18n::IsRTL()) |
226 return type_; | 233 return type_; |
227 | 234 |
228 switch (type_) { | 235 switch (type_) { |
229 case LEFT_BUTTON: return RIGHT_BUTTON; | 236 case LEFT_BUTTON: return RIGHT_BUTTON; |
230 case RIGHT_BUTTON: return LEFT_BUTTON; | 237 case RIGHT_BUTTON: return LEFT_BUTTON; |
231 default: break; | 238 default: break; |
232 } | 239 } |
233 return type_; | 240 return type_; |
234 } | 241 } |
235 | 242 |
236 const ButtonType type_; | 243 const ButtonType type_; |
237 | 244 |
238 // See description above setter for details. | 245 // See description above setter for details. |
239 const CustomButton* left_button_; | 246 const CustomButton* left_button_; |
240 const CustomButton* right_button_; | 247 const CustomButton* right_button_; |
241 | 248 |
249 // Show rounded button background for extension buttons with Material Design. | |
250 int corner_radius_; | |
251 | |
242 DISALLOW_COPY_AND_ASSIGN(InMenuButtonBackground); | 252 DISALLOW_COPY_AND_ASSIGN(InMenuButtonBackground); |
243 }; | 253 }; |
244 | 254 |
245 base::string16 GetAccessibleNameForAppMenuItem(ButtonMenuItemModel* model, | 255 base::string16 GetAccessibleNameForAppMenuItem(ButtonMenuItemModel* model, |
246 int item_index, | 256 int item_index, |
247 int accessible_string_id) { | 257 int accessible_string_id) { |
248 base::string16 accessible_name = | 258 base::string16 accessible_name = |
249 l10n_util::GetStringUTF16(accessible_string_id); | 259 l10n_util::GetStringUTF16(accessible_string_id); |
250 base::string16 accelerator_text; | 260 base::string16 accelerator_text; |
251 | 261 |
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1131 } | 1141 } |
1132 | 1142 |
1133 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) | 1143 if (model->GetTypeAt(i) == MenuModel::TYPE_SUBMENU) |
1134 PopulateMenu(item, model->GetSubmenuModelAt(i)); | 1144 PopulateMenu(item, model->GetSubmenuModelAt(i)); |
1135 | 1145 |
1136 switch (model->GetCommandIdAt(i)) { | 1146 switch (model->GetCommandIdAt(i)) { |
1137 case IDC_EXTENSIONS_OVERFLOW_MENU: { | 1147 case IDC_EXTENSIONS_OVERFLOW_MENU: { |
1138 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar( | 1148 scoped_ptr<ExtensionToolbarMenuView> extension_toolbar( |
1139 new ExtensionToolbarMenuView(browser_, this)); | 1149 new ExtensionToolbarMenuView(browser_, this)); |
1140 extension_toolbar_ = extension_toolbar.get(); | 1150 extension_toolbar_ = extension_toolbar.get(); |
1141 if (extension_toolbar->ShouldShow()) | 1151 if (!extension_toolbar->ShouldShow()) { |
1142 item->AddChildView(extension_toolbar.release()); | |
1143 else | |
1144 item->SetVisible(false); | 1152 item->SetVisible(false); |
1153 break; | |
Peter Kasting
2016/02/06 02:51:12
It really worries me that |extension_toolbar_| is
varkha
2016/02/16 19:59:44
Done.
| |
1154 } | |
1155 if (ui::MaterialDesignController::IsModeMaterial()) { | |
1156 for (int i = 0; i < extension_toolbar->contents()->child_count(); | |
1157 ++i) { | |
1158 View* action_view = extension_toolbar->contents()->child_at(i); | |
1159 InMenuButtonBackground* in_menu_background = | |
1160 new InMenuButtonBackground( | |
1161 InMenuButtonBackground::RIGHT_BUTTON); | |
Peter Kasting
2016/02/06 02:51:12
Nit: I would inline this into the next statement m
varkha
2016/02/16 19:59:44
Done.
| |
1162 action_view->set_background(in_menu_background); | |
1163 DCHECK(action_view->IsFocusable()); | |
1164 } | |
1165 } | |
1166 item->AddChildView(extension_toolbar.release()); | |
1145 break; | 1167 break; |
1146 } | 1168 } |
1147 | 1169 |
1148 case IDC_EDIT_MENU: { | 1170 case IDC_EDIT_MENU: { |
1149 ui::ButtonMenuItemModel* submodel = model->GetButtonMenuItemAt(i); | 1171 ui::ButtonMenuItemModel* submodel = model->GetButtonMenuItemAt(i); |
1150 DCHECK_EQ(IDC_CUT, submodel->GetCommandIdAt(0)); | 1172 DCHECK_EQ(IDC_CUT, submodel->GetCommandIdAt(0)); |
1151 DCHECK_EQ(IDC_COPY, submodel->GetCommandIdAt(1)); | 1173 DCHECK_EQ(IDC_COPY, submodel->GetCommandIdAt(1)); |
1152 DCHECK_EQ(IDC_PASTE, submodel->GetCommandIdAt(2)); | 1174 DCHECK_EQ(IDC_PASTE, submodel->GetCommandIdAt(2)); |
1153 item->SetTitle(l10n_util::GetStringUTF16(IDS_EDIT2)); | 1175 item->SetTitle(l10n_util::GetStringUTF16(IDS_EDIT2)); |
1154 item->AddChildView(new CutCopyPasteView(this, submodel, 0, 1, 2)); | 1176 item->AddChildView(new CutCopyPasteView(this, submodel, 0, 1, 2)); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1266 0, | 1288 0, |
1267 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, | 1289 BookmarkMenuDelegate::SHOW_PERMANENT_FOLDERS, |
1268 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); | 1290 BOOKMARK_LAUNCH_LOCATION_WRENCH_MENU); |
1269 } | 1291 } |
1270 | 1292 |
1271 int AppMenu::ModelIndexFromCommandId(int command_id) const { | 1293 int AppMenu::ModelIndexFromCommandId(int command_id) const { |
1272 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); | 1294 CommandIDToEntry::const_iterator ix = command_id_to_entry_.find(command_id); |
1273 DCHECK(ix != command_id_to_entry_.end()); | 1295 DCHECK(ix != command_id_to_entry_.end()); |
1274 return ix->second.second; | 1296 return ix->second.second; |
1275 } | 1297 } |
OLD | NEW |