drag_util_views.cc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/ui/drag_util.h"
  5. #include "ui/aura/client/drag_drop_client.h"
  6. #include "ui/aura/window.h"
  7. #include "ui/base/dragdrop/drag_drop_types.h"
  8. #include "ui/base/dragdrop/file_info/file_info.h"
  9. #include "ui/base/dragdrop/os_exchange_data.h"
  10. #include "ui/display/screen.h"
  11. #include "ui/gfx/geometry/point.h"
  12. #include "ui/views/button_drag_utils.h"
  13. #include "ui/views/widget/widget.h"
  14. #include "url/gurl.h"
  15. namespace electron {
  16. void DragFileItems(const std::vector<base::FilePath>& files,
  17. const gfx::Image& icon,
  18. gfx::NativeView view) {
  19. // Set up our OLE machinery
  20. auto data = std::make_unique<ui::OSExchangeData>();
  21. button_drag_utils::SetDragImage(
  22. GURL(), files[0].LossyDisplayName(), icon.AsImageSkia(), nullptr,
  23. *views::Widget::GetTopLevelWidgetForNativeView(view), data.get());
  24. std::vector<ui::FileInfo> file_infos;
  25. file_infos.reserve(files.size());
  26. for (const base::FilePath& file : files) {
  27. file_infos.emplace_back(file, base::FilePath());
  28. }
  29. data->SetFilenames(file_infos);
  30. aura::Window* root_window = view->GetRootWindow();
  31. if (!root_window || !aura::client::GetDragDropClient(root_window))
  32. return;
  33. gfx::Point location = display::Screen::GetScreen()->GetCursorScreenPoint();
  34. // TODO(varunjain): Properly determine and send DRAG_EVENT_SOURCE below.
  35. aura::client::GetDragDropClient(root_window)
  36. ->StartDragAndDrop(
  37. std::move(data), root_window, view, location,
  38. ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK,
  39. ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
  40. }
  41. } // namespace electron