cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Ian Vollick | 4e26ac32 | 2017-07-11 14:58:21 | [diff] [blame^] | 5 | #include "chrome/browser/vr/elements/loading_indicator.h" |
cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 6 | |
| 7 | #include "base/memory/ptr_util.h" |
Ian Vollick | 4e26ac32 | 2017-07-11 14:58:21 | [diff] [blame^] | 8 | #include "chrome/browser/vr/elements/loading_indicator_texture.h" |
cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 9 | |
Ian Vollick | 4e26ac32 | 2017-07-11 14:58:21 | [diff] [blame^] | 10 | namespace vr { |
cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 11 | |
| 12 | namespace { |
| 13 | |
| 14 | static constexpr int kVisibilityTimeoutMs = 200; |
| 15 | } |
| 16 | |
| 17 | LoadingIndicator::LoadingIndicator(int preferred_width) |
| 18 | : TexturedElement(preferred_width), |
klausw | 66af134 | 2017-05-18 18:19:14 | [diff] [blame] | 19 | texture_(base::MakeUnique<LoadingIndicatorTexture>()) { |
| 20 | set_visible(false); |
| 21 | } |
cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 22 | |
| 23 | LoadingIndicator::~LoadingIndicator() = default; |
| 24 | |
| 25 | UiTexture* LoadingIndicator::GetTexture() const { |
| 26 | return texture_.get(); |
| 27 | } |
| 28 | |
| 29 | void LoadingIndicator::SetEnabled(bool enabled) { |
| 30 | if (enabled_ == enabled) |
| 31 | return; |
| 32 | enabled_ = enabled; |
cjgrant | 2a75587 | 2017-05-13 19:30:21 | [diff] [blame] | 33 | SetVisibility(); |
| 34 | } |
| 35 | |
| 36 | void LoadingIndicator::SetLoading(bool loading) { |
| 37 | if (loading_ == loading) |
| 38 | return; |
| 39 | loading_ = loading; |
| 40 | texture_->SetLoading(loading); |
| 41 | texture_->SetLoadProgress(0); |
| 42 | ResetVisibilityTimer(); |
| 43 | SetVisibility(); |
| 44 | } |
| 45 | |
| 46 | void LoadingIndicator::SetLoadProgress(float progress) { |
| 47 | texture_->SetLoadProgress(progress); |
| 48 | } |
| 49 | |
| 50 | void LoadingIndicator::ResetVisibilityTimer() { |
| 51 | if (enabled_ && !loading_) { |
| 52 | visibility_timer_.Start( |
| 53 | FROM_HERE, base::TimeDelta::FromMilliseconds(kVisibilityTimeoutMs), |
| 54 | this, &LoadingIndicator::SetVisibility); |
| 55 | } else { |
| 56 | visibility_timer_.Stop(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void LoadingIndicator::SetVisibility() { |
| 61 | set_visible(enabled_ && (loading_ || visibility_timer_.IsRunning())); |
| 62 | } |
| 63 | |
Ian Vollick | 4e26ac32 | 2017-07-11 14:58:21 | [diff] [blame^] | 64 | } // namespace vr |