Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_platform_data_aura.cc
1 // Copyright 2014 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.
4
5 #include "content/shell/browser/shell_platform_data_aura.h"
6
7 #include "content/shell/browser/shell.h"
8 #include "ui/aura/client/aura_constants.h"
9 #include "ui/aura/client/default_capture_client.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/layout_manager.h"
12 #include "ui/aura/test/test_focus_client.h"
13 #include "ui/aura/test/test_window_tree_client.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/base/ime/input_method.h"
17 #include "ui/base/ime/input_method_delegate.h"
18 #include "ui/base/ime/input_method_factory.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/wm/core/default_activation_client.h"
21
22 namespace content {
23
24 namespace {
25
26 class FillLayout : public aura::LayoutManager {
27  public:
28   explicit FillLayout(aura::Window* root)
29       : root_(root) {
30   }
31
32   ~FillLayout() override {}
33
34  private:
35   // aura::LayoutManager:
36   void OnWindowResized() override {}
37
38   void OnWindowAddedToLayout(aura::Window* child) override {
39     child->SetBounds(root_->bounds());
40   }
41
42   void OnWillRemoveWindowFromLayout(aura::Window* child) override {}
43
44   void OnWindowRemovedFromLayout(aura::Window* child) override {}
45
46   void OnChildWindowVisibilityChanged(aura::Window* child,
47                                       bool visible) override {}
48
49   void SetChildBounds(aura::Window* child,
50                       const gfx::Rect& requested_bounds) override {
51     SetChildBoundsDirect(child, requested_bounds);
52   }
53
54   aura::Window* root_;
55
56   DISALLOW_COPY_AND_ASSIGN(FillLayout);
57 };
58
59 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
60                                 public ui::EventHandler {
61  public:
62   explicit MinimalInputEventFilter(aura::WindowTreeHost* host)
63       : host_(host),
64         input_method_(ui::CreateInputMethod(this,
65                                             gfx::kNullAcceleratedWidget)) {
66     input_method_->Init(true);
67     host_->window()->AddPreTargetHandler(this);
68     host_->window()->SetProperty(aura::client::kRootWindowInputMethodKey,
69                                  input_method_.get());
70   }
71
72   ~MinimalInputEventFilter() override {
73     host_->window()->RemovePreTargetHandler(this);
74     host_->window()->SetProperty(aura::client::kRootWindowInputMethodKey,
75                                  static_cast<ui::InputMethod*>(NULL));
76   }
77
78  private:
79   // ui::EventHandler:
80   void OnKeyEvent(ui::KeyEvent* event) override {
81     // See the comment in InputMethodEventFilter::OnKeyEvent() for details.
82     if (event->IsTranslated()) {
83       event->SetTranslated(false);
84     } else {
85       if (input_method_->DispatchKeyEvent(*event))
86         event->StopPropagation();
87     }
88   }
89
90   // ui::internal::InputMethodDelegate:
91   bool DispatchKeyEventPostIME(const ui::KeyEvent& event) override {
92     // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for
93     // details.
94     ui::KeyEvent aura_event(event);
95     aura_event.SetTranslated(true);
96     ui::EventDispatchDetails details =
97         host_->dispatcher()->OnEventFromSource(&aura_event);
98     return aura_event.handled() || details.dispatcher_destroyed;
99   }
100
101   aura::WindowTreeHost* host_;
102   scoped_ptr<ui::InputMethod> input_method_;
103
104   DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
105 };
106
107 }
108
109 ShellPlatformDataAura* Shell::platform_ = NULL;
110
111 ShellPlatformDataAura::ShellPlatformDataAura(const gfx::Size& initial_size) {
112   CHECK(aura::Env::GetInstance());
113   host_.reset(aura::WindowTreeHost::Create(gfx::Rect(initial_size)));
114   host_->InitHost();
115   host_->window()->SetLayoutManager(new FillLayout(host_->window()));
116
117   focus_client_.reset(new aura::test::TestFocusClient());
118   aura::client::SetFocusClient(host_->window(), focus_client_.get());
119
120   new wm::DefaultActivationClient(host_->window());
121   capture_client_.reset(
122       new aura::client::DefaultCaptureClient(host_->window()));
123   window_tree_client_.reset(
124       new aura::test::TestWindowTreeClient(host_->window()));
125   ime_filter_.reset(new MinimalInputEventFilter(host_.get()));
126 }
127
128 ShellPlatformDataAura::~ShellPlatformDataAura() {
129 }
130
131 void ShellPlatformDataAura::ShowWindow() {
132   host_->Show();
133 }
134
135 void ShellPlatformDataAura::ResizeWindow(const gfx::Size& size) {
136   host_->SetBounds(gfx::Rect(size));
137 }
138
139 }  // namespace content