OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/views/browser_actions_container.h" | 5 #include "chrome/browser/views/browser_actions_container.h" |
6 | 6 |
7 #include "app/gfx/canvas.h" | 7 #include "app/gfx/canvas.h" |
8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 | 47 |
48 //////////////////////////////////////////////////////////////////////////////// | 48 //////////////////////////////////////////////////////////////////////////////// |
49 // BrowserActionButton | 49 // BrowserActionButton |
50 | 50 |
51 BrowserActionButton::BrowserActionButton(Extension* extension, | 51 BrowserActionButton::BrowserActionButton(Extension* extension, |
52 BrowserActionsContainer* panel) | 52 BrowserActionsContainer* panel) |
53 : MenuButton(this, L"", NULL, false), | 53 : MenuButton(this, L"", NULL, false), |
54 browser_action_(extension->browser_action()), | 54 browser_action_(extension->browser_action()), |
55 extension_(extension), | 55 extension_(extension), |
56 tracker_(NULL), | 56 tracker_(NULL), |
57 showing_context_menu_(false), | |
58 panel_(panel) { | 57 panel_(panel) { |
59 set_alignment(TextButton::ALIGN_CENTER); | 58 set_alignment(TextButton::ALIGN_CENTER); |
60 | 59 |
61 // No UpdateState() here because View hierarchy not setup yet. Our parent | 60 // No UpdateState() here because View heirarchy not setup yet. Our parent |
62 // should call UpdateState() after creation. | 61 // should call UpdateState() after creation. |
63 | 62 |
64 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, | 63 registrar_.Add(this, NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, |
65 Source<ExtensionAction>(browser_action_)); | 64 Source<ExtensionAction>(browser_action_)); |
66 | 65 |
67 // The Browser Action API does not allow the default icon path to be changed | 66 // The Browser Action API does not allow the default icon path to be changed |
68 // at runtime, so we can load this now and cache it. | 67 // at runtime, so we can load this now and cache it. |
69 std::string relative_path = browser_action_->default_icon_path(); | 68 std::string relative_path = browser_action_->default_icon_path(); |
70 if (relative_path.empty()) | 69 if (relative_path.empty()) |
71 return; | 70 return; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 // The return value of this method is returned via OnMousePressed. | 146 // The return value of this method is returned via OnMousePressed. |
148 // We need to return false here since we're handing off focus to another | 147 // We need to return false here since we're handing off focus to another |
149 // widget/view, and true will grab it right back and try to send events | 148 // widget/view, and true will grab it right back and try to send events |
150 // to us. | 149 // to us. |
151 return false; | 150 return false; |
152 } | 151 } |
153 return true; | 152 return true; |
154 } | 153 } |
155 | 154 |
156 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& e) { | 155 bool BrowserActionButton::OnMousePressed(const views::MouseEvent& e) { |
157 showing_context_menu_ = e.IsRightMouseButton(); | 156 if (IsPopup()) |
158 if (showing_context_menu_) { | |
159 SetButtonPushed(); | |
160 | |
161 // Get the top left point of this button in screen coordinates. | |
162 gfx::Point point = gfx::Point(0, 0); | |
163 ConvertPointToScreen(this, &point); | |
164 | |
165 // Make the menu appear below the button. | |
166 point.Offset(0, height()); | |
167 | |
168 if (!context_menu_.get()) | |
169 context_menu_.reset(new ExtensionActionContextMenu()); | |
170 context_menu_->Run(extension(), point); | |
171 | |
172 SetButtonNotPushed(); | |
173 return false; | |
174 } else if (IsPopup()) { | |
175 return MenuButton::OnMousePressed(e); | 157 return MenuButton::OnMousePressed(e); |
176 } | |
177 return TextButton::OnMousePressed(e); | 158 return TextButton::OnMousePressed(e); |
178 } | 159 } |
179 | 160 |
180 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& e, | 161 void BrowserActionButton::OnMouseReleased(const views::MouseEvent& e, |
181 bool canceled) { | 162 bool canceled) { |
182 if (IsPopup() || showing_context_menu_) { | 163 if (IsPopup()) { |
183 // TODO(erikkay) this never actually gets called (probably because of the | 164 // TODO(erikkay) this never actually gets called (probably because of the |
184 // loss of focus). | 165 // loss of focus). |
185 MenuButton::OnMouseReleased(e, canceled); | 166 MenuButton::OnMouseReleased(e, canceled); |
186 } else { | 167 } else { |
187 TextButton::OnMouseReleased(e, canceled); | 168 TextButton::OnMouseReleased(e, canceled); |
188 } | 169 } |
189 } | 170 } |
190 | 171 |
191 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& e) { | 172 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& e) { |
192 if (IsPopup()) | 173 if (IsPopup()) |
193 return MenuButton::OnKeyReleased(e); | 174 return MenuButton::OnKeyReleased(e); |
194 return TextButton::OnKeyReleased(e); | 175 return TextButton::OnKeyReleased(e); |
195 } | 176 } |
196 | 177 |
197 void BrowserActionButton::OnMouseExited(const views::MouseEvent& e) { | 178 void BrowserActionButton::OnMouseExited(const views::MouseEvent& e) { |
198 if (IsPopup() || showing_context_menu_) | 179 if (IsPopup()) |
199 MenuButton::OnMouseExited(e); | 180 MenuButton::OnMouseExited(e); |
200 else | 181 else |
201 TextButton::OnMouseExited(e); | 182 TextButton::OnMouseExited(e); |
202 } | 183 } |
203 | 184 |
204 void BrowserActionButton::SetButtonPushed() { | 185 void BrowserActionButton::PopupDidShow() { |
205 SetState(views::CustomButton::BS_PUSHED); | 186 SetState(views::CustomButton::BS_PUSHED); |
206 menu_visible_ = true; | 187 menu_visible_ = true; |
207 } | 188 } |
208 | 189 |
209 void BrowserActionButton::SetButtonNotPushed() { | 190 void BrowserActionButton::PopupDidHide() { |
210 SetState(views::CustomButton::BS_NORMAL); | 191 SetState(views::CustomButton::BS_NORMAL); |
211 menu_visible_ = false; | 192 menu_visible_ = false; |
212 } | 193 } |
213 | 194 |
214 | 195 |
215 //////////////////////////////////////////////////////////////////////////////// | 196 //////////////////////////////////////////////////////////////////////////////// |
216 // BrowserActionView | 197 // BrowserActionView |
217 | 198 |
218 BrowserActionView::BrowserActionView(Extension* extension, | 199 BrowserActionView::BrowserActionView(Extension* extension, |
219 BrowserActionsContainer* panel) | 200 BrowserActionsContainer* panel) |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 // Save these variables in local temporaries since destroying the popup | 341 // Save these variables in local temporaries since destroying the popup |
361 // calls BubbleLostFocus to be called, which will try to call HidePopup() | 342 // calls BubbleLostFocus to be called, which will try to call HidePopup() |
362 // again if popup_ is non-null. | 343 // again if popup_ is non-null. |
363 ExtensionPopup* closing_popup = popup_; | 344 ExtensionPopup* closing_popup = popup_; |
364 BrowserActionButton* closing_button = popup_button_; | 345 BrowserActionButton* closing_button = popup_button_; |
365 popup_ = NULL; | 346 popup_ = NULL; |
366 popup_button_ = NULL; | 347 popup_button_ = NULL; |
367 | 348 |
368 closing_popup->DetachFromBrowser(); | 349 closing_popup->DetachFromBrowser(); |
369 delete closing_popup; | 350 delete closing_popup; |
370 closing_button->SetButtonNotPushed(); | 351 closing_button->PopupDidHide(); |
371 return; | 352 return; |
372 } | 353 } |
373 } | 354 } |
374 | 355 |
375 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { | 356 void BrowserActionsContainer::TestExecuteBrowserAction(int index) { |
376 BrowserActionButton* button = browser_action_views_[index]->button(); | 357 BrowserActionButton* button = browser_action_views_[index]->button(); |
377 OnBrowserActionExecuted(button); | 358 OnBrowserActionExecuted(button); |
378 } | 359 } |
379 | 360 |
380 void BrowserActionsContainer::OnBrowserActionExecuted( | 361 void BrowserActionsContainer::OnBrowserActionExecuted( |
(...skipping 17 matching lines...) Expand all Loading... |
398 View::ConvertPointToScreen(button, &origin); | 379 View::ConvertPointToScreen(button, &origin); |
399 gfx::Rect rect = button->bounds(); | 380 gfx::Rect rect = button->bounds(); |
400 rect.set_x(origin.x()); | 381 rect.set_x(origin.x()); |
401 rect.set_y(origin.y()); | 382 rect.set_y(origin.y()); |
402 popup_ = ExtensionPopup::Show(browser_action->popup_url(), | 383 popup_ = ExtensionPopup::Show(browser_action->popup_url(), |
403 toolbar_->browser(), | 384 toolbar_->browser(), |
404 rect, | 385 rect, |
405 BubbleBorder::TOP_RIGHT); | 386 BubbleBorder::TOP_RIGHT); |
406 popup_->set_delegate(this); | 387 popup_->set_delegate(this); |
407 popup_button_ = button; | 388 popup_button_ = button; |
408 popup_button_->SetButtonPushed(); | 389 popup_button_->PopupDidShow(); |
409 return; | 390 return; |
410 } | 391 } |
411 | 392 |
412 // Otherwise, we send the action to the extension. | 393 // Otherwise, we send the action to the extension. |
413 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( | 394 ExtensionBrowserEventRouter::GetInstance()->BrowserActionExecuted( |
414 profile_, browser_action->extension_id(), toolbar_->browser()); | 395 profile_, browser_action->extension_id(), toolbar_->browser()); |
415 } | 396 } |
416 | 397 |
417 gfx::Size BrowserActionsContainer::GetPreferredSize() { | 398 gfx::Size BrowserActionsContainer::GetPreferredSize() { |
418 if (browser_action_views_.empty()) | 399 if (browser_action_views_.empty()) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
503 std::min(static_cast<int>(browser_action_views_.size()), | 484 std::min(static_cast<int>(browser_action_views_.size()), |
504 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; | 485 kMinimumNumberOfVisibleBrowserActions) * kButtonSize; |
505 | 486 |
506 // Even if available_width is <= 0, we still return at least the |min_width|. | 487 // Even if available_width is <= 0, we still return at least the |min_width|. |
507 if (available_width <= 0) | 488 if (available_width <= 0) |
508 return min_width; | 489 return min_width; |
509 | 490 |
510 return std::max(min_width, available_width - available_width % kButtonSize + | 491 return std::max(min_width, available_width - available_width % kButtonSize + |
511 kHorizontalPadding * 2); | 492 kHorizontalPadding * 2); |
512 } | 493 } |
OLD | NEW |