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

Side by Side 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: Addressed comments 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 unified diff | Download patch
« no previous file with comments | « ash/system/toast/toast_manager.h ('k') | ash/system/toast/toast_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ash/system/toast/toast_manager.h" 5 #include "ash/system/toast/toast_manager.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 10
11 namespace ash { 11 namespace ash {
12 12
13 namespace { 13 namespace {
14 14
15 // Minimum duration for a toast to be visible (in millisecond). 15 // Minimum duration for a toast to be visible (in millisecond).
16 uint64_t kMinimumDurationMs = 200; 16 uint64_t kMinimumDurationMs = 200;
17 17
18 } // anonymous namespace 18 } // anonymous namespace
19 19
20 ToastManager::ToastManager() : weak_ptr_factory_(this) {} 20 ToastManager::ToastManager() : weak_ptr_factory_(this) {}
21 21
22 ToastManager::~ToastManager() {} 22 ToastManager::~ToastManager() {}
23 23
24 void ToastManager::Show(const std::string& text, uint64_t duration_ms) { 24 void ToastManager::Show(const ToastData& data) {
25 queue_.emplace(std::make_pair(text, duration_ms)); 25 const std::string& id = data.id;
26 DCHECK(!id.empty());
27
28 if (current_toast_id_ == id) {
29 // TODO(yoshiki): Replaces the visible toast.
30 return;
31 }
32
33 auto existing_toast =
34 std::find_if(queue_.begin(), queue_.end(),
35 [&id](const ToastData& data) { return data.id == id; });
36
37 if (existing_toast == queue_.end()) {
38 queue_.emplace_back(data);
39 } else {
40 *existing_toast = data;
41 }
26 42
27 if (queue_.size() == 1 && overlay_ == nullptr) 43 if (queue_.size() == 1 && overlay_ == nullptr)
28 ShowLatest(); 44 ShowLatest();
29 } 45 }
30 46
47 void ToastManager::Cancel(const std::string& id) {
48 if (id == current_toast_id_) {
49 overlay_->Show(false);
50 return;
51 }
52
53 auto cancelled_toast =
54 std::find_if(queue_.begin(), queue_.end(),
55 [id](const ToastData& data) { return data.id == id; });
dcheng 2016/05/20 08:16:33 (This should probably also capture by ref as well)
yoshiki 2016/05/20 09:30:31 Thank you for letting me know!
56 if (cancelled_toast != queue_.end())
57 queue_.erase(cancelled_toast);
58 }
59
31 void ToastManager::OnClosed() { 60 void ToastManager::OnClosed() {
32 overlay_.reset(); 61 overlay_.reset();
62 current_toast_id_.clear();
33 63
34 // Show the next toast if available. 64 // Show the next toast if available.
35 if (queue_.size() != 0) 65 if (!queue_.empty())
36 ShowLatest(); 66 ShowLatest();
37 } 67 }
38 68
39 void ToastManager::ShowLatest() { 69 void ToastManager::ShowLatest() {
40 DCHECK(!overlay_); 70 DCHECK(!overlay_);
41 71
42 auto data = queue_.front(); 72 const ToastData& data = std::move(queue_.front());
43 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); 73 uint64_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs);
44 74
45 toast_id_++; 75 current_toast_id_ = data.id;
76 serial_++;
46 77
47 overlay_.reset(new ToastOverlay(this, data.first /* text */)); 78 overlay_.reset(new ToastOverlay(this, data.text));
48 overlay_->Show(true); 79 overlay_->Show(true);
49 80
50 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 81 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
51 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, 82 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed,
52 weak_ptr_factory_.GetWeakPtr(), toast_id_), 83 weak_ptr_factory_.GetWeakPtr(), serial_),
53 base::TimeDelta::FromMilliseconds(duration_ms)); 84 base::TimeDelta::FromMilliseconds(duration_ms));
54 85
55 queue_.pop(); 86 queue_.pop_front();
56 } 87 }
57 88
58 void ToastManager::OnDurationPassed(int toast_id) { 89 void ToastManager::OnDurationPassed(int toast_number) {
59 if (overlay_ && toast_id_ == toast_id) 90 if (overlay_ && serial_ == toast_number)
60 overlay_->Show(false); 91 overlay_->Show(false);
61 } 92 }
62 93
63 } // namespace ash 94 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/toast/toast_manager.h ('k') | ash/system/toast/toast_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698