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
153
154
155
156
157
158
159
160
161
|
// Copyright (c) 2012 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/wm/window_modality_controller.h"
#include <algorithm>
#include "ash/wm/window_animations.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/window_util.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/base/events/event.h"
#include "ui/base/ui_base_types.h"
namespace ash {
namespace wm {
namespace {
bool HasAncestor(aura::Window* window, aura::Window* ancestor) {
if (!window)
return false;
if (window == ancestor)
return true;
return HasAncestor(window->parent(), ancestor);
}
bool TransientChildIsWindowModal(aura::Window* window) {
return window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_WINDOW;
}
bool TransientChildIsChildModal(aura::Window* window) {
return window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_CHILD;
}
aura::Window* GetModalParent(aura::Window* window) {
return window->GetProperty(ash::internal::kModalParentKey);
}
bool IsModalTransientChild(aura::Window* transient, aura::Window* original) {
return transient->IsVisible() &&
(TransientChildIsWindowModal(transient) ||
(TransientChildIsChildModal(transient) &&
(HasAncestor(original, GetModalParent(transient)))));
}
aura::Window* GetModalTransientChild(
aura::Window* activatable,
aura::Window* original) {
aura::Window::Windows::const_iterator it;
for (it = activatable->transient_children().begin();
it != activatable->transient_children().end();
++it) {
aura::Window* transient = *it;
if (IsModalTransientChild(transient, original)) {
return transient->transient_children().empty() ?
transient : GetModalTransientChild(transient, original);
}
}
return NULL;
}
} // namespace
void SetModalParent(aura::Window* child, aura::Window* parent) {
child->SetProperty(ash::internal::kModalParentKey, parent);
}
aura::Window* GetModalTransient(aura::Window* window) {
if (!window)
return NULL;
// We always want to check the for the transient child of the activatable
// window.
aura::Window* activatable = wm::GetActivatableWindow(window);
if (!activatable)
return NULL;
return GetModalTransientChild(activatable, window);
}
} // namespace wm
namespace internal {
////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, public:
WindowModalityController::WindowModalityController() {
aura::Env::GetInstance()->AddObserver(this);
}
WindowModalityController::~WindowModalityController() {
aura::Env::GetInstance()->RemoveObserver(this);
for (size_t i = 0; i < windows_.size(); ++i)
windows_[i]->RemoveObserver(this);
}
////////////////////////////////////////////////////////////////////////////////
// WindowModalityController, aura::EventFilter implementation:
ui::EventResult WindowModalityController::OnKeyEvent(ui::KeyEvent* event) {
aura::Window* target = static_cast<aura::Window*>(event->target());
return wm::GetModalTransient(target) ? ui::ER_CONSUMED :
ui::ER_UNHANDLED;
}
ui::EventResult WindowModalityController::OnMouseEvent(ui::MouseEvent* event) {
aura::Window* target = static_cast<aura::Window*>(event->target());
return ProcessLocatedEvent(target, event) ? ui::ER_CONSUMED :
ui::ER_UNHANDLED;
}
ui::EventResult WindowModalityController::OnTouchEvent(ui::TouchEvent* event) {
aura::Window* target = static_cast<aura::Window*>(event->target());
return ProcessLocatedEvent(target, event) ? ui::ER_CONSUMED :
ui::ER_UNHANDLED;
}
void WindowModalityController::OnWindowInitialized(aura::Window* window) {
windows_.push_back(window);
window->AddObserver(this);
}
void WindowModalityController::OnWindowVisibilityChanged(
aura::Window* window,
bool visible) {
if (visible && window->GetProperty(aura::client::kModalKey) ==
ui::MODAL_TYPE_WINDOW) {
// Make sure no other window has capture, otherwise |window| won't get mouse
// events.
aura::Window* capture_window = aura::client::GetCaptureWindow(window);
if (capture_window)
capture_window->ReleaseCapture();
}
}
void WindowModalityController::OnWindowDestroyed(aura::Window* window) {
windows_.erase(std::find(windows_.begin(), windows_.end(), window));
window->RemoveObserver(this);
}
bool WindowModalityController::ProcessLocatedEvent(aura::Window* target,
ui::LocatedEvent* event) {
aura::Window* modal_transient_child = wm::GetModalTransient(target);
if (modal_transient_child && (event->type() == ui::ET_MOUSE_PRESSED ||
event->type() == ui::ET_TOUCH_PRESSED)) {
ash::internal::AnimateWindow(modal_transient_child,
ash::WINDOW_ANIMATION_TYPE_BOUNCE);
}
return !!modal_transient_child;
}
} // namespace internal
} // namespace ash
|