Index: ui/gfx/compositor/layer.cc |
diff --git a/ui/gfx/compositor/layer.cc b/ui/gfx/compositor/layer.cc |
index 67f2cde86f4d91735d590b44c0cf2a98e1714f9b..2b98d44e7a61015df987bcbf31fb4d9513761a49 100644 |
--- a/ui/gfx/compositor/layer.cc |
+++ b/ui/gfx/compositor/layer.cc |
@@ -115,14 +115,29 @@ void Layer::Remove(Layer* child) { |
} |
void Layer::MoveToFront(Layer* child) { |
- std::vector<Layer*>::iterator i = |
- std::find(children_.begin(), children_.end(), child); |
- DCHECK(i != children_.end()); |
- children_.erase(i); |
- children_.push_back(child); |
+ if (children_.size() <= 1 || child == children_.back()) |
+ return; // Already in front. |
+ MoveAbove(child, children_.back()); |
+} |
+ |
+void Layer::MoveAbove(Layer* child, Layer* other) { |
+ DCHECK_NE(child, other); |
+ DCHECK_EQ(this, child->parent()); |
+ DCHECK_EQ(this, other->parent()); |
+ size_t child_i = |
+ std::find(children_.begin(), children_.end(), child) - children_.begin(); |
+ size_t other_i = |
+ std::find(children_.begin(), children_.end(), other) - children_.begin(); |
+ if (child_i > other_i) |
+ return; // Already in front of |other|. |
+ |
+ // Reorder children. |
+ children_.erase(children_.begin() + child_i); |
+ children_.insert(children_.begin() + other_i, child); |
+ |
#if defined(USE_WEBKIT_COMPOSITOR) |
child->web_layer_.removeFromParent(); |
- web_layer_.addChild(child->web_layer_); |
+ web_layer_.insertChild(child->web_layer_, other_i); |
#endif |
} |