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

Side by Side Diff: ash/common/system/toast/toast_manager.cc

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase Created 3 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
« no previous file with comments | « ash/common/system/toast/toast_manager.h ('k') | ash/common/system/toast/toast_overlay.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/common/system/toast/toast_manager.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/threading/thread_task_runner_handle.h"
12
13 namespace ash {
14
15 namespace {
16
17 // Minimum duration for a toast to be visible (in millisecond).
18 const int32_t kMinimumDurationMs = 200;
19
20 } // anonymous namespace
21
22 ToastManager::ToastManager() : weak_ptr_factory_(this) {}
23
24 ToastManager::~ToastManager() {}
25
26 void ToastManager::Show(const ToastData& data) {
27 const std::string& id = data.id;
28 DCHECK(!id.empty());
29
30 if (current_toast_id_ == id) {
31 // TODO(yoshiki): Replaces the visible toast.
32 return;
33 }
34
35 auto existing_toast =
36 std::find_if(queue_.begin(), queue_.end(),
37 [&id](const ToastData& data) { return data.id == id; });
38
39 if (existing_toast == queue_.end()) {
40 queue_.emplace_back(data);
41 } else {
42 *existing_toast = data;
43 }
44
45 if (queue_.size() == 1 && overlay_ == nullptr)
46 ShowLatest();
47 }
48
49 void ToastManager::Cancel(const std::string& id) {
50 if (id == current_toast_id_) {
51 overlay_->Show(false);
52 return;
53 }
54
55 auto cancelled_toast =
56 std::find_if(queue_.begin(), queue_.end(),
57 [&id](const ToastData& data) { return data.id == id; });
58 if (cancelled_toast != queue_.end())
59 queue_.erase(cancelled_toast);
60 }
61
62 void ToastManager::OnClosed() {
63 overlay_.reset();
64 current_toast_id_.clear();
65
66 // Show the next toast if available.
67 if (!queue_.empty())
68 ShowLatest();
69 }
70
71 void ToastManager::ShowLatest() {
72 DCHECK(!overlay_);
73
74 const ToastData data = std::move(queue_.front());
75 queue_.pop_front();
76
77 current_toast_id_ = data.id;
78 serial_++;
79
80 overlay_.reset(new ToastOverlay(this, data.text, data.dismiss_text));
81 overlay_->Show(true);
82
83 if (data.duration_ms != ToastData::kInfiniteDuration) {
84 int32_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs);
85 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
86 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed,
87 weak_ptr_factory_.GetWeakPtr(), serial_),
88 base::TimeDelta::FromMilliseconds(duration_ms));
89 }
90 }
91
92 void ToastManager::OnDurationPassed(int toast_number) {
93 if (overlay_ && serial_ == toast_number)
94 overlay_->Show(false);
95 }
96
97 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/toast/toast_manager.h ('k') | ash/common/system/toast/toast_overlay.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698