| 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 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 browser_actions_width = | 481 const int browser_actions_width = |
| 482 browser_actions_->GetPreferredSize().width(); | 482 browser_actions_->GetPreferredSize().width(); |
| 483 const int right_padding = |
| 484 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); |
| 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) + | 494 (browser_actions_width > 0 ? browser_actions_width : right_padding) + |
| 493 browser_actions_width + | |
| 494 (browser_actions_width > 0 ? element_padding : 0) + | |
| 495 app_menu_button_->GetPreferredSize().width(); | 495 app_menu_button_->GetPreferredSize().width(); |
| 496 size.Enlarge(content_width, 0); | 496 size.Enlarge(content_width, 0); |
| 497 } | 497 } |
| 498 return SizeForContentSize(size); | 498 return SizeForContentSize(size); |
| 499 } | 499 } |
| 500 | 500 |
| 501 gfx::Size ToolbarView::GetMinimumSize() const { | 501 gfx::Size ToolbarView::GetMinimumSize() const { |
| 502 gfx::Size size(location_bar_->GetMinimumSize()); | 502 gfx::Size size(location_bar_->GetMinimumSize()); |
| 503 if (is_display_mode_normal()) { | 503 if (is_display_mode_normal()) { |
| 504 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); | 504 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); |
| 505 const int browser_actions_width = | 505 const int browser_actions_width = |
| 506 browser_actions_->GetMinimumSize().width(); | 506 browser_actions_->GetMinimumSize().width(); |
| 507 const int right_padding = |
| 508 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); |
| 507 const int content_width = | 509 const int content_width = |
| 508 GetLayoutInsets(TOOLBAR).width() + | 510 GetLayoutInsets(TOOLBAR).width() + |
| 509 back_->GetMinimumSize().width() + element_padding + | 511 back_->GetMinimumSize().width() + element_padding + |
| 510 forward_->GetMinimumSize().width() + element_padding + | 512 forward_->GetMinimumSize().width() + element_padding + |
| 511 reload_->GetMinimumSize().width() + | 513 reload_->GetMinimumSize().width() + |
| 512 (show_home_button_.GetValue() | 514 (show_home_button_.GetValue() |
| 513 ? element_padding + home_->GetMinimumSize().width() | 515 ? element_padding + home_->GetMinimumSize().width() |
| 514 : 0) + | 516 : 0) + |
| 515 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + | 517 GetLayoutConstant(TOOLBAR_STANDARD_SPACING) + |
| 516 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING) + | 518 (browser_actions_width > 0 ? browser_actions_width : right_padding) + |
| 517 browser_actions_width + | |
| 518 (browser_actions_width > 0 ? element_padding : 0) + | |
| 519 app_menu_button_->GetMinimumSize().width(); | 519 app_menu_button_->GetMinimumSize().width(); |
| 520 size.Enlarge(content_width, 0); | 520 size.Enlarge(content_width, 0); |
| 521 } | 521 } |
| 522 return SizeForContentSize(size); | 522 return SizeForContentSize(size); |
| 523 } | 523 } |
| 524 | 524 |
| 525 void ToolbarView::Layout() { | 525 void ToolbarView::Layout() { |
| 526 // If we have not been initialized yet just do nothing. | 526 // If we have not been initialized yet just do nothing. |
| 527 if (back_ == NULL) | 527 if (back_ == NULL) |
| 528 return; | 528 return; |
| 529 | 529 |
| 530 if (!is_display_mode_normal()) { | 530 if (!is_display_mode_normal()) { |
| 531 location_bar_->SetBounds(0, PopupTopSpacing(), width(), | 531 location_bar_->SetBounds(0, PopupTopSpacing(), width(), |
| 532 location_bar_->GetPreferredSize().height()); | 532 location_bar_->GetPreferredSize().height()); |
| 533 return; | 533 return; |
| 534 } | 534 } |
| 535 | 535 |
| 536 // We assume all child elements except the location bar are the same height. | 536 // We assume all child elements except the location bar are the same height. |
| 537 // Set child_y such that buttons appear vertically centered. | 537 // Set child_y such that buttons appear vertically centered. |
| 538 int child_height = | 538 const int child_height = |
| 539 std::min(back_->GetPreferredSize().height(), height()); | 539 std::min(back_->GetPreferredSize().height(), height()); |
| 540 int child_y = CenteredChildY(height(), child_height); | 540 const int child_y = CenteredChildY(height(), child_height); |
| 541 | 541 |
| 542 // If the window is maximized, we extend the back button to the left so that | 542 // 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. | 543 // clicking on the left-most pixel will activate the back button. |
| 544 // TODO(abarth): If the window becomes maximized but is not resized, | 544 // TODO(abarth): If the window becomes maximized but is not resized, |
| 545 // then Layout() might not be called and the back button | 545 // then Layout() might not be called and the back button |
| 546 // will be slightly the wrong size. We should force a | 546 // will be slightly the wrong size. We should force a |
| 547 // Layout() in this case. | 547 // Layout() in this case. |
| 548 // http://crbug.com/5540 | 548 // http://crbug.com/5540 |
| 549 bool maximized = browser_->window() && browser_->window()->IsMaximized(); | 549 const bool maximized = |
| 550 int back_width = back_->GetPreferredSize().width(); | 550 browser_->window() && browser_->window()->IsMaximized(); |
| 551 const int back_width = back_->GetPreferredSize().width(); |
| 551 const gfx::Insets insets(GetLayoutInsets(TOOLBAR)); | 552 const gfx::Insets insets(GetLayoutInsets(TOOLBAR)); |
| 552 if (maximized) { | 553 if (maximized) { |
| 553 back_->SetBounds(0, child_y, back_width + insets.left(), child_height); | 554 back_->SetBounds(0, child_y, back_width + insets.left(), child_height); |
| 554 back_->SetLeadingMargin(insets.left()); | 555 back_->SetLeadingMargin(insets.left()); |
| 555 } else { | 556 } else { |
| 556 back_->SetBounds(insets.left(), child_y, back_width, child_height); | 557 back_->SetBounds(insets.left(), child_y, back_width, child_height); |
| 557 back_->SetLeadingMargin(0); | 558 back_->SetLeadingMargin(0); |
| 558 } | 559 } |
| 559 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); | 560 const int element_padding = GetLayoutConstant(TOOLBAR_ELEMENT_PADDING); |
| 560 int next_element_x = back_->bounds().right() + element_padding; | 561 int next_element_x = back_->bounds().right() + element_padding; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 573 home_->SetVisible(true); | 574 home_->SetVisible(true); |
| 574 home_->SetBounds(next_element_x, child_y, | 575 home_->SetBounds(next_element_x, child_y, |
| 575 home_->GetPreferredSize().width(), child_height); | 576 home_->GetPreferredSize().width(), child_height); |
| 576 } else { | 577 } else { |
| 577 home_->SetVisible(false); | 578 home_->SetVisible(false); |
| 578 home_->SetBounds(next_element_x, child_y, 0, child_height); | 579 home_->SetBounds(next_element_x, child_y, 0, child_height); |
| 579 } | 580 } |
| 580 next_element_x = | 581 next_element_x = |
| 581 home_->bounds().right() + GetLayoutConstant(TOOLBAR_STANDARD_SPACING); | 582 home_->bounds().right() + GetLayoutConstant(TOOLBAR_STANDARD_SPACING); |
| 582 | 583 |
| 583 int browser_actions_desired_width = | |
| 584 browser_actions_->GetPreferredSize().width(); | |
| 585 int app_menu_width = app_menu_button_->GetPreferredSize().width(); | 584 int app_menu_width = app_menu_button_->GetPreferredSize().width(); |
| 586 const int location_bar_right_padding = | 585 const int right_padding = |
| 587 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); | 586 GetLayoutConstant(TOOLBAR_LOCATION_BAR_RIGHT_PADDING); |
| 588 | 587 |
| 588 // Note that the browser actions container has its own internal left and right |
| 589 // padding to visually separate it from the location bar and app menu button. |
| 590 // However if the container is empty we must account for the |right_padding| |
| 591 // value used to visually separate the location bar and app menu button. |
| 589 int available_width = std::max( | 592 int available_width = std::max( |
| 590 0, width() - insets.right() - app_menu_width - | 593 0, |
| 591 (browser_actions_desired_width > 0 ? element_padding : 0) - | 594 width() - insets.right() - app_menu_width - |
| 592 location_bar_right_padding - next_element_x); | 595 (browser_actions_->GetPreferredSize().IsEmpty() ? right_padding : 0) - |
| 596 next_element_x); |
| 593 // Don't allow the omnibox to shrink to the point of non-existence, so | 597 // 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. | 598 // subtract its minimum width from the available width to reserve it. |
| 595 int minimum_location_bar_width = location_bar_->GetMinimumSize().width(); | 599 const int browser_actions_width = browser_actions_->GetWidthForMaxWidth( |
| 596 int browser_actions_width = browser_actions_->GetWidthForMaxWidth( | 600 available_width - location_bar_->GetMinimumSize().width()); |
| 597 available_width - minimum_location_bar_width); | |
| 598 available_width -= browser_actions_width; | 601 available_width -= browser_actions_width; |
| 599 int location_bar_width = available_width; | 602 const int location_bar_width = available_width; |
| 600 | 603 |
| 601 int location_height = location_bar_->GetPreferredSize().height(); | 604 const int location_height = location_bar_->GetPreferredSize().height(); |
| 602 int location_y = CenteredChildY(height(), location_height); | 605 const int location_y = CenteredChildY(height(), location_height); |
| 603 | 606 |
| 604 location_bar_->SetBounds(next_element_x, location_y, | 607 location_bar_->SetBounds(next_element_x, location_y, |
| 605 location_bar_width, location_height); | 608 location_bar_width, location_height); |
| 606 next_element_x = location_bar_->bounds().right() + location_bar_right_padding; | |
| 607 | 609 |
| 610 next_element_x = location_bar_->bounds().right(); |
| 608 browser_actions_->SetBounds( | 611 browser_actions_->SetBounds( |
| 609 next_element_x, child_y, browser_actions_width, child_height); | 612 next_element_x, child_y, browser_actions_width, child_height); |
| 610 next_element_x = browser_actions_->bounds().right(); | 613 next_element_x = browser_actions_->bounds().right(); |
| 611 if (browser_actions_width > 0) | 614 if (!browser_actions_width) |
| 612 next_element_x += element_padding; | 615 next_element_x += right_padding; |
| 613 | 616 |
| 614 // The browser actions need to do a layout explicitly, because when an | 617 // The browser actions need to do a layout explicitly, because when an |
| 615 // extension is loaded/unloaded/changed, BrowserActionContainer removes and | 618 // extension is loaded/unloaded/changed, BrowserActionContainer removes and |
| 616 // re-adds everything, regardless of whether it has a page action. For a | 619 // 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 | 620 // page action, browser action bounds do not change, as a result of which |
| 618 // SetBounds does not do a layout at all. | 621 // SetBounds does not do a layout at all. |
| 619 // TODO(sidchat): Rework the above behavior so that explicit layout is not | 622 // TODO(sidchat): Rework the above behavior so that explicit layout is not |
| 620 // required. | 623 // required. |
| 621 browser_actions_->Layout(); | 624 browser_actions_->Layout(); |
| 622 | 625 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 818 void ToolbarView::OnShowHomeButtonChanged() { | 821 void ToolbarView::OnShowHomeButtonChanged() { |
| 819 Layout(); | 822 Layout(); |
| 820 SchedulePaint(); | 823 SchedulePaint(); |
| 821 } | 824 } |
| 822 | 825 |
| 823 int ToolbarView::content_shadow_height() const { | 826 int ToolbarView::content_shadow_height() const { |
| 824 return GetLayoutConstant( | 827 return GetLayoutConstant( |
| 825 (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) ? | 828 (browser_->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH) ? |
| 826 TOOLBAR_CONTENT_SHADOW_HEIGHT_ASH : TOOLBAR_CONTENT_SHADOW_HEIGHT); | 829 TOOLBAR_CONTENT_SHADOW_HEIGHT_ASH : TOOLBAR_CONTENT_SHADOW_HEIGHT); |
| 827 } | 830 } |
| OLD | NEW |