Chromium Code Reviews| 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 views::Widget* widget = new views::Widget; | |
| 70 views::Widget::InitParams params( | |
| 71 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
| 72 params.native_widget = | |
| 73 new NativeWidgetViewManager(widget, app_impl_->shell(), root); | |
| 74 params.delegate = widget_delegate; | |
| 75 params.bounds = root->bounds().To<gfx::Rect>(); | |
| 76 widget->Init(params); | |
| 77 widget->Show(); | |
| 78 root->SetFocus(); | |
| 79 edit_->SetText(url_.To<base::string16>()); | |
| 80 edit_->SelectAll(false); | |
| 81 edit_->RequestFocus(); | |
| 82 } | |
| 83 | |
| 84 void OmniboxImpl::OnViewManagerDisconnected(mojo::ViewManager* view_manager) {} | |
| 85 | |
| 86 //////////////////////////////////////////////////////////////////////////////// | |
| 87 // OmniboxImpl, views::LayoutManager implementation: | |
| 88 | |
| 89 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const { | |
| 90 return gfx::Size(); | |
| 91 } | |
| 92 | |
| 93 void OmniboxImpl::Layout(views::View* host) { | |
| 94 gfx::Rect edit_bounds = host->bounds(); | |
| 95 edit_bounds.Inset(10, 10, 10, host->bounds().height() - 40); | |
| 96 edit_->SetBoundsRect(edit_bounds); | |
| 97 | |
| 98 // TODO(beng): layout dropdown... | |
| 99 } | |
| 100 | |
| 101 //////////////////////////////////////////////////////////////////////////////// | |
| 102 // OmniboxImpl, views::TextfieldController implementation: | |
| 103 | |
| 104 bool OmniboxImpl::HandleKeyEvent(views::Textfield* sender, | |
| 105 const ui::KeyEvent& key_event) { | |
| 106 if (key_event.key_code() == ui::VKEY_RETURN) { | |
| 107 // TODO(beng): call back to browser. | |
| 108 client_->OpenURL( mojo::String::From<base::string16>(sender->text())); | |
|
sky
2015/05/12 21:49:54
nit: '( m' -> '(m'
| |
| 109 return true; | |
| 110 } | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 //////////////////////////////////////////////////////////////////////////////// | |
| 115 // OmniboxImpl, mojo::InterfaceFactory<Omnibox> implementation: | |
| 116 | |
| 117 void OmniboxImpl::Create(mojo::ApplicationConnection* connection, | |
| 118 mojo::InterfaceRequest<Omnibox> request) { | |
| 119 bindings_.AddBinding(this, request.Pass()); | |
| 120 } | |
| 121 | |
| 122 //////////////////////////////////////////////////////////////////////////////// | |
| 123 // OmniboxImpl, Omnibox implementation: | |
| 124 | |
| 125 void OmniboxImpl::SetClient(OmniboxClientPtr client) { | |
| 126 client_ = client.Pass(); | |
| 127 } | |
| 128 | |
| 129 void OmniboxImpl::ShowForURL(const mojo::String& url) { | |
| 130 url_ = url; | |
|
sky
2015/05/12 21:49:54
Don't you want to push url_ to the edit_?
Ben Goodger (Google)
2015/05/12 23:03:55
I do this in OnEmbed() because the textfield doesn
| |
| 131 mojo::WindowManagerPtr window_manager; | |
| 132 app_impl_->ConnectToService("mojo:window_manager", &window_manager); | |
| 133 window_manager->Embed("mojo:omnibox", nullptr, nullptr); | |
| 134 } | |
| 135 | |
| 136 } // namespace mandoline | |
| OLD | NEW |