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
3// found in the LICENSE file.
4
5#include "ash/wm/resize_shadow_controller.h"
6
7#include <utility>
8
9#include "ash/wm/resize_shadow.h"
10#include "ui/aura/window.h"
11
12namespace ash {
13
14ResizeShadowController::ResizeShadowController() {
15}
16
17ResizeShadowController::~ResizeShadowController() {
18  for (WindowShadowMap::const_iterator it = window_shadows_.begin();
19       it != window_shadows_.end(); ++it) {
20    it->first->RemoveObserver(this);
21  }
22}
23
24void ResizeShadowController::ShowShadow(aura::Window* window, int hit_test) {
25  ResizeShadow* shadow = GetShadowForWindow(window);
26  if (!shadow)
27    shadow = CreateShadow(window);
28  shadow->ShowForHitTest(hit_test);
29}
30
31void ResizeShadowController::HideShadow(aura::Window* window) {
32  ResizeShadow* shadow = GetShadowForWindow(window);
33  if (shadow)
34    shadow->Hide();
35}
36
37ResizeShadow* ResizeShadowController::GetShadowForWindowForTest(
38    aura::Window* window) {
39  return GetShadowForWindow(window);
40}
41
42void ResizeShadowController::OnWindowBoundsChanged(
43    aura::Window* window,
44    const gfx::Rect& old_bounds,
45    const gfx::Rect& new_bounds) {
46  ResizeShadow* shadow = GetShadowForWindow(window);
47  if (shadow)
48    shadow->Layout(new_bounds);
49}
50
51void ResizeShadowController::OnWindowDestroyed(aura::Window* window) {
52  window_shadows_.erase(window);
53}
54
55ResizeShadow* ResizeShadowController::CreateShadow(aura::Window* window) {
56  linked_ptr<ResizeShadow> shadow(new ResizeShadow());
57  window_shadows_.insert(std::make_pair(window, shadow));
58  // Attach the layers to this window.
59  shadow->Init(window);
60  // Ensure initial bounds are correct.
61  shadow->Layout(window->bounds());
62  // Watch for bounds changes.
63  window->AddObserver(this);
64  return shadow.get();
65}
66
67ResizeShadow* ResizeShadowController::GetShadowForWindow(aura::Window* window) {
68  WindowShadowMap::const_iterator it = window_shadows_.find(window);
69  return it != window_shadows_.end() ? it->second.get() : NULL;
70}
71
72}  // namespace ash
73