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