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 "mandoline/ui/aura/aura_init.h"
10 #include "mandoline/ui/aura/native_widget_view_manager.h"
11 #include "mojo/application/public/cpp/application_impl.h"
12 #include "mojo/common/common_type_converters.h"
13 #include "mojo/converters/geometry/geometry_type_converters.h"
14 #include "ui/views/background.h"
15 #include "ui/views/controls/textfield/textfield.h"
16 #include "ui/views/widget/widget_delegate.h"
20 ////////////////////////////////////////////////////////////////////////////////
21 // OmniboxImpl, public:
23 OmniboxImpl::OmniboxImpl()
27 OmniboxImpl::~OmniboxImpl() {}
29 ////////////////////////////////////////////////////////////////////////////////
30 // OmniboxImpl, mojo::ApplicationDelegate implementation:
32 void OmniboxImpl::Initialize(mojo::ApplicationImpl
* app
) {
34 view_manager_client_factory_
.reset(
35 new mojo::ViewManagerClientFactory(app
->shell(), this));
38 bool OmniboxImpl::ConfigureIncomingConnection(
39 mojo::ApplicationConnection
* connection
) {
40 connection
->AddService
<Omnibox
>(this);
41 connection
->AddService(view_manager_client_factory_
.get());
42 connection
->ConnectToService(&view_embedder_
);
46 bool OmniboxImpl::ConfigureOutgoingConnection(
47 mojo::ApplicationConnection
* connection
) {
51 ////////////////////////////////////////////////////////////////////////////////
52 // OmniboxImpl, mojo::ViewManagerDelegate implementation:
54 void OmniboxImpl::OnEmbed(mojo::View
* root
) {
55 if (!aura_init_
.get()) {
56 aura_init_
.reset(new AuraInit(root
, app_impl_
->shell()));
57 edit_
= new views::Textfield
;
58 edit_
->set_controller(this);
61 const int kOpacity
= 0xC0;
62 views::WidgetDelegateView
* widget_delegate
= new views::WidgetDelegateView
;
63 widget_delegate
->GetContentsView()->set_background(
64 views::Background::CreateSolidBackground(
65 SkColorSetA(0xDDDDDD, kOpacity
)));
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 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
81 widget
->GetCompositor()->SetBackgroundColor(
82 SkColorSetA(SK_ColorBLACK
, kOpacity
));
84 edit_
->SetText(url_
.To
<base::string16
>());
85 edit_
->SelectAll(false);
86 edit_
->RequestFocus();
89 void OmniboxImpl::OnViewManagerDestroyed(mojo::ViewManager
* view_manager
) {
92 ////////////////////////////////////////////////////////////////////////////////
93 // OmniboxImpl, views::LayoutManager implementation:
95 gfx::Size
OmniboxImpl::GetPreferredSize(const views::View
* view
) const {
99 void OmniboxImpl::Layout(views::View
* host
) {
100 gfx::Rect edit_bounds
= host
->bounds();
101 edit_bounds
.Inset(10, 10, 10, host
->bounds().height() - 40);
102 edit_
->SetBoundsRect(edit_bounds
);
104 // TODO(beng): layout dropdown...
107 ////////////////////////////////////////////////////////////////////////////////
108 // OmniboxImpl, views::TextfieldController implementation:
110 bool OmniboxImpl::HandleKeyEvent(views::Textfield
* sender
,
111 const ui::KeyEvent
& key_event
) {
112 if (key_event
.key_code() == ui::VKEY_RETURN
) {
113 // TODO(beng): call back to browser.
114 client_
->OpenURL(mojo::String::From
<base::string16
>(sender
->text()));
120 ////////////////////////////////////////////////////////////////////////////////
121 // OmniboxImpl, mojo::InterfaceFactory<Omnibox> implementation:
123 void OmniboxImpl::Create(mojo::ApplicationConnection
* connection
,
124 mojo::InterfaceRequest
<Omnibox
> request
) {
125 bindings_
.AddBinding(this, request
.Pass());
128 ////////////////////////////////////////////////////////////////////////////////
129 // OmniboxImpl, Omnibox implementation:
131 void OmniboxImpl::SetClient(OmniboxClientPtr client
) {
132 client_
= client
.Pass();
135 void OmniboxImpl::ShowForURL(const mojo::String
& url
) {
137 mojo::URLRequestPtr
request(mojo::URLRequest::New());
138 request
->url
= mojo::String::From("mojo:omnibox");
139 view_embedder_
->Embed(request
.Pass());
142 } // namespace mandoline