Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / ui / wm / core / transient_window_stacking_client.cc
blob9be72b6eae0f90560de2163a6de8934b752e5e68
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/wm/core/transient_window_stacking_client.h"
7 #include <algorithm>
9 #include "ui/wm/core/transient_window_manager.h"
10 #include "ui/wm/core/window_util.h"
12 using aura::Window;
14 namespace wm {
16 namespace {
18 // Populates |ancestors| with all transient ancestors of |window| that are
19 // siblings of |window|. Returns true if any ancestors were found, false if not.
20 bool GetAllTransientAncestors(Window* window, Window::Windows* ancestors) {
21 Window* parent = window->parent();
22 for (; window; window = GetTransientParent(window)) {
23 if (window->parent() == parent)
24 ancestors->push_back(window);
26 return (!ancestors->empty());
29 // Replaces |window1| and |window2| with their possible transient ancestors that
30 // are still siblings (have a common transient parent). |window1| and |window2|
31 // are not modified if such ancestors cannot be found.
32 void FindCommonTransientAncestor(Window** window1, Window** window2) {
33 DCHECK(window1);
34 DCHECK(window2);
35 DCHECK(*window1);
36 DCHECK(*window2);
37 // Assemble chains of ancestors of both windows.
38 Window::Windows ancestors1;
39 Window::Windows ancestors2;
40 if (!GetAllTransientAncestors(*window1, &ancestors1) ||
41 !GetAllTransientAncestors(*window2, &ancestors2)) {
42 return;
44 // Walk the two chains backwards and look for the first difference.
45 Window::Windows::reverse_iterator it1 = ancestors1.rbegin();
46 Window::Windows::reverse_iterator it2 = ancestors2.rbegin();
47 for (; it1 != ancestors1.rend() && it2 != ancestors2.rend(); ++it1, ++it2) {
48 if (*it1 != *it2) {
49 *window1 = *it1;
50 *window2 = *it2;
51 break;
56 } // namespace
58 // static
59 TransientWindowStackingClient* TransientWindowStackingClient::instance_ = NULL;
61 TransientWindowStackingClient::TransientWindowStackingClient() {
62 instance_ = this;
65 TransientWindowStackingClient::~TransientWindowStackingClient() {
66 if (instance_ == this)
67 instance_ = NULL;
70 bool TransientWindowStackingClient::AdjustStacking(
71 Window** child,
72 Window** target,
73 Window::StackDirection* direction) {
74 const TransientWindowManager* transient_manager =
75 TransientWindowManager::Get(static_cast<const Window*>(*child));
76 if (transient_manager && transient_manager->IsStackingTransient(*target))
77 return true;
79 // For windows that have transient children stack the transient ancestors that
80 // are siblings. This prevents one transient group from being inserted in the
81 // middle of another.
82 FindCommonTransientAncestor(child, target);
84 // When stacking above skip to the topmost transient descendant of the target.
85 if (*direction == Window::STACK_ABOVE &&
86 !HasTransientAncestor(*child, *target)) {
87 const Window::Windows& siblings((*child)->parent()->children());
88 size_t target_i =
89 std::find(siblings.begin(), siblings.end(), *target) - siblings.begin();
90 while (target_i + 1 < siblings.size() &&
91 HasTransientAncestor(siblings[target_i + 1], *target)) {
92 ++target_i;
94 *target = siblings[target_i];
97 return *child != *target;
100 } // namespace wm