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/basictypes.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "mojo/examples/keyboard/keyboard.mojom.h"
9 #include "mojo/examples/keyboard/keyboard_delegate.h"
10 #include "mojo/examples/keyboard/keyboard_view.h"
11 #include "mojo/public/cpp/application/application_connection.h"
12 #include "mojo/public/cpp/application/application_delegate.h"
13 #include "mojo/public/cpp/application/interface_factory_impl.h"
14 #include "mojo/services/public/cpp/view_manager/view.h"
15 #include "mojo/services/public/cpp/view_manager/view_manager.h"
16 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h"
17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h"
18 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h"
19 #include "mojo/views/native_widget_view_manager.h"
20 #include "mojo/views/views_init.h"
21 #include "ui/events/event.h"
22 #include "ui/views/layout/fill_layout.h"
23 #include "ui/views/widget/widget.h"
24 #include "ui/views/widget/widget_delegate.h"
32 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> {
34 explicit KeyboardServiceImpl(Keyboard* keyboard);
35 virtual ~KeyboardServiceImpl() {}
38 virtual void SetTarget(uint32_t view_id) OVERRIDE;
43 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl);
46 class Keyboard : public ApplicationDelegate,
47 public ViewManagerDelegate,
48 public KeyboardDelegate {
51 : keyboard_service_factory_(this),
53 view_manager_client_factory_(this),
54 keyboard_service_(NULL),
60 void set_target(Id id) { target_ = id; }
62 void set_keyboard_service(KeyboardServiceImpl* keyboard) {
63 keyboard_service_ = keyboard;
67 // Overridden from ApplicationDelegate:
68 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection)
70 views_init_.reset(new ViewsInit);
71 connection->AddService(&view_manager_client_factory_);
72 connection->AddService(&keyboard_service_factory_);
76 void CreateWidget(View* view) {
77 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
78 widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this));
79 widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout);
81 views::Widget* widget = new views::Widget;
82 views::Widget::InitParams params(
83 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
84 params.native_widget = new NativeWidgetViewManager(widget, view);
85 params.delegate = widget_delegate;
86 params.bounds = gfx::Rect(view->bounds().width(), view->bounds().height());
91 // ViewManagerDelegate:
92 virtual void OnEmbed(ViewManager* view_manager,
94 ServiceProviderImpl* exported_services,
95 scoped_ptr<ServiceProvider> imported_services) OVERRIDE {
96 // TODO: deal with OnEmbed() being invoked multiple times.
97 view_manager_ = view_manager;
100 virtual void OnViewManagerDisconnected(
101 ViewManager* view_manager) OVERRIDE {
102 DCHECK_EQ(view_manager_, view_manager);
103 view_manager_ = NULL;
104 base::MessageLoop::current()->Quit();
108 virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE {
111 keyboard_service_->client()->OnKeyboardEvent(target_, key_code,
115 InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard>
116 keyboard_service_factory_;
118 scoped_ptr<ViewsInit> views_init_;
120 ViewManager* view_manager_;
121 ViewManagerClientFactory view_manager_client_factory_;
123 KeyboardServiceImpl* keyboard_service_;
127 DISALLOW_COPY_AND_ASSIGN(Keyboard);
130 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard)
131 : keyboard_(keyboard) {
132 keyboard_->set_keyboard_service(this);
135 void KeyboardServiceImpl::SetTarget(uint32_t view_id) {
136 keyboard_->set_target(view_id);
139 } // namespace examples
142 ApplicationDelegate* ApplicationDelegate::Create() {
143 return new examples::Keyboard;