Chromium Code Reviews| 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..7d723b2cd3e1543af05c63f6029e09e1c204271f 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,34 @@ 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; |
| + |
| +// Drag widget is positioned off screen when drag image is not set or is |
| +// completely transparent. |
| +const gfx::Size kNullDragWidgetSize = gfx::Size(1, 1); |
| +const gfx::Point kNullDragWidgetLocation = gfx::Point(-100, -100); |
|
pkotwicz
2014/05/12 16:04:29
I do not know if there is much precedent for non p
varkha
2014/05/20 17:13:35
Good catch. https://engdoc.corp.google.com/eng/doc
|
| + |
| +bool CheckIfIconValid(const gfx::ImageSkia& drag_image) { |
|
pkotwicz
2014/05/12 16:04:29
Is CheckIfIconValid() still needed in a world wher
varkha
2014/05/20 17:13:35
Done.
|
| + // Because we need a GL context per window, we do a quick check so that we |
| + // don't make another context if the window would just be displaying a mostly |
| + // transparent image. |
| + const SkBitmap* in_bitmap = drag_image.bitmap(); |
| + SkAutoLockPixels in_lock(*in_bitmap); |
| + for (int y = 0; y < in_bitmap->height(); ++y) { |
| + uint32* in_row = in_bitmap->getAddr32(0, y); |
| + |
| + for (int x = 0; x < in_bitmap->width(); ++x) { |
| + if (SkColorGetA(in_row[x]) > kMinAlpha) |
| + return true; |
| + } |
| + } |
| + |
| + return false; |
| +} |
| + |
| const int kMinXdndVersion = 5; |
| const int kWillAcceptDrop = 1; |
| @@ -504,17 +535,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 +651,16 @@ int DesktopDragDropClientAuraX11::StartDragAndDrop( |
| base::WeakPtr<DesktopDragDropClientAuraX11> alive( |
| weak_ptr_factory_.GetWeakPtr()); |
| + SetDragImage(source_provider_->GetDragImage(), |
| + source_provider_->GetDragImageOffset()); |
| + CreateDragImageWindow(); |
| // 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 +744,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 +760,16 @@ void DesktopDragDropClientAuraX11::ProcessMouseMove( |
| if (source_state_ != SOURCE_STATE_OTHER) |
| return; |
| + if (drag_widget_.get()) { |
|
pkotwicz
2014/05/12 16:04:29
If we intentionally moved |drag_widget_| offscreen
varkha
2014/05/20 17:13:35
I am using drag_image_.isNull() for that (and rese
|
| + gfx::Point location = drag_image_.isNull() ? |
| + kNullDragWidgetLocation : |
| + gfx::ToFlooredPoint(screen_point - drag_offset_); |
| + drag_widget_->SetBounds(gfx::Rect( |
| + location, |
| + drag_image_.isNull() ? kNullDragWidgetSize : drag_image_.size())); |
| + drag_widget_->StackAtTop(); |
| + } |
| + |
| // Find the current window the cursor is over. |
| ::Window mouse_window = None; |
| ::Window dest_window = None; |
| @@ -871,6 +904,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 +1083,52 @@ void DesktopDragDropClientAuraX11::SendXClientEvent(::Window xid, |
| XSendEvent(xdisplay_, xid, False, 0, xev); |
| } |
| +void DesktopDragDropClientAuraX11::SetDragImage(const gfx::ImageSkia& image, |
| + gfx::Vector2dF offset) { |
| + /*if (drag_image_.isNull() || !CheckIfIconValid(drag_image_))*/ { |
|
pkotwicz
2014/05/12 16:04:29
Remove the if block entirely. It is not needed.
varkha
2014/05/20 17:13:35
Done.
|
| + drag_image_ = gfx::ImageSkia(); |
| + drag_offset_ = gfx::Vector2dF(); |
| + return; |
| + } |
| + 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; |
| + params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| + params.accept_events = false; |
| + |
| + gfx::Point location = drag_image_.isNull() ? |
|
pkotwicz
2014/05/12 16:04:29
I will defer to sadrul@/erg@ as to whether they li
varkha
2014/05/20 17:13:35
Right. Keeping the drag widget offscreen matches t
|
| + kNullDragWidgetLocation : |
| + gfx::ToFlooredPoint( |
| + gfx::Screen::GetNativeScreen()->GetCursorScreenPoint() - |
| + drag_offset_); |
| + params.bounds = gfx::Rect( |
| + location, |
| + drag_image_.isNull() ? kNullDragWidgetSize : 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); |
|
pkotwicz
2014/05/12 16:04:29
We should have a comment as to why we set capture
varkha
2014/05/20 17:13:35
Done.
|
| + widget->GetNativeWindow()->SetCapture(); |
| + drag_widget_.reset(widget); |
| +} |
| + |
| } // namespace views |