OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/views/link_disambiguation/link_disambiguation_popup.
h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
| 9 #include "ui/aura/client/screen_position_client.h" |
| 10 #include "ui/events/event.h" |
| 11 #include "ui/events/event_processor.h" |
| 12 #include "ui/events/event_utils.h" |
| 13 #include "ui/events/gesture_event_details.h" |
| 14 #include "ui/gfx/image/image_skia.h" |
| 15 #include "ui/views/bubble/bubble_delegate.h" |
| 16 #include "ui/views/controls/image_view.h" |
| 17 |
| 18 class LinkDisambiguationPopup::ZoomBubbleView |
| 19 : public views::BubbleDelegateView { |
| 20 public: |
| 21 typedef base::Callback<void(void)> InvalidationCallback; |
| 22 |
| 23 ZoomBubbleView(const gfx::Rect& target_rect, |
| 24 const gfx::ImageSkia* zoomed_skia_image, |
| 25 const aura::Window* content, |
| 26 InvalidationCallback invalidation_cb, |
| 27 content::WebContentsViewDelegate::GestureCallback gesture_cb); |
| 28 |
| 29 void Close(); |
| 30 |
| 31 private: |
| 32 // views::View overrides |
| 33 virtual gfx::Size GetPreferredSize() const OVERRIDE; |
| 34 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; |
| 35 |
| 36 // WidgetObserver overrides |
| 37 virtual void OnWidgetDestroying(views::Widget* widget) OVERRIDE; |
| 38 |
| 39 float scale_; |
| 40 const aura::Window* content_; |
| 41 content::WebContentsViewDelegate::GestureCallback gesture_cb_; |
| 42 InvalidationCallback invalidation_cb_; |
| 43 gfx::Rect target_rect_; |
| 44 }; |
| 45 |
| 46 LinkDisambiguationPopup::ZoomBubbleView::ZoomBubbleView( |
| 47 const gfx::Rect& target_rect, |
| 48 const gfx::ImageSkia* zoomed_skia_image, |
| 49 const aura::Window* content, |
| 50 InvalidationCallback invalidation_cb, |
| 51 content::WebContentsViewDelegate::GestureCallback gesture_cb) |
| 52 : BubbleDelegateView(NULL, views::BubbleBorder::FLOAT), |
| 53 scale_(0.0f), |
| 54 content_(content), |
| 55 gesture_cb_(gesture_cb), |
| 56 invalidation_cb_(invalidation_cb), |
| 57 target_rect_(target_rect) { |
| 58 scale_ = static_cast<float>(zoomed_skia_image->width()) / |
| 59 static_cast<float>(target_rect.width()); |
| 60 |
| 61 views::ImageView* image_view = new views::ImageView(); |
| 62 image_view->SetBounds( |
| 63 0, 0, zoomed_skia_image->width(), zoomed_skia_image->height()); |
| 64 image_view->SetImage(zoomed_skia_image); |
| 65 |
| 66 AddChildView(image_view); |
| 67 |
| 68 views::BubbleDelegateView::CreateBubble(this); |
| 69 } |
| 70 |
| 71 void LinkDisambiguationPopup::ZoomBubbleView::Close() { |
| 72 if (GetWidget()) |
| 73 GetWidget()->Close(); |
| 74 } |
| 75 |
| 76 gfx::Size LinkDisambiguationPopup::ZoomBubbleView::GetPreferredSize() const { |
| 77 return target_rect_.size(); |
| 78 } |
| 79 |
| 80 void LinkDisambiguationPopup::ZoomBubbleView::OnGestureEvent( |
| 81 ui::GestureEvent* event) { |
| 82 // Scale the gesture event back to the size of the original |target_rect_|, |
| 83 // and then offset it to be relative to that |target_rect_| before sending |
| 84 // it back to the callback. |
| 85 gfx::PointF xform_location( |
| 86 (event->location().x() / scale_) + target_rect_.x(), |
| 87 (event->location().y() / scale_) + target_rect_.y()); |
| 88 ui::GestureEventDetails xform_details(event->details()); |
| 89 xform_details.set_bounding_box(gfx::RectF( |
| 90 (event->details().bounding_box().x() / scale_) + target_rect_.x(), |
| 91 (event->details().bounding_box().y() / scale_) + target_rect_.y(), |
| 92 event->details().bounding_box().width() / scale_, |
| 93 event->details().bounding_box().height() / scale_)); |
| 94 ui::GestureEvent xform_event(xform_location.x(), |
| 95 xform_location.y(), |
| 96 event->flags(), |
| 97 event->time_stamp(), |
| 98 xform_details); |
| 99 gesture_cb_.Run(&xform_event); |
| 100 event->SetHandled(); |
| 101 |
| 102 // If we completed a tap we close ourselves, as the web content will navigate |
| 103 // if the user hit a link. |
| 104 if (xform_event.type() == ui::EventType::ET_GESTURE_TAP) { |
| 105 invalidation_cb_.Run(); |
| 106 Close(); |
| 107 } |
| 108 } |
| 109 |
| 110 void LinkDisambiguationPopup::ZoomBubbleView::OnWidgetDestroying( |
| 111 views::Widget* widget) { |
| 112 invalidation_cb_.Run(); |
| 113 } |
| 114 |
| 115 LinkDisambiguationPopup::LinkDisambiguationPopup() |
| 116 : view_(NULL) { } |
| 117 |
| 118 LinkDisambiguationPopup::~LinkDisambiguationPopup() { |
| 119 Close(); |
| 120 } |
| 121 |
| 122 void LinkDisambiguationPopup::SetBitmap(const SkBitmap& zoomed_bitmap) { |
| 123 // Need to make a deep copy of this image as it is promptly deleted on behalf |
| 124 // of the caller after this function is called. |
| 125 SkBitmap zoom_copy; |
| 126 if (!zoomed_bitmap.deepCopyTo(&zoom_copy)) |
| 127 return; |
| 128 zoomed_image_ = gfx::Image::CreateFrom1xBitmap(zoom_copy); |
| 129 } |
| 130 |
| 131 void LinkDisambiguationPopup::Show( |
| 132 const gfx::Rect& target_rect, |
| 133 const gfx::NativeView content, |
| 134 content::WebContentsViewDelegate::GestureCallback callback) { |
| 135 if (zoomed_image_.IsEmpty()) |
| 136 return; |
| 137 content_ = content; |
| 138 |
| 139 view_ = new ZoomBubbleView( |
| 140 target_rect, |
| 141 zoomed_image_.ToImageSkia(), |
| 142 content_, |
| 143 base::Bind(&LinkDisambiguationPopup::InvalidateBubbleView, |
| 144 base::Unretained(this)), |
| 145 callback); |
| 146 |
| 147 // Center the zoomed bubble over the target rectangle. Since |target_rect| |
| 148 // is provided in |content_| coordinate system, we must convert it into |
| 149 // Screen coordinates for correct window positioning. |
| 150 aura::client::ScreenPositionClient* screen_position_client = |
| 151 aura::client::GetScreenPositionClient(content_->GetRootWindow()); |
| 152 gfx::Point target_screen(target_rect.x(), target_rect.y()); |
| 153 if (screen_position_client) |
| 154 screen_position_client->ConvertPointToScreen(content_, &target_screen); |
| 155 int bounds_x = std::max(0, target_screen.x() - (zoomed_image_.Width() / 2)); |
| 156 int bounds_y = std::max(0, target_screen.y() - (zoomed_image_.Height() / 2)); |
| 157 view_->GetWidget()->SetBounds(gfx::Rect(bounds_x, bounds_y, |
| 158 zoomed_image_.Width(), zoomed_image_.Height())); |
| 159 view_->GetWidget()->Show(); |
| 160 view_->GetWidget()->SetCapture(view_); |
| 161 } |
| 162 |
| 163 void LinkDisambiguationPopup::Close() { |
| 164 if (view_) { |
| 165 view_->Close(); |
| 166 view_ = NULL; |
| 167 } |
| 168 } |
| 169 |
| 170 void LinkDisambiguationPopup::InvalidateBubbleView() { |
| 171 view_ = NULL; |
| 172 } |
OLD | NEW |