Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #import "ui/views/controls/scrollbar/cocoa_scroll_bar.h" | 5 #import "ui/views/controls/scrollbar/cocoa_scroll_bar.h" |
| 6 | 6 |
| 7 #import "base/mac/sdk_forward_declarations.h" | 7 #import "base/mac/sdk_forward_declarations.h" |
| 8 #include "third_party/skia/include/core/SkColor.h" | 8 #include "third_party/skia/include/core/SkColor.h" |
| 9 #include "third_party/skia/include/effects/SkGradientShader.h" | 9 #include "third_party/skia/include/effects/SkGradientShader.h" |
| 10 #include "ui/compositor/layer.h" | 10 #include "ui/compositor/layer.h" |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 // CocoaScrollBar class | 161 // CocoaScrollBar class |
| 162 | 162 |
| 163 CocoaScrollBar::CocoaScrollBar(bool horizontal) | 163 CocoaScrollBar::CocoaScrollBar(bool horizontal) |
| 164 : BaseScrollBar(horizontal, new CocoaScrollBarThumb(this)), | 164 : BaseScrollBar(horizontal, new CocoaScrollBarThumb(this)), |
| 165 hide_scrollbar_timer_( | 165 hide_scrollbar_timer_( |
| 166 FROM_HERE, | 166 FROM_HERE, |
| 167 base::TimeDelta::FromMilliseconds(kScrollbarHideTimeoutMs), | 167 base::TimeDelta::FromMilliseconds(kScrollbarHideTimeoutMs), |
| 168 base::Bind(&CocoaScrollBar::HideScrollbar, base::Unretained(this)), | 168 base::Bind(&CocoaScrollBar::HideScrollbar, base::Unretained(this)), |
| 169 false), | 169 false), |
| 170 thickness_animation_(this), | 170 thickness_animation_(this), |
| 171 last_contents_scroll_offset_(0), | |
| 171 is_expanded_(false), | 172 is_expanded_(false), |
| 172 did_start_dragging_(false) { | 173 did_start_dragging_(false) { |
| 173 bridge_.reset([[ViewsScrollbarBridge alloc] initWithDelegate:this]); | 174 bridge_.reset([[ViewsScrollbarBridge alloc] initWithDelegate:this]); |
| 174 scroller_style_ = [ViewsScrollbarBridge getPreferredScrollerStyle]; | 175 scroller_style_ = [ViewsScrollbarBridge getPreferredScrollerStyle]; |
| 175 | 176 |
| 176 thickness_animation_.SetSlideDuration(kExpandDurationMs); | 177 thickness_animation_.SetSlideDuration(kExpandDurationMs); |
| 177 | 178 |
| 178 SetPaintToLayer(true); | 179 SetPaintToLayer(true); |
| 179 has_scrolltrack_ = scroller_style_ == NSScrollerStyleLegacy; | 180 has_scrolltrack_ = scroller_style_ == NSScrollerStyleLegacy; |
| 180 layer()->SetOpacity(scroller_style_ == NSScrollerStyleOverlay ? 0.0f : 1.0f); | 181 layer()->SetOpacity(scroller_style_ == NSScrollerStyleOverlay ? 0.0f : 1.0f); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 315 } | 316 } |
| 316 | 317 |
| 317 hide_scrollbar_timer_.Reset(); | 318 hide_scrollbar_timer_.Reset(); |
| 318 } | 319 } |
| 319 | 320 |
| 320 void CocoaScrollBar::OnMouseExited(const ui::MouseEvent& event) { | 321 void CocoaScrollBar::OnMouseExited(const ui::MouseEvent& event) { |
| 321 ResetOverlayScrollbar(); | 322 ResetOverlayScrollbar(); |
| 322 } | 323 } |
| 323 | 324 |
| 324 ////////////////////////////////////////////////////////////////// | 325 ////////////////////////////////////////////////////////////////// |
| 325 // CocoaScrollBar::BaseScrollBar: | 326 // CocoaScrollBar::ScrollBar: |
| 326 | 327 |
| 327 void CocoaScrollBar::ScrollToPosition(int position) { | 328 void CocoaScrollBar::Update(int viewport_size, |
| 328 BaseScrollBar::ScrollToPosition(position); | 329 int content_size, |
| 330 int contents_scroll_offset) { | |
| 331 // TODO(tapted): Pass in overscroll amounts from the Layer and "Squish" the | |
| 332 // scroller thumb accordingly. | |
| 333 BaseScrollBar::Update(viewport_size, content_size, contents_scroll_offset); | |
| 334 | |
| 335 // Only reveal the scroller when |contents_scroll_offset| changes. Note this | |
| 336 // is different to GetPosition() which can change due to layout. A layout | |
| 337 // change can also change the offset; show the scroller in these cases. This | |
| 338 // is consistent with WebContents (Cocoa will also show a scroller with any | |
| 339 // mouse-initiated layout, but not programmatic size changes). | |
| 340 if (contents_scroll_offset == last_contents_scroll_offset_) | |
| 341 return; | |
| 342 | |
| 343 last_contents_scroll_offset_ = contents_scroll_offset; | |
| 329 | 344 |
| 330 if (GetCocoaScrollBarThumb()->IsStatePressed()) | 345 if (GetCocoaScrollBarThumb()->IsStatePressed()) |
| 331 did_start_dragging_ = true; | 346 did_start_dragging_ = true; |
| 332 | 347 |
| 333 if (scroller_style_ == NSScrollerStyleOverlay) { | 348 if (scroller_style_ == NSScrollerStyleOverlay) { |
| 334 ShowScrollbar(); | 349 ShowScrollbar(); |
| 335 hide_scrollbar_timer_.Reset(); | 350 hide_scrollbar_timer_.Reset(); |
| 336 } | 351 } |
| 337 } | 352 } |
| 338 | 353 |
| 354 void CocoaScrollBar::ObserveScrollEvent(const ui::ScrollEvent& event) { | |
| 355 // Do nothing if the delayed hide timer is running. This means there has been | |
| 356 // some recent scrolling in this direction already. | |
| 357 if (scroller_style_ != NSScrollerStyleOverlay || | |
| 358 hide_scrollbar_timer_.IsRunning()) | |
|
spqchan
2016/11/14 23:27:58
multiline if statement needs curly brackets
tapted
2016/11/15 12:22:23
Done. (I think technically the condition isn't a f
| |
| 359 return; | |
| 360 | |
| 361 // Otherwise, when starting the event stream, show an overlay scrollbar to | |
| 362 // indicate possible scroll directions, but do not start the hide timer. | |
| 363 if (event.momentum_phase() == ui::EventMomentumPhase::MAY_BEGIN) { | |
| 364 // Show only if the direction isn't yet known. | |
| 365 if (event.x_offset() == 0 && event.y_offset() == 0) | |
| 366 ShowScrollbar(); | |
| 367 return; | |
| 368 } | |
| 369 | |
| 370 // If the direction matches, do nothing. This is needed in addition to the | |
| 371 // hide timer check because Update() is called asynchronously, after event | |
| 372 // processing. So when |event| is the first event in a particular direction | |
| 373 // the hide timer will not have started. | |
| 374 if ((IsHorizontal() ? event.x_offset() : event.y_offset()) != 0) | |
| 375 return; | |
| 376 | |
| 377 // Otherwise, scrolling has started, but not in this scroller direction. If | |
| 378 // already faded out, don't start another fade animation since that would | |
| 379 // immediately finish the first fade animation. | |
| 380 if (layer()->GetTargetOpacity() != 0) { | |
| 381 // If canceling rather than picking a direction, fade out after a delay. | |
| 382 if (event.momentum_phase() == ui::EventMomentumPhase::END) | |
| 383 hide_scrollbar_timer_.Reset(); | |
| 384 else | |
| 385 HideScrollbar(); // Fade out immediately. | |
| 386 } | |
| 387 } | |
| 388 | |
| 339 ////////////////////////////////////////////////////////////////// | 389 ////////////////////////////////////////////////////////////////// |
| 340 // CocoaScrollBar::ViewsScrollbarBridge: | 390 // CocoaScrollBar::ViewsScrollbarBridge: |
| 341 | 391 |
| 342 void CocoaScrollBar::OnScrollerStyleChanged() { | 392 void CocoaScrollBar::OnScrollerStyleChanged() { |
| 343 NSScrollerStyle scroller_style = | 393 NSScrollerStyle scroller_style = |
| 344 [ViewsScrollbarBridge getPreferredScrollerStyle]; | 394 [ViewsScrollbarBridge getPreferredScrollerStyle]; |
| 345 if (scroller_style_ == scroller_style) | 395 if (scroller_style_ == scroller_style) |
| 346 return; | 396 return; |
| 347 | 397 |
| 348 // Cancel all of the animations. | 398 // Cancel all of the animations. |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 | 528 |
| 479 void CocoaScrollBar::SetScrolltrackVisible(bool visible) { | 529 void CocoaScrollBar::SetScrolltrackVisible(bool visible) { |
| 480 has_scrolltrack_ = visible; | 530 has_scrolltrack_ = visible; |
| 481 SchedulePaint(); | 531 SchedulePaint(); |
| 482 } | 532 } |
| 483 | 533 |
| 484 CocoaScrollBarThumb* CocoaScrollBar::GetCocoaScrollBarThumb() const { | 534 CocoaScrollBarThumb* CocoaScrollBar::GetCocoaScrollBarThumb() const { |
| 485 return static_cast<CocoaScrollBarThumb*>(GetThumb()); | 535 return static_cast<CocoaScrollBarThumb*>(GetThumb()); |
| 486 } | 536 } |
| 487 | 537 |
| 538 // static | |
| 539 base::Timer* BaseScrollBar::GetHideTimerForTest(BaseScrollBar* scroll_bar) { | |
| 540 return &static_cast<CocoaScrollBar*>(scroll_bar)->hide_scrollbar_timer_; | |
| 541 } | |
| 542 | |
| 488 } // namespace views | 543 } // namespace views |
| OLD | NEW |