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