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/example/wm/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/public/cpp/window.h" | 8 #include "components/mus/public/cpp/window.h" |
9 #include "components/mus/public/interfaces/input_event_constants.mojom.h" | 9 #include "components/mus/public/interfaces/input_event_constants.mojom.h" |
10 #include "ui/gfx/geometry/point_conversions.h" | 10 #include "ui/gfx/geometry/point_conversions.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 return nullptr; | 47 return nullptr; |
48 } | 48 } |
49 | 49 |
50 // Start a move on left mouse, or any other type of pointer. | 50 // Start a move on left mouse, or any other type of pointer. |
51 if (event.pointer_data->kind == mus::mojom::POINTER_KIND_MOUSE && | 51 if (event.pointer_data->kind == mus::mojom::POINTER_KIND_MOUSE && |
52 MouseOnlyEventFlags(event.flags) != | 52 MouseOnlyEventFlags(event.flags) != |
53 mus::mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON) { | 53 mus::mojom::EVENT_FLAGS_LEFT_MOUSE_BUTTON) { |
54 return nullptr; | 54 return nullptr; |
55 } | 55 } |
56 | 56 |
57 return make_scoped_ptr(new MoveLoop(target, event)); | 57 Type type; |
| 58 HorizontalLocation h_loc; |
| 59 VerticalLocation v_loc; |
| 60 DetermineType(target, location, &type, &h_loc, &v_loc); |
| 61 |
| 62 return make_scoped_ptr(new MoveLoop(target, event, type, h_loc, v_loc)); |
58 } | 63 } |
59 | 64 |
60 MoveLoop::MoveResult MoveLoop::Move(const mus::mojom::Event& event) { | 65 MoveLoop::MoveResult MoveLoop::Move(const mus::mojom::Event& event) { |
61 switch (event.action) { | 66 switch (event.action) { |
62 case mus::mojom::EVENT_TYPE_POINTER_CANCEL: | 67 case mus::mojom::EVENT_TYPE_POINTER_CANCEL: |
63 if (event.pointer_data->pointer_id == pointer_id_) { | 68 if (event.pointer_data->pointer_id == pointer_id_) { |
64 if (target_) | 69 if (target_) |
65 Revert(); | 70 Revert(); |
66 return MoveResult::DONE; | 71 return MoveResult::DONE; |
67 } | 72 } |
(...skipping 13 matching lines...) Expand all Loading... |
81 } | 86 } |
82 return MoveResult::CONTINUE; | 87 return MoveResult::CONTINUE; |
83 | 88 |
84 default: | 89 default: |
85 break; | 90 break; |
86 } | 91 } |
87 | 92 |
88 return MoveResult::CONTINUE; | 93 return MoveResult::CONTINUE; |
89 } | 94 } |
90 | 95 |
91 MoveLoop::MoveLoop(mus::Window* target, const mus::mojom::Event& event) | 96 MoveLoop::MoveLoop(mus::Window* target, |
| 97 const mus::mojom::Event& event, |
| 98 Type type, |
| 99 HorizontalLocation h_loc, |
| 100 VerticalLocation v_loc) |
92 : target_(target), | 101 : target_(target), |
| 102 type_(type), |
| 103 h_loc_(h_loc), |
| 104 v_loc_(v_loc), |
93 pointer_id_(event.pointer_data->pointer_id), | 105 pointer_id_(event.pointer_data->pointer_id), |
94 initial_event_screen_location_(EventScreenLocationToPoint(event)), | 106 initial_event_screen_location_(EventScreenLocationToPoint(event)), |
95 initial_window_bounds_(target->bounds()), | 107 initial_window_bounds_(target->bounds()), |
96 changing_bounds_(false) { | 108 changing_bounds_(false) { |
97 target->AddObserver(this); | 109 target->AddObserver(this); |
98 } | 110 } |
99 | 111 |
| 112 // static |
| 113 void MoveLoop::DetermineType(mus::Window* target, |
| 114 const gfx::Point& location, |
| 115 Type* type, |
| 116 HorizontalLocation* h_loc, |
| 117 VerticalLocation* v_loc) { |
| 118 *h_loc = HorizontalLocation::OTHER; |
| 119 *v_loc = VerticalLocation::OTHER; |
| 120 const int resize_size = static_cast<int>( |
| 121 kResizeSize * |
| 122 std::max(1.f, target->viewport_metrics().device_pixel_ratio)); |
| 123 |
| 124 if (location.x() < target->client_area().x()) |
| 125 *h_loc = HorizontalLocation::LEFT; |
| 126 else if (location.x() >= target->client_area().right()) |
| 127 *h_loc = HorizontalLocation::RIGHT; |
| 128 else |
| 129 *h_loc = HorizontalLocation::OTHER; |
| 130 |
| 131 if (location.y() < resize_size) |
| 132 *v_loc = VerticalLocation::TOP; |
| 133 else if (location.y() >= target->client_area().bottom()) |
| 134 *v_loc = VerticalLocation::BOTTOM; |
| 135 else |
| 136 *v_loc = VerticalLocation::OTHER; |
| 137 |
| 138 if (*v_loc == VerticalLocation::OTHER && location.y() >= resize_size && |
| 139 *h_loc == HorizontalLocation::OTHER) { |
| 140 *type = Type::MOVE; |
| 141 return; |
| 142 } |
| 143 *type = Type::RESIZE; |
| 144 DCHECK(*h_loc != HorizontalLocation::OTHER || |
| 145 *v_loc != VerticalLocation::OTHER); |
| 146 } |
| 147 |
100 void MoveLoop::MoveImpl(const mus::mojom::Event& event) { | 148 void MoveLoop::MoveImpl(const mus::mojom::Event& event) { |
101 const gfx::Vector2d delta = | 149 const gfx::Vector2d delta = |
102 EventScreenLocationToPoint(event) - initial_event_screen_location_; | 150 EventScreenLocationToPoint(event) - initial_event_screen_location_; |
103 const gfx::Rect new_bounds(initial_window_bounds_.origin() + delta, | 151 const gfx::Rect new_bounds(DetermineBoundsFromDelta(delta)); |
104 initial_window_bounds_.size()); | |
105 base::AutoReset<bool> resetter(&changing_bounds_, true); | 152 base::AutoReset<bool> resetter(&changing_bounds_, true); |
106 target_->SetBounds(new_bounds); | 153 target_->SetBounds(new_bounds); |
107 } | 154 } |
108 | 155 |
109 void MoveLoop::Cancel() { | 156 void MoveLoop::Cancel() { |
110 target_->RemoveObserver(this); | 157 target_->RemoveObserver(this); |
111 target_ = nullptr; | 158 target_ = nullptr; |
112 } | 159 } |
113 | 160 |
114 void MoveLoop::Revert() { | 161 void MoveLoop::Revert() { |
115 base::AutoReset<bool> resetter(&changing_bounds_, true); | 162 base::AutoReset<bool> resetter(&changing_bounds_, true); |
116 target_->SetBounds(initial_window_bounds_); | 163 target_->SetBounds(initial_window_bounds_); |
117 } | 164 } |
118 | 165 |
| 166 gfx::Rect MoveLoop::DetermineBoundsFromDelta(const gfx::Vector2d& delta) { |
| 167 if (type_ == Type::MOVE) { |
| 168 return gfx::Rect(initial_window_bounds_.origin() + delta, |
| 169 initial_window_bounds_.size()); |
| 170 } |
| 171 |
| 172 // TODO(sky): support better min sizes, make sure doesn't get bigger than |
| 173 // screen and max. Also make sure keep some portion on screen. |
| 174 gfx::Rect bounds(initial_window_bounds_); |
| 175 if (h_loc_ == HorizontalLocation::LEFT) { |
| 176 const int x = std::min(bounds.right() - 1, bounds.x() + delta.x()); |
| 177 const int width = bounds.right() - x; |
| 178 bounds.set_x(x); |
| 179 bounds.set_width(width); |
| 180 } else if (h_loc_ == HorizontalLocation::RIGHT) { |
| 181 bounds.set_width(std::max(1, bounds.width() + delta.x())); |
| 182 } |
| 183 |
| 184 if (v_loc_ == VerticalLocation::TOP) { |
| 185 const int y = std::min(bounds.bottom() - 1, bounds.y() + delta.y()); |
| 186 const int height = bounds.bottom() - y; |
| 187 bounds.set_y(y); |
| 188 bounds.set_height(height); |
| 189 } else if (v_loc_ == VerticalLocation::BOTTOM) { |
| 190 bounds.set_height(std::max(1, bounds.height() + delta.y())); |
| 191 } |
| 192 |
| 193 return bounds; |
| 194 } |
| 195 |
119 void MoveLoop::OnTreeChanged(const TreeChangeParams& params) { | 196 void MoveLoop::OnTreeChanged(const TreeChangeParams& params) { |
120 if (params.target == target_) | 197 if (params.target == target_) |
121 Cancel(); | 198 Cancel(); |
122 } | 199 } |
123 | 200 |
124 void MoveLoop::OnWindowBoundsChanged(mus::Window* window, | 201 void MoveLoop::OnWindowBoundsChanged(mus::Window* window, |
125 const gfx::Rect& old_bounds, | 202 const gfx::Rect& old_bounds, |
126 const gfx::Rect& new_bounds) { | 203 const gfx::Rect& new_bounds) { |
127 DCHECK_EQ(window, target_); | 204 DCHECK_EQ(window, target_); |
128 if (!changing_bounds_) | 205 if (!changing_bounds_) |
129 Cancel(); | 206 Cancel(); |
130 } | 207 } |
131 | 208 |
132 void MoveLoop::OnWindowVisibilityChanged(mus::Window* window) { | 209 void MoveLoop::OnWindowVisibilityChanged(mus::Window* window) { |
133 DCHECK_EQ(window, target_); | 210 DCHECK_EQ(window, target_); |
134 Cancel(); | 211 Cancel(); |
135 } | 212 } |
OLD | NEW |