Roll harfbuzz-ng to 1.0.2
[chromium-blink-merge.git] / mandoline / ui / omnibox / omnibox_impl.cc
bloba54eea8e1891393853a53adbbbd5cd104a7a83d1
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"
18 namespace mandoline {
20 ////////////////////////////////////////////////////////////////////////////////
21 // OmniboxImpl, public:
23 OmniboxImpl::OmniboxImpl()
24 : app_impl_(nullptr), root_(nullptr), edit_(nullptr) {}
25 OmniboxImpl::~OmniboxImpl() {}
27 ////////////////////////////////////////////////////////////////////////////////
28 // OmniboxImpl, mojo::ApplicationDelegate implementation:
30 void OmniboxImpl::Initialize(mojo::ApplicationImpl* app) {
31 app_impl_ = app;
32 view_manager_client_factory_.reset(
33 new mojo::ViewManagerClientFactory(app->shell(), this));
36 bool OmniboxImpl::ConfigureIncomingConnection(
37 mojo::ApplicationConnection* connection) {
38 connection->AddService<Omnibox>(this);
39 connection->AddService(view_manager_client_factory_.get());
40 connection->ConnectToService(&view_embedder_);
41 return true;
44 bool OmniboxImpl::ConfigureOutgoingConnection(
45 mojo::ApplicationConnection* connection) {
46 return true;
49 ////////////////////////////////////////////////////////////////////////////////
50 // OmniboxImpl, mojo::ViewManagerDelegate implementation:
52 void OmniboxImpl::OnEmbed(mojo::View* root) {
53 root_ = 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);
59 edit_->SetTextInputType(ui::TEXT_INPUT_TYPE_URL);
62 const int kOpacity = 0xC0;
63 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
64 widget_delegate->GetContentsView()->set_background(
65 views::Background::CreateSolidBackground(
66 SkColorSetA(0xDDDDDD, kOpacity)));
67 widget_delegate->GetContentsView()->AddChildView(edit_);
68 widget_delegate->GetContentsView()->SetLayoutManager(this);
70 // TODO(beng): we may be leaking these on subsequent calls to OnEmbed()...
71 // probably should only allow once instance per view.
72 views::Widget* widget = new views::Widget;
73 views::Widget::InitParams params(
74 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
75 params.native_widget =
76 new NativeWidgetViewManager(widget, app_impl_->shell(), root);
77 params.delegate = widget_delegate;
78 params.bounds = root->bounds().To<gfx::Rect>();
79 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
80 widget->Init(params);
81 widget->Show();
82 widget->GetCompositor()->SetBackgroundColor(
83 SkColorSetA(SK_ColorBLACK, kOpacity));
84 edit_->SetText(url_.To<base::string16>());
85 edit_->SelectAll(false);
86 edit_->RequestFocus();
88 ShowWindow();
91 void OmniboxImpl::OnViewManagerDestroyed(mojo::ViewManager* view_manager) {
92 root_ = nullptr;
95 ////////////////////////////////////////////////////////////////////////////////
96 // OmniboxImpl, views::LayoutManager implementation:
98 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const {
99 return gfx::Size();
102 void OmniboxImpl::Layout(views::View* host) {
103 gfx::Rect edit_bounds = host->bounds();
104 edit_bounds.Inset(10, 10, 10, host->bounds().height() - 40);
105 edit_->SetBoundsRect(edit_bounds);
107 // TODO(beng): layout dropdown...
110 ////////////////////////////////////////////////////////////////////////////////
111 // OmniboxImpl, views::TextfieldController implementation:
113 bool OmniboxImpl::HandleKeyEvent(views::Textfield* sender,
114 const ui::KeyEvent& key_event) {
115 if (key_event.key_code() == ui::VKEY_RETURN) {
116 // TODO(beng): call back to browser.
117 mojo::URLRequestPtr request(mojo::URLRequest::New());
118 request->url = mojo::String::From<base::string16>(sender->text());
119 view_embedder_->Embed(request.Pass());
120 HideWindow();
121 return true;
123 return false;
126 ////////////////////////////////////////////////////////////////////////////////
127 // OmniboxImpl, mojo::InterfaceFactory<Omnibox> implementation:
129 void OmniboxImpl::Create(mojo::ApplicationConnection* connection,
130 mojo::InterfaceRequest<Omnibox> request) {
131 // TODO(beng): methinks this doesn't work well across multiple browser
132 // windows...
133 bindings_.AddBinding(this, request.Pass());
136 ////////////////////////////////////////////////////////////////////////////////
137 // OmniboxImpl, Omnibox implementation:
139 void OmniboxImpl::ShowForURL(const mojo::String& url) {
140 url_ = url;
141 if (root_) {
142 ShowWindow();
143 } else {
144 mojo::URLRequestPtr request(mojo::URLRequest::New());
145 request->url = mojo::String::From("mojo:omnibox");
146 view_embedder_->Embed(request.Pass());
150 ////////////////////////////////////////////////////////////////////////////////
151 // OmniboxImpl, private:
153 void OmniboxImpl::ShowWindow() {
154 DCHECK(root_);
155 root_->SetVisible(true);
156 root_->SetFocus();
157 root_->MoveToFront();
160 void OmniboxImpl::HideWindow() {
161 DCHECK(root_);
162 root_->SetVisible(false);
165 } // namespace mandoline