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

Side by Side Diff: ash/system/toast/toast_overlay.cc

Issue 1782793002: Ash: Implement Toasts (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments 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_overlay.h"
6
7 #include "ash/shelf/shelf.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shell.h"
10 #include "ash/shell_window_ids.h"
11 #include "ash/wm/window_animations.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "grit/ash_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/gfx/canvas.h"
19 #include "ui/gfx/font_list.h"
20 #include "ui/views/border.h"
21 #include "ui/views/controls/button/label_button.h"
22 #include "ui/views/controls/label.h"
23 #include "ui/views/layout/box_layout.h"
24 #include "ui/views/view.h"
25 #include "ui/views/widget/widget.h"
26 #include "ui/views/widget/widget_delegate.h"
27
28 namespace ash {
29
30 namespace {
31
32 // Horizontal offset of the overlay from the bottom of the screen.
33 const int kVerticalOffset = 5;
34
35 // Font style used for modifier key labels.
36 const ui::ResourceBundle::FontStyle kTextFontStyle =
37 ui::ResourceBundle::MediumFont;
38
39 // Duration of slide animation when overlay is shown or hidden.
40 const int kSlideAnimationDurationMs = 100;
41
42 } // anonymous namespace
43
44 ///////////////////////////////////////////////////////////////////////////////
45 // ToastOverlayLabel
46 class ToastOverlayLabel : public views::Label {
47 public:
48 explicit ToastOverlayLabel(const std::string& label);
49 ~ToastOverlayLabel() override;
50
51 private:
52 DISALLOW_COPY_AND_ASSIGN(ToastOverlayLabel);
53 };
54
55 ToastOverlayLabel::ToastOverlayLabel(const std::string& label) {
56 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
57
58 SetText(base::UTF8ToUTF16(label));
59 SetHorizontalAlignment(gfx::ALIGN_LEFT);
60 SetFontList(rb->GetFontList(kTextFontStyle));
61 SetAutoColorReadabilityEnabled(false);
62 SetFocusable(false);
63 SetEnabledColor(SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF));
64 SetDisabledColor(SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF));
65 SetSubpixelRenderingEnabled(false);
66 }
67
68 ToastOverlayLabel::~ToastOverlayLabel() {}
69
70 ///////////////////////////////////////////////////////////////////////////////
71 // ToastOverlayButton
72 class ToastOverlayButton : public views::LabelButton {
73 public:
74 explicit ToastOverlayButton(const base::string16& label,
75 views::ButtonListener* listener);
76 ~ToastOverlayButton() override {}
77
78 void NotifyClick(const ui::Event& event) override {
79 views::LabelButton::NotifyClick(event);
80 }
oshima 2016/03/11 09:40:24 do you need this?
yoshiki 2016/03/11 14:30:35 This is because the method in the parent class is
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(ToastOverlayButton);
84 };
85
86 ToastOverlayButton::ToastOverlayButton(const base::string16& label,
87 views::ButtonListener* listener)
88 : views::LabelButton(listener, label) {
89 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
90
91 SetTextColor(STATE_NORMAL, SkColorSetARGB(0xFF, 0x64, 0xA5, 0xF5));
92 SetTextColor(STATE_HOVERED, SkColorSetARGB(0xFF, 0xE3, 0xF2, 0xFD));
93 SetTextColor(STATE_PRESSED, SkColorSetARGB(0xFF, 0x64, 0xA5, 0xF5));
94 SetFontList(rb->GetFontList(kTextFontStyle));
95 SetBorder(views::Border::NullBorder());
96 }
97
98 ///////////////////////////////////////////////////////////////////////////////
99 // ToastOverlayView
100 class ToastOverlayView : public views::View, public views::ButtonListener {
101 public:
102 // This object is not owned by the views hiearchy or by the widget.
103 ToastOverlayView(ToastOverlay* overlay, const std::string& text);
104 ~ToastOverlayView() override;
105
106 // views::View overrides:
107 void OnPaint(gfx::Canvas* canvas) override;
108
109 ToastOverlayButton* button() { return button_; }
110
111 private:
112 ToastOverlay* overlay_; // weak
113 ToastOverlayButton* button_; // weak
114
115 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
116
117 DISALLOW_COPY_AND_ASSIGN(ToastOverlayView);
118 };
119
120 ToastOverlayView::ToastOverlayView(ToastOverlay* overlay,
121 const std::string& text)
122 : overlay_(overlay),
123 button_(new ToastOverlayButton(
124 l10n_util::GetStringUTF16(IDS_ASH_TOAST_DISMISS_BUTTON),
125 this)) {
126 // The parent ToastOverlay object owns this view.
127 set_owned_by_client();
128
129 const gfx::Font& font =
130 ui::ResourceBundle::GetSharedInstance().GetFont(kTextFontStyle);
131 int font_size = font.GetFontSize();
132
133 // Text should have a margin of 0.5 times the font size on each side, so
134 // the spacing between two labels will be the same as the font size.
135 int horizontal_spacing = font_size * 2;
136 int vertical_spacing = font_size;
137 int child_spacing = font_size * 4;
138
139 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
140 horizontal_spacing, vertical_spacing,
141 child_spacing));
142
143 ToastOverlayLabel* label = new ToastOverlayLabel(text);
144 AddChildView(label);
145 label->SetVisible(true);
146
147 AddChildView(button_);
148 }
149
150 ToastOverlayView::~ToastOverlayView() {}
151
152 void ToastOverlayView::OnPaint(gfx::Canvas* canvas) {
153 SkPaint paint;
154 paint.setStyle(SkPaint::kFill_Style);
155 paint.setColor(SkColorSetARGB(0xFF, 0x32, 0x32, 0x32));
156 canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
157 views::View::OnPaint(canvas);
158 }
159
160 void ToastOverlayView::ButtonPressed(views::Button* sender,
161 const ui::Event& event) {
162 overlay_->Show(false);
163 }
164
165 ///////////////////////////////////////////////////////////////////////////////
166 // ToastOverlay
167 ToastOverlay::ToastOverlay(Delegate* delegate, const std::string& text)
168 : delegate_(delegate),
169 overlay_view_(new ToastOverlayView(this, text)),
170 widget_size_(overlay_view_->GetPreferredSize()) {
171 views::Widget::InitParams params;
172 params.type = views::Widget::InitParams::TYPE_POPUP;
173 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
174 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
175 params.accept_events = true;
176 params.keep_on_top = true;
177 params.remove_standard_frame = true;
178 params.bounds = CalculateOverlayBounds();
179 // Show toasts above the app list and below the lock screen.
180 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(),
181 kShellWindowId_SystemModalContainer);
182 overlay_widget_.reset(new views::Widget);
183 overlay_widget_->Init(params);
184 overlay_widget_->SetVisibilityChangedAnimationsEnabled(true);
185 overlay_widget_->SetContentsView(overlay_view_.get());
186 overlay_widget_->SetBounds(CalculateOverlayBounds());
187 overlay_widget_->GetNativeView()->SetName("ToastOverlay");
188
189 gfx::NativeWindow native_view = overlay_widget_->GetNativeView();
190 wm::SetWindowVisibilityAnimationType(
191 native_view, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
192 wm::SetWindowVisibilityAnimationDuration(
193 native_view,
194 base::TimeDelta::FromMilliseconds(kSlideAnimationDurationMs));
195 }
196
197 ToastOverlay::~ToastOverlay() {
198 gfx::NativeWindow native_view = overlay_widget_->GetNativeView();
199 wm::SetWindowVisibilityAnimationTransition(native_view, wm::ANIMATE_NONE);
200
201 // Remove ourself from the animator to avoid being re-entrantly called in
202 // |overlay_widget_|'s destructor.
203 ui::Layer* layer = overlay_widget_->GetLayer();
204 if (layer) {
205 ui::LayerAnimator* animator = layer->GetAnimator();
206 if (animator)
207 animator->RemoveObserver(this);
208 }
209
210 overlay_widget_->Close();
211 }
212
213 void ToastOverlay::Show(bool visible) {
214 if (is_visible_ == visible)
215 return;
216
217 is_visible_ = visible;
218
219 overlay_widget_->GetLayer()->GetAnimator()->AddObserver(this);
220
221 if (is_visible_)
222 overlay_widget_->Show();
223 else
224 overlay_widget_->Hide();
225 }
226
227 gfx::Rect ToastOverlay::CalculateOverlayBounds() {
228 ShelfLayoutManager* shelf_layout_manager =
229 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
230 gfx::Rect root_bounds = Shell::GetPrimaryRootWindow()->bounds();
231 int bottom = (shelf_layout_manager->GetAlignment() == SHELF_ALIGNMENT_BOTTOM)
232 ? shelf_layout_manager->GetIdealBounds().y()
233 : root_bounds.height();
234
235 return gfx::Rect(gfx::Point((root_bounds.width() - widget_size_.width()) / 2,
236 bottom - widget_size_.height() - kVerticalOffset),
237 widget_size_);
238 }
239
240 void ToastOverlay::OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) {
241 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
242 if (animator)
243 animator->RemoveObserver(this);
244 if (!is_visible_) {
245 // Acync operation, since delegate may remove this instance and removing
246 // this here causes crash.
247 base::ThreadTaskRunnerHandle::Get()->PostTask(
248 FROM_HERE,
249 base::Bind(&Delegate::OnClosed, base::Unretained(delegate_)));
250 }
251 }
252
253 void ToastOverlay::OnLayerAnimationAborted(
254 ui::LayerAnimationSequence* sequence) {
255 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
256 if (animator)
257 animator->RemoveObserver(this);
258 }
259
260 void ToastOverlay::OnLayerAnimationScheduled(
261 ui::LayerAnimationSequence* sequence) {}
262
263 void ToastOverlay::ClickDismissButtonForTesting(const ui::Event& event) {
264 overlay_view_->button()->NotifyClick(event);
265 }
266
267 } // namespace ash
OLDNEW
« ash/system/toast/toast_manager_unittest.cc ('K') | « ash/system/toast/toast_overlay.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698