Avi Drissman | 3a215d1e | 2022-09-07 19:43:09 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef ASH_DRAG_DROP_DRAG_IMAGE_VIEW_H_ |
| 6 | #define ASH_DRAG_DROP_DRAG_IMAGE_VIEW_H_ |
| 7 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 8 | #include "ash/ash_export.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 9 | #include "ui/base/dragdrop/drag_drop_types.h" |
Henrique Ferreiro | 1748fd1 | 2020-08-04 12:51:46 | [diff] [blame] | 10 | #include "ui/base/dragdrop/mojom/drag_drop_types.mojom-forward.h" |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 11 | #include "ui/gfx/geometry/point.h" |
| 12 | #include "ui/gfx/geometry/size.h" |
| 13 | #include "ui/views/controls/image_view.h" |
| 14 | |
sky | f14333c9 | 2017-04-18 22:21:01 | [diff] [blame] | 15 | namespace aura { |
| 16 | class Window; |
| 17 | } |
| 18 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 19 | namespace gfx { |
| 20 | class Image; |
| 21 | } |
| 22 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 23 | namespace ash { |
| 24 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 25 | // This class allows to show a (native) view always on top of everything. It |
| 26 | // does this by creating a widget and setting the content as the given view. The |
| 27 | // caller can then use this object to freely move / drag it around on the |
| 28 | // desktop in screen coordinates. |
| 29 | class ASH_EXPORT DragImageView : public views::ImageView { |
| 30 | public: |
Peter Boström | ec31a04 | 2021-09-16 23:37:34 | [diff] [blame] | 31 | DragImageView(const DragImageView&) = delete; |
| 32 | DragImageView& operator=(const DragImageView&) = delete; |
| 33 | |
Allen Bauer | 35d4e72 | 2020-06-16 22:47:56 | [diff] [blame] | 34 | ~DragImageView() override; |
| 35 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 36 | // |root_window| is the root window on which to create the drag image widget. |
| 37 | // |source| is the event source that started this drag drop operation (touch |
| 38 | // or mouse). It is used to determine attributes of the drag image such as |
| 39 | // whether to show drag operation hint on top of the image. |
Ana Salazar | 40d497c | 2023-02-23 22:47:07 | [diff] [blame] | 40 | static std::unique_ptr<views::Widget> Create( |
| 41 | aura::Window* root_window, |
| 42 | ui::mojom::DragEventSource source); |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 43 | |
| 44 | // Sets the bounds of the native widget in screen |
| 45 | // coordinates. |
| 46 | // TODO(oshima): Looks like this is root window's |
| 47 | // coordinate. Change this to screen's coordinate. |
| 48 | void SetBoundsInScreen(const gfx::Rect& bounds); |
| 49 | |
| 50 | // Sets the position of the native widget. |
| 51 | void SetScreenPosition(const gfx::Point& position); |
| 52 | |
| 53 | // Gets the image position in screen coordinates. |
| 54 | gfx::Rect GetBoundsInScreen() const; |
| 55 | |
| 56 | // Sets the visibility of the native widget. |
| 57 | void SetWidgetVisible(bool visible); |
| 58 | |
| 59 | // For touch drag drop, we show a hint corresponding to the drag operation |
| 60 | // (since no mouse cursor is visible). These functions set the hint |
| 61 | // parameters. |
| 62 | void SetTouchDragOperationHintOff(); |
| 63 | |
| 64 | // |operation| is a bit field indicating allowable drag operations from |
| 65 | // ui::DragDropTypes::DragOperation. |
| 66 | void SetTouchDragOperation(int operation); |
| 67 | void SetTouchDragOperationHintPosition(const gfx::Point& position); |
| 68 | |
| 69 | // Sets the |opacity| of the image view between 0.0 and 1.0. |
| 70 | void SetOpacity(float opacity); |
| 71 | |
| 72 | gfx::Size GetMinimumSize() const override; |
| 73 | |
| 74 | private: |
Lei Zhang | 020052d4 | 2021-11-05 17:23:02 | [diff] [blame] | 75 | explicit DragImageView(ui::mojom::DragEventSource source); |
Allen Bauer | 35d4e72 | 2020-06-16 22:47:56 | [diff] [blame] | 76 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 77 | gfx::Image* DragHint() const; |
| 78 | // Drag hint images are only drawn when the input source is touch. |
| 79 | bool ShouldDrawDragHint() const; |
| 80 | |
| 81 | // Overridden from views::ImageView. |
| 82 | void OnPaint(gfx::Canvas* canvas) override; |
| 83 | |
| 84 | // Overridden from views::view |
Peter Kasting | 1e3a1e99 | 2024-02-01 18:10:41 | [diff] [blame] | 85 | void Layout(PassKey) override; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 86 | |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 87 | // Save the requested drag image size. We may need to display a drag hint |
| 88 | // image, which potentially expands |widget_|'s size. That drag hint image |
| 89 | // may be disabled (e.g. during the drag cancel animation). In that case, |
| 90 | // we need to know the originally requested size to render the drag image. |
| 91 | gfx::Size drag_image_size_; |
| 92 | |
Henrique Ferreiro | 1748fd1 | 2020-08-04 12:51:46 | [diff] [blame] | 93 | ui::mojom::DragEventSource drag_event_source_; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 94 | |
| 95 | // Bitmask of ui::DragDropTypes::DragOperation values. |
Allen Bauer | 35d4e72 | 2020-06-16 22:47:56 | [diff] [blame] | 96 | int touch_drag_operation_ = ui::DragDropTypes::DRAG_NONE; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 97 | gfx::Point touch_drag_operation_indicator_position_; |
James Cook | b0bf8e8 | 2017-04-09 17:01:44 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | } // namespace ash |
| 101 | |
| 102 | #endif // ASH_DRAG_DROP_DRAG_IMAGE_VIEW_H_ |