summaryrefslogtreecommitdiffstats
path: root/athena/screen/screen_manager_impl.cc
blob: 8f960eb442413207454b9e9b8d0d4e6c7ddad8e0 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// 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/screen_manager_impl.h"

#include "athena/input/public/accelerator_manager.h"
#include "athena/screen/modal_window_controller.h"
#include "athena/screen/screen_accelerator_handler.h"
#include "athena/util/container_priorities.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/test/test_screen.h"
#include "ui/aura/window.h"
#include "ui/aura/window_property.h"
#include "ui/aura/window_targeter.h"
#include "ui/aura/window_tree_host.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/wm/core/base_focus_rules.h"
#include "ui/wm/core/capture_controller.h"
#include "ui/wm/core/default_screen_position_client.h"
#include "ui/wm/core/focus_controller.h"
#include "ui/wm/core/window_util.h"

// This is to avoid creating type definitoin for kAlwaysOnTopKey.
DECLARE_EXPORTED_WINDOW_PROPERTY_TYPE(ATHENA_EXPORT, bool);

namespace athena {
namespace {

DEFINE_OWNED_WINDOW_PROPERTY_KEY(ScreenManager::ContainerParams,
                                 kContainerParamsKey,
                                 nullptr);

ScreenManagerImpl* instance = nullptr;

// A functor to find a container that has the higher priority.
struct HigherPriorityFinder {
  HigherPriorityFinder(int p) : priority(p) {}
  bool operator()(aura::Window* window) {
    return window->GetProperty(kContainerParamsKey)->z_order_priority >
           priority;
  }
  int priority;
};

bool BlockEvents(aura::Window* container) {
  ScreenManager::ContainerParams* params =
      container->GetProperty(kContainerParamsKey);
  return params && params->block_events && container->IsVisible();
}

bool DefaultContainer(aura::Window* container) {
  ScreenManager::ContainerParams* params =
      container->GetProperty(kContainerParamsKey);
  return params && params->default_parent;
}

bool HasModalContainerPriority(aura::Window* container) {
  ScreenManager::ContainerParams* params =
      container->GetProperty(kContainerParamsKey);
  return params && params->modal_container_priority != -1;
}

bool IsSystemModal(aura::Window* window) {
  return window->GetProperty(aura::client::kModalKey) == ui::MODAL_TYPE_SYSTEM;
}

// Returns the container which contains |window|.
aura::Window* GetContainer(aura::Window* window) {
  aura::Window* container = window;
  while (container && !container->GetProperty(kContainerParamsKey))
    container = container->parent();
  return container;
}

class AthenaFocusRules : public wm::BaseFocusRules {
 public:
  AthenaFocusRules() {}
  ~AthenaFocusRules() override {}

  // wm::BaseFocusRules:
  virtual bool SupportsChildActivation(aura::Window* window) const override {
    ScreenManager::ContainerParams* params =
        window->GetProperty(kContainerParamsKey);
    return params && params->can_activate_children;
  }
  virtual bool CanActivateWindow(aura::Window* window) const override {
    if (!window)
      return true;

    // Check if containers of higher z-order than |window| have 'block_events'
    // fields.
    if (window->GetRootWindow()) {
      const aura::Window::Windows& containers =
          window->GetRootWindow()->children();
      aura::Window::Windows::const_iterator iter =
          std::find(containers.begin(), containers.end(), GetContainer(window));
      DCHECK(iter != containers.end());
      for (++iter; iter != containers.end(); ++iter) {
        if (BlockEvents(*iter))
          return false;
      }
    }
    return BaseFocusRules::CanActivateWindow(window);
  }

  aura::Window* GetTopmostWindowToActivateInContainer(
      aura::Window* container,
      aura::Window* ignore) const {
    for (aura::Window::Windows::const_reverse_iterator i =
             container->children().rbegin();
         i != container->children().rend();
         ++i) {
      if (*i != ignore && CanActivateWindow(*i))
        return *i;
    }
    return NULL;
  }

  virtual aura::Window* GetNextActivatableWindow(
      aura::Window* ignore) const override {
    const aura::Window::Windows& containers =
        ignore->GetRootWindow()->children();
    auto starting_container_iter = containers.begin();
    for (auto container_iter = containers.begin();
         container_iter != containers.end();
         container_iter++) {
      if ((*container_iter)->Contains(ignore)) {
        starting_container_iter = container_iter;
        break;
      }
    }

    // Find next window from the front containers.
    aura::Window* next = nullptr;
    for (auto container_iter = starting_container_iter;
         !next && container_iter != containers.end();
         container_iter++) {
      next = GetTopmostWindowToActivateInContainer(*container_iter, ignore);
    }

    // Find next window from the back containers.
    auto container_iter = starting_container_iter;
    while (!next && container_iter != containers.begin()) {
      container_iter--;
      next = GetTopmostWindowToActivateInContainer(*container_iter, ignore);
    }
    return next;
  }

 private:
  DISALLOW_COPY_AND_ASSIGN(AthenaFocusRules);
};

class AthenaScreenPositionClient : public wm::DefaultScreenPositionClient {
 public:
  AthenaScreenPositionClient() {
  }
  ~AthenaScreenPositionClient() override {}

 private:
  // aura::client::ScreenPositionClient:
  void ConvertHostPointToScreen(aura::Window* window,
                                gfx::Point* point) override {
    // TODO(oshima): Implement this when adding multiple display support.
    NOTREACHED();
  }

  DISALLOW_COPY_AND_ASSIGN(AthenaScreenPositionClient);
};

class AthenaWindowTargeter : public aura::WindowTargeter {
 public:
  explicit AthenaWindowTargeter(aura::Window* root_window)
      : root_window_(root_window) {}

  ~AthenaWindowTargeter() override {}

 private:
  // aura::WindowTargeter:
  virtual bool SubtreeCanAcceptEvent(
      ui::EventTarget* target,
      const ui::LocatedEvent& event) const override {
    const aura::Window::Windows& containers = root_window_->children();
    auto r_iter =
        std::find_if(containers.rbegin(), containers.rend(), &BlockEvents);
    if (r_iter == containers.rend())
      return aura::WindowTargeter::SubtreeCanAcceptEvent(target, event);

    aura::Window* window = static_cast<aura::Window*>(target);
    for (;; --r_iter) {
      if ((*r_iter)->Contains(window))
        return aura::WindowTargeter::SubtreeCanAcceptEvent(target, event);
      if (r_iter == containers.rbegin())
        break;
    }
    return false;
  }

  virtual ui::EventTarget* FindTargetForLocatedEvent(
      ui::EventTarget* root,
      ui::LocatedEvent* event) override {
    ui::EventTarget* target =
        aura::WindowTargeter::FindTargetForLocatedEvent(root, event);
    if (target)
      return target;
    // If the root target is blocking the event, return the container even if
    // there is no target found so that windows behind it will not be searched.
    const ScreenManager::ContainerParams* params =
        static_cast<aura::Window*>(root)->GetProperty(kContainerParamsKey);
    return (params && params->block_events) ? root : nullptr;
  }

  // Not owned.
  aura::Window* root_window_;

  DISALLOW_COPY_AND_ASSIGN(AthenaWindowTargeter);
};

}  // namespace

ScreenManagerImpl::ScreenManagerImpl(aura::Window* root_window)
    : root_window_(root_window),
      last_requested_rotation_(gfx::Display::ROTATE_0),
      rotation_locked_(false) {
  DCHECK(root_window_);
  DCHECK(!instance);
  instance = this;
}

ScreenManagerImpl::~ScreenManagerImpl() {
  aura::client::SetScreenPositionClient(root_window_, nullptr);
  aura::client::SetWindowTreeClient(root_window_, nullptr);
  wm::FocusController* focus_controller =
      static_cast<wm::FocusController*>(focus_client_.get());
  root_window_->RemovePreTargetHandler(focus_controller);
  aura::client::SetActivationClient(root_window_, nullptr);
  aura::client::SetFocusClient(root_window_, nullptr);
  aura::Window::Windows children = root_window_->children();
  // Close All children:
  for (aura::Window::Windows::iterator iter = children.begin();
       iter != children.end();
       ++iter) {
    delete *iter;
  }
  instance = nullptr;
}

void ScreenManagerImpl::Init() {
  wm::FocusController* focus_controller =
      new wm::FocusController(new AthenaFocusRules());

  aura::client::SetFocusClient(root_window_, focus_controller);
  root_window_->AddPreTargetHandler(focus_controller);
  aura::client::SetActivationClient(root_window_, focus_controller);
  focus_client_.reset(focus_controller);

  capture_client_.reset(new ::wm::ScopedCaptureClient(root_window_));
  accelerator_handler_.reset(new ScreenAcceleratorHandler());

  aura::client::SetWindowTreeClient(root_window_, this);

  screen_position_client_.reset(new AthenaScreenPositionClient());
  aura::client::SetScreenPositionClient(root_window_,
                                        screen_position_client_.get());
  root_window_->SetEventTargeter(
      make_scoped_ptr(new AthenaWindowTargeter(root_window_)));
}

aura::Window* ScreenManagerImpl::FindContainerByPriority(int priority) {
  for (aura::Window* window : root_window_->children()) {
    if (window->GetProperty(kContainerParamsKey)->z_order_priority == priority)
      return window;
  }
  return nullptr;
}

aura::Window* ScreenManagerImpl::CreateContainer(
    const ContainerParams& params) {
  const aura::Window::Windows& children = root_window_->children();

  if (params.default_parent) {
    CHECK(std::find_if(children.begin(), children.end(), &DefaultContainer) ==
          children.end());
  }
  // mmodal container's priority must be higher than the container's priority.
  DCHECK(params.modal_container_priority == -1 ||
         params.modal_container_priority > params.z_order_priority);
  // Default parent must specify modal_container_priority.
  DCHECK(!params.default_parent || params.modal_container_priority != -1);

  aura::Window* container = new aura::Window(nullptr);
  CHECK_GE(params.z_order_priority, 0);
  container->Init(aura::WINDOW_LAYER_NOT_DRAWN);
  container->SetName(params.name);

  DCHECK(!FindContainerByPriority(params.z_order_priority))
      << "The container with the priority " << params.z_order_priority
      << " already exists.";

  container->SetProperty(kContainerParamsKey, new ContainerParams(params));

  root_window_->AddChild(container);

  aura::Window::Windows::const_iterator iter =
      std::find_if(children.begin(),
                   children.end(),
                   HigherPriorityFinder(params.z_order_priority));
  if (iter != children.end())
    root_window_->StackChildBelow(container, *iter);

  container->Show();
  return container;
}

aura::Window* ScreenManagerImpl::GetContext() {
  return root_window_;
}

void ScreenManagerImpl::SetRotation(gfx::Display::Rotation rotation) {
  last_requested_rotation_ = rotation;
  if (rotation_locked_ || rotation ==
      gfx::Screen::GetNativeScreen()->GetPrimaryDisplay().rotation()) {
    return;
  }

  // TODO(flackr): Use display manager to update display rotation:
  // http://crbug.com/401044.
  static_cast<aura::TestScreen*>(gfx::Screen::GetNativeScreen())->
      SetDisplayRotation(rotation);
}

void ScreenManagerImpl::SetRotationLocked(bool rotation_locked) {
  rotation_locked_ = rotation_locked;
  if (!rotation_locked_)
    SetRotation(last_requested_rotation_);
}

int ScreenManagerImpl::GetModalContainerPriority(aura::Window* window,
                                                 aura::Window* parent) {
  const aura::Window::Windows& children = root_window_->children();
  if (window->GetProperty(aura::client::kAlwaysOnTopKey)) {
    // Use top most modal container.
    auto iter = std::find_if(
        children.rbegin(), children.rend(), &HasModalContainerPriority);
    DCHECK(iter != children.rend());
    return (*iter)->GetProperty(kContainerParamsKey)->modal_container_priority;
  } else {
    // use the container closest to the parent which has modal
    // container priority.
    auto iter = std::find(children.rbegin(), children.rend(), parent);
    DCHECK(iter != children.rend());
    iter = std::find_if(iter, children.rend(), &HasModalContainerPriority);
    DCHECK(iter != children.rend());
    return (*iter)->GetProperty(kContainerParamsKey)->modal_container_priority;
  }
}

aura::Window* ScreenManagerImpl::GetDefaultParent(aura::Window* context,
                                                  aura::Window* window,
                                                  const gfx::Rect& bounds) {
  aura::Window* parent = wm::GetTransientParent(window);
  if (parent)
    parent = GetContainer(parent);
  else
    parent = GetDefaultContainer();

  if (IsSystemModal(window)) {
    DCHECK(window->type() == ui::wm::WINDOW_TYPE_NORMAL ||
           window->type() == ui::wm::WINDOW_TYPE_POPUP);
    int priority = GetModalContainerPriority(window, parent);

    parent = FindContainerByPriority(priority);
    if (!parent) {
      ModalWindowController* controller = new ModalWindowController(priority);
      parent = controller->modal_container();
    }
  }
  return parent;
}

aura::Window* ScreenManagerImpl::GetDefaultContainer() {
  const aura::Window::Windows& children = root_window_->children();
  return *(std::find_if(children.begin(), children.end(), &DefaultContainer));
}

ScreenManager::ContainerParams::ContainerParams(const std::string& n,
                                                int priority)
    : name(n),
      can_activate_children(false),
      block_events(false),
      z_order_priority(priority),
      default_parent(false),
      modal_container_priority(-1) {
}

// static
ScreenManager* ScreenManager::Create(aura::Window* root_window) {
  (new ScreenManagerImpl(root_window))->Init();
  DCHECK(instance);
  return instance;
}

// static
ScreenManager* ScreenManager::Get() {
  DCHECK(instance);
  return instance;
}

// static
void ScreenManager::Shutdown() {
  DCHECK(instance);
  delete instance;
  DCHECK(!instance);
}

}  // namespace athena