Merge pull request #2531 from abarth/mozart_keyboard

Wire up raw keyboard events in Mozart
This commit is contained in:
Adam Barth 2016-03-18 14:18:02 -07:00
commit fff30cbefa
3 changed files with 80 additions and 2 deletions

View File

@ -43,6 +43,7 @@ mojo_native_application("mojo") {
"//sky/engine/public/sky",
"//sky/engine/tonic",
"//sky/engine/web",
"//sky/services/raw_keyboard:interfaces",
"//sky/shell:common",
"//sky/shell:gpu_mojo",
"//third_party/icu",

View File

@ -10,11 +10,44 @@
namespace sky {
namespace shell {
namespace {
sky::InputEventPtr ConvertKeyEvent(mojo::EventPtr event) {
if (!event->key_data)
return nullptr;
sky::InputEventPtr result = sky::InputEvent::New();
result->time_stamp = event->time_stamp;
switch (event->action) {
case mojo::EventType::KEY_PRESSED:
result->type = sky::EventType::KEY_PRESSED;
break;
case mojo::EventType::KEY_RELEASED:
result->type = sky::EventType::KEY_RELEASED;
break;
default:
return nullptr;
}
result->key_data = sky::KeyData::New();
result->key_data->key_code = event->key_data->key_code;
if (static_cast<int>(event->flags) & static_cast<int>(mojo::EventFlags::SHIFT_DOWN))
result->key_data->meta_state |= 0x00000001;
if (static_cast<int>(event->flags) & static_cast<int>(mojo::EventFlags::CONTROL_DOWN))
result->key_data->meta_state |= 0x00001000;
if (static_cast<int>(event->flags) & static_cast<int>(mojo::EventFlags::ALT_DOWN))
result->key_data->meta_state |= 0x00000002;
return result.Pass();
}
} // namespace
ViewImpl::ViewImpl(mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner,
ServicesDataPtr services,
const std::string& url)
: binding_(this), url_(url), listener_binding_(this) {
: binding_(this),
url_(url),
listener_binding_(this),
view_services_binding_(this) {
DCHECK(services);
// Once we're done invoking |Shell|, we put it back inside |services| and pass
@ -50,7 +83,11 @@ ViewImpl::ViewImpl(mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner,
shell->CreateApplicationConnector(mojo::GetProxy(&connector));
platform_view()->InitRasterizer(connector.Pass(), scene.Pass());
mojo::ServiceProviderPtr view_services;
view_services_binding_.Bind(mojo::GetProxy(&view_services));
services->shell = shell.Pass();
services->view_services = view_services.Pass();
engine_->SetServices(services.Pass());
}
@ -96,11 +133,33 @@ void ViewImpl::OnEvent(mojo::EventPtr event, const OnEventCallback& callback) {
}
break;
}
case mojo::EventType::KEY_PRESSED:
case mojo::EventType::KEY_RELEASED: {
auto sky_event = ConvertKeyEvent(event.Pass());
for (auto& listener : raw_keyboard_listeners_)
listener->OnKey(sky_event.Clone());
break;
}
default:
break;
}
callback.Run(consumed);
}
void ViewImpl::ConnectToService(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle handle) {
if (service_name == raw_keyboard::RawKeyboardService::Name_) {
raw_keyboard_bindings_.AddBinding(
this,
mojo::MakeRequest<raw_keyboard::RawKeyboardService>(handle.Pass()));
}
}
void ViewImpl::AddListener(
mojo::InterfaceHandle<raw_keyboard::RawKeyboardListener> listener) {
raw_keyboard_listeners_.push_back(
raw_keyboard::RawKeyboardListenerPtr::Create(listener.Pass()));
}
} // namespace shell
} // namespace sky

View File

@ -5,7 +5,10 @@
#ifndef SKY_SHELL_PLATFORM_MOJO_VIEW_IMPL_H_
#define SKY_SHELL_PLATFORM_MOJO_VIEW_IMPL_H_
#include <vector>
#include "base/macros.h"
#include "mojo/common/binding_set.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
#include "mojo/public/interfaces/application/application_connector.mojom.h"
#include "mojo/services/gfx/composition/interfaces/scenes.mojom.h"
@ -13,6 +16,7 @@
#include "mojo/services/ui/views/interfaces/view_manager.mojom.h"
#include "mojo/services/ui/views/interfaces/view_provider.mojom.h"
#include "mojo/services/ui/views/interfaces/views.mojom.h"
#include "sky/services/raw_keyboard/raw_keyboard.mojom.h"
#include "sky/shell/platform/mojo/platform_view_mojo.h"
#include "sky/shell/platform/mojo/pointer_converter_mojo.h"
#include "sky/shell/shell_view.h"
@ -21,7 +25,9 @@ namespace sky {
namespace shell {
class ViewImpl : public mojo::ui::ViewListener,
public mojo::ui::InputListener {
public mojo::ui::InputListener,
public mojo::ServiceProvider,
public raw_keyboard::RawKeyboardService {
public:
ViewImpl(mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner,
ServicesDataPtr services,
@ -41,6 +47,15 @@ class ViewImpl : public mojo::ui::ViewListener,
// mojo::ui::InputListener
void OnEvent(mojo::EventPtr event, const OnEventCallback& callback) override;
// mojo::ServiceProvider
void ConnectToService(const mojo::String& service_name,
mojo::ScopedMessagePipeHandle client_handle) override;
// raw_keyboard::RawKeyboardService
void AddListener(
mojo::InterfaceHandle<raw_keyboard::RawKeyboardListener> listener)
override;
PlatformViewMojo* platform_view() {
return static_cast<PlatformViewMojo*>(shell_view_->view());
}
@ -51,6 +66,9 @@ class ViewImpl : public mojo::ui::ViewListener,
mojo::ServiceProviderPtr view_service_provider_;
mojo::ui::InputConnectionPtr input_connection_;
mojo::Binding<mojo::ui::InputListener> listener_binding_;
mojo::Binding<mojo::ServiceProvider> view_services_binding_;
mojo::BindingSet<raw_keyboard::RawKeyboardService> raw_keyboard_bindings_;
std::vector<raw_keyboard::RawKeyboardListenerPtr> raw_keyboard_listeners_;
std::unique_ptr<ShellView> shell_view_;
SkyEnginePtr engine_;