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

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: . 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
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 Queue::iterator existing_toast =
oshima 2016/05/18 16:59:44 optional: you may use auto and remove using Queue.
yoshiki 2016/05/19 09:14:12 Done.
29 std::find_if(queue_.begin(), queue_.end(),
30 [id](const ToastData& data) { return data.id == id; });
31
32 if (current_toast_id_ == id) {
33 // TODO(yoshiki): Replaces the toast;
oshima 2016/05/18 16:59:44 note: you can do this before looking up the toast.
yoshiki 2016/05/19 09:14:12 Done.
34 } else if (existing_toast == queue_.end()) {
35 queue_.emplace_back(data);
36 } else {
37 *existing_toast = data;
38 }
26 39
27 if (queue_.size() == 1 && overlay_ == nullptr) 40 if (queue_.size() == 1 && overlay_ == nullptr)
28 ShowLatest(); 41 ShowLatest();
29 } 42 }
30 43
44 void ToastManager::Cancel(const std::string& id) {
45 if (id == current_toast_id_) {
46 overlay_->Show(false);
47 return;
48 }
49
50 Queue::iterator cancelled_toast =
51 std::find_if(queue_.begin(), queue_.end(),
52 [id](const ToastData& data) { return data.id == id; });
53 if (cancelled_toast != queue_.end())
54 queue_.erase(cancelled_toast);
55 }
56
31 void ToastManager::OnClosed() { 57 void ToastManager::OnClosed() {
32 overlay_.reset(); 58 overlay_.reset();
59 current_toast_id_.clear();
33 60
34 // Show the next toast if available. 61 // Show the next toast if available.
35 if (queue_.size() != 0) 62 if (queue_.size() != 0)
36 ShowLatest(); 63 ShowLatest();
37 } 64 }
38 65
39 void ToastManager::ShowLatest() { 66 void ToastManager::ShowLatest() {
40 DCHECK(!overlay_); 67 DCHECK(!overlay_);
41 68
42 auto data = queue_.front(); 69 const ToastData& data = queue_.front(); // Can be used until pop_front().
43 uint64_t duration_ms = std::max(data.second, kMinimumDurationMs); 70 uint64_t duration_ms = std::max(data.duration_ms, kMinimumDurationMs);
44 71
45 toast_id_++; 72 current_toast_id_ = data.id;
73 serial_++;
46 74
47 overlay_.reset(new ToastOverlay(this, data.first /* text */)); 75 overlay_.reset(new ToastOverlay(this, data.text /* text */));
48 overlay_->Show(true); 76 overlay_->Show(true);
49 77
50 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 78 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
51 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed, 79 FROM_HERE, base::Bind(&ToastManager::OnDurationPassed,
52 weak_ptr_factory_.GetWeakPtr(), toast_id_), 80 weak_ptr_factory_.GetWeakPtr(), serial_),
53 base::TimeDelta::FromMilliseconds(duration_ms)); 81 base::TimeDelta::FromMilliseconds(duration_ms));
54 82
55 queue_.pop(); 83 queue_.pop_front();
56 } 84 }
57 85
58 void ToastManager::OnDurationPassed(int toast_id) { 86 void ToastManager::OnDurationPassed(int toast_number) {
59 if (overlay_ && toast_id_ == toast_id) 87 if (overlay_ && serial_ == toast_number)
60 overlay_->Show(false); 88 overlay_->Show(false);
61 } 89 }
62 90
63 } // namespace ash 91 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698