| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/link_disambiguation/link_disambiguation_popup.
h" | 5 #include "chrome/browser/ui/views/link_disambiguation/link_disambiguation_popup.
h" |
| 6 | 6 |
| 7 #include "base/macros.h" | 7 #include "base/macros.h" |
| 8 #include "ui/aura/client/screen_position_client.h" | 8 #include "ui/aura/client/screen_position_client.h" |
| 9 #include "ui/events/event.h" | 9 #include "ui/events/event.h" |
| 10 #include "ui/events/event_processor.h" | 10 #include "ui/events/event_processor.h" |
| 11 #include "ui/events/event_utils.h" | 11 #include "ui/events/event_utils.h" |
| 12 #include "ui/events/gesture_event_details.h" | 12 #include "ui/events/gesture_event_details.h" |
| 13 #include "ui/gfx/display.h" | 13 #include "ui/gfx/display.h" |
| 14 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
| 15 #include "ui/gfx/image/image_skia.h" | 15 #include "ui/gfx/image/image_skia.h" |
| 16 #include "ui/gfx/screen.h" | 16 #include "ui/gfx/screen.h" |
| 17 #include "ui/views/bubble/bubble_delegate.h" | 17 #include "ui/views/bubble/bubble_dialog_delegate.h" |
| 18 #include "ui/views/controls/image_view.h" | 18 #include "ui/views/controls/image_view.h" |
| 19 | 19 |
| 20 class LinkDisambiguationPopup::ZoomBubbleView | 20 class LinkDisambiguationPopup::ZoomBubbleView |
| 21 : public views::BubbleDelegateView { | 21 : public views::BubbleDialogDelegateView { |
| 22 public: | 22 public: |
| 23 ZoomBubbleView(views::Widget* top_level_widget, | 23 ZoomBubbleView(views::Widget* top_level_widget, |
| 24 const gfx::Rect& target_rect, | 24 const gfx::Rect& target_rect, |
| 25 const gfx::ImageSkia* zoomed_skia_image, | 25 const gfx::ImageSkia* zoomed_skia_image, |
| 26 LinkDisambiguationPopup* popup, | 26 LinkDisambiguationPopup* popup, |
| 27 const base::Callback<void(ui::GestureEvent*)>& gesture_cb, | 27 const base::Callback<void(ui::GestureEvent*)>& gesture_cb, |
| 28 const base::Callback<void(ui::MouseEvent*)>& mouse_cb); | 28 const base::Callback<void(ui::MouseEvent*)>& mouse_cb); |
| 29 | 29 |
| 30 void Close(); | |
| 31 | |
| 32 private: | 30 private: |
| 33 // views::View overrides | 31 // views::View overrides |
| 34 gfx::Size GetPreferredSize() const override; | 32 gfx::Size GetPreferredSize() const override; |
| 35 void OnGestureEvent(ui::GestureEvent* event) override; | 33 void OnGestureEvent(ui::GestureEvent* event) override; |
| 36 void OnMouseEvent(ui::MouseEvent* event) override; | 34 void OnMouseEvent(ui::MouseEvent* event) override; |
| 37 | 35 |
| 38 // WidgetObserver overrides | 36 // views::WidgetObserver overrides |
| 39 void OnWidgetClosing(views::Widget* widget) override; | 37 void OnWidgetClosing(views::Widget* widget) override; |
| 40 | 38 |
| 39 // views::BubbleDialogDelegate overrides |
| 40 int GetDialogButtons() const override; |
| 41 |
| 41 const float scale_; | 42 const float scale_; |
| 42 const base::Callback<void(ui::GestureEvent*)> gesture_cb_; | 43 const base::Callback<void(ui::GestureEvent*)> gesture_cb_; |
| 43 const base::Callback<void(ui::MouseEvent*)> mouse_cb_; | 44 const base::Callback<void(ui::MouseEvent*)> mouse_cb_; |
| 44 LinkDisambiguationPopup* popup_; | 45 LinkDisambiguationPopup* popup_; |
| 45 const gfx::Rect target_rect_; | 46 const gfx::Rect target_rect_; |
| 46 | 47 |
| 47 DISALLOW_COPY_AND_ASSIGN(ZoomBubbleView); | 48 DISALLOW_COPY_AND_ASSIGN(ZoomBubbleView); |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 LinkDisambiguationPopup::ZoomBubbleView::ZoomBubbleView( | 51 LinkDisambiguationPopup::ZoomBubbleView::ZoomBubbleView( |
| 51 views::Widget* top_level_widget, | 52 views::Widget* top_level_widget, |
| 52 const gfx::Rect& target_rect, | 53 const gfx::Rect& target_rect, |
| 53 const gfx::ImageSkia* zoomed_skia_image, | 54 const gfx::ImageSkia* zoomed_skia_image, |
| 54 LinkDisambiguationPopup* popup, | 55 LinkDisambiguationPopup* popup, |
| 55 const base::Callback<void(ui::GestureEvent*)>& gesture_cb, | 56 const base::Callback<void(ui::GestureEvent*)>& gesture_cb, |
| 56 const base::Callback<void(ui::MouseEvent*)>& mouse_cb) | 57 const base::Callback<void(ui::MouseEvent*)>& mouse_cb) |
| 57 : BubbleDelegateView( | 58 : BubbleDialogDelegateView( |
| 58 top_level_widget ? top_level_widget->GetContentsView() : nullptr, | 59 top_level_widget ? top_level_widget->GetContentsView() : nullptr, |
| 59 views::BubbleBorder::FLOAT), | 60 views::BubbleBorder::FLOAT), |
| 60 scale_(static_cast<float>(zoomed_skia_image->width()) / | 61 scale_(static_cast<float>(zoomed_skia_image->width()) / |
| 61 static_cast<float>(target_rect.width())), | 62 static_cast<float>(target_rect.width())), |
| 62 gesture_cb_(gesture_cb), | 63 gesture_cb_(gesture_cb), |
| 63 mouse_cb_(mouse_cb), | 64 mouse_cb_(mouse_cb), |
| 64 popup_(popup), | 65 popup_(popup), |
| 65 target_rect_(target_rect) { | 66 target_rect_(target_rect) { |
| 66 views::ImageView* image_view = new views::ImageView(); | 67 views::ImageView* image_view = new views::ImageView(); |
| 67 image_view->SetBounds( | 68 image_view->SetSize(zoomed_skia_image->size()); |
| 68 0, 0, zoomed_skia_image->width(), zoomed_skia_image->height()); | |
| 69 image_view->SetImage(zoomed_skia_image); | 69 image_view->SetImage(zoomed_skia_image); |
| 70 | |
| 71 AddChildView(image_view); | 70 AddChildView(image_view); |
| 72 | 71 |
| 73 views::BubbleDelegateView::CreateBubble(this); | 72 views::BubbleDialogDelegateView::CreateBubble(this); |
| 74 } | |
| 75 | |
| 76 void LinkDisambiguationPopup::ZoomBubbleView::Close() { | |
| 77 if (GetWidget()) | |
| 78 GetWidget()->Close(); | |
| 79 } | 73 } |
| 80 | 74 |
| 81 gfx::Size LinkDisambiguationPopup::ZoomBubbleView::GetPreferredSize() const { | 75 gfx::Size LinkDisambiguationPopup::ZoomBubbleView::GetPreferredSize() const { |
| 82 return target_rect_.size(); | 76 return target_rect_.size(); |
| 83 } | 77 } |
| 84 | 78 |
| 85 void LinkDisambiguationPopup::ZoomBubbleView::OnMouseEvent( | 79 void LinkDisambiguationPopup::ZoomBubbleView::OnMouseEvent( |
| 86 ui::MouseEvent* event) { | 80 ui::MouseEvent* event) { |
| 87 // Transform mouse event back to coordinate system of the web content window | 81 // Transform mouse event back to coordinate system of the web content window |
| 88 // before providing to the callback. | 82 // before providing to the callback. |
| 89 ui::MouseEvent xform_event(event->type(), gfx::Point(), gfx::Point(), | 83 ui::MouseEvent xform_event(event->type(), gfx::Point(), gfx::Point(), |
| 90 ui::EventTimeForNow(), event->flags(), | 84 ui::EventTimeForNow(), event->flags(), |
| 91 event->changed_button_flags()); | 85 event->changed_button_flags()); |
| 92 gfx::PointF xform_location( | 86 gfx::PointF xform_location( |
| 93 (event->location().x() / scale_) + target_rect_.x(), | 87 (event->location().x() / scale_) + target_rect_.x(), |
| 94 (event->location().y() / scale_) + target_rect_.y()); | 88 (event->location().y() / scale_) + target_rect_.y()); |
| 95 xform_event.set_location_f(xform_location); | 89 xform_event.set_location_f(xform_location); |
| 96 xform_event.set_root_location_f(xform_location); | 90 xform_event.set_root_location_f(xform_location); |
| 97 mouse_cb_.Run(&xform_event); | 91 mouse_cb_.Run(&xform_event); |
| 98 event->SetHandled(); | 92 event->SetHandled(); |
| 99 | 93 |
| 100 // If user completed a click we can close the window. | 94 // If user completed a click we can close the window. |
| 101 if (event->type() == ui::EventType::ET_MOUSE_RELEASED) | 95 if (event->type() == ui::EventType::ET_MOUSE_RELEASED) |
| 102 Close(); | 96 GetWidget()->Close(); |
| 103 } | 97 } |
| 104 | 98 |
| 105 void LinkDisambiguationPopup::ZoomBubbleView::OnGestureEvent( | 99 void LinkDisambiguationPopup::ZoomBubbleView::OnGestureEvent( |
| 106 ui::GestureEvent* event) { | 100 ui::GestureEvent* event) { |
| 107 // If we receive gesture events that are outside of our bounds we close | 101 // If we receive gesture events that are outside of our bounds we close |
| 108 // ourselves, as perhaps the user has decided on a different part of the page. | 102 // ourselves, as perhaps the user has decided on a different part of the page. |
| 109 if (event->location().x() > bounds().width() || | 103 if (event->location().x() > bounds().width() || |
| 110 event->location().y() > bounds().height()) { | 104 event->location().y() > bounds().height()) { |
| 111 Close(); | 105 GetWidget()->Close(); |
| 112 return; | 106 return; |
| 113 } | 107 } |
| 114 | 108 |
| 115 // Scale the gesture event back to the size of the original |target_rect_|, | 109 // Scale the gesture event back to the size of the original |target_rect_|, |
| 116 // and then offset it to be relative to that |target_rect_| before sending | 110 // and then offset it to be relative to that |target_rect_| before sending |
| 117 // it back to the callback. | 111 // it back to the callback. |
| 118 gfx::PointF xform_location( | 112 gfx::PointF xform_location( |
| 119 (event->location().x() / scale_) + target_rect_.x(), | 113 (event->location().x() / scale_) + target_rect_.x(), |
| 120 (event->location().y() / scale_) + target_rect_.y()); | 114 (event->location().y() / scale_) + target_rect_.y()); |
| 121 ui::GestureEventDetails xform_details(event->details()); | 115 ui::GestureEventDetails xform_details(event->details()); |
| 122 xform_details.set_bounding_box(gfx::RectF( | 116 xform_details.set_bounding_box(gfx::RectF( |
| 123 (event->details().bounding_box().x() / scale_) + target_rect_.x(), | 117 (event->details().bounding_box().x() / scale_) + target_rect_.x(), |
| 124 (event->details().bounding_box().y() / scale_) + target_rect_.y(), | 118 (event->details().bounding_box().y() / scale_) + target_rect_.y(), |
| 125 event->details().bounding_box().width() / scale_, | 119 event->details().bounding_box().width() / scale_, |
| 126 event->details().bounding_box().height() / scale_)); | 120 event->details().bounding_box().height() / scale_)); |
| 127 ui::GestureEvent xform_event(xform_location.x(), | 121 ui::GestureEvent xform_event(xform_location.x(), |
| 128 xform_location.y(), | 122 xform_location.y(), |
| 129 event->flags(), | 123 event->flags(), |
| 130 event->time_stamp(), | 124 event->time_stamp(), |
| 131 xform_details); | 125 xform_details); |
| 132 gesture_cb_.Run(&xform_event); | 126 gesture_cb_.Run(&xform_event); |
| 133 event->SetHandled(); | 127 event->SetHandled(); |
| 134 | 128 |
| 135 // If we completed a tap we close ourselves, as the web content will navigate | 129 // If we completed a tap we close ourselves, as the web content will navigate |
| 136 // if the user hit a link. | 130 // if the user hit a link. |
| 137 if (event->type() == ui::EventType::ET_GESTURE_TAP) | 131 if (event->type() == ui::EventType::ET_GESTURE_TAP) |
| 138 Close(); | 132 GetWidget()->Close(); |
| 139 } | 133 } |
| 140 | 134 |
| 141 void LinkDisambiguationPopup::ZoomBubbleView::OnWidgetClosing( | 135 void LinkDisambiguationPopup::ZoomBubbleView::OnWidgetClosing( |
| 142 views::Widget* widget) { | 136 views::Widget* widget) { |
| 143 popup_->InvalidateBubbleView(); | 137 popup_->InvalidateBubbleView(); |
| 144 } | 138 } |
| 145 | 139 |
| 140 int LinkDisambiguationPopup::ZoomBubbleView::GetDialogButtons() const { |
| 141 return ui::DIALOG_BUTTON_NONE; |
| 142 } |
| 143 |
| 146 LinkDisambiguationPopup::LinkDisambiguationPopup() | 144 LinkDisambiguationPopup::LinkDisambiguationPopup() |
| 147 : content_(NULL), | 145 : content_(NULL), |
| 148 view_(NULL) { | 146 view_(NULL) { |
| 149 } | 147 } |
| 150 | 148 |
| 151 LinkDisambiguationPopup::~LinkDisambiguationPopup() { | 149 LinkDisambiguationPopup::~LinkDisambiguationPopup() { |
| 152 Close(); | 150 Close(); |
| 153 } | 151 } |
| 154 | 152 |
| 155 void LinkDisambiguationPopup::Show( | 153 void LinkDisambiguationPopup::Show( |
| (...skipping 29 matching lines...) Expand all Loading... |
| 185 zoomed_bitmap.width(), | 183 zoomed_bitmap.width(), |
| 186 zoomed_bitmap.height()); | 184 zoomed_bitmap.height()); |
| 187 const gfx::Display display = | 185 const gfx::Display display = |
| 188 gfx::Screen::GetScreen()->GetDisplayNearestWindow(content); | 186 gfx::Screen::GetScreen()->GetDisplayNearestWindow(content); |
| 189 window_bounds.AdjustToFit(display.work_area()); | 187 window_bounds.AdjustToFit(display.work_area()); |
| 190 view_->GetWidget()->SetBounds(window_bounds); | 188 view_->GetWidget()->SetBounds(window_bounds); |
| 191 view_->GetWidget()->Show(); | 189 view_->GetWidget()->Show(); |
| 192 } | 190 } |
| 193 | 191 |
| 194 void LinkDisambiguationPopup::Close() { | 192 void LinkDisambiguationPopup::Close() { |
| 195 if (view_) { | 193 if (view_ && view_->GetWidget()) |
| 196 view_->Close(); | 194 view_->GetWidget()->Close(); |
| 197 view_ = NULL; | |
| 198 } | |
| 199 } | 195 } |
| 200 | 196 |
| 201 void LinkDisambiguationPopup::InvalidateBubbleView() { | 197 void LinkDisambiguationPopup::InvalidateBubbleView() { |
| 202 view_ = NULL; | 198 view_ = nullptr; |
| 203 } | 199 } |
| OLD | NEW |