OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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. |
| 4 |
| 5 #include "ui/views/corewm/transient_window_manager.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <functional> |
| 9 |
| 10 #include "base/auto_reset.h" |
| 11 #include "base/stl_util.h" |
| 12 #include "ui/aura/window.h" |
| 13 #include "ui/aura/window_property.h" |
| 14 #include "ui/views/corewm/transient_window_stacking_client.h" |
| 15 #include "ui/views/corewm/window_util.h" |
| 16 |
| 17 using aura::Window; |
| 18 |
| 19 namespace views { |
| 20 namespace corewm { |
| 21 |
| 22 DEFINE_OWNED_WINDOW_PROPERTY_KEY(TransientWindowManager, kPropertyKey, NULL); |
| 23 |
| 24 TransientWindowManager::~TransientWindowManager() { |
| 25 } |
| 26 |
| 27 // static |
| 28 TransientWindowManager* TransientWindowManager::Get(Window* window) { |
| 29 TransientWindowManager* manager = window->GetProperty(kPropertyKey); |
| 30 if (!manager) { |
| 31 manager = new TransientWindowManager(window); |
| 32 window->SetProperty(kPropertyKey, manager); |
| 33 } |
| 34 return manager; |
| 35 } |
| 36 |
| 37 // static |
| 38 const TransientWindowManager* TransientWindowManager::Get( |
| 39 const Window* window) { |
| 40 return window->GetProperty(kPropertyKey); |
| 41 } |
| 42 |
| 43 void TransientWindowManager::AddTransientChild(Window* child) { |
| 44 // TransientWindowStackingClient does the stacking of transient windows. If it |
| 45 // isn't installed stacking is going to be wrong. |
| 46 DCHECK(TransientWindowStackingClient::instance_); |
| 47 |
| 48 TransientWindowManager* child_manager = Get(child); |
| 49 if (child_manager->transient_parent_) |
| 50 Get(child_manager->transient_parent_)->RemoveTransientChild(child); |
| 51 DCHECK(std::find(transient_children_.begin(), transient_children_.end(), |
| 52 child) == transient_children_.end()); |
| 53 transient_children_.push_back(child); |
| 54 child_manager->transient_parent_ = window_; |
| 55 FOR_EACH_OBSERVER(WindowObserver, window_->observers_, |
| 56 OnAddTransientChild(window_, child)); |
| 57 } |
| 58 |
| 59 void TransientWindowManager::RemoveTransientChild(Window* child) { |
| 60 Windows::iterator i = |
| 61 std::find(transient_children_.begin(), transient_children_.end(), child); |
| 62 DCHECK(i != transient_children_.end()); |
| 63 transient_children_.erase(i); |
| 64 TransientWindowManager* child_manager = Get(child); |
| 65 DCHECK_EQ(window_, child_manager->transient_parent_); |
| 66 child_manager->transient_parent_ = NULL; |
| 67 FOR_EACH_OBSERVER(WindowObserver, window_->observers_, |
| 68 OnRemoveTransientChild(window_, child)); |
| 69 } |
| 70 |
| 71 bool TransientWindowManager::IsStackingTransient( |
| 72 const aura::Window* child, |
| 73 const aura::Window* target) const { |
| 74 return stacking_pair_ && stacking_pair_->child == child && |
| 75 stacking_pair_->target == target; |
| 76 } |
| 77 |
| 78 TransientWindowManager::TransientWindowManager(Window* window) |
| 79 : window_(window), |
| 80 transient_parent_(NULL), |
| 81 stacking_pair_(NULL) { |
| 82 window_->AddObserver(this); |
| 83 } |
| 84 |
| 85 void TransientWindowManager::OnChildStackingChanged(aura::Window* child) { |
| 86 // Do nothing if we initiated the stacking change. |
| 87 // TODO(sky): change args to OnWindowStackingChanged() so that this lookup |
| 88 // can be simpler. |
| 89 if (stacking_pair_ && stacking_pair_->child == child) { |
| 90 Windows::const_iterator child_i = std::find( |
| 91 window_->children().begin(), window_->children().end(), child); |
| 92 DCHECK(child_i != window_->children().end()); |
| 93 if (child_i != window_->children().begin() && |
| 94 (*(child_i - 1) == stacking_pair_->target)) |
| 95 return; |
| 96 } |
| 97 |
| 98 // Stack any transient children that share the same parent to be in front of |
| 99 // |child|. The existing stacking order is preserved by iterating backwards |
| 100 // and always stacking on top. |
| 101 Window::Windows children(window_->children()); |
| 102 for (Window::Windows::reverse_iterator it = children.rbegin(); |
| 103 it != children.rend(); ++it) { |
| 104 if ((*it) != child && HasTransientAncestor(*it, child)) { |
| 105 StackingPair pair(*it, child); |
| 106 base::AutoReset<StackingPair*> resetter(&stacking_pair_, &pair); |
| 107 window_->StackChildAbove((*it), child); |
| 108 } |
| 109 } |
| 110 } |
| 111 |
| 112 void TransientWindowManager::OnWindowVisibilityChanging(Window* window, |
| 113 bool visible) { |
| 114 // TODO(sky): move handling of becoming visible here. |
| 115 if (!visible) { |
| 116 std::for_each(transient_children_.begin(), transient_children_.end(), |
| 117 std::mem_fun(&Window::Hide)); |
| 118 } |
| 119 } |
| 120 |
| 121 void TransientWindowManager::OnWindowStackingChanged(Window* window) { |
| 122 TransientWindowManager* parent_manager = Get(window->parent()); |
| 123 parent_manager->OnChildStackingChanged(window); |
| 124 } |
| 125 |
| 126 void TransientWindowManager::OnWindowDestroying(Window* window) { |
| 127 // TODO(sky): remove notes after safely landing and baking. |
| 128 |
| 129 // Removes ourselves from our transient parent (if it hasn't been done by the |
| 130 // RootWindow). |
| 131 // NOTE: This use to be done after children where removed, now it is before. |
| 132 if (transient_parent_) { |
| 133 TransientWindowManager::Get(transient_parent_)->RemoveTransientChild( |
| 134 window_); |
| 135 } |
| 136 |
| 137 // Destroy transient children, only after we've removed ourselves from our |
| 138 // parent, as destroying an active transient child may otherwise attempt to |
| 139 // refocus us. |
| 140 // NOTE: this use to be after removed from parent, now its before. |
| 141 Windows transient_children(transient_children_); |
| 142 STLDeleteElements(&transient_children); |
| 143 DCHECK(transient_children_.empty()); |
| 144 } |
| 145 |
| 146 } // namespace corewm |
| 147 } // namespace views |
OLD | NEW |