1 // Copyright (c) 2012 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. 4 5 #include "chrome/browser/ui/tabs/hover_tab_selector.h" 6 7 #include "base/bind.h" 8 #include "base/compiler_specific.h" 9 #include "base/message_loop/message_loop.h" 10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 HoverTabSelector(TabStripModel * tab_strip_model)12HoverTabSelector::HoverTabSelector( 13 TabStripModel* tab_strip_model) 14 : tab_strip_model_(tab_strip_model), 15 tab_transition_tab_index_(-1), 16 weak_factory_(this) { 17 DCHECK(tab_strip_model_); 18 } 19 ~HoverTabSelector()20HoverTabSelector::~HoverTabSelector() { 21 } 22 StartTabTransition(int index)23void HoverTabSelector::StartTabTransition(int index) { 24 // If there is a transition underway already, only start a new 25 // transition (canceling the old one) if the target tab differs. 26 if (weak_factory_.HasWeakPtrs()) { 27 if (index == tab_transition_tab_index_) 28 return; 29 CancelTabTransition(); 30 } 31 // Start a new transition if the target isn't active already. 32 if (index != tab_strip_model_->active_index()) { 33 // The delay between beginning to hover over a tab and the transition 34 // to that tab taking place. 35 const base::TimeDelta kHoverTransitionDelay = 36 base::TimeDelta::FromMilliseconds(500); 37 tab_transition_tab_index_ = index; 38 base::MessageLoop::current()->PostDelayedTask( 39 FROM_HERE, 40 base::Bind(&HoverTabSelector::PerformTabTransition, 41 weak_factory_.GetWeakPtr()), 42 kHoverTransitionDelay); 43 } 44 } 45 CancelTabTransition()46void HoverTabSelector::CancelTabTransition() { 47 weak_factory_.InvalidateWeakPtrs(); 48 } 49 PerformTabTransition()50void HoverTabSelector::PerformTabTransition() { 51 DCHECK(tab_transition_tab_index_ >= 0 && 52 tab_transition_tab_index_ < tab_strip_model_->count()); 53 tab_strip_model_->ActivateTabAt(tab_transition_tab_index_, true); 54 } 55 56