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

Unified Diff: chrome/browser/ui/views/location_bar/location_bar_view.cc

Issue 302453002: New animation for the origin chip URL showing/hiding. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix test failure by checking for NULL extension Created 6 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/views/location_bar/location_bar_view.cc
diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc
index b6e2cb0305cd2cbd9c82aaa7398f29bdd1f16078..bf3eee2aaf8135c69394aee53f73e03f90c84e7d 100644
--- a/chrome/browser/ui/views/location_bar/location_bar_view.cc
+++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc
@@ -11,7 +11,6 @@
#include "base/i18n/rtl.h"
#include "base/prefs/pref_service.h"
#include "base/stl_util.h"
-#include "base/strings/string_split.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/chrome_notification_types.h"
@@ -41,7 +40,6 @@
#include "chrome/browser/ui/passwords/manage_passwords_icon.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
-#include "chrome/browser/ui/toolbar/origin_chip_info.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/browser_dialogs.h"
#include "chrome/browser/ui/views/location_bar/content_setting_image_view.h"
@@ -83,6 +81,7 @@
#include "ui/gfx/color_utils.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia_operations.h"
+#include "ui/gfx/scoped_canvas.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/text_utils.h"
#include "ui/native_theme/native_theme.h"
@@ -104,6 +103,9 @@ using views::View;
namespace {
+const gfx::Tween::Type kShowTweenType = gfx::Tween::LINEAR_OUT_SLOW_IN;
+const gfx::Tween::Type kHideTweenType = gfx::Tween::FAST_OUT_LINEAR_IN;
+
// The search button images are made to look as if they overlay the normal edge
// images, but to align things, the search button needs to be inset horizontally
// by 1 px.
@@ -224,7 +226,12 @@ LocationBarView::LocationBarView(Browser* browser,
show_focus_rect_(false),
template_url_service_(NULL),
dropdown_animation_offset_(0),
- animated_host_label_(NULL),
+ starting_omnibox_offset_(0),
+ current_omnibox_offset_(0),
+ starting_omnibox_leading_inset_(0),
+ current_omnibox_leading_inset_(0),
+ current_omnibox_width_(0),
+ ending_omnibox_width_(0),
weak_ptr_factory_(this) {
edit_bookmarks_enabled_.Init(
prefs::kEditBookmarksEnabled, profile->GetPrefs(),
@@ -316,10 +323,6 @@ void LocationBarView::Init() {
ime_inline_autocomplete_view_->SetVisible(false);
AddChildView(ime_inline_autocomplete_view_);
- animated_host_label_ = new views::Label(base::string16(), font_list);
- animated_host_label_->SetVisible(false);
- AddChildView(animated_host_label_);
-
origin_chip_view_ = new OriginChipView(this, profile(), font_list);
origin_chip_view_->SetFocusable(false);
origin_chip_view_->set_drag_controller(this);
@@ -424,12 +427,12 @@ void LocationBarView::Init() {
AddChildView(search_button_);
show_url_animation_.reset(new gfx::SlideAnimation(this));
- show_url_animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
+ show_url_animation_->SetTweenType(kShowTweenType);
show_url_animation_->SetSlideDuration(200);
hide_url_animation_.reset(new gfx::SlideAnimation(this));
- hide_url_animation_->SetTweenType(gfx::Tween::FAST_OUT_SLOW_IN);
- hide_url_animation_->SetSlideDuration(200);
+ hide_url_animation_->SetTweenType(kHideTweenType);
+ hide_url_animation_->SetSlideDuration(175);
content::Source<Profile> profile_source = content::Source<Profile>(profile());
registrar_.Add(this,
@@ -558,11 +561,7 @@ void LocationBarView::SetTranslateIconToggled(bool on) {
gfx::Point LocationBarView::GetOmniboxViewOrigin() const {
gfx::Point origin(omnibox_view_->bounds().origin());
- // If the UI layout is RTL, the coordinate system is not transformed and
- // therefore we need to adjust the X coordinate so that bubble appears on the
- // right hand side of the location bar.
- if (base::i18n::IsRTL())
- origin.set_x(width() - origin.x());
+ origin.set_x(GetMirroredXInView(origin.x() - current_omnibox_offset_));
views::View::ConvertPointToScreen(this, &origin);
return origin;
}
@@ -582,8 +581,8 @@ void LocationBarView::SetGrayTextAutocompletion(const base::string16& text) {
}
base::string16 LocationBarView::GetGrayTextAutocompletion() const {
- return HasValidSuggestText() ? suggested_text_view_->text()
- : base::string16();
+ return HasValidSuggestText() ?
+ suggested_text_view_->text() : base::string16();
}
void LocationBarView::SetShowFocusRect(bool show) {
@@ -738,7 +737,6 @@ void LocationBarView::Layout() {
if (!IsInitialized())
return;
- animated_host_label_->SetVisible(false);
origin_chip_view_->SetVisible(GetToolbarModel()->ShouldShowOriginChip());
selected_keyword_view_->SetVisible(false);
location_icon_view_->SetVisible(false);
@@ -751,50 +749,15 @@ void LocationBarView::Layout() {
LocationBarLayout trailing_decorations(LocationBarLayout::RIGHT_EDGE,
kItemPadding);
- // Show and position the animated host label used in the show and hide URL
- // animations.
- if (show_url_animation_->is_animating() ||
- hide_url_animation_->is_animating()) {
- WebContents* web_contents = GetWebContents();
- const GURL url = web_contents ? web_contents->GetURL() : GURL();
- const base::string16 host =
- OriginChip::LabelFromURLForProfile(url, profile());
- animated_host_label_->SetText(host);
-
- const base::string16 formatted_url = GetToolbarModel()->GetFormattedURL();
-
- // Split the formatted URL on the host name in order to determine the size
- // of the text leading up to it.
- std::vector<base::string16> substrings;
- base::SplitStringUsingSubstr(formatted_url, host, &substrings);
- const base::string16 text_leading_host = substrings[0];
- const int leading_text_width =
- gfx::Canvas::GetStringWidth(text_leading_host,
- origin_chip_view_->GetFontList());
-
- const int position_of_host_name_in_chip = origin_chip_view_->host_label_x();
- const int position_of_host_name_in_url =
- position_of_host_name_in_chip + leading_text_width;
-
- int host_label_x = position_of_host_name_in_chip;
- if (show_url_animation_->is_animating()) {
- host_label_x = show_url_animation_->
- CurrentValueBetween(position_of_host_name_in_chip,
- position_of_host_name_in_url);
- } else if (hide_url_animation_->is_animating()) {
- host_label_x = hide_url_animation_->
- CurrentValueBetween(position_of_host_name_in_url,
- position_of_host_name_in_chip);
- }
- animated_host_label_->SetBounds(
- host_label_x, 0,
- animated_host_label_->GetPreferredSize().width(), height());
- animated_host_label_->SetVisible(true);
- }
-
- const int origin_chip_width = origin_chip_view_->visible() ?
- origin_chip_view_->GetPreferredSize().width() : 0;
- origin_chip_view_->SetBounds(0, 0, origin_chip_width, height());
+ const int origin_chip_preferred_width =
+ origin_chip_view_->GetPreferredSize().width();
+ const int origin_chip_width =
+ origin_chip_view_->visible() ? origin_chip_preferred_width : 0;
+ // Always give the origin chip view its desired size and lay it out, even when
+ // it's not visible, so we can calculate the correct animation values below
+ // when switching to tabs that have the origin chip hidden.
+ origin_chip_view_->SetBounds(0, 0, origin_chip_preferred_width, height());
+ origin_chip_view_->Layout();
const int bubble_location_y = vertical_edge_thickness() + kBubblePadding;
const base::string16 keyword(omnibox_view_->model()->keyword());
@@ -919,6 +882,109 @@ void LocationBarView::Layout() {
leading_decorations.LayoutPass3(&location_bounds, &available_width);
trailing_decorations.LayoutPass3(&location_bounds, &available_width);
+ // Calculate the animation parameters (see comments on these members in the
+ // header). We have to do this in Layout, after |origin_chip_view_| is laid
+ // out, because that may affect the host label offset in the origin chip.
+ const base::string16& chip_text(origin_chip_view_->host_label_text());
+ // If the chip is clicked, the omnibox text will become the toolbar model's
+ // formatted URL. We can't ask the omnibox for its current text, because
+ // while the chip is visible the current text is empty.
+ size_t prefix_end = 0;
+ const base::string16& omnibox_text(
+ GetToolbarModel()->GetFormattedURL(&prefix_end));
+ // Do a case-insensitive search to better match cases like
+ // "Settings" <-> "chrome://settings". Skip any pre-hostname text.
+ size_t chip_text_offset = std::search(
+ omnibox_text.begin() + prefix_end, omnibox_text.end(),
+ chip_text.begin(), chip_text.end(),
+ base::CaseInsensitiveCompare<base::char16>()) - omnibox_text.begin();
+ // If we couldn't find the chip text, try checking whether the omnibox text
+ // starts with it, as is true for e.g. file: URLs.
+ if ((chip_text_offset >= omnibox_text.length()) &&
+ StartsWith(omnibox_text, chip_text, true))
+ chip_text_offset = 0;
+ const gfx::FontList& font_list = omnibox_view_->GetFontList();
+ const int chip_text_width = gfx::GetStringWidth(chip_text, font_list);
+ const int old_starting_offset = starting_omnibox_offset_;
+ const int old_starting_leading_inset = starting_omnibox_leading_inset_;
+ const int old_ending_width = ending_omnibox_width_;
+ starting_omnibox_offset_ = current_omnibox_offset_ = 0;
+ starting_omnibox_leading_inset_ = current_omnibox_leading_inset_ = 0;
+ ending_omnibox_width_ = gfx::GetStringWidth(omnibox_text, font_list);
+ if (chip_text_offset < omnibox_text.length()) {
+ if (base::i18n::IsRTL())
+ chip_text_offset += chip_text.length();
+ base::string16 extra_omnibox_text(base::i18n::IsRTL() ?
+ omnibox_text.substr(chip_text_offset) :
+ omnibox_text.substr(0, chip_text_offset));
+ starting_omnibox_leading_inset_ =
+ gfx::GetStringWidth(extra_omnibox_text, font_list);
+ starting_omnibox_offset_ = origin_chip_view_->HostLabelOffset() -
+ starting_omnibox_leading_inset_;
+ current_omnibox_width_ = chip_text_width;
+ } else {
+ // If the chip text wasn't found in the omnibox text, then instead of
+ // starting the show animation clipped to the "hostname", we'll start with
+ // the entire omnibox text visible, clipped to the remaining chip width, and
+ // only animate any necessary expansion of that width, without moving the
+ // omnibox bounds.
+ current_omnibox_width_ = origin_chip_view_->WidthFromStartOfLabels();
+ }
+
+ // End the animations immediately if the parameters have changed.
+ if ((starting_omnibox_offset_ != old_starting_offset) ||
+ (starting_omnibox_leading_inset_ != old_starting_leading_inset) ||
+ (ending_omnibox_width_ != old_ending_width))
+ EndOriginChipAnimations(true);
+
+ // Also end the animations immediately if there's nothing to animate (but do
+ // allow the chip to fade back in).
+ const ui::NativeTheme* native_theme = GetNativeTheme();
+ const SkColor ending_selection_text_color = native_theme->GetSystemColor(
+ ui::NativeTheme::kColorId_TextfieldSelectionColor);
+ const SkColor ending_selection_background_color =
+ native_theme->GetSystemColor(
+ ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused);
+ if ((starting_omnibox_offset_ == 0) &&
+ (starting_omnibox_leading_inset_ == 0) &&
+ (ending_omnibox_width_ == chip_text_width) &&
+ (hide_url_animation_->is_animating() ||
+ ((ending_selection_text_color ==
+ origin_chip_view_->pressed_text_color()) &&
+ (ending_selection_background_color ==
+ origin_chip_view_->pressed_background_color()))))
+ EndOriginChipAnimations(false);
+
+ if (show_url_animation_->is_animating()) {
+ omnibox_view_->SetSelectionTextColor(gfx::Tween::ColorValueBetween(
+ show_url_animation_->GetCurrentValue(),
+ origin_chip_view_->pressed_text_color(),
+ ending_selection_text_color));
+ omnibox_view_->SetSelectionBackgroundColor(gfx::Tween::ColorValueBetween(
+ show_url_animation_->GetCurrentValue(),
+ origin_chip_view_->pressed_background_color(),
+ ending_selection_background_color));
+ current_omnibox_offset_ =
+ show_url_animation_->CurrentValueBetween(starting_omnibox_offset_, 0);
+ current_omnibox_leading_inset_ = show_url_animation_->CurrentValueBetween(
+ starting_omnibox_leading_inset_, 0);
+ current_omnibox_width_ = show_url_animation_->CurrentValueBetween(
+ chip_text_width, ending_omnibox_width_);
+ } else if (hide_url_animation_->is_animating()) {
+ current_omnibox_offset_ =
+ hide_url_animation_->CurrentValueBetween(0, starting_omnibox_offset_);
+ current_omnibox_leading_inset_ = hide_url_animation_->CurrentValueBetween(
+ 0, starting_omnibox_leading_inset_);
+ current_omnibox_width_ = hide_url_animation_->CurrentValueBetween(
+ ending_omnibox_width_, chip_text_width);
+ }
+ // Contract |available_width| as necessary, but never expand it. This way,
+ // we'll never draw suggested text at first and then have it disappear
+ // midway through the animation.
+ if (current_omnibox_offset_ > 0)
+ available_width -= current_omnibox_offset_;
+ location_bounds.Inset(current_omnibox_offset_, 0, 0, 0);
+
// Layout out the suggested text view right aligned to the location
// entry. Only show the suggested text if we can fit the text from one
// character before the end of the selection to the end of the text and the
@@ -1038,13 +1104,35 @@ void LocationBarView::Update(const WebContents* contents) {
}
void LocationBarView::ShowURL() {
+ // Start the animation before calling ShowURL(), since the latter eventually
+ // calls back to Layout(), and if the animation is not marked as "running",
+ // we'll draw the omnibox in its final position briefly until the first
+ // animation callback reaches us.
if (chrome::ShouldDisplayOriginChip()) {
- omnibox_view_->SetVisible(false);
- omnibox_view_->ShowURL();
- show_url_animation_->Show();
- } else {
- omnibox_view_->ShowURL();
+ // If we're currently hiding, reverse the hide by swapping to the show
+ // animation, offset so that the text is in the same position.
+ if (hide_url_animation_->is_animating()) {
+ const double show_value = GetValueForAnimation(false);
+ hide_url_animation_->Reset();
+ show_url_animation_->Show();
+ // This must be done after calling Show() and is not equivalent to
+ // calling Reset(n) before Show(); Reset() would have caused the entire
+ // animation curve (and time) to run between this value and the final
+ // value, whereas Show() + SetCurrentValue() skips the animation forward
+ // to the supplied value.
+ show_url_animation_->SetCurrentValue(show_value);
+ } else {
+ show_url_animation_->Show();
+ }
}
+ omnibox_view_->ShowURL();
+}
+
+void LocationBarView::EndOriginChipAnimations(bool cancel_fade) {
+ show_url_animation_->End();
+ hide_url_animation_->End();
+ if (cancel_fade)
+ origin_chip_view_->CancelFade();
}
ToolbarModel* LocationBarView::GetToolbarModel() {
@@ -1242,24 +1330,43 @@ bool LocationBarView::ShouldShowEVBubble() const {
(GetToolbarModel()->GetSecurityLevel(false) == ToolbarModel::EV_SECURE);
}
-void LocationBarView::OnShowURLAnimationEnded() {
- animated_host_label_->SetVisible(false);
- omnibox_view_->SetVisible(true);
- omnibox_view_->FadeIn();
- omnibox_view_->SetFocus();
-
- // Sometimes the selection established by OmniboxView::ShowURL() is lost at
- // the call to SetFocus() above. Select all again to be sure.
- // TODO(jdonnelly): Figure out why the selection is sometimes lost and
- // implement a more principled fix.
- omnibox_view_->SelectAll(true);
-}
-
-void LocationBarView::OnHideURLAnimationEnded() {
- animated_host_label_->SetVisible(false);
- omnibox_view_->HideURL();
- omnibox_view_->SetVisible(true);
- origin_chip_view_->FadeIn();
+double LocationBarView::GetValueForAnimation(bool hide) const {
+ int calculated_offset;
+ const gfx::Tween::Type tween_type = hide ? kHideTweenType : kShowTweenType;
+ int start_offset = starting_omnibox_offset_, end_offset = 0;
+ if (hide)
+ std::swap(start_offset, end_offset);
+ const int desired_offset = abs(current_omnibox_offset_);
+ // Binary-search the value space (0 <= value <= 1) to find the appropriate
+ // position. We only bother to iterate to within 1/64 of the desired value,
+ // because the longer of the two animations will only run for twelve frames
+ // anyway (200 ms * 60 Hz), so at this point we'll have a maximum error of
+ // less than a fifth of an animation frame, which the user isn't going to
+ // notice.
+ //
+ // We have to use this method because Tween::CalculateValue() is not
+ // necessarily easily invertible. Luckily, this only runs when the user
+ // reverses the animation (rare), and the limit on how many iterations we'll
+ // do ensures the cost is unnoticeable.
+ double value = 0.5;
+ double step = value / 2;
+ do {
+ calculated_offset = abs(gfx::Tween::IntValueBetween(
+ gfx::Tween::CalculateValue(tween_type, value), start_offset,
+ end_offset));
+ if (calculated_offset < desired_offset)
+ value += step;
+ else if (calculated_offset > desired_offset)
+ value -= step;
+ step /= 2;
+ } while ((calculated_offset != desired_offset) && (step >= (1.0 / 64)));
+ return value;
+}
+
+void LocationBarView::ResetShowAnimationAndColors() {
+ show_url_animation_->Reset();
+ omnibox_view_->UseDefaultSelectionTextColor();
+ omnibox_view_->UseDefaultSelectionBackgroundColor();
}
////////////////////////////////////////////////////////////////////////////////
@@ -1354,6 +1461,11 @@ void LocationBarView::UpdateGeneratedCreditCardView() {
}
void LocationBarView::SaveStateToContents(WebContents* contents) {
+ // If we're about to switch tabs, complete any current animations, so that if
+ // the user is in the midst of hiding the URL, when he returns to this tab,
+ // the URL will be hidden rather than shown.
+ // NOTE: This must be called before SaveStateToTab().
+ EndOriginChipAnimations(true);
omnibox_view_->SaveStateToTab(contents);
}
@@ -1466,13 +1578,27 @@ void LocationBarView::OnPaint(gfx::Canvas* canvas) {
void LocationBarView::PaintChildren(gfx::Canvas* canvas,
const views::CullSet& cull_set) {
- // Paint all the children except for the origin chip and the search button,
+ // Paint all the children except for the omnibox itself, which may need to be
+ // clipped if it's animating in, and the origin chip and the search button,
// which will be painted after the border.
for (int i = 0, count = child_count(); i < count; ++i) {
- if (!child_at(i)->layer() &&
- (child_at(i) != origin_chip_view_) &&
- (child_at(i) != search_button_))
- child_at(i)->Paint(canvas, cull_set);
+ views::View* child = child_at(i);
+ if (!child->layer() && (child != omnibox_view_) &&
+ (child != origin_chip_view_) && (child != search_button_))
+ child->Paint(canvas, cull_set);
+ }
+
+ {
+ gfx::ScopedCanvas scoped_canvas(canvas);
+ if (show_url_animation_->is_animating() ||
+ hide_url_animation_->is_animating()) {
+ gfx::Rect clip_rect(omnibox_view_->bounds());
+ clip_rect.Inset(current_omnibox_leading_inset_, 0, 0, 0);
+ clip_rect.set_width(current_omnibox_width_);
+ clip_rect.set_x(GetMirroredXForRect(clip_rect));
+ canvas->ClipRect(clip_rect);
+ }
+ omnibox_view_->Paint(canvas, cull_set);
}
// For non-InstantExtendedAPI cases, if necessary, show focus rect. As we need
@@ -1572,8 +1698,7 @@ void LocationBarView::OnChanged() {
*GetThemeProvider()->GetImageSkiaNamed((icon_id == IDR_OMNIBOX_SEARCH) ?
IDR_OMNIBOX_SEARCH_BUTTON_LOUPE : IDR_OMNIBOX_SEARCH_BUTTON_ARROW));
- if (origin_chip_view_->visible())
- origin_chip_view_->OnChanged();
+ origin_chip_view_->OnChanged();
Layout();
SchedulePaint();
@@ -1592,8 +1717,20 @@ const ToolbarModel* LocationBarView::GetToolbarModel() const {
}
void LocationBarView::HideURL() {
- omnibox_view_->SetVisible(false);
- hide_url_animation_->Show();
+ DCHECK(chrome::ShouldDisplayOriginChip());
+
+ // If we're currently showing, reverse the hide by swapping to the hide
+ // animation, offset so that the text is in the same position.
+ if (show_url_animation_->is_animating()) {
+ const double hide_value = GetValueForAnimation(true);
+ ResetShowAnimationAndColors();
+ hide_url_animation_->Show();
+ // This must be done after calling Show() and is not equivalent to Reset(n);
+ // see comments in ShowURL().
+ hide_url_animation_->SetCurrentValue(hide_value);
+ } else {
+ hide_url_animation_->Show();
+ }
}
////////////////////////////////////////////////////////////////////////////////
@@ -1611,20 +1748,22 @@ void LocationBarView::SetAnimationOffset(int offset) {
// LocationBarView, private gfx::AnimationDelegate implementation:
void LocationBarView::AnimationProgressed(const gfx::Animation* animation) {
- if (animation == show_url_animation_.get() ||
- animation == hide_url_animation_.get()) {
- Layout();
- SchedulePaint();
- }
+ DCHECK((animation == show_url_animation_.get()) ||
+ (animation == hide_url_animation_.get()));
+ Layout();
+ SchedulePaint();
}
void LocationBarView::AnimationEnded(const gfx::Animation* animation) {
if (animation == show_url_animation_.get()) {
- show_url_animation_->Reset();
- OnShowURLAnimationEnded();
- } else if (animation == hide_url_animation_.get()) {
+ ResetShowAnimationAndColors();
+ Layout();
+ SchedulePaint();
+ } else {
+ DCHECK(animation == hide_url_animation_.get());
hide_url_animation_->Reset();
- OnHideURLAnimationEnded();
+ origin_chip_view_->FadeIn();
+ omnibox_view_->HideURL(); // Calls OnChanged(), triggering layout.
}
}

Powered by Google App Engine
This is Rietveld 408576698