OLD | NEW |
| (Empty) |
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 "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" | |
29 | |
30 namespace mojo { | |
31 namespace examples { | |
32 | |
33 class Keyboard; | |
34 | |
35 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> { | |
36 public: | |
37 explicit KeyboardServiceImpl(Keyboard* keyboard); | |
38 virtual ~KeyboardServiceImpl() {} | |
39 | |
40 // KeyboardService: | |
41 virtual void SetTarget(uint32_t view_id) override; | |
42 | |
43 private: | |
44 Keyboard* keyboard_; | |
45 | |
46 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl); | |
47 }; | |
48 | |
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) {} | |
59 | |
60 virtual ~Keyboard() { | |
61 } | |
62 | |
63 void set_target(Id id) { target_ = id; } | |
64 | |
65 void set_keyboard_service(KeyboardServiceImpl* keyboard) { | |
66 keyboard_service_ = keyboard; | |
67 } | |
68 | |
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)); | |
75 } | |
76 | |
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; | |
83 } | |
84 | |
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); | |
89 | |
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(); | |
98 } | |
99 | |
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); | |
108 } | |
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(); | |
114 } | |
115 | |
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); | |
122 } | |
123 | |
124 Shell* shell_; | |
125 | |
126 InterfaceFactoryImplWithContext<KeyboardServiceImpl, Keyboard> | |
127 keyboard_service_factory_; | |
128 | |
129 scoped_ptr<ViewsInit> views_init_; | |
130 | |
131 ViewManager* view_manager_; | |
132 scoped_ptr<ViewManagerClientFactory> view_manager_client_factory_; | |
133 | |
134 KeyboardServiceImpl* keyboard_service_; | |
135 | |
136 Id target_; | |
137 | |
138 DISALLOW_COPY_AND_ASSIGN(Keyboard); | |
139 }; | |
140 | |
141 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard) | |
142 : keyboard_(keyboard) { | |
143 keyboard_->set_keyboard_service(this); | |
144 } | |
145 | |
146 void KeyboardServiceImpl::SetTarget(uint32_t view_id) { | |
147 keyboard_->set_target(view_id); | |
148 } | |
149 | |
150 } // namespace examples | |
151 } // namespace mojo | |
152 | |
153 MojoResult MojoMain(MojoHandle shell_handle) { | |
154 mojo::ApplicationRunnerChromium runner(new mojo::examples::Keyboard); | |
155 return runner.Run(shell_handle); | |
156 } | |
OLD | NEW |