Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: mandoline/ui/desktop_ui/browser_window.cc

Issue 1326443006: mandoline: Add back/forward support and UI. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: msw nits and comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mandoline/ui/desktop_ui/browser_window.h" 5 #include "mandoline/ui/desktop_ui/browser_window.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h" 9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "components/view_manager/public/cpp/scoped_view_ptr.h" 11 #include "components/view_manager/public/cpp/scoped_view_ptr.h"
12 #include "components/view_manager/public/cpp/view_tree_host_factory.h" 12 #include "components/view_manager/public/cpp/view_tree_host_factory.h"
13 #include "mandoline/ui/aura/native_widget_view_manager.h" 13 #include "mandoline/ui/aura/native_widget_view_manager.h"
14 #include "mandoline/ui/desktop_ui/browser_commands.h" 14 #include "mandoline/ui/desktop_ui/browser_commands.h"
15 #include "mandoline/ui/desktop_ui/browser_manager.h" 15 #include "mandoline/ui/desktop_ui/browser_manager.h"
16 #include "mandoline/ui/desktop_ui/public/interfaces/omnibox.mojom.h" 16 #include "mandoline/ui/desktop_ui/public/interfaces/omnibox.mojom.h"
17 #include "mojo/common/common_type_converters.h" 17 #include "mojo/common/common_type_converters.h"
18 #include "mojo/converters/geometry/geometry_type_converters.h" 18 #include "mojo/converters/geometry/geometry_type_converters.h"
19 #include "mojo/services/tracing/public/cpp/switches.h" 19 #include "mojo/services/tracing/public/cpp/switches.h"
20 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" 20 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h"
21 #include "ui/gfx/canvas.h" 21 #include "ui/gfx/canvas.h"
22 #include "ui/views/background.h" 22 #include "ui/views/background.h"
23 #include "ui/views/controls/button/label_button.h" 23 #include "ui/views/controls/button/label_button.h"
24 #include "ui/views/layout/box_layout.h"
24 #include "ui/views/widget/widget_delegate.h" 25 #include "ui/views/widget/widget_delegate.h"
25 26
26 namespace mandoline { 27 namespace mandoline {
27 28
29 using views::BoxLayout;
msw 2015/09/04 23:49:00 nit: remove, inline one more "views::".
30
31 class BrowserWindow::ToolbarView : public views::View,
32 public views::ButtonListener {
33 public:
34 ToolbarView(BrowserWindow* browser_window)
35 : browser_window_(browser_window),
36 layout_(new views::BoxLayout(BoxLayout::kHorizontal, 0, 0, 5)),
37 back_button_(new views::LabelButton(this, base::ASCIIToUTF16("Back"))),
38 forward_button_(
39 new views::LabelButton(this, base::ASCIIToUTF16("Forward"))),
40 omnibox_launcher_(
41 new views::LabelButton(this, base::ASCIIToUTF16("Open Omnibox"))) {
42 SetLayoutManager(layout_);
43
44 AddChildView(back_button_);
45 AddChildView(forward_button_);
46 AddChildView(omnibox_launcher_);
47
48 layout_->SetDefaultFlex(0);
49 layout_->SetFlexForView(omnibox_launcher_, 1);
50 }
51
52 ~ToolbarView() override {}
53
54 void SetOmniboxText(const base::string16& text) {
55 omnibox_launcher_->SetText(text);
56 }
57
58 void SetBackForwardEnabled(bool back_enabled, bool forward_enabled) {
59 back_button_->SetEnabled(back_enabled);
60 forward_button_->SetEnabled(forward_enabled);
61 }
62
63 // Overridden from views::ButtonListener:
64 void ButtonPressed(views::Button* sender, const ui::Event& event) override {
65 if (sender == omnibox_launcher_)
66 browser_window_->ShowOmnibox();
67 else if (sender == back_button_)
68 browser_window_->GoBack();
69 else if (sender == forward_button_)
70 browser_window_->GoForward();
71 else
72 NOTIMPLEMENTED();
73 }
74
75 private:
76 BrowserWindow* browser_window_;
77
78 views::BoxLayout* layout_;
79 views::LabelButton* back_button_;
80 views::LabelButton* forward_button_;
81 views::LabelButton* omnibox_launcher_;
82
83 DISALLOW_COPY_AND_ASSIGN(ToolbarView);
84 };
85
28 class ProgressView : public views::View { 86 class ProgressView : public views::View {
29 public: 87 public:
30 ProgressView() : progress_(0.f), loading_(false) {} 88 ProgressView() : progress_(0.f), loading_(false) {}
31 ~ProgressView() override {} 89 ~ProgressView() override {}
32 90
33 void SetProgress(double progress) { 91 void SetProgress(double progress) {
34 progress_ = progress; 92 progress_ = progress;
35 SchedulePaint(); 93 SchedulePaint();
36 } 94 }
37 95
(...skipping 26 matching lines...) Expand all
64 122
65 //////////////////////////////////////////////////////////////////////////////// 123 ////////////////////////////////////////////////////////////////////////////////
66 // BrowserWindow, public: 124 // BrowserWindow, public:
67 125
68 BrowserWindow::BrowserWindow(mojo::ApplicationImpl* app, 126 BrowserWindow::BrowserWindow(mojo::ApplicationImpl* app,
69 mojo::ViewTreeHostFactory* host_factory, 127 mojo::ViewTreeHostFactory* host_factory,
70 BrowserManager* manager) 128 BrowserManager* manager)
71 : app_(app), 129 : app_(app),
72 host_client_binding_(this), 130 host_client_binding_(this),
73 manager_(manager), 131 manager_(manager),
74 omnibox_launcher_(nullptr), 132 toolbar_view_(nullptr),
75 progress_bar_(nullptr), 133 progress_bar_(nullptr),
76 root_(nullptr), 134 root_(nullptr),
77 content_(nullptr), 135 content_(nullptr),
78 omnibox_view_(nullptr), 136 omnibox_view_(nullptr),
79 web_view_(this) { 137 web_view_(this) {
80 mojo::ViewTreeHostClientPtr host_client; 138 mojo::ViewTreeHostClientPtr host_client;
81 host_client_binding_.Bind(GetProxy(&host_client)); 139 host_client_binding_.Bind(GetProxy(&host_client));
82 mojo::CreateViewTreeHost(host_factory, host_client.Pass(), this, &host_); 140 mojo::CreateViewTreeHost(host_factory, host_client.Pass(), this, &host_);
83 } 141 }
84 142
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 content_->SetAccessPolicy(mojo::ViewTree::ACCESS_POLICY_EMBED_ROOT); 199 content_->SetAccessPolicy(mojo::ViewTree::ACCESS_POLICY_EMBED_ROOT);
142 200
143 web_view_.Init(app_, content_); 201 web_view_.Init(app_, content_);
144 202
145 host_->AddAccelerator(BrowserCommand_Close, mojo::KEYBOARD_CODE_W, 203 host_->AddAccelerator(BrowserCommand_Close, mojo::KEYBOARD_CODE_W,
146 mojo::EVENT_FLAGS_CONTROL_DOWN); 204 mojo::EVENT_FLAGS_CONTROL_DOWN);
147 host_->AddAccelerator(BrowserCommand_FocusOmnibox, mojo::KEYBOARD_CODE_L, 205 host_->AddAccelerator(BrowserCommand_FocusOmnibox, mojo::KEYBOARD_CODE_L,
148 mojo::EVENT_FLAGS_CONTROL_DOWN); 206 mojo::EVENT_FLAGS_CONTROL_DOWN);
149 host_->AddAccelerator(BrowserCommand_NewWindow, mojo::KEYBOARD_CODE_N, 207 host_->AddAccelerator(BrowserCommand_NewWindow, mojo::KEYBOARD_CODE_N,
150 mojo::EVENT_FLAGS_CONTROL_DOWN); 208 mojo::EVENT_FLAGS_CONTROL_DOWN);
209 host_->AddAccelerator(BrowserCommand_GoBack, mojo::KEYBOARD_CODE_LEFT,
210 mojo::EVENT_FLAGS_ALT_DOWN);
211 host_->AddAccelerator(BrowserCommand_GoForward, mojo::KEYBOARD_CODE_RIGHT,
212 mojo::EVENT_FLAGS_ALT_DOWN);
151 213
152 // Now that we're ready, load the default url. 214 // Now that we're ready, load the default url.
153 LoadURL(default_url_); 215 LoadURL(default_url_);
154 216
155 // Record the time spent opening initial tabs, used for performance testing. 217 // Record the time spent opening initial tabs, used for performance testing.
156 const base::TimeDelta open_tabs_delta = base::Time::Now() - display_time; 218 const base::TimeDelta open_tabs_delta = base::Time::Now() - display_time;
157 219
158 // Record the browser startup time metrics, used for performance testing. 220 // Record the browser startup time metrics, used for performance testing.
159 static bool recorded_browser_startup_metrics = false; 221 static bool recorded_browser_startup_metrics = false;
160 if (!recorded_browser_startup_metrics && 222 if (!recorded_browser_startup_metrics &&
(...skipping 23 matching lines...) Expand all
184 switch (id) { 246 switch (id) {
185 case BrowserCommand_Close: 247 case BrowserCommand_Close:
186 Close(); 248 Close();
187 break; 249 break;
188 case BrowserCommand_NewWindow: 250 case BrowserCommand_NewWindow:
189 manager_->CreateBrowser(GURL()); 251 manager_->CreateBrowser(GURL());
190 break; 252 break;
191 case BrowserCommand_FocusOmnibox: 253 case BrowserCommand_FocusOmnibox:
192 ShowOmnibox(); 254 ShowOmnibox();
193 break; 255 break;
256 case BrowserCommand_GoBack:
257 GoBack();
258 break;
259 case BrowserCommand_GoForward:
260 GoForward();
261 break;
194 default: 262 default:
195 NOTREACHED(); 263 NOTREACHED();
196 break; 264 break;
197 } 265 }
198 } 266 }
199 267
200 //////////////////////////////////////////////////////////////////////////////// 268 ////////////////////////////////////////////////////////////////////////////////
201 // BrowserWindow, web_view::mojom::WebViewClient implementation: 269 // BrowserWindow, web_view::mojom::WebViewClient implementation:
202 270
203 void BrowserWindow::TopLevelNavigate(mojo::URLRequestPtr request) { 271 void BrowserWindow::TopLevelNavigate(mojo::URLRequestPtr request) {
204 Embed(request.Pass()); 272 Embed(request.Pass());
205 } 273 }
206 274
207 void BrowserWindow::LoadingStateChanged(bool is_loading) { 275 void BrowserWindow::LoadingStateChanged(bool is_loading) {
208 progress_bar_->SetIsLoading(is_loading); 276 progress_bar_->SetIsLoading(is_loading);
209 } 277 }
210 278
211 void BrowserWindow::ProgressChanged(double progress) { 279 void BrowserWindow::ProgressChanged(double progress) {
212 progress_bar_->SetProgress(progress); 280 progress_bar_->SetProgress(progress);
213 } 281 }
214 282
283 void BrowserWindow::BackForwardChanged(bool back_enabled,
284 bool forward_enabled) {
285 toolbar_view_->SetBackForwardEnabled(back_enabled, forward_enabled);
286 }
287
215 void BrowserWindow::TitleChanged(const mojo::String& title) { 288 void BrowserWindow::TitleChanged(const mojo::String& title) {
216 base::string16 formatted = 289 base::string16 formatted =
217 title.is_null() ? base::ASCIIToUTF16("Untitled") 290 title.is_null() ? base::ASCIIToUTF16("Untitled")
218 : title.To<base::string16>() + 291 : title.To<base::string16>() +
219 base::ASCIIToUTF16(" - Mandoline"); 292 base::ASCIIToUTF16(" - Mandoline");
220 host_->SetTitle(mojo::String::From(formatted)); 293 host_->SetTitle(mojo::String::From(formatted));
221 } 294 }
222 295
223 //////////////////////////////////////////////////////////////////////////////// 296 ////////////////////////////////////////////////////////////////////////////////
224 // BrowserWindow, ViewEmbedder implementation: 297 // BrowserWindow, ViewEmbedder implementation:
225 298
226 void BrowserWindow::Embed(mojo::URLRequestPtr request) { 299 void BrowserWindow::Embed(mojo::URLRequestPtr request) {
227 const std::string string_url = request->url.To<std::string>(); 300 const std::string string_url = request->url.To<std::string>();
228 if (string_url == "mojo:omnibox") { 301 if (string_url == "mojo:omnibox") {
229 EmbedOmnibox(); 302 EmbedOmnibox();
230 return; 303 return;
231 } 304 }
232 305
233 GURL gurl(string_url); 306 GURL gurl(string_url);
234 bool changed = current_url_ != gurl; 307 bool changed = current_url_ != gurl;
235 current_url_ = gurl; 308 current_url_ = gurl;
236 if (changed) 309 if (changed)
237 omnibox_launcher_->SetText(base::UTF8ToUTF16(current_url_.spec())); 310 toolbar_view_->SetOmniboxText(base::UTF8ToUTF16(current_url_.spec()));
238 311
239 web_view_.web_view()->LoadRequest(request.Pass()); 312 web_view_.web_view()->LoadRequest(request.Pass());
240 } 313 }
241 314
242 //////////////////////////////////////////////////////////////////////////////// 315 ////////////////////////////////////////////////////////////////////////////////
243 // BrowserWindow, mojo::InterfaceFactory<ViewEmbedder> implementation: 316 // BrowserWindow, mojo::InterfaceFactory<ViewEmbedder> implementation:
244 317
245 void BrowserWindow::Create(mojo::ApplicationConnection* connection, 318 void BrowserWindow::Create(mojo::ApplicationConnection* connection,
246 mojo::InterfaceRequest<ViewEmbedder> request) { 319 mojo::InterfaceRequest<ViewEmbedder> request) {
247 view_embedder_bindings_.AddBinding(this, request.Pass()); 320 view_embedder_bindings_.AddBinding(this, request.Pass());
248 } 321 }
249 322
250 //////////////////////////////////////////////////////////////////////////////// 323 ////////////////////////////////////////////////////////////////////////////////
251 // BrowserWindow, views::LayoutManager implementation: 324 // BrowserWindow, views::LayoutManager implementation:
252 325
253 gfx::Size BrowserWindow::GetPreferredSize(const views::View* view) const { 326 gfx::Size BrowserWindow::GetPreferredSize(const views::View* view) const {
254 return gfx::Size(); 327 return gfx::Size();
255 } 328 }
256 329
257 void BrowserWindow::Layout(views::View* host) { 330 void BrowserWindow::Layout(views::View* host) {
258 // TODO(fsamuel): All bounds should be in physical pixels. 331 // TODO(fsamuel): All bounds should be in physical pixels.
259 gfx::Rect bounds_in_physical_pixels(host->bounds()); 332 gfx::Rect bounds_in_physical_pixels(host->bounds());
260 float inverse_device_pixel_ratio = 333 float inverse_device_pixel_ratio =
261 1.0f / root_->viewport_metrics().device_pixel_ratio; 334 1.0f / root_->viewport_metrics().device_pixel_ratio;
262 335
263 gfx::Rect omnibox_launcher_bounds = 336 gfx::Rect toolbar_bounds = gfx::ToEnclosingRect(
264 gfx::ToEnclosingRect(gfx::ScaleRect(bounds_in_physical_pixels, 337 gfx::ScaleRect(bounds_in_physical_pixels, inverse_device_pixel_ratio));
265 inverse_device_pixel_ratio)); 338 toolbar_bounds.Inset(10, 10, 10, toolbar_bounds.height() - 40);
266 omnibox_launcher_bounds.Inset(10, 10, 10, 339 toolbar_view_->SetBoundsRect(toolbar_bounds);
267 omnibox_launcher_bounds.height() - 40);
268 omnibox_launcher_->SetBoundsRect(omnibox_launcher_bounds);
269 340
270 gfx::Rect progress_bar_bounds(omnibox_launcher_bounds.x(), 341 gfx::Rect progress_bar_bounds(toolbar_bounds.x(), toolbar_bounds.bottom() + 2,
271 omnibox_launcher_bounds.bottom() + 2, 342 toolbar_bounds.width(), 5);
272 omnibox_launcher_bounds.width(),
273 5);
274 progress_bar_->SetBoundsRect(progress_bar_bounds); 343 progress_bar_->SetBoundsRect(progress_bar_bounds);
275 344
276 // The content view bounds are in physical pixels. 345 // The content view bounds are in physical pixels.
277 mojo::Rect content_bounds_mojo; 346 mojo::Rect content_bounds_mojo;
278 content_bounds_mojo.x = DIPSToPixels(progress_bar_bounds.x()); 347 content_bounds_mojo.x = DIPSToPixels(progress_bar_bounds.x());
279 content_bounds_mojo.y = DIPSToPixels(progress_bar_bounds.bottom()+ 10); 348 content_bounds_mojo.y = DIPSToPixels(progress_bar_bounds.bottom()+ 10);
280 content_bounds_mojo.width = DIPSToPixels(progress_bar_bounds.width()); 349 content_bounds_mojo.width = DIPSToPixels(progress_bar_bounds.width());
281 content_bounds_mojo.height = 350 content_bounds_mojo.height =
282 host->bounds().height() - content_bounds_mojo.y - DIPSToPixels(10); 351 host->bounds().height() - content_bounds_mojo.y - DIPSToPixels(10);
283 content_->SetBounds(content_bounds_mojo); 352 content_->SetBounds(content_bounds_mojo);
284 353
285 // The omnibox view bounds are in physical pixels. 354 // The omnibox view bounds are in physical pixels.
286 omnibox_view_->SetBounds( 355 omnibox_view_->SetBounds(
287 mojo::TypeConverter<mojo::Rect, gfx::Rect>::Convert( 356 mojo::TypeConverter<mojo::Rect, gfx::Rect>::Convert(
288 bounds_in_physical_pixels)); 357 bounds_in_physical_pixels));
289
290 } 358 }
291 359
292 //////////////////////////////////////////////////////////////////////////////// 360 ////////////////////////////////////////////////////////////////////////////////
293 // BrowserWindow, views::ButtonListener implementation:
294
295 void BrowserWindow::ButtonPressed(views::Button* sender,
296 const ui::Event& event) {
297 DCHECK_EQ(sender, omnibox_launcher_);
298 ShowOmnibox();
299 }
300
301 ////////////////////////////////////////////////////////////////////////////////
302 // BrowserWindow, private: 361 // BrowserWindow, private:
303 362
304 void BrowserWindow::Init(mojo::View* root) { 363 void BrowserWindow::Init(mojo::View* root) {
305 DCHECK_GT(root->viewport_metrics().device_pixel_ratio, 0); 364 DCHECK_GT(root->viewport_metrics().device_pixel_ratio, 0);
306 if (!aura_init_) 365 if (!aura_init_)
307 aura_init_.reset(new AuraInit(root, app_->shell())); 366 aura_init_.reset(new AuraInit(root, app_->shell()));
308 367
309 root_ = root; 368 root_ = root;
310 omnibox_view_ = root_->connection()->CreateView(); 369 omnibox_view_ = root_->connection()->CreateView();
311 root_->AddChild(omnibox_view_); 370 root_->AddChild(omnibox_view_);
312 371
313 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView; 372 views::WidgetDelegateView* widget_delegate = new views::WidgetDelegateView;
314 widget_delegate->GetContentsView()->set_background( 373 widget_delegate->GetContentsView()->set_background(
315 views::Background::CreateSolidBackground(0xFFDDDDDD)); 374 views::Background::CreateSolidBackground(0xFFDDDDDD));
316 omnibox_launcher_ = 375 toolbar_view_ = new ToolbarView(this);
317 new views::LabelButton(this, base::ASCIIToUTF16("Open Omnibox"));
318 progress_bar_ = new ProgressView; 376 progress_bar_ = new ProgressView;
319 377 widget_delegate->GetContentsView()->AddChildView(toolbar_view_);
320 widget_delegate->GetContentsView()->AddChildView(omnibox_launcher_);
321 widget_delegate->GetContentsView()->AddChildView(progress_bar_); 378 widget_delegate->GetContentsView()->AddChildView(progress_bar_);
322 widget_delegate->GetContentsView()->SetLayoutManager(this); 379 widget_delegate->GetContentsView()->SetLayoutManager(this);
323 380
324 views::Widget* widget = new views::Widget; 381 views::Widget* widget = new views::Widget;
325 views::Widget::InitParams params( 382 views::Widget::InitParams params(
326 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); 383 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
327 params.native_widget = 384 params.native_widget =
328 new NativeWidgetViewManager(widget, app_->shell(), root_); 385 new NativeWidgetViewManager(widget, app_->shell(), root_);
329 params.delegate = widget_delegate; 386 params.delegate = widget_delegate;
330 params.bounds = root_->bounds().To<gfx::Rect>(); 387 params.bounds = root_->bounds().To<gfx::Rect>();
(...skipping 12 matching lines...) Expand all
343 omnibox_connection_->SetRemoteServiceProviderConnectionErrorHandler( 400 omnibox_connection_->SetRemoteServiceProviderConnectionErrorHandler(
344 [this]() { 401 [this]() {
345 // This will cause the connection to be re-established the next time 402 // This will cause the connection to be re-established the next time
346 // we come through this codepath. 403 // we come through this codepath.
347 omnibox_.reset(); 404 omnibox_.reset();
348 }); 405 });
349 } 406 }
350 omnibox_->ShowForURL(mojo::String::From(current_url_.spec())); 407 omnibox_->ShowForURL(mojo::String::From(current_url_.spec()));
351 } 408 }
352 409
410 void BrowserWindow::GoBack() {
411 web_view_.web_view()->GoBack();
412 }
413
414 void BrowserWindow::GoForward() {
415 web_view_.web_view()->GoForward();
416 }
417
353 void BrowserWindow::EmbedOmnibox() { 418 void BrowserWindow::EmbedOmnibox() {
354 mojo::ViewTreeClientPtr view_tree_client; 419 mojo::ViewTreeClientPtr view_tree_client;
355 omnibox_->GetViewTreeClient(GetProxy(&view_tree_client)); 420 omnibox_->GetViewTreeClient(GetProxy(&view_tree_client));
356 omnibox_view_->Embed(view_tree_client.Pass()); 421 omnibox_view_->Embed(view_tree_client.Pass());
357 422
358 // TODO(beng): This should be handled sufficiently by 423 // TODO(beng): This should be handled sufficiently by
359 // OmniboxImpl::ShowWindow() but unfortunately view manager policy 424 // OmniboxImpl::ShowWindow() but unfortunately view manager policy
360 // currently prevents the embedded app from changing window z for 425 // currently prevents the embedded app from changing window z for
361 // its own window. 426 // its own window.
362 omnibox_view_->MoveToFront(); 427 omnibox_view_->MoveToFront();
363 } 428 }
364 429
365 } // namespace mandoline 430 } // namespace mandoline
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698