mojo: Fix map of booleans for python bindings.
[chromium-blink-merge.git] / mojo / examples / keyboard / keyboard.cc
blob3cd4a4ea71aeaea5c1a399a97408c342ec33e5f4
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.
5 #include "base/macros.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/application/application_runner_chromium.h"
9 #include "mojo/examples/keyboard/keyboard.mojom.h"
10 #include "mojo/examples/keyboard/keyboard_delegate.h"
11 #include "mojo/examples/keyboard/keyboard_view.h"
12 #include "mojo/public/c/system/main.h"
13 #include "mojo/public/cpp/application/application_connection.h"
14 #include "mojo/public/cpp/application/application_delegate.h"
15 #include "mojo/public/cpp/application/application_impl.h"
16 #include "mojo/public/cpp/application/interface_factory_impl.h"
17 #include "mojo/services/public/cpp/view_manager/view.h"
18 #include "mojo/services/public/cpp/view_manager/view_manager.h"
19 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
20 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
21 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
22 #include "mojo/views/native_widget_view_manager.h"
23 #include "mojo/views/views_init.h"
24 #include "ui/events/event.h"
25 #include "ui/views/layout/fill_layout.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/widget/widget_delegate.h"
28 #include "url/gurl.h"
30 namespace mojo {
31 namespace examples {
33 class Keyboard;
35 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> {
36 public:
37 explicit KeyboardServiceImpl(Keyboard* keyboard);
38 virtual ~KeyboardServiceImpl() {}
40 // KeyboardService:
41 virtual void SetTarget(uint32_t view_id) override;
43 private:
44 Keyboard* keyboard_;
46 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl);
49 class Keyboard : public ApplicationDelegate,
50 public ViewManagerDelegate,
51 public KeyboardDelegate {
52 public:
53 Keyboard()
54 : shell_(nullptr),
55 keyboard_service_factory_(this),
56 view_manager_(NULL),
57 keyboard_service_(NULL),
58 target_(0) {}
60 virtual ~Keyboard() {
63 void set_target(Id id) { target_ = id; }
65 void set_keyboard_service(KeyboardServiceImpl* keyboard) {
66 keyboard_service_ = keyboard;
69 private:
70 // Overridden from ApplicationDelegate:
71 virtual void Initialize(ApplicationImpl* app) override {
72 shell_ = app->shell();
73 view_manager_client_factory_.reset(
74 new ViewManagerClientFactory(shell_, this));
77 virtual bool ConfigureIncomingConnection(
78 ApplicationConnection* connection) override {
79 views_init_.reset(new ViewsInit);
80 connection->AddService(view_manager_client_factory_.get());
81 connection->AddService(&keyboard_service_factory_);
82 return true;
85 void CreateWidget(View* view) {
86 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
87 widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this));
88 widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout);
90 views::Widget* widget = new views::Widget;
91 views::Widget::InitParams params(
92 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
93 params.native_widget = new NativeWidgetViewManager(widget, shell_, view);
94 params.delegate = widget_delegate;
95 params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height());
96 widget->Init(params);
97 widget->Show();
100 // ViewManagerDelegate:
101 virtual void OnEmbed(ViewManager* view_manager,
102 View* root,
103 ServiceProviderImpl* exported_services,
104 scoped_ptr<ServiceProvider> imported_services) override {
105 // TODO: deal with OnEmbed() being invoked multiple times.
106 view_manager_ = view_manager;
107 CreateWidget(root);
109 virtual void OnViewManagerDisconnected(
110 ViewManager* view_manager) override {
111 DCHECK_EQ(view_manager_, view_manager);
112 view_manager_ = NULL;
113 base::MessageLoop::current()->Quit();
116 // KeyboardDelegate:
117 virtual void OnKeyPressed(int key_code, int event_flags) override {
118 if (!target_)
119 return;
120 keyboard_service_->client()->OnKeyboardEvent(target_, key_code,
121 event_flags);
124 Shell* shell_;
126 InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard>
127 keyboard_service_factory_;
129 scoped_ptr<ViewsInit> views_init_;
131 ViewManager* view_manager_;
132 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_;
134 KeyboardServiceImpl* keyboard_service_;
136 Id target_;
138 DISALLOW_COPY_AND_ASSIGN(Keyboard);
141 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard)
142 : keyboard_(keyboard) {
143 keyboard_->set_keyboard_service(this);
146 void KeyboardServiceImpl::SetTarget(uint32_t view_id) {
147 keyboard_->set_target(view_id);
150 } // namespace examples
151 } // namespace mojo
153 MojoResult MojoMain(MojoHandle shell_handle) {
154 mojo::ApplicationRunnerChromium runner(new mojo::examples::Keyboard);
155 return runner.Run(shell_handle);