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

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

Issue 1782793002: Ash: Implement Toasts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comment Created 4 years, 9 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
new file mode 100644
index 0000000000000000000000000000000000000000..3093f8dfd4538024b0ca237f60a193d27bf4cc9f
--- /dev/null
+++ b/ash/system/toast/toast_manager.cc
@@ -0,0 +1,65 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ash/system/toast/toast_manager.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/thread_task_runner_handle.h"
+
+namespace ash {
+
+namespace {
+
+// Minimum duration for a toast to be visible (in millisecond).
+uint64_t kMinimumDurationMs = 200;
+
+} // anonymous namespace
+
+ToastManager::ToastManager() {}
+
+ToastManager::~ToastManager() {}
+
+void ToastManager::Show(const std::string& text, uint64_t duration_ms) {
+ queue_.emplace(std::make_pair(text, duration_ms));
+
+ if (queue_.size() == 1 && overlay_ == nullptr)
+ ShowLatest();
+}
+
+void ToastManager::OnClosed() {
+ overlay_.reset();
+
+ // Show the next toast if available.
+ if (queue_.size() != 0)
+ ShowLatest();
+}
+
+void ToastManager::ShowLatest() {
+ DCHECK(!overlay_);
+
+ auto data = queue_.front();
+ uint64_t duration_ms = std::max(data.second, kMinimumDurationMs);
+
+ toast_id_++;
+
+ overlay_.reset(new ToastOverlay(this, data.first /* text */));
+ overlay_->Show(true);
+
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&ToastManager::OnDurationPassed,
+ base::Unretained(this), // |this| is never destroyed.
+ toast_id_),
+ base::TimeDelta::FromMilliseconds(duration_ms));
+
+ queue_.pop();
+}
+
+void ToastManager::OnDurationPassed(int toast_id) {
+ if (overlay_ && toast_id_ == toast_id)
+ overlay_->Show(false);
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698