summaryrefslogtreecommitdiffstats
path: root/ash/monitor/monitor_controller.cc
blob: 9bc85b434e06fbdb7733137fb94d4c6b5b777487 (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
// 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/monitor/monitor_controller.h"

#include "ash/monitor/multi_monitor_manager.h"
#include "ash/shell.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/monitor.h"

namespace ash {
namespace internal {

MonitorController::MonitorController() {
  aura::Env::GetInstance()->monitor_manager()->AddObserver(this);
  Init();
}

MonitorController::~MonitorController() {
  aura::Env::GetInstance()->monitor_manager()->RemoveObserver(this);
  // Remove the root first.
  int monitor_id = Shell::GetPrimaryRootWindow()->GetProperty(kMonitorIdKey);
  DCHECK(monitor_id >= 0);
  root_windows_.erase(monitor_id);
  STLDeleteContainerPairSecondPointers(
      root_windows_.begin(), root_windows_.end());
}

void MonitorController::OnMonitorBoundsChanged(const gfx::Monitor& monitor) {
  root_windows_[monitor.id()]->SetHostBounds(monitor.bounds_in_pixel());
}

void MonitorController::OnMonitorAdded(const gfx::Monitor& monitor) {
  if (root_windows_.empty()) {
    root_windows_[monitor.id()] = Shell::GetPrimaryRootWindow();
    Shell::GetPrimaryRootWindow()->SetHostBounds(monitor.bounds_in_pixel());
    return;
  }
  aura::RootWindow* root = aura::Env::GetInstance()->monitor_manager()->
      CreateRootWindowForMonitor(monitor);
  root_windows_[monitor.id()] = root;
  Shell::GetInstance()->InitRootWindowForSecondaryMonitor(root);
}

void MonitorController::OnMonitorRemoved(const gfx::Monitor& monitor) {
  aura::RootWindow* root = root_windows_[monitor.id()];
  DCHECK(root);
  // Primary monitor should never be removed by MonitorManager.
  DCHECK(root != Shell::GetPrimaryRootWindow());
  // Monitor for root window will be deleted when the Primary RootWindow
  // is deleted by the Shell.
  if (root != Shell::GetPrimaryRootWindow()) {
    root_windows_.erase(monitor.id());
    delete root;
  }
}

void MonitorController::Init() {
  aura::MonitorManager* monitor_manager =
      aura::Env::GetInstance()->monitor_manager();
  for (size_t i = 0; i < monitor_manager->GetNumMonitors(); ++i) {
    const gfx::Monitor& monitor = monitor_manager->GetMonitorAt(i);
    if (i == 0) {
      // Primary monitor
      root_windows_[monitor.id()] = Shell::GetPrimaryRootWindow();
      Shell::GetPrimaryRootWindow()->SetHostBounds(monitor.bounds_in_pixel());
    } else {
      aura::RootWindow* root =
          monitor_manager->CreateRootWindowForMonitor(monitor);
      root_windows_[monitor.id()] = root;
      Shell::GetInstance()->InitRootWindowForSecondaryMonitor(root);
    }
  }
}

}  // namespace internal
}  // namespace ash