OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser_actions_container.h" | 5 #include "chrome/browser/ui/views/browser_actions_container.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/extensions/extension_browser_event_router.h" | 10 #include "chrome/browser/extensions/extension_browser_event_router.h" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 #include "third_party/skia/include/effects/SkGradientShader.h" | 35 #include "third_party/skia/include/effects/SkGradientShader.h" |
36 #include "ui/base/accessibility/accessible_view_state.h" | 36 #include "ui/base/accessibility/accessible_view_state.h" |
37 #include "ui/base/animation/slide_animation.h" | 37 #include "ui/base/animation/slide_animation.h" |
38 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
39 #include "ui/base/resource/resource_bundle.h" | 39 #include "ui/base/resource/resource_bundle.h" |
40 #include "ui/base/theme_provider.h" | 40 #include "ui/base/theme_provider.h" |
41 #include "ui/gfx/canvas.h" | 41 #include "ui/gfx/canvas.h" |
42 #include "ui/gfx/canvas_skia.h" | 42 #include "ui/gfx/canvas_skia.h" |
43 #include "views/controls/button/menu_button.h" | 43 #include "views/controls/button/menu_button.h" |
44 #include "views/controls/button/text_button.h" | 44 #include "views/controls/button/text_button.h" |
45 #include "views/controls/menu/menu_2.h" | 45 #include "views/controls/menu/menu_item_view.h" |
46 #include "views/controls/menu/menu_model_adapter.h" | |
46 #include "views/drag_utils.h" | 47 #include "views/drag_utils.h" |
47 #include "views/metrics.h" | 48 #include "views/metrics.h" |
48 #include "views/window/window.h" | 49 #include "views/window/window.h" |
49 | 50 |
50 // Horizontal spacing between most items in the container, as well as after the | 51 // Horizontal spacing between most items in the container, as well as after the |
51 // last item or chevron (if visible). | 52 // last item or chevron (if visible). |
52 static const int kItemSpacing = ToolbarView::kStandardSpacing; | 53 static const int kItemSpacing = ToolbarView::kStandardSpacing; |
53 // Horizontal spacing before the chevron (if visible). | 54 // Horizontal spacing before the chevron (if visible). |
54 static const int kChevronSpacing = kItemSpacing - 2; | 55 static const int kChevronSpacing = kItemSpacing - 2; |
55 | 56 |
56 // static | 57 // static |
57 bool BrowserActionsContainer::disable_animations_during_testing_ = false; | 58 bool BrowserActionsContainer::disable_animations_during_testing_ = false; |
58 | 59 |
59 //////////////////////////////////////////////////////////////////////////////// | 60 //////////////////////////////////////////////////////////////////////////////// |
60 // BrowserActionButton | 61 // BrowserActionButton |
61 | 62 |
62 BrowserActionButton::BrowserActionButton(const Extension* extension, | 63 BrowserActionButton::BrowserActionButton(const Extension* extension, |
63 BrowserActionsContainer* panel) | 64 BrowserActionsContainer* panel) |
64 : ALLOW_THIS_IN_INITIALIZER_LIST( | 65 : ALLOW_THIS_IN_INITIALIZER_LIST( |
65 MenuButton(this, std::wstring(), NULL, false)), | 66 MenuButton(this, std::wstring(), NULL, false)), |
66 browser_action_(extension->browser_action()), | 67 browser_action_(extension->browser_action()), |
67 extension_(extension), | 68 extension_(extension), |
68 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 69 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
69 showing_context_menu_(false), | 70 showing_context_menu_(false), |
70 panel_(panel) { | 71 panel_(panel), |
72 context_menu_menu_(NULL) { | |
71 set_border(NULL); | 73 set_border(NULL); |
72 set_alignment(TextButton::ALIGN_CENTER); | 74 set_alignment(TextButton::ALIGN_CENTER); |
73 | 75 |
74 // No UpdateState() here because View hierarchy not setup yet. Our parent | 76 // No UpdateState() here because View hierarchy not setup yet. Our parent |
75 // should call UpdateState() after creation. | 77 // should call UpdateState() after creation. |
76 | 78 |
77 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 79 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
78 Source<ExtensionAction>(browser_action_)); | 80 Source<ExtensionAction>(browser_action_)); |
79 } | 81 } |
80 | 82 |
81 void BrowserActionButton::Destroy() { | 83 void BrowserActionButton::Destroy() { |
82 if (showing_context_menu_) { | 84 if (showing_context_menu_) { |
83 context_menu_menu_->CancelMenu(); | 85 context_menu_menu_->Cancel(); |
84 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 86 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
85 } else { | 87 } else { |
86 delete this; | 88 delete this; |
87 } | 89 } |
88 } | 90 } |
89 | 91 |
90 void BrowserActionButton::ViewHierarchyChanged( | 92 void BrowserActionButton::ViewHierarchyChanged( |
91 bool is_add, View* parent, View* child) { | 93 bool is_add, View* parent, View* child) { |
92 if (is_add && child == this) { | 94 if (is_add && child == this) { |
93 // The Browser Action API does not allow the default icon path to be | 95 // The Browser Action API does not allow the default icon path to be |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 // to us. | 210 // to us. |
209 return false; | 211 return false; |
210 } | 212 } |
211 | 213 |
212 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& event) { | 214 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& event) { |
213 if (!event.IsRightMouseButton()) { | 215 if (!event.IsRightMouseButton()) { |
214 return IsPopup() ? | 216 return IsPopup() ? |
215 MenuButton::OnMousePressed(event) : TextButton::OnMousePressed(event); | 217 MenuButton::OnMousePressed(event) : TextButton::OnMousePressed(event); |
216 } | 218 } |
217 | 219 |
218 // Get the top left point of this button in screen coordinates. | 220 ShowContextMenu(gfx::Point(), true); |
219 gfx::Point point = gfx::Point(0, 0); | |
220 ConvertPointToScreen(this, &point); | |
221 | |
222 // Make the menu appear below the button. | |
223 point.Offset(0, height()); | |
224 | |
225 ShowContextMenu(point, true); | |
226 return false; | 221 return false; |
227 } | 222 } |
228 | 223 |
229 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& event) { | 224 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& event) { |
230 if (IsPopup() || showing_context_menu_) { | 225 if (IsPopup() || showing_context_menu_) { |
231 // TODO(erikkay) this never actually gets called (probably because of the | 226 // TODO(erikkay) this never actually gets called (probably because of the |
232 // loss of focus). | 227 // loss of focus). |
233 MenuButton::OnMouseReleased(event); | 228 MenuButton::OnMouseReleased(event); |
234 } else { | 229 } else { |
235 TextButton::OnMouseReleased(event); | 230 TextButton::OnMouseReleased(event); |
(...skipping 16 matching lines...) Expand all Loading... | |
252 bool is_mouse_gesture) { | 247 bool is_mouse_gesture) { |
253 if (!extension()->ShowConfigureContextMenus()) | 248 if (!extension()->ShowConfigureContextMenus()) |
254 return; | 249 return; |
255 | 250 |
256 showing_context_menu_ = true; | 251 showing_context_menu_ = true; |
257 SetButtonPushed(); | 252 SetButtonPushed(); |
258 | 253 |
259 // Reconstructs the menu every time because the menu's contents are dynamic. | 254 // Reconstructs the menu every time because the menu's contents are dynamic. |
260 context_menu_contents_ = | 255 context_menu_contents_ = |
261 new ExtensionContextMenuModel(extension(), panel_->browser(), panel_); | 256 new ExtensionContextMenuModel(extension(), panel_->browser(), panel_); |
262 context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get())); | 257 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); |
263 context_menu_menu_->RunContextMenuAt(p); | 258 context_menu_menu_ = new views::MenuItemView(&menu_model_adapter); |
Peter Kasting
2011/06/09 00:39:09
Nit: Safer against leaks would be to stack-allocat
| |
259 menu_model_adapter.BuildMenu(context_menu_menu_); | |
260 | |
261 gfx::Point screen_loc; | |
262 views::View::ConvertPointToScreen(this, &screen_loc); | |
263 context_menu_menu_->RunMenuAt(GetWindow()->GetNativeWindow(), NULL, | |
264 gfx::Rect(screen_loc, size()), views::MenuItemView::TOPLEFT, true); | |
264 | 265 |
265 SetButtonNotPushed(); | 266 SetButtonNotPushed(); |
266 showing_context_menu_ = false; | 267 showing_context_menu_ = false; |
268 delete context_menu_menu_; | |
269 context_menu_menu_ = NULL; | |
267 } | 270 } |
268 | 271 |
269 void BrowserActionButton::SetButtonPushed() { | 272 void BrowserActionButton::SetButtonPushed() { |
270 SetState(views::CustomButton::BS_PUSHED); | 273 SetState(views::CustomButton::BS_PUSHED); |
271 menu_visible_ = true; | 274 menu_visible_ = true; |
272 } | 275 } |
273 | 276 |
274 void BrowserActionButton::SetButtonNotPushed() { | 277 void BrowserActionButton::SetButtonNotPushed() { |
275 SetState(views::CustomButton::BS_NORMAL); | 278 SetState(views::CustomButton::BS_NORMAL); |
276 menu_visible_ = false; | 279 menu_visible_ = false; |
(...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1095 } | 1098 } |
1096 } | 1099 } |
1097 | 1100 |
1098 bool BrowserActionsContainer::ShouldDisplayBrowserAction( | 1101 bool BrowserActionsContainer::ShouldDisplayBrowserAction( |
1099 const Extension* extension) { | 1102 const Extension* extension) { |
1100 // Only display incognito-enabled extensions while in incognito mode. | 1103 // Only display incognito-enabled extensions while in incognito mode. |
1101 return | 1104 return |
1102 (!profile_->IsOffTheRecord() || | 1105 (!profile_->IsOffTheRecord() || |
1103 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); | 1106 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); |
1104 } | 1107 } |
OLD | NEW |