Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/ui/views/toolbar/toolbar_view.h" | 5 #include "chrome/browser/ui/views/toolbar/toolbar_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 471 return GetWidget()->GetAccelerator(command_id, accelerator); | 471 return GetWidget()->GetAccelerator(command_id, accelerator); |
| 472 } | 472 } |
| 473 | 473 |
| 474 //////////////////////////////////////////////////////////////////////////////// | 474 //////////////////////////////////////////////////////////////////////////////// |
| 475 // ToolbarView, views::View overrides: | 475 // ToolbarView, views::View overrides: |
| 476 | 476 |
| 477 gfx::Size ToolbarView::GetPreferredSize() const { | 477 gfx::Size ToolbarView::GetPreferredSize() const { |
| 478 gfx::Size size(location_bar_->GetPreferredSize()); | 478 gfx::Size size(location_bar_->GetPreferredSize()); |
| 479 if (is_display_mode_normal()) { | 479 if (is_display_mode_normal()) { |
| 480 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); | 480 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); |
| 481 const int right_padding = | |
| 482 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); | |
|
Peter Kasting
2015/12/08 21:56:25
Tiny nit: Define this below |browser_actions_width
tdanderson
2015/12/09 18:22:58
Done.
| |
| 481 const int browser_actions_width = | 483 const int browser_actions_width = |
| 482 browser_actions_->GetPreferredSize().width(); | 484 browser_actions_->GetPreferredSize().width(); |
| 483 const int content_width = | 485 const int content_width = |
| 484 GetLayoutInsets(TOOLBAR).width() + | 486 GetLayoutInsets(TOOLBAR).width() + |
| 485 back_->GetPreferredSize().width() + element_padding + | 487 back_->GetPreferredSize().width() + element_padding + |
| 486 forward_->GetPreferredSize().width() + element_padding + | 488 forward_->GetPreferredSize().width() + element_padding + |
| 487 reload_->GetPreferredSize().width() + | 489 reload_->GetPreferredSize().width() + |
| 488 (show_home_button_.GetValue() | 490 (show_home_button_.GetValue() |
| 489 ? element_padding + home_->GetPreferredSize().width() | 491 ? element_padding + home_->GetPreferredSize().width() |
| 490 : 0) + | 492 : 0) + |
| 491 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + | 493 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + |
| 492 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING) + | |
| 493 browser_actions_width + | 494 browser_actions_width + |
| 494 (browser_actions_width > 0 ? element_padding : 0) + | 495 (browser_actions_width == 0 ? right_padding : 0) + |
| 495 app_menu_button_->GetPreferredSize().width(); | 496 app_menu_button_->GetPreferredSize().width(); |
|
Evan Stade
2015/12/09 01:05:10
these two functions copy each other a lot. Can we
tdanderson
2015/12/09 18:22:59
I'll refactor in a follow-on CL once this one land
| |
| 496 size.Enlarge(content_width, 0); | 497 size.Enlarge(content_width, 0); |
| 497 } | 498 } |
| 498 return SizeForContentSize(size); | 499 return SizeForContentSize(size); |
| 499 } | 500 } |
| 500 | 501 |
| 501 gfx::Size ToolbarView::GetMinimumSize() const { | 502 gfx::Size ToolbarView::GetMinimumSize() const { |
| 502 gfx::Size size(location_bar_->GetMinimumSize()); | 503 gfx::Size size(location_bar_->GetMinimumSize()); |
| 503 if (is_display_mode_normal()) { | 504 if (is_display_mode_normal()) { |
| 504 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); | 505 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); |
| 506 const int right_padding = | |
| 507 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); | |
| 505 const int browser_actions_width = | 508 const int browser_actions_width = |
| 506 browser_actions_->GetMinimumSize().width(); | 509 browser_actions_->GetMinimumSize().width(); |
| 507 const int content_width = | 510 const int content_width = |
| 508 GetLayoutInsets(TOOLBAR).width() + | 511 GetLayoutInsets(TOOLBAR).width() + |
| 509 back_->GetMinimumSize().width() + element_padding + | 512 back_->GetMinimumSize().width() + element_padding + |
| 510 forward_->GetMinimumSize().width() + element_padding + | 513 forward_->GetMinimumSize().width() + element_padding + |
| 511 reload_->GetMinimumSize().width() + | 514 reload_->GetMinimumSize().width() + |
| 512 (show_home_button_.GetValue() | 515 (show_home_button_.GetValue() |
| 513 ? element_padding + home_->GetMinimumSize().width() | 516 ? element_padding + home_->GetMinimumSize().width() |
| 514 : 0) + | 517 : 0) + |
| 515 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + | 518 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + |
| 516 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING) + | |
| 517 browser_actions_width + | 519 browser_actions_width + |
| 518 (browser_actions_width > 0 ? element_padding : 0) + | 520 (browser_actions_width == 0 ? right_padding : 0) + |
|
Evan Stade
2015/12/09 01:05:09
nit: these two lines could perhaps be
(browser_ac
tdanderson
2015/12/09 18:22:58
Done.
| |
| 519 app_menu_button_->GetMinimumSize().width(); | 521 app_menu_button_->GetMinimumSize().width(); |
| 520 size.Enlarge(content_width, 0); | 522 size.Enlarge(content_width, 0); |
| 521 } | 523 } |
| 522 return SizeForContentSize(size); | 524 return SizeForContentSize(size); |
| 523 } | 525 } |
| 524 | 526 |
| 525 void ToolbarView::Layout() { | 527 void ToolbarView::Layout() { |
| 526 // If we have not been initialized yet just do nothing. | 528 // If we have not been initialized yet just do nothing. |
| 527 if (back_ == NULL) | 529 if (back_ == NULL) |
| 528 return; | 530 return; |
| 529 | 531 |
| 530 if (!is_display_mode_normal()) { | 532 if (!is_display_mode_normal()) { |
| 531 location_bar_->SetBounds(0, PopupTopSpacing(), width(), | 533 location_bar_->SetBounds(0, PopupTopSpacing(), width(), |
| 532 location_bar_->GetPreferredSize().height()); | 534 location_bar_->GetPreferredSize().height()); |
| 533 return; | 535 return; |
| 534 } | 536 } |
| 535 | 537 |
| 536 // We assume all child elements except the location bar are the same height. | 538 // We assume all child elements except the location bar are the same height. |
| 537 // Set child_y such that buttons appear vertically centered. | 539 // Set child_y such that buttons appear vertically centered. |
| 538 int child_height = | 540 const int child_height = |
| 539 std::min(back_->GetPreferredSize().height(), height()); | 541 std::min(back_->GetPreferredSize().height(), height()); |
| 540 int child_y = CenteredChildY(height(), child_height); | 542 const int child_y = CenteredChildY(height(), child_height); |
| 541 | 543 |
| 542 // If the window is maximized, we extend the back button to the left so that | 544 // If the window is maximized, we extend the back button to the left so that |
| 543 // clicking on the left-most pixel will activate the back button. | 545 // clicking on the left-most pixel will activate the back button. |
| 544 // TODO(abarth): If the window becomes maximized but is not resized, | 546 // TODO(abarth): If the window becomes maximized but is not resized, |
| 545 // then Layout() might not be called and the back button | 547 // then Layout() might not be called and the back button |
| 546 // will be slightly the wrong size. We should force a | 548 // will be slightly the wrong size. We should force a |
| 547 // Layout() in this case. | 549 // Layout() in this case. |
| 548 // http://crbug.com/5540 | 550 // http://crbug.com/5540 |
| 549 bool maximized = browser_->window() && browser_->window()->IsMaximized(); | 551 const bool maximized = |
| 550 int back_width = back_->GetPreferredSize().width(); | 552 browser_->window() && browser_->window()->IsMaximized(); |
| 553 const int back_width = back_->GetPreferredSize().width(); | |
| 551 const gfx::Insets insets(GetLayoutInsets(TOOLBAR)); | 554 const gfx::Insets insets(GetLayoutInsets(TOOLBAR)); |
| 552 if (maximized) { | 555 if (maximized) { |
| 553 back_->SetBounds(0, child_y, back_width + insets.left(), child_height); | 556 back_->SetBounds(0, child_y, back_width + insets.left(), child_height); |
| 554 back_->SetLeadingMargin(insets.left()); | 557 back_->SetLeadingMargin(insets.left()); |
| 555 } else { | 558 } else { |
| 556 back_->SetBounds(insets.left(), child_y, back_width, child_height); | 559 back_->SetBounds(insets.left(), child_y, back_width, child_height); |
| 557 back_->SetLeadingMargin(0); | 560 back_->SetLeadingMargin(0); |
| 558 } | 561 } |
| 559 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); | 562 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); |
| 560 int next_element_x = back_->bounds().right() + element_padding; | 563 int next_element_x = back_->bounds().right() + element_padding; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 573 home_->SetVisible(true); | 576 home_->SetVisible(true); |
| 574 home_->SetBounds(next_element_x, child_y, | 577 home_->SetBounds(next_element_x, child_y, |
| 575 home_->GetPreferredSize().width(), child_height); | 578 home_->GetPreferredSize().width(), child_height); |
| 576 } else { | 579 } else { |
| 577 home_->SetVisible(false); | 580 home_->SetVisible(false); |
| 578 home_->SetBounds(next_element_x, child_y, 0, child_height); | 581 home_->SetBounds(next_element_x, child_y, 0, child_height); |
| 579 } | 582 } |
| 580 next_element_x = | 583 next_element_x = |
| 581 home_->bounds().right() + GetLayoutConstant(TOOLBAR_STANDARD_SPACING); | 584 home_->bounds().right() + GetLayoutConstant(TOOLBAR_STANDARD_SPACING); |
| 582 | 585 |
| 583 int browser_actions_desired_width = | |
| 584 browser_actions_->GetPreferredSize().width(); | |
| 585 int app_menu_width = app_menu_button_->GetPreferredSize().width(); | 586 int app_menu_width = app_menu_button_->GetPreferredSize().width(); |
| 586 const int location_bar_right_padding = | 587 const int right_padding = |
| 587 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); | 588 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); |
| 588 | 589 |
| 589 int available_width = std::max( | 590 int available_width = std::max( |
| 590 0, width() - insets.right() - app_menu_width - | 591 0, |
| 591 (browser_actions_desired_width > 0 ? element_padding : 0) - | 592 width() - insets.right() - app_menu_width - |
| 592 location_bar_right_padding - next_element_x); | 593 (browser_actions_->GetPreferredSize().width() == 0 ? right_padding : 0) - |
|
Devlin
2015/12/08 22:13:44
A short comment explaining this calculation would
Evan Stade
2015/12/09 01:05:09
perhaps better as GetPreferredSize().IsEmpty()
tdanderson
2015/12/09 18:22:59
Done and done.
| |
| 594 next_element_x); | |
| 593 // Don't allow the omnibox to shrink to the point of non-existence, so | 595 // Don't allow the omnibox to shrink to the point of non-existence, so |
| 594 // subtract its minimum width from the available width to reserve it. | 596 // subtract its minimum width from the available width to reserve it. |
| 595 int minimum_location_bar_width = location_bar_->GetMinimumSize().width(); | 597 const int browser_actions_width = browser_actions_->GetWidthForMaxWidth( |
| 596 int browser_actions_width = browser_actions_->GetWidthForMaxWidth( | 598 available_width - location_bar_->GetMinimumSize().width()); |
| 597 available_width - minimum_location_bar_width); | |
| 598 available_width -= browser_actions_width; | 599 available_width -= browser_actions_width; |
| 599 int location_bar_width = available_width; | 600 const int location_bar_width = available_width; |
| 600 | 601 |
| 601 int location_height = location_bar_->GetPreferredSize().height(); | 602 const int location_height = location_bar_->GetPreferredSize().height(); |
| 602 int location_y = CenteredChildY(height(), location_height); | 603 const int location_y = CenteredChildY(height(), location_height); |
| 603 | 604 |
| 604 location_bar_->SetBounds(next_element_x, location_y, | 605 location_bar_->SetBounds(next_element_x, location_y, |
| 605 location_bar_width, location_height); | 606 location_bar_width, location_height); |
| 606 next_element_x = location_bar_->bounds().right() + location_bar_right_padding; | |
| 607 | 607 |
| 608 next_element_x = location_bar_->bounds().right(); | |
| 608 browser_actions_->SetBounds( | 609 browser_actions_->SetBounds( |
| 609 next_element_x, child_y, browser_actions_width, child_height); | 610 next_element_x, child_y, browser_actions_width, child_height); |
| 610 next_element_x = browser_actions_->bounds().right(); | 611 next_element_x = browser_actions_->bounds().right(); |
| 611 if (browser_actions_width > 0) | 612 if (!browser_actions_width) |
| 612 next_element_x += element_padding; | 613 next_element_x += right_padding; |
| 613 | 614 |
| 614 // The browser actions need to do a layout explicitly, because when an | 615 // The browser actions need to do a layout explicitly, because when an |
| 615 // extension is loaded/unloaded/changed, BrowserActionContainer removes and | 616 // extension is loaded/unloaded/changed, BrowserActionContainer removes and |
| 616 // re-adds everything, regardless of whether it has a page action. For a | 617 // re-adds everything, regardless of whether it has a page action. For a |
| 617 // page action, browser action bounds do not change, as a result of which | 618 // page action, browser action bounds do not change, as a result of which |
| 618 // SetBounds does not do a layout at all. | 619 // SetBounds does not do a layout at all. |
| 619 // TODO(sidchat): Rework the above behavior so that explicit layout is not | 620 // TODO(sidchat): Rework the above behavior so that explicit layout is not |
| 620 // required. | 621 // required. |
| 621 browser_actions_->Layout(); | 622 browser_actions_->Layout(); |
| 622 | 623 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 818 void ToolbarView::OnShowHomeButtonChanged() { | 819 void ToolbarView::OnShowHomeButtonChanged() { |
| 819 Layout(); | 820 Layout(); |
| 820 SchedulePaint(); | 821 SchedulePaint(); |
| 821 } | 822 } |
| 822 | 823 |
| 823 int ToolbarView::content_shadow_height() const { | 824 int ToolbarView::content_shadow_height() const { |
| 824 return GetLayoutConstant( | 825 return GetLayoutConstant( |
| 825 (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) ? | 826 (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) ? |
| 826 TOOLBAR_CONTENT_SHADOW_HEIGHT_ASH : TOOLBAR_CONTENT_SHADOW_HEIGHT); | 827 TOOLBAR_CONTENT_SHADOW_HEIGHT_ASH : TOOLBAR_CONTENT_SHADOW_HEIGHT); |
| 827 } | 828 } |
| OLD | NEW |