summaryrefslogtreecommitdiffstats
path: root/chromium/ash/first_run/desktop_cleaner.cc
blob: cf0ffa6f974038e81d7f3a68c5cb158efa82cb62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/first_run/desktop_cleaner.h"

#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_blocker.h"

namespace ash {
namespace internal {

namespace {

const int kContainerIdsToHide[] = {
  kShellWindowId_DefaultContainer,
  kShellWindowId_AlwaysOnTopContainer,
  kShellWindowId_PanelContainer,
  // TODO(dzhioev): uncomment this when issue with BrowserView::CanActivate
  // will be fixed.
  // kShellWindowId_SystemModalContainer
};

}  // namespace

class ContainerHider : public aura::WindowObserver,
                       public ui::ImplicitAnimationObserver {
 public:
  explicit ContainerHider(aura::Window* container)
      : container_was_hidden_(!container->IsVisible()),
        container_(container) {
    if (container_was_hidden_)
      return;
    ui::Layer* layer = container_->layer();
    ui::ScopedLayerAnimationSettings animation_settings(layer->GetAnimator());
    animation_settings.AddObserver(this);
    layer->SetOpacity(0.0);
  }

  virtual ~ContainerHider() {
    if (container_was_hidden_ || !container_)
      return;
    if (!WasAnimationCompletedForProperty(ui::LayerAnimationElement::OPACITY)) {
      // We are in the middle of animation.
      StopObservingImplicitAnimations();
    } else {
      container_->Show();
    }
    ui::Layer* layer = container_->layer();
    ui::ScopedLayerAnimationSettings animation_settings(layer->GetAnimator());
    layer->SetOpacity(1.0);
  }

 private:
  // Overriden from ui::ImplicitAnimationObserver.
  virtual void OnImplicitAnimationsCompleted() OVERRIDE {
    container_->Hide();
  }

  // Overriden from aura::WindowObserver.
  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
    DCHECK(window == container_);
    container_ = NULL;
  }

  const bool container_was_hidden_;
  aura::Window* container_;

  DISALLOW_COPY_AND_ASSIGN(ContainerHider);
};

class NotificationBlocker : public message_center::NotificationBlocker {
 public:
  NotificationBlocker()
      : message_center::NotificationBlocker(
            message_center::MessageCenter::Get()) {
    NotifyBlockingStateChanged();
  }

  virtual ~NotificationBlocker() {};

 private:
  // Overriden from message_center::NotificationBlocker.
  virtual bool ShouldShowNotificationAsPopup(
      const message_center::NotifierId& notifier_id) const OVERRIDE {
    return false;
  }

  DISALLOW_COPY_AND_ASSIGN(NotificationBlocker);
};

DesktopCleaner::DesktopCleaner() {
  // TODO(dzhioev): Add support for secondary displays.
  aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
  for (size_t i = 0; i < arraysize(kContainerIdsToHide); ++i) {
    aura::Window* container =
        Shell::GetContainer(root_window, kContainerIdsToHide[i]);
    container_hiders_.push_back(make_linked_ptr(new ContainerHider(container)));
  }
  notification_blocker_.reset(new NotificationBlocker());
}

DesktopCleaner::~DesktopCleaner() {}

// static
std::vector<int> DesktopCleaner::GetContainersToHideForTest() {
  return std::vector<int>(kContainerIdsToHide,
                          kContainerIdsToHide + arraysize(kContainerIdsToHide));
}

}  // namespace internal
}  // namespace ash