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 #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 Queue::iterator existing_toast = | |
| 29 std::find_if(std::begin(queue_), std::end(queue_), | |
|
dcheng
2016/05/17 02:57:25
queue_.begin(), queue_.end() is a little more stan
yoshiki
2016/05/17 06:25:21
Done.
| |
| 30 [id](const QueueElement& e) { return e.first == id; }); | |
| 31 | |
| 32 if (toast_id_ == id) { | |
| 33 // TODO(yoshiki): Replaces the toast; | |
| 34 } else if (existing_toast == std::end(queue_)) { | |
| 35 queue_.emplace_back(std::make_pair(id, data)); | |
|
dcheng
2016/05/17 02:57:25
queue_.emplace_back(id, data) is better: it avoids
yoshiki
2016/05/17 06:25:21
Done.
| |
| 36 } else { | |
| 37 existing_toast->second = data; | |
| 38 } | |
| 26 | 39 |
| 27 if (queue_.size() == 1 && overlay_ == nullptr) | 40 if (queue_.size() == 1 && overlay_ == nullptr) |
| 28 ShowLatest(); | 41 ShowLatest(); |
| 29 } | 42 } |
| 30 | 43 |
| 44 void ToastManager::Cancel(const std::string& id) { | |
| 45 if (id == toast_id_) { | |
| 46 overlay_->Show(false); | |
| 47 return; | |
| 48 } | |
| 49 | |
| 50 Queue::iterator cancelled_toast = | |
| 51 std::find_if(std::begin(queue_), std::end(queue_), | |
| 52 [id](const QueueElement& data) { return data.first == id; }); | |
| 53 if (cancelled_toast != std::end(queue_)) | |
| 54 queue_.erase(cancelled_toast); | |
| 55 } | |
| 56 | |
| 31 void ToastManager::OnClosed() { | 57 void ToastManager::OnClosed() { |
| 32 overlay_.reset(); | 58 overlay_.reset(); |
| 59 toast_id_.clear(); | |
| 33 | 60 |
| 34 // Show the next toast if available. | 61 // Show the next toast if available. |
| 35 if (queue_.size() != 0) | 62 if (queue_.size() != 0) |
| 36 ShowLatest(); | 63 ShowLatest(); |
| 37 } | 64 } |
| 38 | 65 |
| 39 void ToastManager::ShowLatest() { | 66 void ToastManager::ShowLatest() { |
| 40 DCHECK(!overlay_); | 67 DCHECK(!overlay_); |
| 41 | 68 |
| 42 auto data = queue_.front(); | 69 auto data = queue_.front().second; |
| 43 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); | 70 uint64_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs); |
| 44 | 71 |
| 45 toast_id_++; | 72 toast_id_ = data.id; |
| 73 serial_++; | |
|
oshima
2016/05/16 22:37:25
Can you just use id instead? Do we need both?
yoshiki
2016/05/17 06:25:21
I'm using an internal serial number because "id" i
oshima
2016/05/18 16:59:43
Acknowledged.
| |
| 46 | 74 |
| 47 overlay_.reset(new ToastOverlay(this, data.first /* text */)); | 75 overlay_.reset(new ToastOverlay(this, data.text /* text */)); |
| 48 overlay_->Show(true); | 76 overlay_->Show(true); |
| 49 | 77 |
| 50 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 78 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 51 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, | 79 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, |
| 52 weak_ptr_factory_.GetWeakPtr(), toast_id_), | 80 weak_ptr_factory_.GetWeakPtr(), serial_), |
| 53 base::TimeDelta::FromMilliseconds(duration_ms)); | 81 base::TimeDelta::FromMilliseconds(duration_ms)); |
| 54 | 82 |
| 55 queue_.pop(); | 83 queue_.pop_front(); |
| 56 } | 84 } |
| 57 | 85 |
| 58 void ToastManager::OnDurationPassed(int toast_id) { | 86 void ToastManager::OnDurationPassed(int toast_number) { |
| 59 if (overlay_ && toast_id_ == toast_id) | 87 if (overlay_ && serial_ == toast_number) |
| 60 overlay_->Show(false); | 88 overlay_->Show(false); |
| 61 } | 89 } |
| 62 | 90 |
| 63 } // namespace ash | 91 } // namespace ash |
| OLD | NEW |