Chromium Code Reviews| Index: ash/system/toast/toast_manager.cc |
| diff --git a/ash/system/toast/toast_manager.cc b/ash/system/toast/toast_manager.cc |
| index 86f8653be2f0e51c9499bf7b00f44397c40c6b94..6b911737250fb23e91740ef8fbf49384cf9da644 100644 |
| --- a/ash/system/toast/toast_manager.cc |
| +++ b/ash/system/toast/toast_manager.cc |
| @@ -21,15 +21,42 @@ ToastManager::ToastManager() : weak_ptr_factory_(this) {} |
| ToastManager::~ToastManager() {} |
| -void ToastManager::Show(const std::string& text, uint64_t duration_ms) { |
| - queue_.emplace(std::make_pair(text, duration_ms)); |
| +void ToastManager::Show(const ToastData& data) { |
| + const std::string& id = data.id; |
| + DCHECK(!id.empty()); |
| + |
| + Queue::iterator existing_toast = |
| + 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.
|
| + [id](const QueueElement& e) { return e.first == id; }); |
| + |
| + if (toast_id_ == id) { |
| + // TODO(yoshiki): Replaces the toast; |
| + } else if (existing_toast == std::end(queue_)) { |
| + 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.
|
| + } else { |
| + existing_toast->second = data; |
| + } |
| if (queue_.size() == 1 && overlay_ == nullptr) |
| ShowLatest(); |
| } |
| +void ToastManager::Cancel(const std::string& id) { |
| + if (id == toast_id_) { |
| + overlay_->Show(false); |
| + return; |
| + } |
| + |
| + Queue::iterator cancelled_toast = |
| + std::find_if(std::begin(queue_), std::end(queue_), |
| + [id](const QueueElement& data) { return data.first == id; }); |
| + if (cancelled_toast != std::end(queue_)) |
| + queue_.erase(cancelled_toast); |
| +} |
| + |
| void ToastManager::OnClosed() { |
| overlay_.reset(); |
| + toast_id_.clear(); |
| // Show the next toast if available. |
| if (queue_.size() != 0) |
| @@ -39,24 +66,25 @@ void ToastManager::OnClosed() { |
| void ToastManager::ShowLatest() { |
| DCHECK(!overlay_); |
| - auto data = queue_.front(); |
| - uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); |
| + auto data = queue_.front().second; |
| + uint64_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs); |
| - toast_id_++; |
| + toast_id_ = data.id; |
| + 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.
|
| - overlay_.reset(new ToastOverlay(this, data.first /* text */)); |
| + overlay_.reset(new ToastOverlay(this, data.text /* text */)); |
| overlay_->Show(true); |
| base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, |
| - weak_ptr_factory_.GetWeakPtr(), toast_id_), |
| + weak_ptr_factory_.GetWeakPtr(), serial_), |
| base::TimeDelta::FromMilliseconds(duration_ms)); |
| - queue_.pop(); |
| + queue_.pop_front(); |
| } |
| -void ToastManager::OnDurationPassed(int toast_id) { |
| - if (overlay_ && toast_id_ == toast_id) |
| +void ToastManager::OnDurationPassed(int toast_number) { |
| + if (overlay_ && serial_ == toast_number) |
| overlay_->Show(false); |
| } |