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