summaryrefslogtreecommitdiffstats
path: root/components/view_manager/view_tree_host_impl.cc
blob: 1490e1edb6b2cec3be5528470a533b633f1d8bd7 (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
// 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 "components/view_manager/view_tree_host_impl.h"

#include "components/view_manager/connection_manager.h"
#include "components/view_manager/display_manager.h"
#include "components/view_manager/public/cpp/types.h"
#include "components/view_manager/view_tree_host_delegate.h"
#include "mojo/converters/geometry/geometry_type_converters.h"

namespace view_manager {

ViewTreeHostImpl::ViewTreeHostImpl(
    mojo::ViewTreeHostClientPtr client,
    ConnectionManager* connection_manager,
    bool is_headless,
    mojo::ApplicationImpl* app_impl,
    const scoped_refptr<gles2::GpuState>& gpu_state,
    const scoped_refptr<surfaces::SurfacesState>& surfaces_state)
    : delegate_(nullptr),
      connection_manager_(connection_manager),
      client_(client.Pass()),
      display_manager_(
          DisplayManager::Create(is_headless,
                                 app_impl,
                                 gpu_state,
                                 surfaces_state)) {
  display_manager_->Init(this);
}

ViewTreeHostImpl::~ViewTreeHostImpl() {
}

void ViewTreeHostImpl::Init(ViewTreeHostDelegate* delegate) {
  delegate_ = delegate;
  if (delegate_ && root_)
    delegate_->OnDisplayInitialized();
}

ViewTreeImpl* ViewTreeHostImpl::GetViewTree() {
  return delegate_ ? delegate_->GetViewTree() : nullptr;
}

bool ViewTreeHostImpl::IsViewAttachedToRoot(const ServerView* view) const {
  return root_->Contains(view) && view != root_.get();
}

bool ViewTreeHostImpl::SchedulePaintIfInViewport(const ServerView* view,
                                                 const gfx::Rect& bounds) {
  if (root_->Contains(view)) {
    display_manager_->SchedulePaint(view, bounds);
    return true;
  }
  return false;
}

const mojo::ViewportMetrics& ViewTreeHostImpl::GetViewportMetrics() const {
  return display_manager_->GetViewportMetrics();
}

void ViewTreeHostImpl::UpdateTextInputState(const ui::TextInputState& state) {
  display_manager_->UpdateTextInputState(state);
}

void ViewTreeHostImpl::SetImeVisibility(bool visible) {
  display_manager_->SetImeVisibility(visible);
}

void ViewTreeHostImpl::SetSize(mojo::SizePtr size) {
  display_manager_->SetViewportSize(size.To<gfx::Size>());
}

void ViewTreeHostImpl::AddAccelerator(uint32_t id,
                                      mojo::KeyboardCode keyboard_code,
                                      mojo::EventFlags flags) {
  connection_manager_->AddAccelerator(this, id, keyboard_code, flags);
}

void ViewTreeHostImpl::RemoveAccelerator(uint32_t id) {
  connection_manager_->RemoveAccelerator(this, id);
}

ServerView* ViewTreeHostImpl::GetRootView() {
  return root_.get();
}

void ViewTreeHostImpl::OnEvent(mojo::EventPtr event) {
  connection_manager_->OnEvent(this, event.Pass());
}

void ViewTreeHostImpl::OnDisplayClosed() {
  if (delegate_)
    delegate_->OnDisplayClosed();
}

void ViewTreeHostImpl::OnViewportMetricsChanged(
    const mojo::ViewportMetrics& old_metrics,
    const mojo::ViewportMetrics& new_metrics) {
  if (!root_) {
    root_.reset(connection_manager_->CreateServerView(
        RootViewId(connection_manager_->GetAndAdvanceNextHostId())));
    root_->SetBounds(gfx::Rect(new_metrics.size_in_pixels.To<gfx::Size>()));
    root_->SetVisible(true);
    if (delegate_)
      delegate_->OnDisplayInitialized();
  } else {
    root_->SetBounds(gfx::Rect(new_metrics.size_in_pixels.To<gfx::Size>()));
  }
  // TODO(fsamuel): We shouldn't broadcast this to all connections but only
  // those within a window root.
  connection_manager_->ProcessViewportMetricsChanged(old_metrics, new_metrics);
}

}  // namespace view_manager