summaryrefslogtreecommitdiffstats
path: root/ash/wm/maximize_mode/maximize_mode_window_manager.cc
blob: a265bdb0e2c9f44ead97e4fde9ddbf2b3fecc422 (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
// 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 "ash/wm/maximize_mode/maximize_mode_window_manager.h"

#include "ash/shell.h"
#include "ash/switchable_windows.h"
#include "ash/wm/mru_window_tracker.h"
#include "ui/aura/window.h"
#include "ui/gfx/screen.h"

namespace ash {
namespace internal {

MaximizeModeWindowManager::~MaximizeModeWindowManager() {
  Shell::GetScreen()->RemoveObserver(this);
  RemoveWindowCreationObservers();
  RestoreAllWindows();
}

int MaximizeModeWindowManager::GetNumberOfManagedWindows() {
  return initial_show_state_.size();
}

void MaximizeModeWindowManager::OnWindowDestroying(aura::Window* window) {
  // If a known window gets destroyed we need to remove all knowledge about it.
  if (!IsContainerWindow(window))
    ForgetWindow(window);
}

void MaximizeModeWindowManager::OnWindowAdded(
    aura::Window* window) {
  // A window can get removed and then re-added by a drag and drop operation.
  if (IsContainerWindow(window->parent()) &&
      initial_show_state_.find(window) == initial_show_state_.end())
    MaximizeAndTrackWindow(window);
}

void MaximizeModeWindowManager::OnWindowBoundsChanged(
    aura::Window* window,
    const gfx::Rect& old_bounds,
    const gfx::Rect& new_bounds) {
  if (!IsContainerWindow(window))
    return;
  // Reposition all non maximizeable windows.
  for (WindowToShowState::iterator it = initial_show_state_.begin();
       it != initial_show_state_.end();
       ++it) {
    if (!CanMaximize(it->first))
      CenterWindow(it->first);
  }
}

void MaximizeModeWindowManager::OnDisplayBoundsChanged(
    const gfx::Display& display) {
  // Nothing to do here.
}

void MaximizeModeWindowManager::OnDisplayAdded(const gfx::Display& display) {
  DisplayConfigurationChanged();
}

void MaximizeModeWindowManager::OnDisplayRemoved(const gfx::Display& display) {
  DisplayConfigurationChanged();
}

MaximizeModeWindowManager::MaximizeModeWindowManager() {
  MaximizeAllWindows();
  AddWindowCreationObservers();
  Shell::GetScreen()->AddObserver(this);
}

void MaximizeModeWindowManager::MaximizeAllWindows() {
  MruWindowTracker::WindowList windows =
      MruWindowTracker::BuildWindowList(false);
  // Add all existing Mru windows.
  for (MruWindowTracker::WindowList::iterator window = windows.begin();
      window != windows.end(); ++window) {
    MaximizeAndTrackWindow(*window);
  }
}

void MaximizeModeWindowManager::RestoreAllWindows() {
  WindowToShowState::iterator it = initial_show_state_.begin();
  while (it != initial_show_state_.end()) {
    RestoreAndForgetWindow(it->first);
    // |RestoreAndForgetWindow| will change the |initial_show_state_| list.
    it = initial_show_state_.begin();
  }
}

void MaximizeModeWindowManager::MaximizeAndTrackWindow(
    aura::Window* window) {
  if (!ShouldHandleWindow(window))
    return;

  DCHECK(initial_show_state_.find(window) == initial_show_state_.end());
  window->AddObserver(this);
  // Remember the state at the creation.
  ash::wm::WindowState* window_state = ash::wm::GetWindowState(window);
  ui::WindowShowState state = window_state->GetShowState();
  initial_show_state_[window] = state;
  // If it is not maximized yet we may need to maximize it now.
  if (state != ui::SHOW_STATE_MAXIMIZED) {
    if (!CanMaximize(window)) {
      // This window type should not be able to have a restore state set (since
      // it cannot maximize).
      DCHECK(!window_state->HasRestoreBounds());
      // Store the coordinates as restore coordinates.
      gfx::Rect initial_rect = window->bounds();
      if (window->parent())
        window_state->SetRestoreBoundsInParent(initial_rect);
      else
        window_state->SetRestoreBoundsInScreen(initial_rect);
      CenterWindow(window);
      // TODO(skuhne): Add a background cover layer.
    } else {
      // Minimized windows can remain as they are.
      if (state != ui::SHOW_STATE_MINIMIZED)
        ash::wm::GetWindowState(window)->Maximize();
    }
  }
}

void MaximizeModeWindowManager::RestoreAndForgetWindow(
    aura::Window* window) {
  ui::WindowShowState state = ForgetWindow(window);
  // Restore window if it can be restored.
  if (state != ui::SHOW_STATE_MAXIMIZED) {
    if (!CanMaximize(window)) {
      // TODO(skuhne): Remove the background cover layer.
      ash::wm::WindowState* window_state = ash::wm::GetWindowState(window);
      if (window_state->HasRestoreBounds()) {
        gfx::Rect initial_bounds =
            window->parent() ? window_state->GetRestoreBoundsInParent() :
                               window_state->GetRestoreBoundsInScreen();
        window_state->ClearRestoreBounds();
        // TODO(skuhne): The screen might have changed and we should make sure
        // that the bounds are in a visible area.
        window->SetBounds(initial_bounds);
      }
    } else {
      // If the window neither was minimized or maximized and it is currently
      // not minimized, we restore it.
      if (state != ui::SHOW_STATE_MINIMIZED &&
          !ash::wm::GetWindowState(window)->IsMinimized()) {
        ash::wm::GetWindowState(window)->Restore();
      }
    }
  }
}

ui::WindowShowState MaximizeModeWindowManager::ForgetWindow(
    aura::Window* window) {
  WindowToShowState::iterator it = initial_show_state_.find(window);
  DCHECK(it != initial_show_state_.end());
  window->RemoveObserver(this);
  ui::WindowShowState state = it->second;
  initial_show_state_.erase(it);
  return state;
}

bool MaximizeModeWindowManager::ShouldHandleWindow(aura::Window* window) {
  DCHECK(window);
  return window->type() == ui::wm::WINDOW_TYPE_NORMAL;
}

bool MaximizeModeWindowManager::CanMaximize(aura::Window* window) {
  DCHECK(window);
  return ash::wm::GetWindowState(window)->CanMaximize();
}

void MaximizeModeWindowManager::CenterWindow(aura::Window* window) {
  gfx::Size window_size = window->bounds().size();
  gfx::Rect work_area = gfx::Screen::GetScreenFor(window)->
      GetDisplayNearestWindow(window).work_area();

  gfx::Rect window_bounds(
      work_area.x() + (work_area.width() - window_size.width()) / 2,
      work_area.y() + (work_area.height() - window_size.height()) / 2,
      window_size.width(),
      window_size.height());

  window->SetBounds(window_bounds);
}

void MaximizeModeWindowManager::AddWindowCreationObservers() {
  DCHECK(observed_container_windows_.empty());
  // Observe window activations and switchable containers on all root windows
  // for newly created windows during overview.
  aura::Window::Windows root_windows = Shell::GetAllRootWindows();
  for (aura::Window::Windows::const_iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter) {
    for (size_t i = 0; i < kSwitchableWindowContainerIdsLength; ++i) {
      aura::Window* container = Shell::GetContainer(*iter,
          kSwitchableWindowContainerIds[i]);
      DCHECK(observed_container_windows_.find(container) ==
                observed_container_windows_.end());
      container->AddObserver(this);
      observed_container_windows_.insert(container);
    }
  }
}

void MaximizeModeWindowManager::RemoveWindowCreationObservers() {
  for (std::set<aura::Window*>::iterator iter =
           observed_container_windows_.begin();
       iter != observed_container_windows_.end(); ++iter) {
    (*iter)->RemoveObserver(this);
  }
  observed_container_windows_.clear();
}

void MaximizeModeWindowManager::DisplayConfigurationChanged() {
  RemoveWindowCreationObservers();
  AddWindowCreationObservers();
}

bool MaximizeModeWindowManager::IsContainerWindow(aura::Window* window) {
  return observed_container_windows_.find(window) !=
             observed_container_windows_.end();
}

}  // namespace internal
}  // namespace ash