OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mandoline/ui/omnibox/omnibox_impl.h" |
| 6 |
| 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/common/common_type_converters.h" |
| 13 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 14 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h" |
| 15 #include "ui/views/background.h" |
| 16 #include "ui/views/controls/textfield/textfield.h" |
| 17 #include "ui/views/widget/widget_delegate.h" |
| 18 |
| 19 namespace mandoline { |
| 20 |
| 21 //////////////////////////////////////////////////////////////////////////////// |
| 22 // OmniboxImpl, public: |
| 23 |
| 24 OmniboxImpl::OmniboxImpl() |
| 25 : app_impl_(nullptr), |
| 26 edit_(nullptr) { |
| 27 } |
| 28 OmniboxImpl::~OmniboxImpl() {} |
| 29 |
| 30 //////////////////////////////////////////////////////////////////////////////// |
| 31 // OmniboxImpl, mojo::ApplicationDelegate implementation: |
| 32 |
| 33 void OmniboxImpl::Initialize(mojo::ApplicationImpl* app) { |
| 34 app_impl_ = app; |
| 35 view_manager_client_factory_.reset( |
| 36 new mojo::ViewManagerClientFactory(app->shell(), this)); |
| 37 } |
| 38 |
| 39 bool OmniboxImpl::ConfigureIncomingConnection( |
| 40 mojo::ApplicationConnection* connection) { |
| 41 connection->AddService<Omnibox>(this); |
| 42 connection->AddService(view_manager_client_factory_.get()); |
| 43 return true; |
| 44 } |
| 45 |
| 46 bool OmniboxImpl::ConfigureOutgoingConnection( |
| 47 mojo::ApplicationConnection* connection) { |
| 48 return true; |
| 49 } |
| 50 |
| 51 //////////////////////////////////////////////////////////////////////////////// |
| 52 // OmniboxImpl, mojo::ViewManagerDelegate implementation: |
| 53 |
| 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); |
| 61 } |
| 62 |
| 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); |
| 68 |
| 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(); |
| 84 } |
| 85 |
| 86 void OmniboxImpl::OnViewManagerDisconnected(mojo::ViewManager* view_manager) {} |
| 87 |
| 88 //////////////////////////////////////////////////////////////////////////////// |
| 89 // OmniboxImpl, views::LayoutManager implementation: |
| 90 |
| 91 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const { |
| 92 return gfx::Size(); |
| 93 } |
| 94 |
| 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); |
| 99 |
| 100 // TODO(beng): layout dropdown... |
| 101 } |
| 102 |
| 103 //////////////////////////////////////////////////////////////////////////////// |
| 104 // OmniboxImpl, views::TextfieldController implementation: |
| 105 |
| 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; |
| 112 } |
| 113 return false; |
| 114 } |
| 115 |
| 116 //////////////////////////////////////////////////////////////////////////////// |
| 117 // OmniboxImpl, mojo::InterfaceFactory<Omnibox> implementation: |
| 118 |
| 119 void OmniboxImpl::Create(mojo::ApplicationConnection* connection, |
| 120 mojo::InterfaceRequest<Omnibox> request) { |
| 121 bindings_.AddBinding(this, request.Pass()); |
| 122 } |
| 123 |
| 124 //////////////////////////////////////////////////////////////////////////////// |
| 125 // OmniboxImpl, Omnibox implementation: |
| 126 |
| 127 void OmniboxImpl::SetClient(OmniboxClientPtr client) { |
| 128 client_ = client.Pass(); |
| 129 } |
| 130 |
| 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); |
| 136 } |
| 137 |
| 138 } // namespace mandoline |
OLD | NEW |