| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/system/toast/toast_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/threading/thread_task_runner_handle.h" | |
| 10 | |
| 11 namespace ash { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Minimum duration for a toast to be visible (in millisecond). | |
| 16 const int32_t kMinimumDurationMs = 200; | |
| 17 | |
| 18 } // anonymous namespace | |
| 19 | |
| 20 ToastManager::ToastManager() : weak_ptr_factory_(this) {} | |
| 21 | |
| 22 ToastManager::~ToastManager() {} | |
| 23 | |
| 24 void ToastManager::Show(const ToastData& data) { | |
| 25 const std::string& id = data.id; | |
| 26 DCHECK(!id.empty()); | |
| 27 | |
| 28 if (current_toast_id_ == id) { | |
| 29 // TODO(yoshiki): Replaces the visible toast. | |
| 30 return; | |
| 31 } | |
| 32 | |
| 33 auto existing_toast = | |
| 34 std::find_if(queue_.begin(), queue_.end(), | |
| 35 [&id](const ToastData& data) { return data.id == id; }); | |
| 36 | |
| 37 if (existing_toast == queue_.end()) { | |
| 38 queue_.emplace_back(data); | |
| 39 } else { | |
| 40 *existing_toast = data; | |
| 41 } | |
| 42 | |
| 43 if (queue_.size() == 1 && overlay_ == nullptr) | |
| 44 ShowLatest(); | |
| 45 } | |
| 46 | |
| 47 void ToastManager::Cancel(const std::string& id) { | |
| 48 if (id == current_toast_id_) { | |
| 49 overlay_->Show(false); | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 auto cancelled_toast = | |
| 54 std::find_if(queue_.begin(), queue_.end(), | |
| 55 [&id](const ToastData& data) { return data.id == id; }); | |
| 56 if (cancelled_toast != queue_.end()) | |
| 57 queue_.erase(cancelled_toast); | |
| 58 } | |
| 59 | |
| 60 void ToastManager::OnClosed() { | |
| 61 overlay_.reset(); | |
| 62 current_toast_id_.clear(); | |
| 63 | |
| 64 // Show the next toast if available. | |
| 65 if (!queue_.empty()) | |
| 66 ShowLatest(); | |
| 67 } | |
| 68 | |
| 69 void ToastManager::ShowLatest() { | |
| 70 DCHECK(!overlay_); | |
| 71 | |
| 72 const ToastData data = std::move(queue_.front()); | |
| 73 queue_.pop_front(); | |
| 74 | |
| 75 current_toast_id_ = data.id; | |
| 76 serial_++; | |
| 77 | |
| 78 overlay_.reset(new ToastOverlay(this, data.text, data.dismiss_text)); | |
| 79 overlay_->Show(true); | |
| 80 | |
| 81 if (data.duration_ms != ToastData::kInfiniteDuration) { | |
| 82 int32_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs); | |
| 83 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
| 84 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, | |
| 85 weak_ptr_factory_.GetWeakPtr(), serial_), | |
| 86 base::TimeDelta::FromMilliseconds(duration_ms)); | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 void ToastManager::OnDurationPassed(int toast_number) { | |
| 91 if (overlay_ && serial_ == toast_number) | |
| 92 overlay_->Show(false); | |
| 93 } | |
| 94 | |
| 95 } // namespace ash | |
| OLD | NEW |