extended_drag_source.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2020 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. #ifndef COMPONENTS_EXO_EXTENDED_DRAG_SOURCE_H_
  5. #define COMPONENTS_EXO_EXTENDED_DRAG_SOURCE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/drag_drop/toplevel_window_drag_delegate.h"
  9. #include "ash/wm/toplevel_window_event_handler.h"
  10. #include "base/observer_list.h"
  11. #include "components/exo/data_source_observer.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "ui/aura/scoped_window_event_targeting_blocker.h"
  14. #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
  15. #include "ui/gfx/geometry/point.h"
  16. #include "ui/gfx/geometry/point_f.h"
  17. namespace aura {
  18. class Window;
  19. }
  20. namespace gfx {
  21. class Vector2d;
  22. }
  23. namespace ui {
  24. class LocatedEvent;
  25. }
  26. namespace exo {
  27. class DataSource;
  28. class Surface;
  29. class ExtendedDragSource : public DataSourceObserver,
  30. public aura::WindowObserver,
  31. public ash::ToplevelWindowDragDelegate {
  32. public:
  33. class Delegate {
  34. public:
  35. virtual bool ShouldAllowDropAnywhere() const = 0;
  36. virtual bool ShouldLockCursor() const = 0;
  37. virtual void OnSwallowed(const std::string& mime_type) = 0;
  38. virtual void OnUnswallowed(const std::string& mime_type,
  39. const gfx::Vector2d& offset) = 0;
  40. virtual void OnDataSourceDestroying() = 0;
  41. protected:
  42. virtual ~Delegate() = default;
  43. };
  44. class Observer {
  45. public:
  46. virtual void OnExtendedDragSourceDestroying(ExtendedDragSource* source) = 0;
  47. protected:
  48. virtual ~Observer() = default;
  49. };
  50. static ExtendedDragSource* Get();
  51. ExtendedDragSource(DataSource* source, Delegate* delegate);
  52. ExtendedDragSource(const ExtendedDragSource&) = delete;
  53. ExtendedDragSource& operator=(const ExtendedDragSource&) = delete;
  54. ~ExtendedDragSource() override;
  55. void AddObserver(Observer* observer);
  56. void RemoveObserver(Observer* observer);
  57. bool IsActive() const;
  58. void Drag(Surface* surface, const gfx::Vector2d& offset);
  59. // ash::ToplevelWindowDragDelegate:
  60. void OnToplevelWindowDragStarted(const gfx::PointF& start_location,
  61. ui::mojom::DragEventSource source,
  62. aura::Window* drag_source_window) override;
  63. ui::mojom::DragOperation OnToplevelWindowDragDropped() override;
  64. void OnToplevelWindowDragCancelled() override;
  65. void OnToplevelWindowDragEvent(ui::LocatedEvent* event) override;
  66. // DataSourceObserver:
  67. void OnDataSourceDestroying(DataSource* source) override;
  68. // aura::WindowObserver:
  69. void OnWindowDestroyed(aura::Window* window) override;
  70. aura::Window* GetDraggedWindowForTesting();
  71. absl::optional<gfx::Vector2d> GetDragOffsetForTesting() const;
  72. aura::Window* GetDragSourceWindowForTesting();
  73. private:
  74. class DraggedWindowHolder;
  75. void MaybeLockCursor();
  76. void UnlockCursor();
  77. void StartDrag(aura::Window* toplevel);
  78. void OnDraggedWindowVisibilityChanging(bool visible);
  79. void OnDraggedWindowVisibilityChanged(bool visible);
  80. void Cleanup();
  81. static ExtendedDragSource* instance_;
  82. DataSource* source_ = nullptr;
  83. // Created and destroyed at wayland/zcr_extended_drag.cc and its lifetime is
  84. // tied to the zcr_extended_drag_source_v1 object it's attached to.
  85. Delegate* const delegate_;
  86. // The pointer location in screen coordinates.
  87. gfx::PointF pointer_location_;
  88. ui::mojom::DragEventSource drag_event_source_;
  89. bool cursor_locked_ = false;
  90. std::unique_ptr<DraggedWindowHolder> dragged_window_holder_;
  91. std::unique_ptr<aura::ScopedWindowEventTargetingBlocker> event_blocker_;
  92. aura::Window* drag_source_window_ = nullptr;
  93. base::ObserverList<Observer>::Unchecked observers_;
  94. base::WeakPtrFactory<ExtendedDragSource> weak_factory_{this};
  95. };
  96. } // namespace exo
  97. #endif // COMPONENTS_EXO_EXTENDED_DRAG_SOURCE_H_