| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/location_bar/location_icon_view.h" | 5 #include "chrome/browser/ui/views/location_bar/location_icon_view.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/view_ids.h" | 7 #include "chrome/browser/ui/view_ids.h" |
| 8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 8 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 9 #include "chrome/browser/ui/views/page_info/page_info_popup_view.h" | 9 #include "chrome/browser/ui/views/page_info/page_info_popup_view.h" |
| 10 #include "chrome/grit/generated_resources.h" | 10 #include "chrome/grit/generated_resources.h" |
| 11 #include "chrome/grit/theme_resources.h" | 11 #include "chrome/grit/theme_resources.h" |
| 12 #include "components/grit/components_scaled_resources.h" | 12 #include "components/grit/components_scaled_resources.h" |
| 13 #include "components/omnibox/browser/omnibox_edit_model.h" | 13 #include "components/omnibox/browser/omnibox_edit_model.h" |
| 14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 15 #include "ui/accessibility/ax_node_data.h" | 15 #include "ui/accessibility/ax_node_data.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
| 18 | 18 |
| 19 using content::WebContents; | 19 using content::WebContents; |
| 20 | 20 |
| 21 LocationIconView::LocationIconView(const gfx::FontList& font_list, | 21 LocationIconView::LocationIconView(const gfx::FontList& font_list, |
| 22 LocationBarView* location_bar) | 22 LocationBarView* location_bar) |
| 23 : IconLabelBubbleView(font_list, true), | 23 : IconLabelBubbleView(font_list, true), |
| 24 suppress_mouse_released_action_(false), | |
| 25 location_bar_(location_bar), | 24 location_bar_(location_bar), |
| 26 animation_(this) { | 25 animation_(this) { |
| 27 set_id(VIEW_ID_LOCATION_ICON); | 26 set_id(VIEW_ID_LOCATION_ICON); |
| 27 SetInkDropMode(InkDropMode::ON); |
| 28 | 28 |
| 29 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 30 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | 30 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); |
| 31 #else | 31 #else |
| 32 SetFocusBehavior(FocusBehavior::ALWAYS); | 32 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 animation_.SetSlideDuration(kOpenTimeMS); | 35 animation_.SetSlideDuration(kOpenTimeMS); |
| 36 } | 36 } |
| 37 | 37 |
| 38 LocationIconView::~LocationIconView() { | 38 LocationIconView::~LocationIconView() { |
| 39 } | 39 } |
| 40 | 40 |
| 41 gfx::Size LocationIconView::GetMinimumSize() const { | 41 gfx::Size LocationIconView::GetMinimumSize() const { |
| 42 return GetMinimumSizeForPreferredSize(GetPreferredSize()); | 42 return GetMinimumSizeForPreferredSize(GetPreferredSize()); |
| 43 } | 43 } |
| 44 | 44 |
| 45 bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) { | 45 bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) { |
| 46 if (event.IsOnlyMiddleMouseButton() && | 46 if (event.IsOnlyMiddleMouseButton() && |
| 47 ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION)) { | 47 ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION)) { |
| 48 base::string16 text; | 48 base::string16 text; |
| 49 ui::Clipboard::GetForCurrentThread()->ReadText( | 49 ui::Clipboard::GetForCurrentThread()->ReadText( |
| 50 ui::CLIPBOARD_TYPE_SELECTION, &text); | 50 ui::CLIPBOARD_TYPE_SELECTION, &text); |
| 51 text = OmniboxView::SanitizeTextForPaste(text); | 51 text = OmniboxView::SanitizeTextForPaste(text); |
| 52 OmniboxEditModel* model = location_bar_->GetOmniboxView()->model(); | 52 OmniboxEditModel* model = location_bar_->GetOmniboxView()->model(); |
| 53 if (model->CanPasteAndGo(text)) | 53 if (model->CanPasteAndGo(text)) |
| 54 model->PasteAndGo(text); | 54 model->PasteAndGo(text); |
| 55 } | 55 } |
| 56 | 56 |
| 57 suppress_mouse_released_action_ = | 57 IconLabelBubbleView::OnMousePressed(event); |
| 58 PageInfoPopupView::GetShownPopupType() != PageInfoPopupView::POPUP_NONE; | |
| 59 return true; | 58 return true; |
| 60 } | 59 } |
| 61 | 60 |
| 62 bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) { | 61 bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) { |
| 63 location_bar_->GetOmniboxView()->CloseOmniboxPopup(); | 62 location_bar_->GetOmniboxView()->CloseOmniboxPopup(); |
| 64 return false; | 63 return IconLabelBubbleView::OnMouseDragged(event); |
| 65 } | |
| 66 | |
| 67 void LocationIconView::OnMouseReleased(const ui::MouseEvent& event) { | |
| 68 if (event.IsOnlyMiddleMouseButton()) | |
| 69 return; | |
| 70 | |
| 71 // If this is the second click on this view then the bubble was showing on | |
| 72 // the mouse pressed event and is hidden now. Prevent the bubble from | |
| 73 // reshowing by doing nothing here. | |
| 74 if (suppress_mouse_released_action_) { | |
| 75 suppress_mouse_released_action_ = false; | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 OnClickOrTap(event); | |
| 80 } | |
| 81 | |
| 82 void LocationIconView::OnGestureEvent(ui::GestureEvent* event) { | |
| 83 if (event->type() != ui::ET_GESTURE_TAP) | |
| 84 return; | |
| 85 OnClickOrTap(*event); | |
| 86 event->SetHandled(); | |
| 87 } | 64 } |
| 88 | 65 |
| 89 bool LocationIconView::GetTooltipText(const gfx::Point& p, | 66 bool LocationIconView::GetTooltipText(const gfx::Point& p, |
| 90 base::string16* tooltip) const { | 67 base::string16* tooltip) const { |
| 91 if (show_tooltip_) | 68 if (show_tooltip_) |
| 92 *tooltip = l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON); | 69 *tooltip = l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON); |
| 93 return show_tooltip_; | 70 return show_tooltip_; |
| 94 } | 71 } |
| 95 | 72 |
| 96 SkColor LocationIconView::GetTextColor() const { | 73 SkColor LocationIconView::GetTextColor() const { |
| 97 return location_bar_->GetColor(LocationBarView::SECURITY_CHIP_TEXT); | 74 return location_bar_->GetColor(LocationBarView::SECURITY_CHIP_TEXT); |
| 98 } | 75 } |
| 99 | 76 |
| 100 bool LocationIconView::OnActivate(const ui::Event& event) { | 77 bool LocationIconView::OnActivate(const ui::Event& event) { |
| 101 WebContents* contents = location_bar_->GetWebContents(); | 78 WebContents* contents = location_bar_->GetWebContents(); |
| 102 if (!contents) | 79 if (!contents) |
| 103 return false; | 80 return false; |
| 104 location_bar_->delegate()->ShowPageInfo(contents); | 81 location_bar_->delegate()->ShowPageInfo(contents); |
| 105 return true; | 82 return true; |
| 106 } | 83 } |
| 107 | 84 |
| 108 void LocationIconView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 85 void LocationIconView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 109 IconLabelBubbleView::GetAccessibleNodeData(node_data); | 86 IconLabelBubbleView::GetAccessibleNodeData(node_data); |
| 110 node_data->role = ui::AX_ROLE_POP_UP_BUTTON; | 87 node_data->role = ui::AX_ROLE_POP_UP_BUTTON; |
| 111 } | 88 } |
| 112 | 89 |
| 90 bool LocationIconView::IsBubbleShown() const { |
| 91 return PageInfoPopupView::GetShownPopupType() != |
| 92 PageInfoPopupView::POPUP_NONE; |
| 93 } |
| 94 |
| 113 gfx::Size LocationIconView::GetMinimumSizeForLabelText( | 95 gfx::Size LocationIconView::GetMinimumSizeForLabelText( |
| 114 const base::string16& text) const { | 96 const base::string16& text) const { |
| 115 views::Label label(text, {font_list()}); | 97 views::Label label(text, {font_list()}); |
| 116 return GetMinimumSizeForPreferredSize( | 98 return GetMinimumSizeForPreferredSize( |
| 117 GetSizeForLabelWidth(label.GetPreferredSize().width())); | 99 GetSizeForLabelWidth(label.GetPreferredSize().width())); |
| 118 } | 100 } |
| 119 | 101 |
| 120 void LocationIconView::SetTextVisibility(bool should_show, | 102 void LocationIconView::SetTextVisibility(bool should_show, |
| 121 bool should_animate) { | 103 bool should_animate) { |
| 122 if (!should_animate) { | 104 if (!should_animate) { |
| 123 animation_.Reset(should_show); | 105 animation_.Reset(should_show); |
| 124 } else if (should_show) { | 106 } else if (should_show) { |
| 125 animation_.Show(); | 107 animation_.Show(); |
| 126 } else { | 108 } else { |
| 127 animation_.Hide(); | 109 animation_.Hide(); |
| 128 } | 110 } |
| 129 // The label text color may have changed. | 111 // The label text color may have changed. |
| 130 OnNativeThemeChanged(GetNativeTheme()); | 112 OnNativeThemeChanged(GetNativeTheme()); |
| 131 } | 113 } |
| 132 | 114 |
| 115 bool LocationIconView::IsTriggerableEvent(const ui::Event& event) { |
| 116 if (location_bar_->GetOmniboxView()->IsEditingOrEmpty()) |
| 117 return false; |
| 118 |
| 119 if (event.IsMouseEvent()) { |
| 120 const ui::MouseEvent& mouseev = static_cast<const ui::MouseEvent&>(event); |
| 121 if (mouseev.IsOnlyMiddleMouseButton()) |
| 122 return false; |
| 123 } else if (event.IsGestureEvent() && event.type() != ui::ET_GESTURE_TAP) { |
| 124 return false; |
| 125 } |
| 126 |
| 127 return IconLabelBubbleView::IsTriggerableEvent(event); |
| 128 } |
| 129 |
| 133 double LocationIconView::WidthMultiplier() const { | 130 double LocationIconView::WidthMultiplier() const { |
| 134 return animation_.GetCurrentValue(); | 131 return animation_.GetCurrentValue(); |
| 135 } | 132 } |
| 136 | 133 |
| 137 void LocationIconView::AnimationProgressed(const gfx::Animation*) { | 134 void LocationIconView::AnimationProgressed(const gfx::Animation*) { |
| 138 location_bar_->Layout(); | 135 location_bar_->Layout(); |
| 139 location_bar_->SchedulePaint(); | 136 location_bar_->SchedulePaint(); |
| 140 } | 137 } |
| 141 | 138 |
| 142 void LocationIconView::ProcessLocatedEvent(const ui::LocatedEvent& event) { | |
| 143 if (HitTestPoint(event.location())) | |
| 144 OnActivate(event); | |
| 145 } | |
| 146 | |
| 147 gfx::Size LocationIconView::GetMinimumSizeForPreferredSize( | 139 gfx::Size LocationIconView::GetMinimumSizeForPreferredSize( |
| 148 gfx::Size size) const { | 140 gfx::Size size) const { |
| 149 const int kMinCharacters = 10; | 141 const int kMinCharacters = 10; |
| 150 size.SetToMin( | 142 size.SetToMin( |
| 151 GetSizeForLabelWidth(font_list().GetExpectedTextWidth(kMinCharacters))); | 143 GetSizeForLabelWidth(font_list().GetExpectedTextWidth(kMinCharacters))); |
| 152 return size; | 144 return size; |
| 153 } | 145 } |
| 154 | |
| 155 void LocationIconView::OnClickOrTap(const ui::LocatedEvent& event) { | |
| 156 // Do not show page info if the user has been editing the location bar or the | |
| 157 // location bar is at the NTP. | |
| 158 if (location_bar_->GetOmniboxView()->IsEditingOrEmpty()) | |
| 159 return; | |
| 160 ProcessLocatedEvent(event); | |
| 161 } | |
| OLD | NEW |