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

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

Issue 2139183003: mash: Migrate //ash/system/toast to WM common types (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: move test back Created 4 years, 5 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_overlay.h ('k') | ui/arc/notification/arc_notification_manager.cc » ('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/system/toast/toast_overlay.h"
6
7 #include "ash/common/shelf/wm_shelf.h"
8 #include "ash/common/shell_window_ids.h"
9 #include "ash/common/wm_root_window_controller.h"
10 #include "ash/common/wm_shell.h"
11 #include "ash/common/wm_window.h"
12 #include "ash/screen_util.h"
13 #include "ash/shell.h"
14 #include "ash/wm/window_animations.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/threading/thread_task_runner_handle.h"
18 #include "grit/ash_strings.h"
19 #include "third_party/skia/include/core/SkColor.h"
20 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/font_list.h"
24 #include "ui/views/border.h"
25 #include "ui/views/controls/button/label_button.h"
26 #include "ui/views/controls/label.h"
27 #include "ui/views/layout/box_layout.h"
28 #include "ui/views/view.h"
29 #include "ui/views/widget/widget.h"
30 #include "ui/views/widget/widget_delegate.h"
31
32 namespace ash {
33
34 namespace {
35
36 // Offset of the overlay from the edge of the work area.
37 const int kOffset = 5;
38
39 // Font style used for modifier key labels.
40 const ui::ResourceBundle::FontStyle kTextFontStyle =
41 ui::ResourceBundle::MediumFont;
42
43 // Duration of slide animation when overlay is shown or hidden.
44 const int kSlideAnimationDurationMs = 100;
45
46 // Colors for the dismiss button.
47 const SkColor kButtonBackgroundColor = SkColorSetARGB(0xFF, 0x32, 0x32, 0x32);
48 const SkColor kButtonTextColor = SkColorSetARGB(0xFF, 0x7B, 0xAA, 0xF7);
49
50 // These values are in DIP.
51 const int kToastHorizontalSpacing = 16;
52 const int kToastVerticalSpacing = 16;
53 const int kToastMaximumWidth = 568;
54 const int kToastMinimumWidth = 288;
55
56 // Returns the shelf for the primary display.
57 WmShelf* GetPrimaryShelf() {
58 return WmShell::Get()
59 ->GetPrimaryRootWindow()
60 ->GetRootWindowController()
61 ->GetShelf();
62 }
63
64 } // anonymous namespace
65
66 ///////////////////////////////////////////////////////////////////////////////
67 // ToastOverlayLabel
68 class ToastOverlayLabel : public views::Label {
69 public:
70 explicit ToastOverlayLabel(const std::string& label);
71 ~ToastOverlayLabel() override;
72
73 private:
74 DISALLOW_COPY_AND_ASSIGN(ToastOverlayLabel);
75 };
76
77 ToastOverlayLabel::ToastOverlayLabel(const std::string& label) {
78 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
79
80 SetText(base::UTF8ToUTF16(label));
81 SetHorizontalAlignment(gfx::ALIGN_LEFT);
82 SetFontList(rb->GetFontList(kTextFontStyle));
83 SetAutoColorReadabilityEnabled(false);
84 SetMultiLine(true);
85 SetEnabledColor(SK_ColorWHITE);
86 SetDisabledColor(SK_ColorWHITE);
87 SetSubpixelRenderingEnabled(false);
88
89 int verticalSpacing =
90 kToastVerticalSpacing - (GetPreferredSize().height() - GetBaseline());
91 SetBorder(views::Border::CreateEmptyBorder(
92 verticalSpacing, kToastHorizontalSpacing, verticalSpacing,
93 kToastHorizontalSpacing));
94 }
95
96 ToastOverlayLabel::~ToastOverlayLabel() {}
97
98 ///////////////////////////////////////////////////////////////////////////////
99 // ToastOverlayButton
100 class ToastOverlayButton : public views::LabelButton {
101 public:
102 explicit ToastOverlayButton(views::ButtonListener* listener,
103 const base::string16& label);
104 ~ToastOverlayButton() override {}
105
106 private:
107 friend class ToastOverlay; // for ToastOverlay::ClickDismissButtonForTesting.
108
109 DISALLOW_COPY_AND_ASSIGN(ToastOverlayButton);
110 };
111
112 ToastOverlayButton::ToastOverlayButton(views::ButtonListener* listener,
113 const base::string16& text)
114 : views::LabelButton(listener, text) {
115 SetInkDropMode(InkDropMode::ON);
116 set_has_ink_drop_action_on_click(true);
117 set_ink_drop_base_color(SK_ColorWHITE);
118
119 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
120
121 SetEnabledTextColors(kButtonTextColor);
122 SetFontList(rb->GetFontList(kTextFontStyle));
123
124 // Treat the space below the baseline as a margin.
125 int verticalSpacing = kToastVerticalSpacing -
126 (GetPreferredSize().height() - label()->GetBaseline());
127 SetBorder(views::Border::CreateEmptyBorder(
128 verticalSpacing, kToastHorizontalSpacing, verticalSpacing,
129 kToastHorizontalSpacing));
130 }
131
132 ///////////////////////////////////////////////////////////////////////////////
133 // ToastOverlayView
134 class ToastOverlayView : public views::View, public views::ButtonListener {
135 public:
136 // This object is not owned by the views hiearchy or by the widget.
137 ToastOverlayView(ToastOverlay* overlay,
138 const std::string& text,
139 const std::string& dismiss_text);
140 ~ToastOverlayView() override;
141
142 // views::View overrides:
143 void OnPaint(gfx::Canvas* canvas) override;
144
145 ToastOverlayButton* button() { return button_; }
146
147 private:
148 ToastOverlay* overlay_; // weak
149 ToastOverlayButton* button_; // weak
150
151 gfx::Size GetMaximumSize() const override;
152 gfx::Size GetMinimumSize() const override;
153
154 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
155
156 DISALLOW_COPY_AND_ASSIGN(ToastOverlayView);
157 };
158
159 ToastOverlayView::ToastOverlayView(ToastOverlay* overlay,
160 const std::string& text,
161 const std::string& dismiss_text)
162 : overlay_(overlay),
163 button_(new ToastOverlayButton(
164 this,
165 dismiss_text.empty()
166 ? l10n_util::GetStringUTF16(IDS_ASH_TOAST_DISMISS_BUTTON)
167 : base::UTF8ToUTF16(dismiss_text))) {
168 ToastOverlayLabel* label = new ToastOverlayLabel(text);
169 label->SetMaximumWidth(
170 GetMaximumSize().width() - button_->GetPreferredSize().width() -
171 kToastHorizontalSpacing * 2 - kToastHorizontalSpacing * 2);
172 AddChildView(label);
173
174 AddChildView(button_);
175
176 auto* layout = new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, 0);
177 SetLayoutManager(layout);
178 layout->SetFlexForView(label, 1);
179 layout->SetFlexForView(button_, 0);
180 }
181
182 ToastOverlayView::~ToastOverlayView() {}
183
184 void ToastOverlayView::OnPaint(gfx::Canvas* canvas) {
185 SkPaint paint;
186 paint.setStyle(SkPaint::kFill_Style);
187 paint.setColor(kButtonBackgroundColor);
188 canvas->DrawRoundRect(GetLocalBounds(), 2, paint);
189 views::View::OnPaint(canvas);
190 }
191
192 gfx::Size ToastOverlayView::GetMinimumSize() const {
193 return gfx::Size(kToastMinimumWidth, 0);
194 }
195
196 gfx::Size ToastOverlayView::GetMaximumSize() const {
197 gfx::Rect work_area_bounds = GetPrimaryShelf()->GetUserWorkAreaBounds();
198 return gfx::Size(kToastMaximumWidth, work_area_bounds.height() - kOffset * 2);
199 }
200
201 void ToastOverlayView::ButtonPressed(views::Button* sender,
202 const ui::Event& event) {
203 overlay_->Show(false);
204 }
205
206 ///////////////////////////////////////////////////////////////////////////////
207 // ToastOverlay
208 ToastOverlay::ToastOverlay(Delegate* delegate,
209 const std::string& text,
210 const std::string& dismiss_text)
211 : delegate_(delegate),
212 text_(text),
213 dismiss_text_(dismiss_text),
214 overlay_view_(new ToastOverlayView(this, text, dismiss_text)),
215 widget_size_(overlay_view_->GetPreferredSize()) {
216 views::Widget::InitParams params;
217 params.type = views::Widget::InitParams::TYPE_POPUP;
218 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
219 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
220 params.accept_events = true;
221 params.keep_on_top = true;
222 params.remove_standard_frame = true;
223 params.bounds = CalculateOverlayBounds();
224 // Show toasts above the app list and below the lock screen.
225 // TODO(jamescook): Either this should be the primary root window, or the
226 // work area bounds computation should be for the target root window.
227 params.parent = Shell::GetContainer(Shell::GetTargetRootWindow(),
228 kShellWindowId_SystemModalContainer);
229 overlay_widget_.reset(new views::Widget);
230 overlay_widget_->Init(params);
231 overlay_widget_->SetVisibilityChangedAnimationsEnabled(true);
232 overlay_widget_->SetContentsView(overlay_view_.get());
233 overlay_widget_->SetBounds(CalculateOverlayBounds());
234 overlay_widget_->GetNativeView()->SetName("ToastOverlay");
235
236 gfx::NativeWindow native_view = overlay_widget_->GetNativeView();
237 ::wm::SetWindowVisibilityAnimationType(
238 native_view, ::wm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
239 ::wm::SetWindowVisibilityAnimationDuration(
240 native_view,
241 base::TimeDelta::FromMilliseconds(kSlideAnimationDurationMs));
242 }
243
244 ToastOverlay::~ToastOverlay() {
245 overlay_widget_->Close();
246 }
247
248 void ToastOverlay::Show(bool visible) {
249 if (overlay_widget_->GetLayer()->GetTargetVisibility() == visible)
250 return;
251
252 ui::LayerAnimator* animator = overlay_widget_->GetLayer()->GetAnimator();
253 DCHECK(animator);
254
255 base::TimeDelta original_duration = animator->GetTransitionDuration();
256 ui::ScopedLayerAnimationSettings animation_settings(animator);
257 // ScopedLayerAnimationSettings ctor chanes the transition duration, so change
258 // back it to the original value (should be zero).
259 animation_settings.SetTransitionDuration(original_duration);
260
261 animation_settings.AddObserver(this);
262
263 if (visible) {
264 overlay_widget_->Show();
265
266 // Notify accessibility about the overlay.
267 overlay_view_->NotifyAccessibilityEvent(ui::AX_EVENT_ALERT, false);
268 } else {
269 overlay_widget_->Hide();
270 }
271 }
272
273 gfx::Rect ToastOverlay::CalculateOverlayBounds() {
274 gfx::Rect bounds = GetPrimaryShelf()->GetUserWorkAreaBounds();
275 int target_y = bounds.bottom() - widget_size_.height() - kOffset;
276 bounds.ClampToCenteredSize(widget_size_);
277 bounds.set_y(target_y);
278 return bounds;
279 }
280
281 void ToastOverlay::OnImplicitAnimationsScheduled() {}
282
283 void ToastOverlay::OnImplicitAnimationsCompleted() {
284 if (!overlay_widget_->GetLayer()->GetTargetVisibility())
285 delegate_->OnClosed();
286 }
287
288 views::Widget* ToastOverlay::widget_for_testing() {
289 return overlay_widget_.get();
290 }
291
292 void ToastOverlay::ClickDismissButtonForTesting(const ui::Event& event) {
293 overlay_view_->button()->NotifyClick(event);
294 }
295
296 } // namespace ash
OLDNEW
« no previous file with comments | « ash/system/toast/toast_overlay.h ('k') | ui/arc/notification/arc_notification_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698