blob: 8a140882b073a78e1b4c31809f7827b63106c382 [file] [log] [blame]
cjgrant2a755872017-05-13 19:30:211// 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 Vollick4e26ac322017-07-11 14:58:215#include "chrome/browser/vr/elements/loading_indicator.h"
cjgrant2a755872017-05-13 19:30:216
7#include "base/memory/ptr_util.h"
Ian Vollick4e26ac322017-07-11 14:58:218#include "chrome/browser/vr/elements/loading_indicator_texture.h"
cjgrant2a755872017-05-13 19:30:219
Ian Vollick4e26ac322017-07-11 14:58:2110namespace vr {
cjgrant2a755872017-05-13 19:30:2111
12namespace {
13
14static constexpr int kVisibilityTimeoutMs = 200;
15}
16
17LoadingIndicator::LoadingIndicator(int preferred_width)
18 : TexturedElement(preferred_width),
klausw66af1342017-05-18 18:19:1419 texture_(base::MakeUnique<LoadingIndicatorTexture>()) {
20 set_visible(false);
21}
cjgrant2a755872017-05-13 19:30:2122
23LoadingIndicator::~LoadingIndicator() = default;
24
25UiTexture* LoadingIndicator::GetTexture() const {
26 return texture_.get();
27}
28
29void LoadingIndicator::SetEnabled(bool enabled) {
30 if (enabled_ == enabled)
31 return;
32 enabled_ = enabled;
cjgrant2a755872017-05-13 19:30:2133 SetVisibility();
34}
35
36void 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
46void LoadingIndicator::SetLoadProgress(float progress) {
47 texture_->SetLoadProgress(progress);
48}
49
50void 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
60void LoadingIndicator::SetVisibility() {
61 set_visible(enabled_ && (loading_ || visibility_timer_.IsRunning()));
62}
63
Ian Vollick4e26ac322017-07-11 14:58:2164} // namespace vr