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