OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/strings/string_util.h" | 6 #include "base/strings/string_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "mojo/examples/keyboard/keyboard.mojom.h" | 8 #include "mojo/examples/keyboard/keyboard.mojom.h" |
9 #include "mojo/examples/keyboard/keyboard_delegate.h" | 9 #include "mojo/examples/keyboard/keyboard_delegate.h" |
10 #include "mojo/examples/keyboard/keyboard_view.h" | 10 #include "mojo/examples/keyboard/keyboard_view.h" |
11 #include "mojo/public/cpp/application/application_connection.h" | 11 #include "mojo/public/cpp/application/application_connection.h" |
12 #include "mojo/public/cpp/application/application_delegate.h" | 12 #include "mojo/public/cpp/application/application_delegate.h" |
13 #include "mojo/public/cpp/application/interface_factory_with_context.h" | 13 #include "mojo/public/cpp/application/interface_factory_with_context.h" |
14 #include "mojo/services/public/cpp/view_manager/node.h" | 14 #include "mojo/services/public/cpp/view_manager/node.h" |
15 #include "mojo/services/public/cpp/view_manager/view.h" | 15 #include "mojo/services/public/cpp/view_manager/view.h" |
16 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 16 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_client_factory.h" |
18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 18 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
19 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 19 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
20 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" | 20 #include "mojo/services/public/interfaces/navigation/navigation.mojom.h" |
21 #include "mojo/views/native_widget_view_manager.h" | 21 #include "mojo/views/native_widget_view_manager.h" |
22 #include "mojo/views/views_init.h" | 22 #include "mojo/views/views_init.h" |
23 #include "ui/events/event.h" | 23 #include "ui/events/event.h" |
24 #include "ui/views/layout/fill_layout.h" | 24 #include "ui/views/layout/fill_layout.h" |
25 #include "ui/views/widget/widget.h" | 25 #include "ui/views/widget/widget.h" |
26 #include "ui/views/widget/widget_delegate.h" | 26 #include "ui/views/widget/widget_delegate.h" |
27 #include "url/gurl.h" | 27 #include "url/gurl.h" |
28 | 28 |
29 using mojo::view_manager::Id; | |
30 | |
31 namespace mojo { | 29 namespace mojo { |
32 namespace examples { | 30 namespace examples { |
33 | 31 |
34 class Keyboard; | 32 class Keyboard; |
35 | 33 |
36 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> { | 34 class KeyboardServiceImpl : public InterfaceImpl<KeyboardService> { |
37 public: | 35 public: |
38 explicit KeyboardServiceImpl(Keyboard* keyboard); | 36 explicit KeyboardServiceImpl(Keyboard* keyboard); |
39 virtual ~KeyboardServiceImpl() {} | 37 virtual ~KeyboardServiceImpl() {} |
40 | 38 |
41 // KeyboardService: | 39 // KeyboardService: |
42 virtual void SetTarget(uint32_t node_id) OVERRIDE; | 40 virtual void SetTarget(uint32_t node_id) OVERRIDE; |
43 | 41 |
44 private: | 42 private: |
45 Keyboard* keyboard_; | 43 Keyboard* keyboard_; |
46 | 44 |
47 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl); | 45 DISALLOW_COPY_AND_ASSIGN(KeyboardServiceImpl); |
48 }; | 46 }; |
49 | 47 |
50 class Keyboard | 48 class Keyboard |
51 : public ApplicationDelegate, | 49 : public ApplicationDelegate, |
52 public view_manager::ViewManagerDelegate, | 50 public ViewManagerDelegate, |
53 public KeyboardDelegate, | 51 public KeyboardDelegate, |
54 public InterfaceFactoryWithContext<KeyboardServiceImpl, Keyboard> { | 52 public InterfaceFactoryWithContext<KeyboardServiceImpl, Keyboard> { |
55 public: | 53 public: |
56 Keyboard() | 54 Keyboard() |
57 : InterfaceFactoryWithContext(this), | 55 : InterfaceFactoryWithContext(this), |
58 view_manager_(NULL), | 56 view_manager_(NULL), |
59 view_manager_client_factory_(this), | 57 view_manager_client_factory_(this), |
60 keyboard_service_(NULL), | 58 keyboard_service_(NULL), |
61 target_(0) {} | 59 target_(0) {} |
62 | 60 |
63 virtual ~Keyboard() { | 61 virtual ~Keyboard() { |
64 } | 62 } |
65 | 63 |
66 void set_target(Id id) { target_ = id; } | 64 void set_target(Id id) { target_ = id; } |
67 | 65 |
68 void set_keyboard_service(KeyboardServiceImpl* keyboard) { | 66 void set_keyboard_service(KeyboardServiceImpl* keyboard) { |
69 keyboard_service_ = keyboard; | 67 keyboard_service_ = keyboard; |
70 } | 68 } |
71 | 69 |
72 private: | 70 private: |
73 // Overridden from ApplicationDelegate: | 71 // Overridden from ApplicationDelegate: |
74 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) | 72 virtual bool ConfigureIncomingConnection(ApplicationConnection* connection) |
75 MOJO_OVERRIDE { | 73 MOJO_OVERRIDE { |
76 views_init_.reset(new ViewsInit); | 74 views_init_.reset(new ViewsInit); |
77 connection->AddService(&view_manager_client_factory_); | 75 connection->AddService(&view_manager_client_factory_); |
78 connection->AddService(this); | 76 connection->AddService(this); |
79 return true; | 77 return true; |
80 } | 78 } |
81 | 79 |
82 void CreateWidget(view_manager::Node* node) { | 80 void CreateWidget(Node* node) { |
83 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | 81 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; |
84 widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this)); | 82 widget_delegate->GetContentsView()->AddChildView(new KeyboardView(this)); |
85 widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout); | 83 widget_delegate->GetContentsView()->SetLayoutManager(new views::FillLayout); |
86 | 84 |
87 views::Widget* widget = new views::Widget; | 85 views::Widget* widget = new views::Widget; |
88 views::Widget::InitParams params( | 86 views::Widget::InitParams params( |
89 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | 87 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); |
90 params.native_widget = new NativeWidgetViewManager(widget, node); | 88 params.native_widget = new NativeWidgetViewManager(widget, node); |
91 params.delegate = widget_delegate; | 89 params.delegate = widget_delegate; |
92 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); | 90 params.bounds = gfx::Rect(node->bounds().width(), node->bounds().height()); |
93 widget->Init(params); | 91 widget->Init(params); |
94 widget->Show(); | 92 widget->Show(); |
95 } | 93 } |
96 | 94 |
97 // view_manager::ViewManagerDelegate: | 95 // ViewManagerDelegate: |
98 virtual void OnRootAdded(view_manager::ViewManager* view_manager, | 96 virtual void OnRootAdded(ViewManager* view_manager, Node* root) OVERRIDE { |
99 view_manager::Node* root) OVERRIDE { | |
100 // TODO: deal with OnRootAdded() being invoked multiple times. | 97 // TODO: deal with OnRootAdded() being invoked multiple times. |
101 view_manager_ = view_manager; | 98 view_manager_ = view_manager; |
102 root->SetActiveView(view_manager::View::Create(view_manager)); | 99 root->SetActiveView(View::Create(view_manager)); |
103 CreateWidget(root); | 100 CreateWidget(root); |
104 } | 101 } |
105 virtual void OnViewManagerDisconnected( | 102 virtual void OnViewManagerDisconnected( |
106 view_manager::ViewManager* view_manager) OVERRIDE { | 103 ViewManager* view_manager) OVERRIDE { |
107 DCHECK_EQ(view_manager_, view_manager); | 104 DCHECK_EQ(view_manager_, view_manager); |
108 view_manager_ = NULL; | 105 view_manager_ = NULL; |
109 base::MessageLoop::current()->Quit(); | 106 base::MessageLoop::current()->Quit(); |
110 } | 107 } |
111 | 108 |
112 // KeyboardDelegate: | 109 // KeyboardDelegate: |
113 virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE { | 110 virtual void OnKeyPressed(int key_code, int event_flags) OVERRIDE { |
114 if (!target_) | 111 if (!target_) |
115 return; | 112 return; |
116 keyboard_service_->client()->OnKeyboardEvent(target_, key_code, | 113 keyboard_service_->client()->OnKeyboardEvent(target_, key_code, |
117 event_flags); | 114 event_flags); |
118 } | 115 } |
119 | 116 |
120 scoped_ptr<ViewsInit> views_init_; | 117 scoped_ptr<ViewsInit> views_init_; |
121 | 118 |
122 view_manager::ViewManager* view_manager_; | 119 ViewManager* view_manager_; |
123 view_manager::ViewManagerClientFactory view_manager_client_factory_; | 120 ViewManagerClientFactory view_manager_client_factory_; |
124 | 121 |
125 KeyboardServiceImpl* keyboard_service_; | 122 KeyboardServiceImpl* keyboard_service_; |
126 | 123 |
127 Id target_; | 124 Id target_; |
128 | 125 |
129 DISALLOW_COPY_AND_ASSIGN(Keyboard); | 126 DISALLOW_COPY_AND_ASSIGN(Keyboard); |
130 }; | 127 }; |
131 | 128 |
132 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard) | 129 KeyboardServiceImpl::KeyboardServiceImpl(Keyboard* keyboard) |
133 : keyboard_(keyboard) { | 130 : keyboard_(keyboard) { |
134 keyboard_->set_keyboard_service(this); | 131 keyboard_->set_keyboard_service(this); |
135 } | 132 } |
136 | 133 |
137 void KeyboardServiceImpl::SetTarget(uint32_t node_id) { | 134 void KeyboardServiceImpl::SetTarget(uint32_t node_id) { |
138 keyboard_->set_target(node_id); | 135 keyboard_->set_target(node_id); |
139 } | 136 } |
140 | 137 |
141 } // namespace examples | 138 } // namespace examples |
142 | 139 |
143 // static | 140 // static |
144 ApplicationDelegate* ApplicationDelegate::Create() { | 141 ApplicationDelegate* ApplicationDelegate::Create() { |
145 return new examples::Keyboard; | 142 return new examples::Keyboard; |
146 } | 143 } |
147 | 144 |
148 } // namespace mojo | 145 } // namespace mojo |
OLD | NEW |