OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/mus/ws/move_loop.h" | 5 #include "components/mus/example/wm/move_loop.h" |
6 | 6 |
7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
8 #include "components/mus/ws/server_window.h" | 8 #include "components/mus/public/cpp/window.h" |
| 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
9 #include "ui/gfx/geometry/point_conversions.h" | 10 #include "ui/gfx/geometry/point_conversions.h" |
10 #include "ui/gfx/geometry/rect.h" | 11 #include "ui/gfx/geometry/rect.h" |
11 #include "ui/mojo/events/input_event_constants.mojom.h" | 12 #include "ui/mojo/events/input_event_constants.mojom.h" |
12 | 13 |
13 namespace mus { | |
14 namespace ws { | |
15 namespace { | 14 namespace { |
16 | 15 |
17 gfx::Point EventLocationToPoint(const mojo::Event& event) { | 16 gfx::Point EventLocationToPoint(const mojo::Event& event) { |
18 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, | 17 return gfx::ToFlooredPoint(gfx::PointF(event.pointer_data->location->x, |
19 event.pointer_data->location->y)); | 18 event.pointer_data->location->y)); |
20 } | 19 } |
21 | 20 |
22 gfx::Point EventScreenLocationToPoint(const mojo::Event& event) { | 21 gfx::Point EventScreenLocationToPoint(const mojo::Event& event) { |
23 return gfx::ToFlooredPoint( | 22 return gfx::ToFlooredPoint( |
24 gfx::PointF(event.pointer_data->location->screen_x, | 23 gfx::PointF(event.pointer_data->location->screen_x, |
25 event.pointer_data->location->screen_y)); | 24 event.pointer_data->location->screen_y)); |
26 } | 25 } |
27 | 26 |
28 mojo::EventFlags MouseOnlyEventFlags(mojo::EventFlags flags) { | 27 mojo::EventFlags MouseOnlyEventFlags(mojo::EventFlags flags) { |
29 return static_cast<mojo::EventFlags>(flags & | 28 return static_cast<mojo::EventFlags>(flags & |
30 (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | | 29 (mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON | |
31 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | | 30 mojo::EVENT_FLAGS_MIDDLE_MOUSE_BUTTON | |
32 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)); | 31 mojo::EVENT_FLAGS_RIGHT_MOUSE_BUTTON)); |
33 } | 32 } |
34 | 33 |
35 } // namespace | 34 } // namespace |
36 | 35 |
37 MoveLoop::~MoveLoop() { | 36 MoveLoop::~MoveLoop() { |
38 if (target_) | 37 if (target_) |
39 target_->RemoveObserver(this); | 38 target_->RemoveObserver(this); |
40 } | 39 } |
41 | 40 |
42 // static | 41 // static |
43 scoped_ptr<MoveLoop> MoveLoop::Create(ServerWindow* target, | 42 scoped_ptr<MoveLoop> MoveLoop::Create(mus::Window* target, |
44 const mojo::Event& event) { | 43 const mojo::Event& event) { |
45 DCHECK(event.action == mojo::EVENT_TYPE_POINTER_DOWN); | 44 DCHECK_EQ(event.action, mojo::EVENT_TYPE_POINTER_DOWN); |
46 const gfx::Point location(EventLocationToPoint(event)); | 45 const gfx::Point location(EventLocationToPoint(event)); |
47 if (!target->parent() || !target->parent()->is_draggable_window_container() || | 46 if (!gfx::Rect(target->bounds().To<gfx::Rect>().size()).Contains(location) || |
48 !gfx::Rect(target->bounds().size()).Contains(location) || | 47 target->client_area().To<gfx::Rect>().Contains(location)) { |
49 target->client_area().Contains(location)) { | |
50 return nullptr; | 48 return nullptr; |
51 } | 49 } |
52 | 50 |
53 // Start a move on left mouse, or any other type of pointer. | 51 // Start a move on left mouse, or any other type of pointer. |
54 if (event.pointer_data->kind == mojo::POINTER_KIND_MOUSE && | 52 if (event.pointer_data->kind == mojo::POINTER_KIND_MOUSE && |
55 MouseOnlyEventFlags(event.flags) != mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 53 MouseOnlyEventFlags(event.flags) != mojo::EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
56 return nullptr; | 54 return nullptr; |
57 } | 55 } |
58 | 56 |
59 return make_scoped_ptr(new MoveLoop(target, event)); | 57 return make_scoped_ptr(new MoveLoop(target, event)); |
(...skipping 23 matching lines...) Expand all Loading... |
83 } | 81 } |
84 return MoveResult::CONTINUE; | 82 return MoveResult::CONTINUE; |
85 | 83 |
86 default: | 84 default: |
87 break; | 85 break; |
88 } | 86 } |
89 | 87 |
90 return MoveResult::CONTINUE; | 88 return MoveResult::CONTINUE; |
91 } | 89 } |
92 | 90 |
93 MoveLoop::MoveLoop(ServerWindow* target, const mojo::Event& event) | 91 MoveLoop::MoveLoop(mus::Window* target, const mojo::Event& event) |
94 : target_(target), | 92 : target_(target), |
95 pointer_id_(event.pointer_data->pointer_id), | 93 pointer_id_(event.pointer_data->pointer_id), |
96 initial_event_screen_location_(EventScreenLocationToPoint(event)), | 94 initial_event_screen_location_(EventScreenLocationToPoint(event)), |
97 initial_window_bounds_(target->bounds()), | 95 initial_window_bounds_(target->bounds().To<gfx::Rect>()), |
98 changing_bounds_(false) { | 96 changing_bounds_(false) { |
99 target->AddObserver(this); | 97 target->AddObserver(this); |
100 } | 98 } |
101 | 99 |
102 void MoveLoop::MoveImpl(const mojo::Event& event) { | 100 void MoveLoop::MoveImpl(const mojo::Event& event) { |
103 const gfx::Vector2d delta = | 101 const gfx::Vector2d delta = |
104 EventScreenLocationToPoint(event) - initial_event_screen_location_; | 102 EventScreenLocationToPoint(event) - initial_event_screen_location_; |
105 const gfx::Rect new_bounds(initial_window_bounds_.origin() + delta, | 103 const gfx::Rect new_bounds(initial_window_bounds_.origin() + delta, |
106 initial_window_bounds_.size()); | 104 initial_window_bounds_.size()); |
107 base::AutoReset<bool> resetter(&changing_bounds_, true); | 105 base::AutoReset<bool> resetter(&changing_bounds_, true); |
108 target_->SetBounds(new_bounds); | 106 target_->SetBounds(*mojo::Rect::From(new_bounds)); |
109 } | 107 } |
110 | 108 |
111 void MoveLoop::Cancel() { | 109 void MoveLoop::Cancel() { |
112 target_->RemoveObserver(this); | 110 target_->RemoveObserver(this); |
113 target_ = nullptr; | 111 target_ = nullptr; |
114 } | 112 } |
115 | 113 |
116 void MoveLoop::Revert() { | 114 void MoveLoop::Revert() { |
117 base::AutoReset<bool> resetter(&changing_bounds_, true); | 115 base::AutoReset<bool> resetter(&changing_bounds_, true); |
118 target_->SetBounds(initial_window_bounds_); | 116 target_->SetBounds(*mojo::Rect::From(initial_window_bounds_)); |
119 } | 117 } |
120 | 118 |
121 void MoveLoop::OnWindowHierarchyChanged(ServerWindow* window, | 119 void MoveLoop::OnTreeChanged(const TreeChangeParams& params) { |
122 ServerWindow* new_parent, | 120 if (params.target == target_) |
123 ServerWindow* old_parent) { | 121 Cancel(); |
124 DCHECK_EQ(window, target_); | |
125 Cancel(); | |
126 } | 122 } |
127 | 123 |
128 void MoveLoop::OnWindowBoundsChanged(ServerWindow* window, | 124 void MoveLoop::OnWindowBoundsChanged(mus::Window* window, |
129 const gfx::Rect& old_bounds, | 125 const mojo::Rect& old_bounds, |
130 const gfx::Rect& new_bounds) { | 126 const mojo::Rect& new_bounds) { |
131 DCHECK_EQ(window, target_); | 127 DCHECK_EQ(window, target_); |
132 if (!changing_bounds_) | 128 if (!changing_bounds_) |
133 Cancel(); | 129 Cancel(); |
134 } | 130 } |
135 | 131 |
136 void MoveLoop::OnWindowVisibilityChanged(ServerWindow* window) { | 132 void MoveLoop::OnWindowVisibilityChanged(mus::Window* window) { |
137 DCHECK_EQ(window, target_); | 133 DCHECK_EQ(window, target_); |
138 Cancel(); | 134 Cancel(); |
139 } | 135 } |
140 | |
141 } // namespace ws | |
142 } // namespace mus | |
OLD | NEW |