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_application.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/macros.h" | |
10 #include "base/strings/string16.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "components/mus/public/cpp/window.h" | |
13 #include "components/mus/public/cpp/window_tree_connection.h" | |
14 #include "components/mus/public/cpp/window_tree_delegate.h" | |
15 #include "components/url_formatter/url_fixer.h" | |
16 #include "mandoline/ui/desktop_ui/public/interfaces/view_embedder.mojom.h" | |
17 #include "mojo/common/common_type_converters.h" | |
18 #include "mojo/shell/public/cpp/shell.h" | |
19 #include "ui/mojo/init/ui_init.h" | |
20 #include "ui/views/background.h" | |
21 #include "ui/views/controls/textfield/textfield.h" | |
22 #include "ui/views/controls/textfield/textfield_controller.h" | |
23 #include "ui/views/layout/layout_manager.h" | |
24 #include "ui/views/mus/aura_init.h" | |
25 #include "ui/views/mus/display_converter.h" | |
26 #include "ui/views/mus/native_widget_mus.h" | |
27 #include "ui/views/widget/widget_delegate.h" | |
28 | |
29 namespace mandoline { | |
30 | |
31 //////////////////////////////////////////////////////////////////////////////// | |
32 // OmniboxImpl | |
33 | |
34 class OmniboxImpl : public mus::WindowTreeDelegate, | |
35 public views::LayoutManager, | |
36 public views::TextfieldController, | |
37 public Omnibox { | |
38 public: | |
39 OmniboxImpl(mojo::Shell* shell, | |
40 mojo::Connection* connection, | |
41 mojo::InterfaceRequest<Omnibox> request); | |
42 ~OmniboxImpl() override; | |
43 | |
44 private: | |
45 // Overridden from mus::WindowTreeDelegate: | |
46 void OnEmbed(mus::Window* root) override; | |
47 void OnConnectionLost(mus::WindowTreeConnection* connection) override; | |
48 | |
49 // Overridden from views::LayoutManager: | |
50 gfx::Size GetPreferredSize(const views::View* view) const override; | |
51 void Layout(views::View* host) override; | |
52 | |
53 // Overridden from views::TextfieldController: | |
54 bool HandleKeyEvent(views::Textfield* sender, | |
55 const ui::KeyEvent& key_event) override; | |
56 | |
57 // Overridden from Omnibox: | |
58 void GetWindowTreeClient( | |
59 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) override; | |
60 void ShowForURL(const mojo::String& url) override; | |
61 | |
62 void HideWindow(); | |
63 void ShowWindow(); | |
64 | |
65 scoped_ptr<ui::mojo::UIInit> ui_init_; | |
66 scoped_ptr<views::AuraInit> aura_init_; | |
67 mojo::Shell* shell_; | |
68 mus::Window* root_; | |
69 mojo::String url_; | |
70 views::Textfield* edit_; | |
71 mojo::Binding<Omnibox> binding_; | |
72 ViewEmbedderPtr view_embedder_; | |
73 | |
74 DISALLOW_COPY_AND_ASSIGN(OmniboxImpl); | |
75 }; | |
76 | |
77 //////////////////////////////////////////////////////////////////////////////// | |
78 // OmniboxApplication, public: | |
79 | |
80 OmniboxApplication::OmniboxApplication() : shell_(nullptr) {} | |
81 OmniboxApplication::~OmniboxApplication() {} | |
82 | |
83 //////////////////////////////////////////////////////////////////////////////// | |
84 // OmniboxApplication, mojo::ShellClient implementation: | |
85 | |
86 void OmniboxApplication::Initialize(mojo::Shell* shell, const std::string& url, | |
87 uint32_t id) { | |
88 shell_ = shell; | |
89 tracing_.Initialize(shell, url); | |
90 } | |
91 | |
92 bool OmniboxApplication::AcceptConnection(mojo::Connection* connection) { | |
93 connection->AddService<Omnibox>(this); | |
94 return true; | |
95 } | |
96 | |
97 //////////////////////////////////////////////////////////////////////////////// | |
98 // OmniboxApplication, mojo::InterfaceFactory<Omnibox> implementation: | |
99 | |
100 void OmniboxApplication::Create(mojo::Connection* connection, | |
101 mojo::InterfaceRequest<Omnibox> request) { | |
102 new OmniboxImpl(shell_, connection, std::move(request)); | |
103 } | |
104 | |
105 //////////////////////////////////////////////////////////////////////////////// | |
106 // OmniboxImpl, public: | |
107 | |
108 OmniboxImpl::OmniboxImpl(mojo::Shell* shell, | |
109 mojo::Connection* connection, | |
110 mojo::InterfaceRequest<Omnibox> request) | |
111 : shell_(shell), | |
112 root_(nullptr), | |
113 edit_(nullptr), | |
114 binding_(this, std::move(request)) { | |
115 connection->ConnectToService(&view_embedder_); | |
116 } | |
117 OmniboxImpl::~OmniboxImpl() {} | |
118 | |
119 //////////////////////////////////////////////////////////////////////////////// | |
120 // OmniboxImpl, mus::WindowTreeDelegate implementation: | |
121 | |
122 void OmniboxImpl::OnEmbed(mus::Window* root) { | |
123 root_ = root; | |
124 | |
125 if (!aura_init_.get()) { | |
126 ui_init_.reset(new ui::mojo::UIInit(views::GetDisplaysFromWindow(root_))); | |
127 aura_init_.reset(new views::AuraInit(shell_, "mandoline_ui.pak")); | |
128 edit_ = new views::Textfield; | |
129 edit_->set_controller(this); | |
130 edit_->SetTextInputType(ui::TEXT_INPUT_TYPE_URL); | |
131 } | |
132 | |
133 const int kOpacity = 0xC0; | |
134 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; | |
135 widget_delegate->GetContentsView()->set_background( | |
136 views::Background::CreateSolidBackground( | |
137 SkColorSetA(0xDDDDDD, kOpacity))); | |
138 widget_delegate->GetContentsView()->AddChildView(edit_); | |
139 widget_delegate->GetContentsView()->SetLayoutManager(this); | |
140 | |
141 // TODO(beng): we may be leaking these on subsequent calls to OnEmbed()... | |
142 // probably should only allow once instance per view. | |
143 views::Widget* widget = new views::Widget; | |
144 views::Widget::InitParams params( | |
145 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
146 params.native_widget = new views::NativeWidgetMus( | |
147 widget, shell_, root, mus::mojom::SurfaceType::DEFAULT); | |
148 params.delegate = widget_delegate; | |
149 params.bounds = root->bounds(); | |
150 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
151 widget->Init(params); | |
152 widget->Show(); | |
153 widget->GetCompositor()->SetBackgroundColor( | |
154 SkColorSetA(SK_ColorBLACK, kOpacity)); | |
155 | |
156 ShowWindow(); | |
157 } | |
158 | |
159 void OmniboxImpl::OnConnectionLost(mus::WindowTreeConnection* connection) { | |
160 root_ = nullptr; | |
161 } | |
162 | |
163 //////////////////////////////////////////////////////////////////////////////// | |
164 // OmniboxImpl, views::LayoutManager implementation: | |
165 | |
166 gfx::Size OmniboxImpl::GetPreferredSize(const views::View* view) const { | |
167 return gfx::Size(); | |
168 } | |
169 | |
170 void OmniboxImpl::Layout(views::View* host) { | |
171 gfx::Rect edit_bounds = host->bounds(); | |
172 edit_bounds.Inset(10, 10, 10, host->bounds().height() - 40); | |
173 edit_->SetBoundsRect(edit_bounds); | |
174 | |
175 // TODO(beng): layout dropdown... | |
176 } | |
177 | |
178 //////////////////////////////////////////////////////////////////////////////// | |
179 // OmniboxImpl, views::TextfieldController implementation: | |
180 | |
181 bool OmniboxImpl::HandleKeyEvent(views::Textfield* sender, | |
182 const ui::KeyEvent& key_event) { | |
183 if (key_event.key_code() == ui::VKEY_RETURN) { | |
184 // TODO(beng): call back to browser. | |
185 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
186 GURL url = url_formatter::FixupURL(base::UTF16ToUTF8(sender->text()), | |
187 std::string()); | |
188 request->url = url.spec(); | |
189 view_embedder_->Embed(std::move(request)); | |
190 HideWindow(); | |
191 return true; | |
192 } | |
193 return false; | |
194 } | |
195 | |
196 //////////////////////////////////////////////////////////////////////////////// | |
197 // OmniboxImpl, Omnibox implementation: | |
198 | |
199 void OmniboxImpl::GetWindowTreeClient( | |
200 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) { | |
201 mus::WindowTreeConnection::Create( | |
202 this, std::move(request), | |
203 mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED); | |
204 } | |
205 | |
206 void OmniboxImpl::ShowForURL(const mojo::String& url) { | |
207 url_ = url; | |
208 if (root_) { | |
209 ShowWindow(); | |
210 } else { | |
211 mojo::URLRequestPtr request(mojo::URLRequest::New()); | |
212 request->url = mojo::String::From("mojo:omnibox"); | |
213 view_embedder_->Embed(std::move(request)); | |
214 } | |
215 } | |
216 | |
217 //////////////////////////////////////////////////////////////////////////////// | |
218 // OmniboxImpl, private: | |
219 | |
220 void OmniboxImpl::ShowWindow() { | |
221 DCHECK(root_); | |
222 root_->SetVisible(true); | |
223 root_->SetFocus(); | |
224 root_->MoveToFront(); | |
225 edit_->SetText(url_.To<base::string16>()); | |
226 edit_->SelectAll(false); | |
227 edit_->RequestFocus(); | |
228 } | |
229 | |
230 void OmniboxImpl::HideWindow() { | |
231 DCHECK(root_); | |
232 root_->SetVisible(false); | |
233 } | |
234 | |
235 } // namespace mandoline | |
OLD | NEW |