Chromium Code Reviews| 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 panel_(panel), |
| 70 panel_(panel) { | 71 context_menu_(NULL) { |
| 71 set_border(NULL); | 72 set_border(NULL); |
| 72 set_alignment(TextButton::ALIGN_CENTER); | 73 set_alignment(TextButton::ALIGN_CENTER); |
| 73 | 74 |
| 74 // No UpdateState() here because View hierarchy not setup yet. Our parent | 75 // No UpdateState() here because View hierarchy not setup yet. Our parent |
| 75 // should call UpdateState() after creation. | 76 // should call UpdateState() after creation. |
| 76 | 77 |
| 77 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 78 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
| 78 Source<ExtensionAction>(browser_action_)); | 79 Source<ExtensionAction>(browser_action_)); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void BrowserActionButton::Destroy() { | 82 void BrowserActionButton::Destroy() { |
| 82 if (showing_context_menu_) { | 83 if (context_menu_) { |
| 83 context_menu_menu_->CancelMenu(); | 84 context_menu_->Cancel(); |
| 84 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 85 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 85 } else { | 86 } else { |
| 86 delete this; | 87 delete this; |
| 87 } | 88 } |
| 88 } | 89 } |
| 89 | 90 |
| 90 void BrowserActionButton::ViewHierarchyChanged( | 91 void BrowserActionButton::ViewHierarchyChanged( |
| 91 bool is_add, View* parent, View* child) { | 92 bool is_add, View* parent, View* child) { |
| 92 if (is_add && child == this) { | 93 if (is_add && child == this) { |
| 93 // The Browser Action API does not allow the default icon path to be | 94 // 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. | 209 // to us. |
| 209 return false; | 210 return false; |
| 210 } | 211 } |
| 211 | 212 |
| 212 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& event) { | 213 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& event) { |
| 213 if (!event.IsRightMouseButton()) { | 214 if (!event.IsRightMouseButton()) { |
| 214 return IsPopup() ? | 215 return IsPopup() ? |
| 215 MenuButton::OnMousePressed(event) : TextButton::OnMousePressed(event); | 216 MenuButton::OnMousePressed(event) : TextButton::OnMousePressed(event); |
| 216 } | 217 } |
| 217 | 218 |
| 218 // Get the top left point of this button in screen coordinates. | 219 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; | 220 return false; |
| 227 } | 221 } |
| 228 | 222 |
| 229 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& event) { | 223 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& event) { |
| 230 if (IsPopup() || showing_context_menu_) { | 224 if (IsPopup() || context_menu_) { |
| 231 // TODO(erikkay) this never actually gets called (probably because of the | 225 // TODO(erikkay) this never actually gets called (probably because of the |
| 232 // loss of focus). | 226 // loss of focus). |
| 233 MenuButton::OnMouseReleased(event); | 227 MenuButton::OnMouseReleased(event); |
| 234 } else { | 228 } else { |
| 235 TextButton::OnMouseReleased(event); | 229 TextButton::OnMouseReleased(event); |
| 236 } | 230 } |
| 237 } | 231 } |
| 238 | 232 |
| 239 void BrowserActionButton::OnMouseExited(const views::MouseEvent& event) { | 233 void BrowserActionButton::OnMouseExited(const views::MouseEvent& event) { |
| 240 if (IsPopup() || showing_context_menu_) | 234 if (IsPopup() || context_menu_) |
| 241 MenuButton::OnMouseExited(event); | 235 MenuButton::OnMouseExited(event); |
| 242 else | 236 else |
| 243 TextButton::OnMouseExited(event); | 237 TextButton::OnMouseExited(event); |
| 244 } | 238 } |
| 245 | 239 |
| 246 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { | 240 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { |
| 247 return IsPopup() ? | 241 return IsPopup() ? |
| 248 MenuButton::OnKeyReleased(event) : TextButton::OnKeyReleased(event); | 242 MenuButton::OnKeyReleased(event) : TextButton::OnKeyReleased(event); |
| 249 } | 243 } |
| 250 | 244 |
| 251 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, | 245 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, |
| 252 bool is_mouse_gesture) { | 246 bool is_mouse_gesture) { |
| 253 if (!extension()->ShowConfigureContextMenus()) | 247 if (!extension()->ShowConfigureContextMenus()) |
| 254 return; | 248 return; |
| 255 | 249 |
| 256 showing_context_menu_ = true; | |
| 257 SetButtonPushed(); | 250 SetButtonPushed(); |
| 258 | 251 |
| 259 // Reconstructs the menu every time because the menu's contents are dynamic. | 252 // Reconstructs the menu every time because the menu's contents are dynamic. |
| 260 context_menu_contents_ = | 253 context_menu_contents_ = |
|
Peter Kasting
2011/06/09 17:43:57
It looks like this leaks. Should this be a scoped
rhashimoto
2011/06/09 18:19:01
It is a scoped_refptr. I thought I would change t
Peter Kasting
2011/06/09 18:20:42
Oh, it's a member. Why is it a member? Seems lik
rhashimoto
2011/06/09 18:27:51
Done.
| |
| 261 new ExtensionContextMenuModel(extension(), panel_->browser(), panel_); | 254 new ExtensionContextMenuModel(extension(), panel_->browser(), panel_); |
| 262 context_menu_menu_.reset(new views::Menu2(context_menu_contents_.get())); | 255 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); |
| 263 context_menu_menu_->RunContextMenuAt(p); | 256 context_menu_ = new views::MenuItemView(&menu_model_adapter); |
|
Peter Kasting
2011/06/09 17:43:57
What about that idea of stack-allocating this obje
rhashimoto
2011/06/09 18:19:01
Done.
| |
| 257 menu_model_adapter.BuildMenu(context_menu_); | |
| 258 | |
| 259 gfx::Point screen_loc; | |
| 260 views::View::ConvertPointToScreen(this, &screen_loc); | |
| 261 context_menu_->RunMenuAt(GetWidget()->GetNativeWindow(), NULL, | |
| 262 gfx::Rect(screen_loc, size()), views::MenuItemView::TOPLEFT, true); | |
| 264 | 263 |
| 265 SetButtonNotPushed(); | 264 SetButtonNotPushed(); |
| 266 showing_context_menu_ = false; | 265 delete context_menu_; |
| 266 context_menu_ = NULL; | |
| 267 } | 267 } |
| 268 | 268 |
| 269 void BrowserActionButton::SetButtonPushed() { | 269 void BrowserActionButton::SetButtonPushed() { |
| 270 SetState(views::CustomButton::BS_PUSHED); | 270 SetState(views::CustomButton::BS_PUSHED); |
| 271 menu_visible_ = true; | 271 menu_visible_ = true; |
| 272 } | 272 } |
| 273 | 273 |
| 274 void BrowserActionButton::SetButtonNotPushed() { | 274 void BrowserActionButton::SetButtonNotPushed() { |
| 275 SetState(views::CustomButton::BS_NORMAL); | 275 SetState(views::CustomButton::BS_NORMAL); |
| 276 menu_visible_ = false; | 276 menu_visible_ = false; |
| (...skipping 818 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1095 } | 1095 } |
| 1096 } | 1096 } |
| 1097 | 1097 |
| 1098 bool BrowserActionsContainer::ShouldDisplayBrowserAction( | 1098 bool BrowserActionsContainer::ShouldDisplayBrowserAction( |
| 1099 const Extension* extension) { | 1099 const Extension* extension) { |
| 1100 // Only display incognito-enabled extensions while in incognito mode. | 1100 // Only display incognito-enabled extensions while in incognito mode. |
| 1101 return | 1101 return |
| 1102 (!profile_->IsOffTheRecord() || | 1102 (!profile_->IsOffTheRecord() || |
| 1103 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); | 1103 profile_->GetExtensionService()->IsIncognitoEnabled(extension->id())); |
| 1104 } | 1104 } |
| OLD | NEW |