1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/view_manager/view_tree_host_impl.h"
7 #include "components/view_manager/connection_manager.h"
8 #include "components/view_manager/display_manager.h"
9 #include "components/view_manager/public/cpp/types.h"
10 #include "components/view_manager/view_tree_host_delegate.h"
11 #include "mojo/converters/geometry/geometry_type_converters.h"
13 namespace view_manager
{
15 ViewTreeHostImpl::ViewTreeHostImpl(
16 mojo::ViewTreeHostClientPtr client
,
17 ConnectionManager
* connection_manager
,
19 mojo::ApplicationImpl
* app_impl
,
20 const scoped_refptr
<gles2::GpuState
>& gpu_state
,
21 const scoped_refptr
<surfaces::SurfacesState
>& surfaces_state
)
23 connection_manager_(connection_manager
),
24 client_(client
.Pass()),
26 DisplayManager::Create(is_headless
,
30 display_manager_
->Init(this);
33 ViewTreeHostImpl::~ViewTreeHostImpl() {
36 void ViewTreeHostImpl::Init(ViewTreeHostDelegate
* delegate
) {
38 if (delegate_
&& root_
)
39 delegate_
->OnDisplayInitialized();
42 ViewTreeImpl
* ViewTreeHostImpl::GetViewTree() {
43 return delegate_
? delegate_
->GetViewTree() : nullptr;
46 bool ViewTreeHostImpl::IsViewAttachedToRoot(const ServerView
* view
) const {
47 return root_
->Contains(view
) && view
!= root_
.get();
50 bool ViewTreeHostImpl::SchedulePaintIfInViewport(const ServerView
* view
,
51 const gfx::Rect
& bounds
) {
52 if (root_
->Contains(view
)) {
53 display_manager_
->SchedulePaint(view
, bounds
);
59 const mojo::ViewportMetrics
& ViewTreeHostImpl::GetViewportMetrics() const {
60 return display_manager_
->GetViewportMetrics();
63 void ViewTreeHostImpl::UpdateTextInputState(const ui::TextInputState
& state
) {
64 display_manager_
->UpdateTextInputState(state
);
67 void ViewTreeHostImpl::SetImeVisibility(bool visible
) {
68 display_manager_
->SetImeVisibility(visible
);
71 void ViewTreeHostImpl::SetSize(mojo::SizePtr size
) {
72 display_manager_
->SetViewportSize(size
.To
<gfx::Size
>());
75 void ViewTreeHostImpl::AddAccelerator(uint32_t id
,
76 mojo::KeyboardCode keyboard_code
,
77 mojo::EventFlags flags
) {
78 connection_manager_
->AddAccelerator(this, id
, keyboard_code
, flags
);
81 void ViewTreeHostImpl::RemoveAccelerator(uint32_t id
) {
82 connection_manager_
->RemoveAccelerator(this, id
);
85 ServerView
* ViewTreeHostImpl::GetRootView() {
89 void ViewTreeHostImpl::OnEvent(mojo::EventPtr event
) {
90 connection_manager_
->OnEvent(this, event
.Pass());
93 void ViewTreeHostImpl::OnDisplayClosed() {
95 delegate_
->OnDisplayClosed();
98 void ViewTreeHostImpl::OnViewportMetricsChanged(
99 const mojo::ViewportMetrics
& old_metrics
,
100 const mojo::ViewportMetrics
& new_metrics
) {
102 root_
.reset(connection_manager_
->CreateServerView(
103 RootViewId(connection_manager_
->GetAndAdvanceNextHostId())));
104 root_
->SetBounds(gfx::Rect(new_metrics
.size_in_pixels
.To
<gfx::Size
>()));
105 root_
->SetVisible(true);
107 delegate_
->OnDisplayInitialized();
109 root_
->SetBounds(gfx::Rect(new_metrics
.size_in_pixels
.To
<gfx::Size
>()));
111 // TODO(fsamuel): We shouldn't broadcast this to all connections but only
112 // those within a window root.
113 connection_manager_
->ProcessViewportMetricsChanged(old_metrics
, new_metrics
);
116 } // namespace view_manager