summaryrefslogtreecommitdiffstats
path: root/mash/wm/layout_manager.cc
blob: 1edef0aa345fc89aa833e699fb85ccc35b544ddb (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
// Copyright 2015 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 "mash/wm/layout_manager.h"

#include <stdint.h>

#include "components/mus/public/cpp/property_type_converters.h"
#include "components/mus/public/cpp/window.h"
#include "components/mus/public/cpp/window_property.h"

namespace mash {
namespace wm {

LayoutManager::~LayoutManager() {
  Uninstall();
}

LayoutManager::LayoutManager(mus::Window* owner) : owner_(owner) {
  owner_->AddObserver(this);
  DCHECK(owner->children().empty());
}

void LayoutManager::Uninstall() {
  if (!owner_)
    return;
  owner_->RemoveObserver(this);
  for (auto child : owner_->children())
    child->RemoveObserver(this);
  owner_ = nullptr;
}

void LayoutManager::OnTreeChanged(
    const mus::WindowObserver::TreeChangeParams& params) {
  DCHECK(params.target);
  if (params.new_parent == owner_) {
    // params.target was added to the layout.
    WindowAdded(params.target);
    params.target->AddObserver(this);
    LayoutWindow(params.target);
  } else if (params.old_parent == owner_) {
    // params.target was removed from the layout.
    params.target->RemoveObserver(this);
    WindowRemoved(params.target);
  }
}

void LayoutManager::OnWindowDestroying(mus::Window* window) {
  if (owner_ == window)
    Uninstall();
}

void LayoutManager::OnWindowBoundsChanged(mus::Window* window,
                                          const gfx::Rect& old_bounds,
                                          const gfx::Rect& new_bounds) {
  if (window != owner_)
    return;

  // Changes to the container's bounds require all windows to be laid out.
  for (auto child : window->children())
    LayoutWindow(child);
}

void LayoutManager::OnWindowSharedPropertyChanged(
    mus::Window* window,
    const std::string& name,
    const std::vector<uint8_t>* old_data,
    const std::vector<uint8_t>* new_data) {
  if (window == owner_)
    return;

  // Changes to the following properties require the window to be laid out.
  if (layout_properties_.count(name) > 0)
    LayoutWindow(window);
}

void LayoutManager::WindowAdded(mus::Window* window) {}
void LayoutManager::WindowRemoved(mus::Window* window) {}

void LayoutManager::AddLayoutProperty(const std::string& name) {
  layout_properties_.insert(name);
}

}  // namespace wm
}  // namespace mash