Index: ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.cc |
diff --git a/ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.cc b/ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.cc |
index b2d4eedb489ac6b696c2e3eb08733f82944d1c77..4bc23a91f23234795c81177e7890dcce426ff08f 100644 |
--- a/ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.cc |
+++ b/ui/views/widget/desktop_aura/desktop_drag_drop_client_aurax11.cc |
@@ -19,8 +19,11 @@ |
#include "ui/base/x/x11_util.h" |
#include "ui/events/event.h" |
#include "ui/events/platform/platform_event_source.h" |
+#include "ui/gfx/screen.h" |
+#include "ui/views/controls/image_view.h" |
#include "ui/views/widget/desktop_aura/desktop_native_cursor_manager.h" |
#include "ui/views/widget/desktop_aura/x11_topmost_window_finder.h" |
+#include "ui/views/widget/widget.h" |
#include "ui/wm/public/drag_drop_client.h" |
#include "ui/wm/public/drag_drop_delegate.h" |
@@ -29,6 +32,11 @@ using ui::OSExchangeData; |
namespace { |
+// The minimum alpha before we declare a pixel transparent when searching in |
+// our source image. |
+const uint32 kMinAlpha = 32; |
+const unsigned char kDragWidgetOpacity = 0xc0; |
+ |
const int kMinXdndVersion = 5; |
const int kWillAcceptDrop = 1; |
@@ -390,6 +398,7 @@ DesktopDragDropClientAuraX11::DesktopDragDropClientAuraX11( |
grab_cursor_(cursor_manager->GetInitializedCursor(ui::kCursorGrabbing)), |
copy_grab_cursor_(cursor_manager->GetInitializedCursor(ui::kCursorCopy)), |
move_grab_cursor_(cursor_manager->GetInitializedCursor(ui::kCursorMove)), |
+ null_drag_widget_bounds_(-100, -100, 1, 1), |
weak_ptr_factory_(this) { |
DCHECK(g_live_client_map.Get().find(xwindow) == |
g_live_client_map.Get().end()); |
@@ -504,17 +513,7 @@ void DesktopDragDropClientAuraX11::OnXdndStatus( |
return; |
} |
- switch (negotiated_operation_) { |
- case ui::DragDropTypes::DRAG_COPY: |
- move_loop_.UpdateCursor(copy_grab_cursor_); |
- break; |
- case ui::DragDropTypes::DRAG_MOVE: |
- move_loop_.UpdateCursor(move_grab_cursor_); |
- break; |
- default: |
- move_loop_.UpdateCursor(grab_cursor_); |
- break; |
- } |
+ UpdateCursor(); |
// Note: event.data.[2,3] specify a rectangle. It is a request by the other |
// window to not send further XdndPosition messages while the cursor is |
@@ -630,15 +629,21 @@ int DesktopDragDropClientAuraX11::StartDragAndDrop( |
base::WeakPtr<DesktopDragDropClientAuraX11> alive( |
weak_ptr_factory_.GetWeakPtr()); |
+ SetDragImage(source_provider_->GetDragImage(), |
+ source_provider_->GetDragImageOffset()); |
+ CreateDragImageWindow(); |
+ // Capture drag widget to continue receiving events even if the drag source |
+ // gets destroyed during the drag such as when we drag an item out of a menu |
+ // and the menu closes. |
+ drag_widget_->GetNativeWindow()->SetCapture(); |
+ |
// Windows has a specific method, DoDragDrop(), which performs the entire |
// drag. We have to emulate this, so we spin off a nested runloop which will |
// track all cursor movement and reroute events to a specific handler. |
- move_loop_.SetDragImage(source_provider_->GetDragImage(), |
- source_provider_->GetDragImageOffset()); |
- move_loop_.RunMoveLoop(source_window, grab_cursor_); |
+ move_loop_.RunMoveLoop(); |
if (alive) { |
- move_loop_.SetDragImage(gfx::ImageSkia(), gfx::Vector2dF()); |
+ SetDragImage(gfx::ImageSkia(), gfx::Vector2dF()); |
source_provider_ = NULL; |
g_current_drag_drop_client = NULL; |
@@ -722,6 +727,7 @@ void DesktopDragDropClientAuraX11::OnMouseReleased() { |
} |
void DesktopDragDropClientAuraX11::OnMoveLoopEnded() { |
+ drag_widget_.reset(); |
if (source_current_window_ != None) { |
SendXdndLeave(source_current_window_); |
source_current_window_ = None; |
@@ -737,6 +743,17 @@ void DesktopDragDropClientAuraX11::ProcessMouseMove( |
if (source_state_ != SOURCE_STATE_OTHER) |
return; |
+ if (drag_widget_.get()) { |
+ if (drag_image_.isNull()) { |
+ drag_widget_->SetBounds(null_drag_widget_bounds_); |
+ } else { |
+ drag_widget_->SetBounds(gfx::Rect( |
+ gfx::ToFlooredPoint(screen_point - drag_offset_), |
+ drag_image_.size())); |
+ } |
+ drag_widget_->StackAtTop(); |
+ } |
+ |
// Find the current window the cursor is over. |
::Window mouse_window = None; |
::Window dest_window = None; |
@@ -871,6 +888,27 @@ ui::SelectionFormatMap DesktopDragDropClientAuraX11::GetFormatMap() const { |
ui::SelectionFormatMap(); |
} |
+void DesktopDragDropClientAuraX11::UpdateCursor() { |
+ gfx::NativeCursor cursor = grab_cursor_; |
+ switch (negotiated_operation_) { |
+ case ui::DragDropTypes::DRAG_COPY: |
+ cursor = copy_grab_cursor_; |
+ break; |
+ case ui::DragDropTypes::DRAG_MOVE: |
+ cursor = move_grab_cursor_; |
+ break; |
+ default: |
+ break; |
+ } |
+ |
+ // Update the pointer with the new cursor if a grab is active. |
+ XChangeActivePointerGrab( |
+ xdisplay_, |
+ ButtonPressMask | ButtonReleaseMask | PointerMotionMask, |
+ cursor.platform(), |
+ CurrentTime); |
+} |
+ |
void DesktopDragDropClientAuraX11::CompleteXdndPosition( |
::Window source_window, |
const gfx::Point& screen_point) { |
@@ -1029,4 +1067,47 @@ void DesktopDragDropClientAuraX11::SendXClientEvent(::Window xid, |
XSendEvent(xdisplay_, xid, False, 0, xev); |
} |
+void DesktopDragDropClientAuraX11::SetDragImage(const gfx::ImageSkia& image, |
+ gfx::Vector2dF offset) { |
+ drag_image_ = image; |
+ drag_offset_ = offset; |
+ // Reset the Y offset, so that the drag-image is always just below the cursor, |
+ // so that it is possible to see where the cursor is going. |
+ drag_offset_.set_y(0.f); |
+} |
+ |
+void DesktopDragDropClientAuraX11::CreateDragImageWindow() { |
+ Widget* widget = new Widget; |
+ Widget::InitParams params(Widget::InitParams::TYPE_DRAG); |
+ params.opacity = Widget::InitParams::OPAQUE_WINDOW; |
sadrul
2014/05/23 14:39:53
Should this be TRANSLUCENT_WINDOW?
varkha
2014/05/23 15:38:36
Done.
|
+ params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
+ params.accept_events = false; |
+ |
+ if (drag_image_.isNull()) { |
+ params.bounds = null_drag_widget_bounds_; |
+ } else { |
+ params.bounds = gfx::Rect( |
+ gfx::ToFlooredPoint( |
+ gfx::Screen::GetNativeScreen()->GetCursorScreenPoint() - |
+ drag_offset_), |
+ drag_image_.size()); |
+ } |
+ widget->set_focus_on_creation(false); |
+ widget->set_frame_type(Widget::FRAME_TYPE_FORCE_NATIVE); |
+ widget->Init(params); |
+ widget->SetOpacity(kDragWidgetOpacity); |
+ widget->GetNativeWindow()->SetName("DragWindow"); |
+ |
+ if (!drag_image_.isNull()) { |
+ ImageView* image = new ImageView(); |
+ image->SetImage(drag_image_); |
+ image->SetBounds(0, 0, drag_image_.width(), drag_image_.height()); |
+ widget->SetContentsView(image); |
+ } |
+ |
+ widget->Show(); |
+ widget->GetNativeWindow()->layer()->SetFillsBoundsOpaquely(false); |
+ drag_widget_.reset(widget); |
+} |
+ |
} // namespace views |