| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 "athena/screen/modal_window_controller.h" | |
| 6 | |
| 7 #include "athena/screen/public/screen_manager.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "ui/aura/window.h" | |
| 10 #include "ui/aura/window_property.h" | |
| 11 #include "ui/compositor/layer.h" | |
| 12 #include "ui/compositor/layer_animation_observer.h" | |
| 13 #include "ui/compositor/scoped_layer_animation_settings.h" | |
| 14 #include "ui/wm/core/window_animations.h" | |
| 15 | |
| 16 DECLARE_WINDOW_PROPERTY_TYPE(athena::ModalWindowController*); | |
| 17 | |
| 18 namespace athena { | |
| 19 namespace { | |
| 20 | |
| 21 DEFINE_OWNED_WINDOW_PROPERTY_KEY(ModalWindowController, | |
| 22 kModalWindowControllerKey, | |
| 23 nullptr); | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 // static | |
| 28 ModalWindowController* ModalWindowController::Get(aura::Window* container) { | |
| 29 ModalWindowController* controller = | |
| 30 container->GetProperty(kModalWindowControllerKey); | |
| 31 CHECK(controller); | |
| 32 return controller; | |
| 33 } | |
| 34 | |
| 35 ModalWindowController::ModalWindowController(int priority) | |
| 36 : modal_container_(nullptr), | |
| 37 dimmer_window_(new aura::Window(nullptr)), | |
| 38 dimmed_(false) { | |
| 39 ScreenManager::ContainerParams params("ModalContainer", priority); | |
| 40 params.can_activate_children = true; | |
| 41 params.block_events = true; | |
| 42 modal_container_ = ScreenManager::Get()->CreateContainer(params); | |
| 43 modal_container_->SetProperty(kModalWindowControllerKey, this); | |
| 44 | |
| 45 dimmer_window_->SetType(ui::wm::WINDOW_TYPE_NORMAL); | |
| 46 dimmer_window_->Init(aura::WINDOW_LAYER_SOLID_COLOR); | |
| 47 dimmer_window_->layer()->SetColor(SK_ColorBLACK); | |
| 48 dimmer_window_->layer()->SetOpacity(0.0f); | |
| 49 dimmer_window_->Show(); | |
| 50 | |
| 51 modal_container_->AddChild(dimmer_window_); | |
| 52 modal_container_->AddObserver(this); | |
| 53 | |
| 54 UpdateDimmerWindowBounds(); | |
| 55 } | |
| 56 | |
| 57 ModalWindowController::~ModalWindowController() { | |
| 58 if (modal_container_) | |
| 59 modal_container_->RemoveObserver(this); | |
| 60 } | |
| 61 | |
| 62 void ModalWindowController::OnWindowAdded(aura::Window* child) { | |
| 63 DCHECK_NE(child, dimmer_window_); | |
| 64 if (IsChildWindow(child)) { | |
| 65 child->AddObserver(this); | |
| 66 UpdateDimming(nullptr); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void ModalWindowController::OnWindowVisibilityChanged(aura::Window* window, | |
| 71 bool visible) { | |
| 72 if (IsChildWindow(window)) | |
| 73 UpdateDimming(nullptr); | |
| 74 } | |
| 75 | |
| 76 void ModalWindowController::OnWindowBoundsChanged(aura::Window* window, | |
| 77 const gfx::Rect& old_bounds, | |
| 78 const gfx::Rect& new_bounds) { | |
| 79 if (window == modal_container_) | |
| 80 UpdateDimmerWindowBounds(); | |
| 81 } | |
| 82 | |
| 83 void ModalWindowController::OnWindowDestroyed(aura::Window* window) { | |
| 84 UpdateDimming(window); | |
| 85 } | |
| 86 | |
| 87 bool ModalWindowController::IsChildWindow(aura::Window* child) const { | |
| 88 return child->parent() == modal_container_ && child != dimmer_window_; | |
| 89 } | |
| 90 | |
| 91 void ModalWindowController::UpdateDimmerWindowBounds() { | |
| 92 gfx::Rect bounds(modal_container_->bounds().size()); | |
| 93 dimmer_window_->SetBounds(bounds); | |
| 94 } | |
| 95 | |
| 96 void ModalWindowController::UpdateDimming(aura::Window* ignore) { | |
| 97 if (!modal_container_ || !dimmer_window_) | |
| 98 return; | |
| 99 bool should_delete = true; | |
| 100 for (aura::Window* window : modal_container_->children()) { | |
| 101 if (window == dimmer_window_ || window == ignore) | |
| 102 continue; | |
| 103 should_delete = false; | |
| 104 if (window->TargetVisibility()) { | |
| 105 SetDimmed(true); | |
| 106 return; | |
| 107 } | |
| 108 } | |
| 109 SetDimmed(false); | |
| 110 | |
| 111 if (should_delete) { | |
| 112 // Remove the container from root so that the container becomes | |
| 113 // invisible, but don't delete it until next event execution | |
| 114 // because the call stack may still have and use the pointer. | |
| 115 modal_container_->RemoveObserver(this); | |
| 116 | |
| 117 // Hide the window before removing it, so the focus manager which will run | |
| 118 // in RemoveChild handler can know that this container is no longer | |
| 119 // available. | |
| 120 modal_container_->Hide(); | |
| 121 | |
| 122 modal_container_->parent()->RemoveChild(modal_container_); | |
| 123 base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, modal_container_); | |
| 124 modal_container_ = nullptr; | |
| 125 dimmer_window_ = nullptr; | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void ModalWindowController::SetDimmed(bool dimmed) { | |
| 130 const float kDimmedOpacity = 0.4f; | |
| 131 | |
| 132 if (!dimmer_window_ || dimmed_ == dimmed) | |
| 133 return; | |
| 134 dimmed_ = dimmed; | |
| 135 | |
| 136 const int kDimmAnimationDurationMs = 500; | |
| 137 if (dimmed) { | |
| 138 ui::ScopedLayerAnimationSettings settings( | |
| 139 dimmer_window_->layer()->GetAnimator()); | |
| 140 settings.SetTransitionDuration( | |
| 141 base::TimeDelta::FromMilliseconds(kDimmAnimationDurationMs)); | |
| 142 dimmer_window_->layer()->SetOpacity(kDimmedOpacity); | |
| 143 } else { | |
| 144 // ScopedHidingAnimationSettings will detach the animating and | |
| 145 // recreate layers for the container so that animation can continue | |
| 146 // even if the container is removed immediately. | |
| 147 wm::ScopedHidingAnimationSettings settings(modal_container_); | |
| 148 settings.layer_animation_settings()->SetTransitionDuration( | |
| 149 base::TimeDelta::FromMilliseconds(kDimmAnimationDurationMs)); | |
| 150 modal_container_->layer()->SetOpacity(0.0f); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 } // namespace athena | |
| OLD | NEW |