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