Fork the mojo shell interfaces used by Mandoline.
[chromium-blink-merge.git] / mandoline / ui / omnibox / omnibox_impl.cc
blob2cf054b87483e96991ac1186fab8d517327f19ff
1 // Copyright 2015 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 "mandoline/ui/omnibox/omnibox_impl.h"
7 #include "base/strings/string16.h"
8 #include "components/view_manager/public/cpp/view_manager_client_factory.h"
9 #include "components/window_manager/public/interfaces/window_manager.mojom.h"
10 #include "mandoline/ui/aura/aura_init.h"
11 #include "mandoline/ui/aura/native_widget_view_manager.h"
12 #include "mojo/application/public/cpp/application_impl.h"
13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/converters/geometry/geometry_type_converters.h"
15 #include "ui/views/background.h"
16 #include "ui/views/controls/textfield/textfield.h"
17 #include "ui/views/widget/widget_delegate.h"
19 namespace mandoline {
21 ////////////////////////////////////////////////////////////////////////////////
22 // OmniboxImpl, public:
24 OmniboxImpl::OmniboxImpl()
25 : app_impl_(nullptr),
26 edit_(nullptr) {
28 OmniboxImpl::~OmniboxImpl() {}
30 ////////////////////////////////////////////////////////////////////////////////
31 // OmniboxImpl, mojo::ApplicationDelegate implementation:
33 void OmniboxImpl::Initialize(mojo::ApplicationImpl* app) {
34 app_impl_ = app;
35 view_manager_client_factory_.reset(
36 new mojo::ViewManagerClientFactory(app->shell(), this));
39 bool OmniboxImpl::ConfigureIncomingConnection(
40 mojo::ApplicationConnection* connection) {
41 connection->AddService<Omnibox>(this);
42 connection->AddService(view_manager_client_factory_.get());
43 return true;
46 bool OmniboxImpl::ConfigureOutgoingConnection(
47 mojo::ApplicationConnection* connection) {
48 return true;
51 ////////////////////////////////////////////////////////////////////////////////
52 // OmniboxImpl, mojo::ViewManagerDelegate implementation:
54 void OmniboxImpl::OnEmbed(mojo::View* root,
55 mojo::InterfaceRequest<mojo::ServiceProvider> services,
56 mojo::ServiceProviderPtr exposed_services) {
57 if (!aura_init_.get()) {
58 aura_init_.reset(new AuraInit);
59 edit_ = new views::Textfield;
60 edit_->set_controller(this);
63 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
64 widget_delegate->GetContentsView()->set_background(
65 views::Background::CreateSolidBackground(0xFFDDDDDD));
66 widget_delegate->GetContentsView()->AddChildView(edit_);
67 widget_delegate->GetContentsView()->SetLayoutManager(this);
69 // TODO(beng): we may be leaking these on subsequent calls to OnEmbed()...
70 // probably should only allow once instance per view.
71 views::Widget* widget = new views::Widget;
72 views::Widget::InitParams params(
73 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
74 params.native_widget =
75 new NativeWidgetViewManager(widget, app_impl_->shell(), root);
76 params.delegate = widget_delegate;
77 params.bounds = root->bounds().To<gfx::Rect>();
78 widget->Init(params);
79 widget->Show();
80 root->SetFocus();
81 edit_->SetText(url_.To<base::string16>());
82 edit_->SelectAll(false);
83 edit_->RequestFocus();
86 void OmniboxImpl::OnViewManagerDisconnected(mojo::ViewManager* view_manager) {}
88 ////////////////////////////////////////////////////////////////////////////////
89 // OmniboxImpl, views::LayoutManager implementation:
91 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const {
92 return gfx::Size();
95 void OmniboxImpl::Layout(views::View* host) {
96 gfx::Rect edit_bounds = host->bounds();
97 edit_bounds.Inset(10, 10, 10, host->bounds().height() - 40);
98 edit_->SetBoundsRect(edit_bounds);
100 // TODO(beng): layout dropdown...
103 ////////////////////////////////////////////////////////////////////////////////
104 // OmniboxImpl, views::TextfieldController implementation:
106 bool OmniboxImpl::HandleKeyEvent(views::Textfield* sender,
107 const ui::KeyEvent& key_event) {
108 if (key_event.key_code() == ui::VKEY_RETURN) {
109 // TODO(beng): call back to browser.
110 client_->OpenURL(mojo::String::From<base::string16>(sender->text()));
111 return true;
113 return false;
116 ////////////////////////////////////////////////////////////////////////////////
117 // OmniboxImpl, mojo::InterfaceFactory<Omnibox> implementation:
119 void OmniboxImpl::Create(mojo::ApplicationConnection* connection,
120 mojo::InterfaceRequest<Omnibox> request) {
121 bindings_.AddBinding(this, request.Pass());
124 ////////////////////////////////////////////////////////////////////////////////
125 // OmniboxImpl, Omnibox implementation:
127 void OmniboxImpl::SetClient(OmniboxClientPtr client) {
128 client_ = client.Pass();
131 void OmniboxImpl::ShowForURL(const mojo::String& url) {
132 url_ = url;
133 mojo::WindowManagerPtr window_manager;
134 app_impl_->ConnectToService("mojo:window_manager", &window_manager);
135 window_manager->Embed("mojo:omnibox", nullptr, nullptr);
138 } // namespace mandoline