Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / mandoline / ui / desktop_ui / browser_window.cc
blob24bc10f394aeebf6d4644f2a8894262ccf54c612
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/desktop_ui/browser_window.h"
7 #include "base/command_line.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "components/view_manager/public/cpp/scoped_view_ptr.h"
12 #include "components/view_manager/public/cpp/view_tree_host_factory.h"
13 #include "mandoline/ui/aura/native_widget_view_manager.h"
14 #include "mandoline/ui/desktop_ui/browser_commands.h"
15 #include "mandoline/ui/desktop_ui/browser_manager.h"
16 #include "mandoline/ui/desktop_ui/public/interfaces/omnibox.mojom.h"
17 #include "mojo/common/common_type_converters.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h"
19 #include "mojo/services/tracing/public/cpp/switches.h"
20 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
21 #include "ui/gfx/canvas.h"
22 #include "ui/views/background.h"
23 #include "ui/views/controls/button/label_button.h"
24 #include "ui/views/widget/widget_delegate.h"
26 namespace mandoline {
28 class ProgressView : public views::View {
29 public:
30 ProgressView() : progress_(0.f), loading_(false) {}
31 ~ProgressView() override {}
33 void SetProgress(double progress) {
34 progress_ = progress;
35 SchedulePaint();
38 void SetIsLoading(bool loading) {
39 if (loading == loading_)
40 return;
42 loading_ = loading;
43 progress_ = 0.f;
44 SchedulePaint();
47 private:
48 void OnPaint(gfx::Canvas* canvas) override {
49 if (loading_) {
50 canvas->FillRect(GetLocalBounds(), SK_ColorGREEN);
51 gfx::Rect progress_rect = GetLocalBounds();
52 progress_rect.set_width(progress_rect.width() * progress_);
53 canvas->FillRect(progress_rect, SK_ColorRED);
54 } else {
55 canvas->FillRect(GetLocalBounds(), SK_ColorGRAY);
59 double progress_;
60 bool loading_;
62 DISALLOW_COPY_AND_ASSIGN(ProgressView);
65 ////////////////////////////////////////////////////////////////////////////////
66 // BrowserWindow, public:
68 BrowserWindow::BrowserWindow(mojo::ApplicationImpl* app,
69 mojo::ViewTreeHostFactory* host_factory,
70 BrowserManager* manager)
71 : app_(app),
72 host_client_binding_(this),
73 manager_(manager),
74 omnibox_launcher_(nullptr),
75 progress_bar_(nullptr),
76 root_(nullptr),
77 content_(nullptr),
78 omnibox_view_(nullptr),
79 web_view_(this) {
80 mojo::ViewTreeHostClientPtr host_client;
81 host_client_binding_.Bind(GetProxy(&host_client));
82 mojo::CreateViewTreeHost(host_factory, host_client.Pass(), this, &host_);
85 void BrowserWindow::LoadURL(const GURL& url) {
86 // Haven't been embedded yet, can't embed.
87 // TODO(beng): remove this.
88 if (!root_) {
89 default_url_ = url;
90 return;
93 if (!url.is_valid()) {
94 ShowOmnibox();
95 return;
98 mojo::URLRequestPtr request(mojo::URLRequest::New());
99 request->url = url.spec();
100 Embed(request.Pass());
103 void BrowserWindow::Close() {
104 if (root_)
105 mojo::ScopedViewPtr::DeleteViewOrViewManager(root_);
106 else
107 delete this;
110 BrowserWindow::~BrowserWindow() {
111 DCHECK(!root_);
112 manager_->BrowserWindowClosed(this);
115 float BrowserWindow::DIPSToPixels(float value) const {
116 return value * root_->viewport_metrics().device_pixel_ratio;
119 ////////////////////////////////////////////////////////////////////////////////
120 // BrowserWindow, mojo::ViewTreeDelegate implementation:
122 void BrowserWindow::OnEmbed(mojo::View* root) {
123 // BrowserWindow does not support being embedded more than once.
124 CHECK(!root_);
126 // Record when the browser window was displayed, used for performance testing.
127 const base::Time display_time = base::Time::Now();
129 // Make it so we get OnWillEmbed() for any Embed()s done by other apps we
130 // Embed().
131 root->connection()->SetEmbedRoot();
133 root_ = root;
135 host_->SetTitle("Mandoline");
137 content_ = root_->connection()->CreateView();
138 Init(root_);
140 host_->SetSize(mojo::Size::From(gfx::Size(1280, 800)));
142 root_->AddChild(content_);
143 content_->SetVisible(true);
145 web_view_.Init(app_, content_);
147 host_->AddAccelerator(BrowserCommand_Close, mojo::KEYBOARD_CODE_W,
148 mojo::EVENT_FLAGS_CONTROL_DOWN);
149 host_->AddAccelerator(BrowserCommand_FocusOmnibox, mojo::KEYBOARD_CODE_L,
150 mojo::EVENT_FLAGS_CONTROL_DOWN);
151 host_->AddAccelerator(BrowserCommand_NewWindow, mojo::KEYBOARD_CODE_N,
152 mojo::EVENT_FLAGS_CONTROL_DOWN);
154 // Now that we're ready, load the default url.
155 LoadURL(default_url_);
157 // Record the time spent opening initial tabs, used for performance testing.
158 const base::TimeDelta open_tabs_delta = base::Time::Now() - display_time;
160 // Record the browser startup time metrics, used for performance testing.
161 static bool recorded_browser_startup_metrics = false;
162 if (!recorded_browser_startup_metrics &&
163 base::CommandLine::ForCurrentProcess()->HasSwitch(
164 tracing::kEnableStatsCollectionBindings)) {
165 mojo::URLRequestPtr request(mojo::URLRequest::New());
166 request->url = mojo::String::From("mojo:tracing");
167 tracing::StartupPerformanceDataCollectorPtr collector;
168 app_->ConnectToService(request.Pass(), &collector);
169 collector->SetBrowserWindowDisplayTime(display_time.ToInternalValue());
170 collector->SetBrowserOpenTabsTimeDelta(open_tabs_delta.ToInternalValue());
171 collector->SetBrowserMessageLoopStartTime(
172 manager_->startup_time().ToInternalValue());
173 recorded_browser_startup_metrics = true;
177 void BrowserWindow::OnConnectionLost(mojo::ViewTreeConnection* connection) {
178 root_ = nullptr;
179 delete this;
182 ////////////////////////////////////////////////////////////////////////////////
183 // BrowserWindow, mojo::ViewTreeHostClient implementation:
185 void BrowserWindow::OnAccelerator(uint32_t id, mojo::EventPtr event) {
186 switch (id) {
187 case BrowserCommand_Close:
188 Close();
189 break;
190 case BrowserCommand_NewWindow:
191 manager_->CreateBrowser(GURL());
192 break;
193 case BrowserCommand_FocusOmnibox:
194 ShowOmnibox();
195 break;
196 default:
197 NOTREACHED();
198 break;
202 ////////////////////////////////////////////////////////////////////////////////
203 // BrowserWindow, web_view::mojom::WebViewClient implementation:
205 void BrowserWindow::TopLevelNavigate(mojo::URLRequestPtr request) {
206 Embed(request.Pass());
209 void BrowserWindow::LoadingStateChanged(bool is_loading) {
210 progress_bar_->SetIsLoading(is_loading);
213 void BrowserWindow::ProgressChanged(double progress) {
214 progress_bar_->SetProgress(progress);
217 void BrowserWindow::TitleChanged(const mojo::String& title) {
218 base::string16 formatted =
219 title.is_null() ? base::ASCIIToUTF16("Untitled")
220 : title.To<base::string16>() +
221 base::ASCIIToUTF16(" - Mandoline");
222 host_->SetTitle(mojo::String::From(formatted));
225 ////////////////////////////////////////////////////////////////////////////////
226 // BrowserWindow, ViewEmbedder implementation:
228 void BrowserWindow::Embed(mojo::URLRequestPtr request) {
229 const std::string string_url = request->url.To<std::string>();
230 if (string_url == "mojo:omnibox") {
231 EmbedOmnibox();
232 return;
235 GURL gurl(string_url);
236 bool changed = current_url_ != gurl;
237 current_url_ = gurl;
238 if (changed)
239 omnibox_launcher_->SetText(base::UTF8ToUTF16(current_url_.spec()));
241 web_view_.web_view()->LoadRequest(request.Pass());
244 ////////////////////////////////////////////////////////////////////////////////
245 // BrowserWindow, mojo::InterfaceFactory<ViewEmbedder> implementation:
247 void BrowserWindow::Create(mojo::ApplicationConnection* connection,
248 mojo::InterfaceRequest<ViewEmbedder> request) {
249 view_embedder_bindings_.AddBinding(this, request.Pass());
252 ////////////////////////////////////////////////////////////////////////////////
253 // BrowserWindow, views::LayoutManager implementation:
255 gfx::Size BrowserWindow::GetPreferredSize(const views::View* view) const {
256 return gfx::Size();
259 void BrowserWindow::Layout(views::View* host) {
260 // TODO(fsamuel): All bounds should be in physical pixels.
261 gfx::Rect bounds_in_physical_pixels(host->bounds());
262 float inverse_device_pixel_ratio =
263 1.0f / root_->viewport_metrics().device_pixel_ratio;
265 gfx::Rect omnibox_launcher_bounds =
266 gfx::ToEnclosingRect(gfx::ScaleRect(bounds_in_physical_pixels,
267 inverse_device_pixel_ratio));
268 omnibox_launcher_bounds.Inset(10, 10, 10,
269 omnibox_launcher_bounds.height() - 40);
270 omnibox_launcher_->SetBoundsRect(omnibox_launcher_bounds);
272 gfx::Rect progress_bar_bounds(omnibox_launcher_bounds.x(),
273 omnibox_launcher_bounds.bottom() + 2,
274 omnibox_launcher_bounds.width(),
276 progress_bar_->SetBoundsRect(progress_bar_bounds);
278 // The content view bounds are in physical pixels.
279 mojo::Rect content_bounds_mojo;
280 content_bounds_mojo.x = DIPSToPixels(progress_bar_bounds.x());
281 content_bounds_mojo.y = DIPSToPixels(progress_bar_bounds.bottom()+ 10);
282 content_bounds_mojo.width = DIPSToPixels(progress_bar_bounds.width());
283 content_bounds_mojo.height =
284 host->bounds().height() - content_bounds_mojo.y - DIPSToPixels(10);
285 content_->SetBounds(content_bounds_mojo);
287 // The omnibox view bounds are in physical pixels.
288 omnibox_view_->SetBounds(
289 mojo::TypeConverter<mojo::Rect, gfx::Rect>::Convert(
290 bounds_in_physical_pixels));
294 ////////////////////////////////////////////////////////////////////////////////
295 // BrowserWindow, views::ButtonListener implementation:
297 void BrowserWindow::ButtonPressed(views::Button* sender,
298 const ui::Event& event) {
299 DCHECK_EQ(sender, omnibox_launcher_);
300 ShowOmnibox();
303 ////////////////////////////////////////////////////////////////////////////////
304 // BrowserWindow, private:
306 void BrowserWindow::Init(mojo::View* root) {
307 DCHECK_GT(root->viewport_metrics().device_pixel_ratio, 0);
308 if (!aura_init_)
309 aura_init_.reset(new AuraInit(root, app_->shell()));
311 root_ = root;
312 omnibox_view_ = root_->connection()->CreateView();
313 root_->AddChild(omnibox_view_);
315 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
316 widget_delegate->GetContentsView()->set_background(
317 views::Background::CreateSolidBackground(0xFFDDDDDD));
318 omnibox_launcher_ =
319 new views::LabelButton(this, base::ASCIIToUTF16("Open Omnibox"));
320 progress_bar_ = new ProgressView;
322 widget_delegate->GetContentsView()->AddChildView(omnibox_launcher_);
323 widget_delegate->GetContentsView()->AddChildView(progress_bar_);
324 widget_delegate->GetContentsView()->SetLayoutManager(this);
326 views::Widget* widget = new views::Widget;
327 views::Widget::InitParams params(
328 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
329 params.native_widget =
330 new NativeWidgetViewManager(widget, app_->shell(), root_);
331 params.delegate = widget_delegate;
332 params.bounds = root_->bounds().To<gfx::Rect>();
333 widget->Init(params);
334 widget->Show();
335 root_->SetFocus();
338 void BrowserWindow::ShowOmnibox() {
339 if (!omnibox_.get()) {
340 mojo::URLRequestPtr request(mojo::URLRequest::New());
341 request->url = mojo::String::From("mojo:omnibox");
342 omnibox_connection_ = app_->ConnectToApplication(request.Pass());
343 omnibox_connection_->AddService<ViewEmbedder>(this);
344 omnibox_connection_->ConnectToService(&omnibox_);
345 omnibox_connection_->SetRemoteServiceProviderConnectionErrorHandler(
346 [this]() {
347 // This will cause the connection to be re-established the next time
348 // we come through this codepath.
349 omnibox_.reset();
352 omnibox_->ShowForURL(mojo::String::From(current_url_.spec()));
355 void BrowserWindow::EmbedOmnibox() {
356 mojo::ViewTreeClientPtr view_tree_client;
357 omnibox_->GetViewTreeClient(GetProxy(&view_tree_client));
358 omnibox_view_->Embed(view_tree_client.Pass());
360 // TODO(beng): This should be handled sufficiently by
361 // OmniboxImpl::ShowWindow() but unfortunately view manager policy
362 // currently prevents the embedded app from changing window z for
363 // its own window.
364 omnibox_view_->MoveToFront();
367 } // namespace mandoline