| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ash/wm/workspace/magnetism_matcher.h" | 5 #include "ash/wm/common/workspace/magnetism_matcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 | 9 |
| 10 namespace ash { | 10 namespace ash { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // Returns true if |a| is close enough to |b| that the two edges snap. | 13 // Returns true if |a| is close enough to |b| that the two edges snap. |
| 14 bool IsCloseEnough(int a, int b) { | 14 bool IsCloseEnough(int a, int b) { |
| 15 return abs(a - b) <= MagnetismMatcher::kMagneticDistance; | 15 return abs(a - b) <= MagnetismMatcher::kMagneticDistance; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 45 } | 45 } |
| 46 return (edges & secondary_as_magnetism_edge) != 0; | 46 return (edges & secondary_as_magnetism_edge) != 0; |
| 47 } | 47 } |
| 48 | 48 |
| 49 } // namespace | 49 } // namespace |
| 50 | 50 |
| 51 // MagnetismEdgeMatcher -------------------------------------------------------- | 51 // MagnetismEdgeMatcher -------------------------------------------------------- |
| 52 | 52 |
| 53 MagnetismEdgeMatcher::MagnetismEdgeMatcher(const gfx::Rect& bounds, | 53 MagnetismEdgeMatcher::MagnetismEdgeMatcher(const gfx::Rect& bounds, |
| 54 MagnetismEdge edge) | 54 MagnetismEdge edge) |
| 55 : bounds_(bounds), | 55 : bounds_(bounds), edge_(edge) { |
| 56 edge_(edge) { | |
| 57 ranges_.push_back(GetSecondaryRange(bounds_)); | 56 ranges_.push_back(GetSecondaryRange(bounds_)); |
| 58 } | 57 } |
| 59 | 58 |
| 60 MagnetismEdgeMatcher::~MagnetismEdgeMatcher() { | 59 MagnetismEdgeMatcher::~MagnetismEdgeMatcher() {} |
| 61 } | |
| 62 | 60 |
| 63 bool MagnetismEdgeMatcher::ShouldAttach(const gfx::Rect& bounds) { | 61 bool MagnetismEdgeMatcher::ShouldAttach(const gfx::Rect& bounds) { |
| 64 if (is_edge_obscured()) | 62 if (is_edge_obscured()) |
| 65 return false; | 63 return false; |
| 66 | 64 |
| 67 if (IsCloseEnough(GetPrimaryCoordinate(bounds_, edge_), | 65 if (IsCloseEnough(GetPrimaryCoordinate(bounds_, edge_), |
| 68 GetPrimaryCoordinate(bounds, FlipEdge(edge_)))) { | 66 GetPrimaryCoordinate(bounds, FlipEdge(edge_)))) { |
| 69 const Range range(GetSecondaryRange(bounds)); | 67 const Range range(GetSecondaryRange(bounds)); |
| 70 Ranges::const_iterator i = | 68 Ranges::const_iterator i = |
| 71 std::lower_bound(ranges_.begin(), ranges_.end(), range); | 69 std::lower_bound(ranges_.begin(), ranges_.end(), range); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 87 | 85 |
| 88 void MagnetismEdgeMatcher::UpdateRanges(const Range& range) { | 86 void MagnetismEdgeMatcher::UpdateRanges(const Range& range) { |
| 89 Ranges::const_iterator it = | 87 Ranges::const_iterator it = |
| 90 std::lower_bound(ranges_.begin(), ranges_.end(), range); | 88 std::lower_bound(ranges_.begin(), ranges_.end(), range); |
| 91 if (it != ranges_.begin() && RangesIntersect(*(it - 1), range)) | 89 if (it != ranges_.begin() && RangesIntersect(*(it - 1), range)) |
| 92 --it; | 90 --it; |
| 93 if (it == ranges_.end()) | 91 if (it == ranges_.end()) |
| 94 return; | 92 return; |
| 95 | 93 |
| 96 for (size_t i = it - ranges_.begin(); | 94 for (size_t i = it - ranges_.begin(); |
| 97 i < ranges_.size() && RangesIntersect(ranges_[i], range); ) { | 95 i < ranges_.size() && RangesIntersect(ranges_[i], range);) { |
| 98 if (range.first <= ranges_[i].first && | 96 if (range.first <= ranges_[i].first && range.second >= ranges_[i].second) { |
| 99 range.second >= ranges_[i].second) { | |
| 100 ranges_.erase(ranges_.begin() + i); | 97 ranges_.erase(ranges_.begin() + i); |
| 101 } else if (range.first < ranges_[i].first) { | 98 } else if (range.first < ranges_[i].first) { |
| 102 DCHECK_GT(range.second, ranges_[i].first); | 99 DCHECK_GT(range.second, ranges_[i].first); |
| 103 ranges_[i] = Range(range.second, ranges_[i].second); | 100 ranges_[i] = Range(range.second, ranges_[i].second); |
| 104 ++i; | 101 ++i; |
| 105 } else { | 102 } else { |
| 106 Range existing(ranges_[i]); | 103 Range existing(ranges_[i]); |
| 107 ranges_[i].second = range.first; | 104 ranges_[i].second = range.first; |
| 108 ++i; | 105 ++i; |
| 109 if (existing.second > range.second) { | 106 if (existing.second > range.second) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 120 // static | 117 // static |
| 121 const int MagnetismMatcher::kMagneticDistance = 8; | 118 const int MagnetismMatcher::kMagneticDistance = 8; |
| 122 | 119 |
| 123 MagnetismMatcher::MagnetismMatcher(const gfx::Rect& bounds, uint32_t edges) | 120 MagnetismMatcher::MagnetismMatcher(const gfx::Rect& bounds, uint32_t edges) |
| 124 : edges_(edges) { | 121 : edges_(edges) { |
| 125 if (edges & MAGNETISM_EDGE_TOP) | 122 if (edges & MAGNETISM_EDGE_TOP) |
| 126 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_TOP)); | 123 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_TOP)); |
| 127 if (edges & MAGNETISM_EDGE_LEFT) | 124 if (edges & MAGNETISM_EDGE_LEFT) |
| 128 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_LEFT)); | 125 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_LEFT)); |
| 129 if (edges & MAGNETISM_EDGE_BOTTOM) { | 126 if (edges & MAGNETISM_EDGE_BOTTOM) { |
| 130 matchers_.push_back(new MagnetismEdgeMatcher(bounds, | 127 matchers_.push_back( |
| 131 MAGNETISM_EDGE_BOTTOM)); | 128 new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_BOTTOM)); |
| 132 } | 129 } |
| 133 if (edges & MAGNETISM_EDGE_RIGHT) | 130 if (edges & MAGNETISM_EDGE_RIGHT) |
| 134 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_RIGHT)); | 131 matchers_.push_back(new MagnetismEdgeMatcher(bounds, MAGNETISM_EDGE_RIGHT)); |
| 135 } | 132 } |
| 136 | 133 |
| 137 MagnetismMatcher::~MagnetismMatcher() { | 134 MagnetismMatcher::~MagnetismMatcher() {} |
| 138 } | |
| 139 | 135 |
| 140 bool MagnetismMatcher::ShouldAttach(const gfx::Rect& bounds, | 136 bool MagnetismMatcher::ShouldAttach(const gfx::Rect& bounds, |
| 141 MatchedEdge* edge) { | 137 MatchedEdge* edge) { |
| 142 for (size_t i = 0; i < matchers_.size(); ++i) { | 138 for (size_t i = 0; i < matchers_.size(); ++i) { |
| 143 if (matchers_[i]->ShouldAttach(bounds)) { | 139 if (matchers_[i]->ShouldAttach(bounds)) { |
| 144 edge->primary_edge = matchers_[i]->edge(); | 140 edge->primary_edge = matchers_[i]->edge(); |
| 145 AttachToSecondaryEdge(bounds, edge->primary_edge, | 141 AttachToSecondaryEdge(bounds, edge->primary_edge, |
| 146 &(edge->secondary_edge)); | 142 &(edge->secondary_edge)); |
| 147 return true; | 143 return true; |
| 148 } | 144 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 edges_) && | 178 edges_) && |
| 183 IsCloseEnough(bounds.right(), src_bounds.right())) { | 179 IsCloseEnough(bounds.right(), src_bounds.right())) { |
| 184 *secondary_edge = SECONDARY_MAGNETISM_EDGE_TRAILING; | 180 *secondary_edge = SECONDARY_MAGNETISM_EDGE_TRAILING; |
| 185 } else { | 181 } else { |
| 186 *secondary_edge = SECONDARY_MAGNETISM_EDGE_NONE; | 182 *secondary_edge = SECONDARY_MAGNETISM_EDGE_NONE; |
| 187 } | 183 } |
| 188 } | 184 } |
| 189 } | 185 } |
| 190 | 186 |
| 191 } // namespace ash | 187 } // namespace ash |
| OLD | NEW |