summaryrefslogtreecommitdiff
path: root/athena/screen/modal_window_controller.cc
blob: 140b2c9abad726296da25ca6e1e58797858ba9e2 (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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2014 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 "athena/screen/modal_window_controller.h"

#include "athena/screen/public/screen_manager.h"
#include "base/message_loop/message_loop.h"
#include "ui/aura/window.h"
#include "ui/aura/window_property.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/wm/core/window_animations.h"

namespace athena {
namespace {

DEFINE_OWNED_WINDOW_PROPERTY_KEY(ModalWindowController,
                                 kModalWindowControllerKey,
                                 nullptr);

}  // namespace

// static
ModalWindowController* ModalWindowController::Get(aura::Window* container) {
  ModalWindowController* controller =
      container->GetProperty(kModalWindowControllerKey);
  CHECK(controller);
  return controller;
}

ModalWindowController::ModalWindowController(int priority)
    : modal_container_(nullptr),
      dimmer_window_(new aura::Window(nullptr)),
      dimmed_(false) {
  ScreenManager::ContainerParams params("ModalContainer", priority);
  params.can_activate_children = true;
  params.block_events = true;
  modal_container_ = ScreenManager::Get()->CreateContainer(params);
  modal_container_->SetProperty(kModalWindowControllerKey, this);

  dimmer_window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
  dimmer_window_->Init(aura::WINDOW_LAYER_SOLID_COLOR);
  dimmer_window_->layer()->SetColor(SK_ColorBLACK);
  dimmer_window_->layer()->SetOpacity(0.0f);
  dimmer_window_->Show();

  modal_container_->AddChild(dimmer_window_);
  modal_container_->AddObserver(this);

  UpdateDimmerWindowBounds();
}

ModalWindowController::~ModalWindowController() {
  if (modal_container_)
    modal_container_->RemoveObserver(this);
}

void ModalWindowController::OnWindowAdded(aura::Window* child) {
  DCHECK_NE(child, dimmer_window_);
  if (IsChildWindow(child)) {
    child->AddObserver(this);
    UpdateDimming(nullptr);
  }
}

void ModalWindowController::OnWindowVisibilityChanged(aura::Window* window,
                                                      bool visible) {
  if (IsChildWindow(window))
    UpdateDimming(nullptr);
}

void ModalWindowController::OnWindowBoundsChanged(aura::Window* window,
                                                  const gfx::Rect& old_bounds,
                                                  const gfx::Rect& new_bounds) {
  if (window == modal_container_)
    UpdateDimmerWindowBounds();
}

void ModalWindowController::OnWindowDestroyed(aura::Window* window) {
  UpdateDimming(window);
}

bool ModalWindowController::IsChildWindow(aura::Window* child) const {
  return child->parent() == modal_container_ && child != dimmer_window_;
}

void ModalWindowController::UpdateDimmerWindowBounds() {
  gfx::Rect bounds(modal_container_->bounds().size());
  dimmer_window_->SetBounds(bounds);
}

void ModalWindowController::UpdateDimming(aura::Window* ignore) {
  if (!modal_container_ || !dimmer_window_)
    return;
  bool should_delete = true;
  for (aura::Window* window : modal_container_->children()) {
    if (window == dimmer_window_ || window == ignore)
      continue;
    should_delete = false;
    if (window->TargetVisibility()) {
      SetDimmed(true);
      return;
    }
  }
  SetDimmed(false);

  if (should_delete) {
    // Remove the container from root so that the container becomes
    // invisible, but don't delete it until next event execution
    // because the call stack may still have and use the pointer.
    modal_container_->RemoveObserver(this);

    // Hide the window before removing it, so the focus manager which will run
    // in RemoveChild handler can know that this container is no longer
    // available.
    modal_container_->Hide();

    modal_container_->parent()->RemoveChild(modal_container_);
    base::MessageLoopForUI::current()->DeleteSoon(FROM_HERE, modal_container_);
    modal_container_ = nullptr;
    dimmer_window_ = nullptr;
  }
}

void ModalWindowController::SetDimmed(bool dimmed) {
  const float kDimmedOpacity = 0.4f;

  if (!dimmer_window_ || dimmed_ == dimmed)
    return;
  dimmed_ = dimmed;

  const int kDimmAnimationDurationMs = 500;
  if (dimmed) {
    ui::ScopedLayerAnimationSettings settings(
        dimmer_window_->layer()->GetAnimator());
    settings.SetTransitionDuration(
        base::TimeDelta::FromMilliseconds(kDimmAnimationDurationMs));
    dimmer_window_->layer()->SetOpacity(kDimmedOpacity);
  } else {
    // ScopedHidingAnimationSettings will detach the animating and
    // recreate layers for the container so that animation can continue
    // even if the container is removed immediately.
    wm::ScopedHidingAnimationSettings settings(modal_container_);
    settings.layer_animation_settings()->SetTransitionDuration(
        base::TimeDelta::FromMilliseconds(kDimmAnimationDurationMs));
    modal_container_->layer()->SetOpacity(0.0f);
  }
}

}  // namespace athena