| 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/website_settings/website_settings_popup_view.h
" | 9 #include "chrome/browser/ui/views/website_settings/website_settings_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/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/views/animation/ink_drop_impl.h" |
| 16 #include "ui/views/controls/label.h" | 17 #include "ui/views/controls/label.h" |
| 17 | 18 |
| 18 using content::WebContents; | 19 using content::WebContents; |
| 19 | 20 |
| 20 LocationIconView::LocationIconView(const gfx::FontList& font_list, | 21 LocationIconView::LocationIconView(const gfx::FontList& font_list, |
| 21 LocationBarView* location_bar) | 22 LocationBarView* location_bar) |
| 22 : IconLabelBubbleView(font_list, true), | 23 : IconLabelBubbleView(font_list, true), |
| 23 suppress_mouse_released_action_(false), | |
| 24 location_bar_(location_bar), | 24 location_bar_(location_bar), |
| 25 animation_(this) { | 25 animation_(this) { |
| 26 set_id(VIEW_ID_LOCATION_ICON); | 26 set_id(VIEW_ID_LOCATION_ICON); |
| 27 SetInkDropMode(InkDropMode::ON); |
| 27 | 28 |
| 28 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 29 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); | 30 SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY); |
| 30 #else | 31 #else |
| 31 SetFocusBehavior(FocusBehavior::ALWAYS); | 32 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 32 #endif | 33 #endif |
| 33 | 34 |
| 34 animation_.SetSlideDuration(kOpenTimeMS); | 35 animation_.SetSlideDuration(kOpenTimeMS); |
| 35 } | 36 } |
| 36 | 37 |
| 37 LocationIconView::~LocationIconView() { | 38 LocationIconView::~LocationIconView() { |
| 38 } | 39 } |
| 39 | 40 |
| 41 bool LocationIconView::CanClick() const { |
| 42 return !location_bar_->GetOmniboxView()->IsEditingOrEmpty(); |
| 43 } |
| 44 |
| 40 gfx::Size LocationIconView::GetMinimumSize() const { | 45 gfx::Size LocationIconView::GetMinimumSize() const { |
| 41 return GetMinimumSizeForPreferredSize(GetPreferredSize()); | 46 return GetMinimumSizeForPreferredSize(GetPreferredSize()); |
| 42 } | 47 } |
| 43 | 48 |
| 44 bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) { | 49 bool LocationIconView::OnMousePressed(const ui::MouseEvent& event) { |
| 45 if (event.IsOnlyMiddleMouseButton() && | 50 if (event.IsOnlyMiddleMouseButton() && |
| 46 ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION)) { | 51 ui::Clipboard::IsSupportedClipboardType(ui::CLIPBOARD_TYPE_SELECTION)) { |
| 47 base::string16 text; | 52 base::string16 text; |
| 48 ui::Clipboard::GetForCurrentThread()->ReadText( | 53 ui::Clipboard::GetForCurrentThread()->ReadText( |
| 49 ui::CLIPBOARD_TYPE_SELECTION, &text); | 54 ui::CLIPBOARD_TYPE_SELECTION, &text); |
| 50 text = OmniboxView::SanitizeTextForPaste(text); | 55 text = OmniboxView::SanitizeTextForPaste(text); |
| 51 OmniboxEditModel* model = location_bar_->GetOmniboxView()->model(); | 56 OmniboxEditModel* model = location_bar_->GetOmniboxView()->model(); |
| 52 if (model->CanPasteAndGo(text)) | 57 if (model->CanPasteAndGo(text)) |
| 53 model->PasteAndGo(text); | 58 model->PasteAndGo(text); |
| 54 } | 59 } |
| 55 | 60 |
| 56 suppress_mouse_released_action_ = | 61 IconLabelBubbleView::OnMousePressed(event); |
| 57 WebsiteSettingsPopupView::GetShownPopupType() != | |
| 58 WebsiteSettingsPopupView::POPUP_NONE; | |
| 59 return true; | 62 return true; |
| 60 } | 63 } |
| 61 | 64 |
| 62 bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) { | 65 bool LocationIconView::OnMouseDragged(const ui::MouseEvent& event) { |
| 63 location_bar_->GetOmniboxView()->CloseOmniboxPopup(); | 66 location_bar_->GetOmniboxView()->CloseOmniboxPopup(); |
| 64 return false; | 67 return false; |
| 65 } | 68 } |
| 66 | 69 |
| 67 void LocationIconView::OnMouseReleased(const ui::MouseEvent& event) { | 70 void LocationIconView::OnMouseReleased(const ui::MouseEvent& event) { |
| 68 if (event.IsOnlyMiddleMouseButton()) | 71 if (event.IsOnlyMiddleMouseButton()) |
| 69 return; | 72 return; |
| 70 | 73 |
| 71 // If this is the second click on this view then the bubble was showing on | 74 IconLabelBubbleView::OnMouseReleased(event); |
| 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 } | 75 } |
| 81 | 76 |
| 82 void LocationIconView::OnGestureEvent(ui::GestureEvent* event) { | 77 void LocationIconView::OnGestureEvent(ui::GestureEvent* event) { |
| 83 if (event->type() != ui::ET_GESTURE_TAP) | 78 if (event->type() != ui::ET_GESTURE_TAP) |
| 84 return; | 79 return; |
| 85 OnClickOrTap(*event); | 80 |
| 81 if (location_bar_->GetOmniboxView()->IsEditingOrEmpty() && |
| 82 HitTestPoint(event->location())) { |
| 83 OnActivate(*event); |
| 84 } |
| 85 |
| 86 event->SetHandled(); | 86 event->SetHandled(); |
| 87 } | 87 } |
| 88 | 88 |
| 89 bool LocationIconView::GetTooltipText(const gfx::Point& p, | 89 bool LocationIconView::GetTooltipText(const gfx::Point& p, |
| 90 base::string16* tooltip) const { | 90 base::string16* tooltip) const { |
| 91 if (show_tooltip_) | 91 if (show_tooltip_) |
| 92 *tooltip = l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON); | 92 *tooltip = l10n_util::GetStringUTF16(IDS_TOOLTIP_LOCATION_ICON); |
| 93 return show_tooltip_; | 93 return show_tooltip_; |
| 94 } | 94 } |
| 95 | 95 |
| 96 SkColor LocationIconView::GetTextColor() const { | 96 SkColor LocationIconView::GetTextColor() const { |
| 97 return location_bar_->GetColor(LocationBarView::SECURITY_CHIP_TEXT); | 97 return location_bar_->GetColor(LocationBarView::SECURITY_CHIP_TEXT); |
| 98 } | 98 } |
| 99 | 99 |
| 100 bool LocationIconView::OnActivate(const ui::Event& event) { | 100 bool LocationIconView::OnActivate(const ui::Event& event) { |
| 101 WebContents* contents = location_bar_->GetWebContents(); | 101 WebContents* contents = location_bar_->GetWebContents(); |
| 102 if (!contents) | 102 if (!contents) |
| 103 return false; | 103 return false; |
| 104 location_bar_->delegate()->ShowWebsiteSettings(contents); | 104 location_bar_->delegate()->ShowWebsiteSettings(contents); |
| 105 return true; | 105 return true; |
| 106 } | 106 } |
| 107 | 107 |
| 108 void LocationIconView::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 108 void LocationIconView::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 109 IconLabelBubbleView::GetAccessibleNodeData(node_data); | 109 IconLabelBubbleView::GetAccessibleNodeData(node_data); |
| 110 node_data->role = ui::AX_ROLE_POP_UP_BUTTON; | 110 node_data->role = ui::AX_ROLE_POP_UP_BUTTON; |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool LocationIconView::IsBubbleShown() const { |
| 114 return WebsiteSettingsPopupView::GetShownPopupType() != |
| 115 WebsiteSettingsPopupView::POPUP_NONE; |
| 116 } |
| 117 |
| 113 gfx::Size LocationIconView::GetMinimumSizeForLabelText( | 118 gfx::Size LocationIconView::GetMinimumSizeForLabelText( |
| 114 const base::string16& text) const { | 119 const base::string16& text) const { |
| 115 views::Label label(text, font_list()); | 120 views::Label label(text, font_list()); |
| 116 return GetMinimumSizeForPreferredSize( | 121 return GetMinimumSizeForPreferredSize( |
| 117 GetSizeForLabelWidth(label.GetPreferredSize().width())); | 122 GetSizeForLabelWidth(label.GetPreferredSize().width())); |
| 118 } | 123 } |
| 119 | 124 |
| 120 void LocationIconView::SetTextVisibility(bool should_show, | 125 void LocationIconView::SetTextVisibility(bool should_show, |
| 121 bool should_animate) { | 126 bool should_animate) { |
| 122 if (!should_animate) { | 127 if (!should_animate) { |
| 123 animation_.Reset(should_show); | 128 animation_.Reset(should_show); |
| 124 } else if (should_show) { | 129 } else if (should_show) { |
| 125 animation_.Show(); | 130 animation_.Show(); |
| 126 } else { | 131 } else { |
| 127 animation_.Hide(); | 132 animation_.Hide(); |
| 128 } | 133 } |
| 129 // The label text color may have changed. | 134 // The label text color may have changed. |
| 130 OnNativeThemeChanged(GetNativeTheme()); | 135 OnNativeThemeChanged(GetNativeTheme()); |
| 131 } | 136 } |
| 132 | 137 |
| 133 double LocationIconView::WidthMultiplier() const { | 138 double LocationIconView::WidthMultiplier() const { |
| 134 return animation_.GetCurrentValue(); | 139 return animation_.GetCurrentValue(); |
| 135 } | 140 } |
| 136 | 141 |
| 137 void LocationIconView::AnimationProgressed(const gfx::Animation*) { | 142 void LocationIconView::AnimationProgressed(const gfx::Animation*) { |
| 138 location_bar_->Layout(); | 143 location_bar_->Layout(); |
| 139 location_bar_->SchedulePaint(); | 144 location_bar_->SchedulePaint(); |
| 140 } | 145 } |
| 141 | 146 |
| 142 void LocationIconView::ProcessLocatedEvent(const ui::LocatedEvent& event) { | |
| 143 if (HitTestPoint(event.location())) | |
| 144 OnActivate(event); | |
| 145 } | |
| 146 | |
| 147 gfx::Size LocationIconView::GetMinimumSizeForPreferredSize( | 147 gfx::Size LocationIconView::GetMinimumSizeForPreferredSize( |
| 148 gfx::Size size) const { | 148 gfx::Size size) const { |
| 149 const int kMinCharacters = 10; | 149 const int kMinCharacters = 10; |
| 150 size.SetToMin( | 150 size.SetToMin( |
| 151 GetSizeForLabelWidth(font_list().GetExpectedTextWidth(kMinCharacters))); | 151 GetSizeForLabelWidth(font_list().GetExpectedTextWidth(kMinCharacters))); |
| 152 return size; | 152 return size; |
| 153 } | 153 } |
| 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 |