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

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: 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_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 private:
79 friend class ToastOverlay; // To call NotifyClick().
80
81 DISALLOW_COPY_AND_ASSIGN(ToastOverlayButton);
82 };
83
84 ToastOverlayButton::ToastOverlayButton(const base::string16& label,
85 views::ButtonListener* listener)
86 : views::LabelButton(listener, label) {
87 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
88
89 SetTextColor(STATE_NORMAL, SkColorSetARGB(0xFF, 0x64, 0xA5, 0xF5));
90 SetTextColor(STATE_HOVERED, SkColorSetARGB(0xFF, 0xE3, 0xF2, 0xFD));
91 SetTextColor(STATE_PRESSED, SkColorSetARGB(0xFF, 0x64, 0xA5, 0xF5));
92 SetFontList(rb->GetFontList(kTextFontStyle));
93 SetBorder(views::Border::NullBorder());
94 }
95
96 ///////////////////////////////////////////////////////////////////////////////
97 // ToastOverlayView
98 class ToastOverlayView : public views::View, public views::ButtonListener {
99 public:
100 // This object is not owned by the views hiearchy or by the widget.
101 ToastOverlayView(ToastOverlay* overlay, const std::string& text);
102 ~ToastOverlayView() override;
103
104 // views::View overrides:
105 void OnPaint(gfx::Canvas* canvas) override;
106
107 ToastOverlayButton* button() { return button_; }
108
109 private:
110 ToastOverlay* overlay_; // weak
111 ToastOverlayButton* button_; // weak
112
113 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
114
115 DISALLOW_COPY_AND_ASSIGN(ToastOverlayView);
116 };
117
118 ToastOverlayView::ToastOverlayView(ToastOverlay* overlay,
119 const std::string& text)
120 : overlay_(overlay),
121 button_(new ToastOverlayButton(
122 l10n_util::GetStringUTF16(IDS_ASH_TOAST_DISMISS_BUTTON),
123 this)) {
124 // The parent ToastOverlay object owns this view.
125 set_owned_by_client();
oshima 2016/03/15 09:07:33 this was necessary because you store the view in s
yoshiki 2016/03/15 15:04:22 Thanks! Done.
126
127 const gfx::Font& font =
128 ui::ResourceBundle::GetSharedInstance().GetFont(kTextFontStyle);
129 int font_size = font.GetFontSize();
130
131 // Text should have a margin of 0.5 times the font size on each side, so
132 // the spacing between two labels will be the same as the font size.
133 int horizontal_spacing = font_size * 2;
134 int vertical_spacing = font_size;
135 int child_spacing = font_size * 4;
136
137 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
138 horizontal_spacing, vertical_spacing,
139 child_spacing));
140
141 ToastOverlayLabel* label = new ToastOverlayLabel(text);
142 AddChildView(label);
143 label->SetVisible(true);
144
145 AddChildView(button_);
146 }
147
148 ToastOverlayView::~ToastOverlayView() {}
149
150 void ToastOverlayView::OnPaint(gfx::Canvas* canvas) {
151 SkPaint paint;
152 paint.setStyle(SkPaint::kFill_Style);
153 paint.setColor(SkColorSetARGB(0xFF, 0x32, 0x32, 0x32));
154 canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
155 views::View::OnPaint(canvas);
156 }
157
158 void ToastOverlayView::ButtonPressed(views::Button* sender,
159 const ui::Event& event) {
160 overlay_->Show(false);
161 }
162
163 ///////////////////////////////////////////////////////////////////////////////
164 // ToastOverlay
165 ToastOverlay::ToastOverlay(Delegate* delegate, const std::string& text)
166 : delegate_(delegate),
167 overlay_view_(new ToastOverlayView(this, text)),
168 widget_size_(overlay_view_->GetPreferredSize()) {
169 views::Widget::InitParams params;
170 params.type = views::Widget::InitParams::TYPE_POPUP;
171 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
172 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
173 params.accept_events = true;
174 params.keep_on_top = true;
175 params.remove_standard_frame = true;
176 params.bounds = CalculateOverlayBounds();
177 // Show toasts above the app list and below the lock screen.
178 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(),
179 kShellWindowId_SystemModalContainer);
180 overlay_widget_.reset(new views::Widget);
181 overlay_widget_->Init(params);
182 overlay_widget_->SetVisibilityChangedAnimationsEnabled(true);
183 overlay_widget_->SetContentsView(overlay_view_.get());
184 overlay_widget_->SetBounds(CalculateOverlayBounds());
185 overlay_widget_->GetNativeView()->SetName("ToastOverlay");
186
187 gfx::NativeWindow native_view = overlay_widget_->GetNativeView();
188 wm::SetWindowVisibilityAnimationType(
189 native_view, wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
190 wm::SetWindowVisibilityAnimationDuration(
191 native_view,
192 base::TimeDelta::FromMilliseconds(kSlideAnimationDurationMs));
193 }
194
195 ToastOverlay::~ToastOverlay() {
196 gfx::NativeWindow native_view = overlay_widget_->GetNativeView();
197 wm::SetWindowVisibilityAnimationTransition(native_view, wm::ANIMATE_NONE);
198
199 // Remove ourself from the animator to avoid being re-entrantly called in
200 // |overlay_widget_|'s destructor.
201 ui::Layer* layer = overlay_widget_->GetLayer();
202 if (layer) {
203 ui::LayerAnimator* animator = layer->GetAnimator();
204 if (animator)
205 animator->RemoveObserver(this);
206 }
207
208 overlay_widget_->Close();
209 }
210
211 void ToastOverlay::Show(bool visible) {
212 if (is_visible_ == visible)
213 return;
214
215 is_visible_ = visible;
216
217 overlay_widget_->GetLayer()->GetAnimator()->AddObserver(this);
218
219 if (is_visible_)
220 overlay_widget_->Show();
221 else
222 overlay_widget_->Hide();
223 }
224
225 gfx::Rect ToastOverlay::CalculateOverlayBounds() {
226 ShelfLayoutManager* shelf_layout_manager =
227 Shelf::ForPrimaryDisplay()->shelf_layout_manager();
228 gfx::Rect root_bounds = Shell::GetPrimaryRootWindow()->bounds();
229 int bottom = (shelf_layout_manager->GetAlignment() == SHELF_ALIGNMENT_BOTTOM)
230 ? shelf_layout_manager->GetIdealBounds().y()
231 : root_bounds.height();
oshima 2016/03/15 09:07:33 Can you use ScreenUtil::GetShelfDisplayBoundsInRoo
yoshiki 2016/03/15 15:04:22 I uses ShelfLayoutManager::user_work_area_bounds()
232
233 return gfx::Rect(gfx::Point((root_bounds.width() - widget_size_.width()) / 2,
234 bottom - widget_size_.height() - kVerticalOffset),
235 widget_size_);
236 }
237
238 void ToastOverlay::OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) {
239 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
240 if (animator)
241 animator->RemoveObserver(this);
242 if (!is_visible_) {
243 // Acync operation, since delegate may remove this instance and removing
244 // this here causes crash.
245 base::ThreadTaskRunnerHandle::Get()->PostTask(
246 FROM_HERE,
247 base::Bind(&Delegate::OnClosed, base::Unretained(delegate_)));
248 }
249 }
250
251 void ToastOverlay::OnLayerAnimationAborted(
252 ui::LayerAnimationSequence* sequence) {
253 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
254 if (animator)
255 animator->RemoveObserver(this);
256 }
257
258 void ToastOverlay::OnLayerAnimationScheduled(
259 ui::LayerAnimationSequence* sequence) {}
260
261 void ToastOverlay::ClickDismissButtonForTesting(const ui::Event& event) {
262 overlay_view_->button()->NotifyClick(event);
263 }
264
265 } // 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