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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "ash/system/toast/toast_manager.h"
6
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/thread_task_runner_handle.h"
10
11 namespace ash {
12
13 namespace {
14
15 // Minimum duration for a toast to be visible (in millisecond).
16 uint64_t kMinimumDurationMs = 200;
17
18 } // anonymous namespace
19
20 ToastManager::ToastManager() {}
21
22 ToastManager::~ToastManager() {}
23
24 void ToastManager::Show(const std::string& text, uint64_t duration_ms) {
25 queue_.emplace(std::make_pair(text, duration_ms));
26
27 if (queue_.size() == 1 && overlay_ == nullptr)
28 ShowLatest();
29 }
30
31 void ToastManager::OnClosed() {
32 overlay_.reset();
33
34 // Show the next toast if available.
35 if (queue_.size() != 0)
36 ShowLatest();
37 }
38
39 void ToastManager::ShowLatest() {
40 DCHECK(!overlay_);
41
42 auto data = queue_.front();
43 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs);
44
45 toast_id_++;
46
47 overlay_.reset(new ToastOverlay(this, data.first /* text */));
48 overlay_->Show(true);
49
50 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
51 FROM_HERE,
52 base::Bind(&ToastManager::OnDurationPassed,
53 base::Unretained(this), // |this| is never destroyed.
54 toast_id_),
55 base::TimeDelta::FromMilliseconds(duration_ms));
56
57 queue_.pop();
58 }
59
60 void ToastManager::OnDurationPassed(int toast_id) {
61 if (overlay_ && toast_id_ == toast_id)
62 overlay_->Show(false);
63 }
64
65 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698