Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ash/tab_scrubber.h" | 5 #include "chrome/browser/ui/views/ash/tab_scrubber.h" |
| 6 | 6 |
| 7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 8 #include "ash/wm/window_util.h" | 8 #include "ash/wm/window_util.h" |
| 9 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 10 #include "chrome/browser/ui/browser_finder.h" | 10 #include "chrome/browser/ui/browser_finder.h" |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/browser/ui/views/frame/browser_view.h" | 12 #include "chrome/browser/ui/views/frame/browser_view.h" |
| 13 #include "chrome/browser/ui/views/immersive_mode_controller.h" | |
| 13 #include "chrome/browser/ui/views/tabs/tab.h" | 14 #include "chrome/browser/ui/views/tabs/tab.h" |
| 14 #include "chrome/browser/ui/views/tabs/tab_strip.h" | 15 #include "chrome/browser/ui/views/tabs/tab_strip.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "content/public/browser/notification_service.h" | |
| 16 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 17 #include "ui/aura/window.h" | 19 #include "ui/aura/window.h" |
| 18 #include "ui/base/events/event.h" | 20 #include "ui/base/events/event.h" |
| 19 #include "ui/base/events/event_utils.h" | 21 #include "ui/base/events/event_utils.h" |
| 22 #include "ui/views/controls/glow_hover_controller.h" | |
| 20 | 23 |
| 21 namespace { | 24 namespace { |
| 22 Tab* GetTabAt(TabStrip* tab_strip, gfx::Point point) { | 25 const int64 kActivationDelayMS = 200; |
| 23 for (int i = 0; i < tab_strip->tab_count(); ++i) { | 26 const int64 kCancelImmersiveRevelDelayMS = 200; |
| 24 Tab* tab = tab_strip->tab_at(i); | |
| 25 if (tab_strip->tab_at(i)->bounds().Contains(point)) | |
| 26 return tab; | |
| 27 } | |
| 28 return NULL; | |
| 29 } | |
| 30 | |
| 31 const int kInitialTabOffset = 10; | |
| 32 } | 27 } |
| 33 | 28 |
| 34 // static | 29 // static |
| 35 TabScrubber* TabScrubber::GetInstance() { | 30 TabScrubber* TabScrubber::GetInstance() { |
| 36 static TabScrubber* instance = NULL; | 31 static TabScrubber* instance = NULL; |
| 37 if (!instance) | 32 if (!instance) |
| 38 instance = new TabScrubber(); | 33 instance = new TabScrubber(); |
| 39 return instance; | 34 return instance; |
| 40 } | 35 } |
| 41 | 36 |
| 37 // static | |
| 38 gfx::Point TabScrubber::GetStartPoint( | |
| 39 TabStrip* tab_strip, | |
| 40 int index, | |
| 41 TabScrubber::Direction direction) { | |
| 42 int initial_tab_offset = Tab::GetMiniWidth() / 2; | |
| 43 gfx::Rect tab_bounds = tab_strip->tab_at(index)->bounds(); | |
| 44 float x = direction == LEFT ? | |
| 45 tab_bounds.x() + initial_tab_offset : | |
| 46 tab_bounds.right() - initial_tab_offset; | |
| 47 return gfx::Point(x, tab_bounds.CenterPoint().y()); | |
| 48 } | |
| 49 | |
| 42 TabScrubber::TabScrubber() | 50 TabScrubber::TabScrubber() |
| 43 : scrubbing_(false), | 51 : scrubbing_(false), |
| 44 browser_(NULL), | 52 browser_(NULL), |
| 45 scroll_x_(-1), | 53 swipe_x_(-1), |
| 46 scroll_y_(-1) { | 54 swipe_y_(-1), |
| 55 swipe_direction_(LEFT), | |
| 56 highlighted_tab_(-1), | |
| 57 activate_timer_(true, false), | |
| 58 activation_delay_(base::TimeDelta::FromMilliseconds(kActivationDelayMS)), | |
| 59 should_cancel_immersive_reveal_(false), | |
| 60 cancel_immersive_reveal_timer_(true, false), | |
| 61 weak_ptr_factory_(this) { | |
| 47 ash::Shell::GetInstance()->AddPreTargetHandler(this); | 62 ash::Shell::GetInstance()->AddPreTargetHandler(this); |
| 63 registrar_.Add( | |
| 64 this, | |
| 65 chrome::NOTIFICATION_BROWSER_CLOSING, | |
| 66 content::NotificationService::AllSources()); | |
| 48 } | 67 } |
| 49 | 68 |
| 50 TabScrubber::~TabScrubber() { | 69 TabScrubber::~TabScrubber() { |
| 51 } | 70 } |
| 52 | 71 |
| 53 void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { | 72 void TabScrubber::OnScrollEvent(ui::ScrollEvent* event) { |
| 54 if (event->type() == ui::ET_SCROLL_FLING_CANCEL) { | 73 if (event->type() == ui::ET_SCROLL_FLING_CANCEL || |
| 55 if (scrubbing_) | 74 event->type() == ui::ET_SCROLL_FLING_START) { |
| 56 StopScrubbing(); | 75 FinishScrub(true); |
| 76 CancelImmersiveReveal(); | |
| 57 return; | 77 return; |
| 58 } | 78 } |
| 59 | 79 |
| 60 if (event->finger_count() != 3 || | 80 if (event->finger_count() != 3) |
| 61 event->type() != ui::ET_SCROLL) | |
| 62 return; | 81 return; |
| 63 | 82 |
| 64 Browser* browser = GetActiveBrowser(); | 83 Browser* browser = GetActiveBrowser(); |
| 65 if (!browser || (browser_ && browser != browser_)) { | 84 if (!browser || (browser_ && browser != browser_) || |
| 66 if (scrubbing_) | 85 (highlighted_tab_ != -1 && |
| 67 StopScrubbing(); | 86 highlighted_tab_ >= browser->tab_strip_model()->count())) { |
| 87 FinishScrub(false); | |
| 68 return; | 88 return; |
| 69 } | 89 } |
| 70 | 90 |
| 91 event->StopPropagation(); | |
| 92 float x_offset = event->x_offset(); | |
| 71 BrowserView* browser_view = | 93 BrowserView* browser_view = |
| 72 BrowserView::GetBrowserViewForNativeWindow( | 94 BrowserView::GetBrowserViewForNativeWindow( |
| 73 browser->window()->GetNativeWindow()); | 95 browser->window()->GetNativeWindow()); |
| 74 TabStrip* tab_strip = browser_view->tabstrip(); | 96 TabStrip* tab_strip = browser_view->tabstrip(); |
| 75 | 97 |
| 76 float x_offset = -event->x_offset(); | 98 if (!ui::IsNaturalScrollEnabled()) |
| 99 x_offset = -x_offset; | |
| 100 int last_tab_index = highlighted_tab_ == -1 ? | |
| 101 browser->tab_strip_model()->active_index() : highlighted_tab_; | |
| 77 if (!scrubbing_) { | 102 if (!scrubbing_) { |
| 103 swipe_direction_ = (x_offset < 0) ? LEFT : RIGHT; | |
| 104 const gfx::Point start_point = | |
| 105 GetStartPoint(tab_strip, | |
| 106 browser->tab_strip_model()->active_index(), | |
| 107 swipe_direction_); | |
| 108 browser_ = browser; | |
| 78 scrubbing_ = true; | 109 scrubbing_ = true; |
| 79 browser_ = browser; | 110 |
| 80 Tab* initial_tab = | 111 swipe_x_ = start_point.x(); |
| 81 tab_strip->tab_at(browser_->tab_strip_model()->active_index()); | 112 swipe_y_ = start_point.y(); |
| 82 scroll_x_ = initial_tab->x(); | 113 ImmersiveModeController* immersive_controller = |
| 83 scroll_x_ += (x_offset < 0) ? | 114 browser_view->immersive_mode_controller(); |
| 84 kInitialTabOffset : initial_tab->width() - kInitialTabOffset; | 115 CancelImmersiveReveal(); |
| 85 scroll_y_ = initial_tab->height() / 2; | 116 if (immersive_controller->enabled() && |
| 86 registrar_.Add( | 117 !immersive_controller->IsRevealed()) { |
| 87 this, | 118 immersive_controller->MaybeStartReveal(); |
| 88 chrome::NOTIFICATION_BROWSER_CLOSING, | 119 should_cancel_immersive_reveal_ = true; |
| 89 content::Source<Browser>(browser_)); | 120 } |
| 121 } else if (highlighted_tab_ == -1) { | |
| 122 Direction direction = (x_offset < 0) ? LEFT : RIGHT; | |
| 123 if (direction != swipe_direction_) { | |
| 124 const gfx::Point start_point = | |
| 125 GetStartPoint(tab_strip, | |
| 126 browser->tab_strip_model()->active_index(), | |
| 127 swipe_direction_); | |
| 128 swipe_x_ = start_point.x(); | |
| 129 swipe_y_ = start_point.y(); | |
| 130 swipe_direction_ = direction; | |
| 131 } | |
| 90 } | 132 } |
| 91 | 133 |
| 92 if (ui::IsNaturalScrollEnabled()) | 134 swipe_x_ += x_offset; |
| 93 scroll_x_ += event->x_offset(); | |
| 94 else | |
| 95 scroll_x_ -= event->x_offset(); | |
| 96 Tab* first_tab = tab_strip->tab_at(0); | 135 Tab* first_tab = tab_strip->tab_at(0); |
| 136 int first_tab_center = first_tab->bounds().CenterPoint().x(); | |
| 97 Tab* last_tab = tab_strip->tab_at(tab_strip->tab_count() - 1); | 137 Tab* last_tab = tab_strip->tab_at(tab_strip->tab_count() - 1); |
| 98 if (scroll_x_ < first_tab->x()) | 138 int last_tab_tab_center = last_tab->bounds().CenterPoint().x(); |
| 99 scroll_x_ = first_tab->x(); | 139 if (swipe_x_ < first_tab_center) |
| 100 if (scroll_x_ > last_tab->bounds().right()) | 140 swipe_x_ = first_tab_center; |
| 101 scroll_x_ = last_tab->bounds().right(); | 141 if (swipe_x_ > last_tab_tab_center) |
| 142 swipe_x_ = last_tab_tab_center; | |
| 102 | 143 |
| 103 gfx::Point tab_point(scroll_x_, scroll_y_); | 144 Tab* initial_tab = tab_strip->tab_at(last_tab_index); |
| 104 Tab* new_tab = GetTabAt(tab_strip, tab_point); | 145 gfx::Point tab_point(swipe_x_, swipe_y_); |
| 105 if (new_tab && !new_tab->IsActive()) { | 146 views::View::ConvertPointToTarget(tab_strip, initial_tab, &tab_point); |
| 106 int new_index = tab_strip->GetModelIndexOfTab(new_tab); | 147 Tab* new_tab = tab_strip->GetTabAt(initial_tab, tab_point); |
| 107 browser->tab_strip_model()->ActivateTabAt(new_index, true); | 148 if (!new_tab) |
| 149 return; | |
| 150 | |
| 151 int new_index = tab_strip->GetModelIndexOfTab(new_tab); | |
| 152 if (new_index != highlighted_tab_) { | |
| 153 if (activate_timer_.IsRunning()) { | |
| 154 activate_timer_.Reset(); | |
| 155 } else { | |
| 156 activate_timer_.Start(FROM_HERE, | |
| 157 activation_delay_, | |
| 158 base::Bind(&TabScrubber::FinishScrub, | |
| 159 weak_ptr_factory_.GetWeakPtr(), | |
| 160 true)); | |
| 161 } | |
| 108 } | 162 } |
| 109 | 163 |
| 110 event->StopPropagation(); | 164 if (highlighted_tab_ != new_index) { |
|
sky
2013/01/28 19:25:29
Merge with if on 152 (since they're the same).
DaveMoore
2013/01/29 01:11:41
Done.
| |
| 165 if (highlighted_tab_ != -1) { | |
| 166 Tab* tab = tab_strip->tab_at(highlighted_tab_); | |
| 167 tab->hover_controller()->HideImmediately(); | |
| 168 } | |
| 169 if (new_index == browser->tab_strip_model()->active_index()) { | |
| 170 highlighted_tab_ = -1; | |
| 171 } else { | |
| 172 highlighted_tab_ = new_index; | |
| 173 new_tab->hover_controller()->Show(views::GlowHoverController::PRONOUNCED); | |
| 174 } | |
| 175 } | |
| 176 if (highlighted_tab_ != -1) { | |
| 177 gfx::Point hover_point(swipe_x_, swipe_y_); | |
| 178 views::View::ConvertPointToTarget(tab_strip, new_tab, &hover_point); | |
| 179 new_tab->hover_controller()->SetLocation(hover_point); | |
| 180 } | |
| 111 } | 181 } |
| 112 | 182 |
| 113 void TabScrubber::Observe(int type, | 183 void TabScrubber::Observe(int type, |
| 114 const content::NotificationSource& source, | 184 const content::NotificationSource& source, |
| 115 const content::NotificationDetails& details) { | 185 const content::NotificationDetails& details) { |
| 116 DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSING && | 186 if (content::Source<Browser>(source).ptr() == browser_) |
| 117 content::Source<Browser>(source).ptr() == browser_); | 187 FinishScrub(false); |
| 118 StopScrubbing(); | 188 browser_ = NULL; |
| 119 } | 189 } |
| 120 | 190 |
| 121 Browser* TabScrubber::GetActiveBrowser() { | 191 Browser* TabScrubber::GetActiveBrowser() { |
| 122 aura::Window* active_window = ash::wm::GetActiveWindow(); | 192 aura::Window* active_window = ash::wm::GetActiveWindow(); |
| 123 if (!active_window) | 193 if (!active_window) |
| 124 return NULL; | 194 return NULL; |
| 125 | 195 |
| 126 Browser* browser = chrome::FindBrowserWithWindow(active_window); | 196 Browser* browser = chrome::FindBrowserWithWindow(active_window); |
| 127 if (!browser || browser->type() != Browser::TYPE_TABBED) | 197 if (!browser || browser->type() != Browser::TYPE_TABBED) |
| 128 return NULL; | 198 return NULL; |
| 129 | 199 |
| 130 return browser; | 200 return browser; |
| 131 } | 201 } |
| 202 void TabScrubber::FinishScrub(bool activate) { | |
| 203 if (activate_timer_.IsRunning()) | |
|
sky
2013/01/28 19:25:29
Its safe to always invoke Stop.
DaveMoore
2013/01/29 01:11:41
Done.
| |
| 204 activate_timer_.Stop(); | |
| 132 | 205 |
| 133 void TabScrubber::StopScrubbing() { | 206 if (browser_) { |
| 134 if (!scrubbing_) | 207 BrowserView* browser_view = |
| 135 return; | 208 BrowserView::GetBrowserViewForNativeWindow( |
| 209 browser_->window()->GetNativeWindow()); | |
| 210 if (activate && highlighted_tab_ != -1) { | |
| 211 Tab* tab = browser_view->tabstrip()->tab_at(highlighted_tab_); | |
| 212 tab->hover_controller()->HideImmediately(); | |
| 213 browser_->tab_strip_model()->ActivateTabAt(highlighted_tab_, true); | |
| 214 } | |
| 215 if (!cancel_immersive_reveal_timer_.IsRunning() && | |
| 216 should_cancel_immersive_reveal_) { | |
| 217 cancel_immersive_reveal_timer_.Start( | |
| 218 FROM_HERE, | |
| 219 base::TimeDelta::FromMilliseconds(kCancelImmersiveRevelDelayMS), | |
| 220 base:: Bind(&TabScrubber::CancelImmersiveReveal, | |
| 221 weak_ptr_factory_.GetWeakPtr())); | |
| 222 } | |
| 223 } | |
| 224 swipe_x_ = -1; | |
| 225 swipe_y_ = -1; | |
| 226 scrubbing_ = false; | |
| 227 highlighted_tab_ = -1; | |
| 228 } | |
| 136 | 229 |
| 137 registrar_.Remove( | 230 void TabScrubber::CancelImmersiveReveal() { |
| 138 this, | 231 if (cancel_immersive_reveal_timer_.IsRunning()) |
| 139 chrome::NOTIFICATION_BROWSER_CLOSING, | 232 cancel_immersive_reveal_timer_.Stop(); |
| 140 content::Source<Browser>(browser_)); | 233 if (browser_ && should_cancel_immersive_reveal_) { |
| 141 scrubbing_ = false; | 234 BrowserView* browser_view = |
| 142 browser_ = NULL; | 235 BrowserView::GetBrowserViewForNativeWindow( |
| 236 browser_->window()->GetNativeWindow()); | |
| 237 browser_view->immersive_mode_controller()->CancelReveal(); | |
| 238 } | |
| 239 should_cancel_immersive_reveal_ = false; | |
| 143 } | 240 } |
| OLD | NEW |