Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(482)

Unified Diff: ash/system/toast/toast_manager.cc

Issue 1978313002: Support cancelling toast (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use std::vector back instead of std::list Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..a296f5210a0f49d517ab2ca3a93f081a4530da29 100644
--- a/ash/system/toast/toast_manager.cc
+++ b/ash/system/toast/toast_manager.cc
@@ -21,15 +21,45 @@ 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());
+
+ if (current_toast_id_ == id) {
+ // TODO(yoshiki): Replaces the visible toast.
+ return;
+ }
+
+ auto existing_toast =
+ std::find_if(queue_.begin(), queue_.end(),
+ [id](const ToastData& data) { return data.id == id; });
+
+ if (existing_toast == queue_.end()) {
+ queue_.emplace_back(data);
+ } else {
+ *existing_toast = data;
+ }
if (queue_.size() == 1 && overlay_ == nullptr)
ShowLatest();
}
+void ToastManager::Cancel(const std::string& id) {
+ if (id == current_toast_id_) {
+ overlay_->Show(false);
+ return;
+ }
+
+ auto cancelled_toast =
+ std::find_if(queue_.begin(), queue_.end(),
+ [id](const ToastData& data) { return data.id == id; });
hidehiko 2016/05/20 07:46:53 &id
yoshiki 2016/05/20 08:14:55 Done.
hidehiko 2016/05/20 08:52:41 Not done?
yoshiki 2016/05/20 09:30:31 I did the first one but forgot the second one (thi
+ if (cancelled_toast != queue_.end())
+ queue_.erase(cancelled_toast);
+}
+
void ToastManager::OnClosed() {
overlay_.reset();
+ current_toast_id_.clear();
// Show the next toast if available.
if (queue_.size() != 0)
hidehiko 2016/05/20 07:46:53 nit: !queue_.empty()
yoshiki 2016/05/20 08:14:54 Done.
@@ -39,24 +69,25 @@ void ToastManager::OnClosed() {
void ToastManager::ShowLatest() {
DCHECK(!overlay_);
- auto data = queue_.front();
- uint64_t duration_ms = std::max(data.second, kMinimumDurationMs);
+ const ToastData& data = queue_.front(); // Can be used until pop_front().
hidehiko 2016/05/20 07:46:53 I'd recommend: ToastData data = std::move(queue_.
yoshiki 2016/05/20 08:14:54 Done.
hidehiko 2016/05/20 08:52:41 I meant, getting rid of ref, so that you can pop j
yoshiki 2016/05/20 09:30:30 Oh, I see. Done.
+ uint64_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs);
- toast_id_++;
+ current_toast_id_ = data.id;
+ serial_++;
hidehiko 2016/05/20 07:46:53 Because you manage the toast UI by ID (in Cancel),
yoshiki 2016/05/20 08:14:55 Same ID may come at the timing after the toast is
- overlay_.reset(new ToastOverlay(this, data.first /* text */));
+ overlay_.reset(new ToastOverlay(this, data.text /* text */));
hidehiko 2016/05/20 07:46:53 /* text */ looks unnecessary now.
yoshiki 2016/05/20 08:14:54 Done.
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);
}

Powered by Google App Engine
This is Rietveld 408576698