1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/notifications/idle_app_name_notification_view.h"
#include <string>
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/utility/wm_util.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "chrome/grit/generated_resources.h"
#include "extensions/common/extension.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/text_utils.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
namespace ui {
class LayerAnimationSequence;
} // namespace ui
namespace ash {
namespace {
// Color of the text of the warning message.
const SkColor kTextColor = SK_ColorBLACK;
// Color of the text of the warning message.
const SkColor kErrorTextColor = SK_ColorRED;
// Color of the window background.
const SkColor kWindowBackgroundColor = SK_ColorWHITE;
// Radius of the rounded corners of the window.
const int kWindowCornerRadius = 4;
// Creates and shows the message widget for |view| with |animation_time_ms|.
void CreateAndShowWidget(views::WidgetDelegateView* delegate,
int animation_time_ms) {
gfx::Size display_size =
display::Screen::GetScreen()->GetPrimaryDisplay().size();
gfx::Size view_size = delegate->GetPreferredSize();
gfx::Rect bounds((display_size.width() - view_size.width()) / 2,
-view_size.height(), view_size.width(), view_size.height());
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
views::Widget::InitParams::TYPE_POPUP);
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.accept_events = false;
params.z_order = ui::ZOrderLevel::kFloatingUIElement;
params.delegate = delegate;
params.bounds = bounds;
ash_util::SetupWidgetInitParamsForContainer(
¶ms, ash::kShellWindowId_SettingBubbleContainer);
views::Widget* widget = new views::Widget;
widget->Init(std::move(params));
gfx::NativeView native_view = widget->GetNativeView();
native_view->SetName("KioskIdleAppNameNotification");
// Note: We cannot use the Window show/hide animations since they are disabled
// for kiosk by command line.
ui::LayerAnimator* animator =
new ui::LayerAnimator(base::Milliseconds(animation_time_ms));
native_view->layer()->SetAnimator(animator);
widget->Show();
// We don't care about the show animation since it is off screen, so stop the
// started animation and move the message into view.
animator->StopAnimating();
bounds.set_y((display_size.height() - view_size.height()) / 20);
widget->SetBounds(bounds);
// Allow to use the message for spoken feedback.
delegate->NotifyAccessibilityEventDeprecated(ax::mojom::Event::kAlert, true);
}
} // namespace
// The class which implements the content view for the message.
class IdleAppNameNotificationDelegateView
: public views::WidgetDelegateView,
public ui::ImplicitAnimationObserver {
METADATA_HEADER(IdleAppNameNotificationDelegateView,
views::WidgetDelegateView)
public:
// An idle message which will get shown from the caller and hides itself after
// a time, calling |owner->CloseMessage| to inform the owner that it got
// destroyed. The |app_name| is a string which gets used as message and
// |error| is true if something is not correct.
// |message_visibility_time_in_ms| ms's after creation the message will start
// to remove itself from the screen.
IdleAppNameNotificationDelegateView(IdleAppNameNotificationView* owner,
const std::u16string& app_name,
bool error,
int message_visibility_time_in_ms)
: owner_(owner), widget_closed_(false) {
ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
// Add the application name label to the message.
AddLabel(app_name, rb->GetFontList(ui::ResourceBundle::BoldFont),
error ? kErrorTextColor : kTextColor);
SetLayoutManager(std::make_unique<views::FillLayout>());
// Set a timer which will trigger to remove the message after the given
// time.
hide_timer_.Start(FROM_HERE,
base::Milliseconds(message_visibility_time_in_ms), this,
&IdleAppNameNotificationDelegateView::RemoveMessage);
GetViewAccessibility().SetRole(ax::mojom::Role::kAlert);
GetViewAccessibility().SetName(app_name);
}
IdleAppNameNotificationDelegateView(
const IdleAppNameNotificationDelegateView&) = delete;
IdleAppNameNotificationDelegateView& operator=(
const IdleAppNameNotificationDelegateView&) = delete;
~IdleAppNameNotificationDelegateView() override {
// The widget is already closing, but the other cleanup items need to be
// performed.
widget_closed_ = true;
Close();
}
// Close the widget immediately. This can be called from the owner or from
// this class.
void Close() {
// Stop the timer (if it was running).
hide_timer_.Stop();
// Inform our owner that we are going away.
if (owner_) {
IdleAppNameNotificationView* owner = owner_;
owner_ = nullptr;
owner->CloseMessage();
}
// Close the owning widget - if required.
if (!widget_closed_) {
widget_closed_ = true;
GetWidget()->Close();
}
}
// Animate the window away (and close once done).
void RemoveMessage() {
aura::Window* widget_view = GetWidget()->GetNativeView();
ui::Layer* layer = widget_view->layer();
ui::ScopedLayerAnimationSettings settings(layer->GetAnimator());
settings.AddObserver(this);
gfx::Rect rect = widget_view->bounds();
rect.set_y(-GetPreferredSize().height());
layer->SetBounds(rect);
}
void OnPaint(gfx::Canvas* canvas) override {
cc::PaintFlags flags;
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setColor(kWindowBackgroundColor);
canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, flags);
views::WidgetDelegateView::OnPaint(canvas);
}
// ImplicitAnimationObserver overrides
void OnImplicitAnimationsCompleted() override { Close(); }
private:
// Adds the label to the view, using |text| with a |font| and a |text_color|.
void AddLabel(const std::u16string& text,
const gfx::FontList& font,
SkColor text_color) {
views::Label* label = new views::Label;
label->SetText(text);
label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
label->SetFontList(font);
label->SetEnabledColor(text_color);
label->SetAutoColorReadabilityEnabled(false);
AddChildViewRaw(label);
}
// A timer which calls us to remove the message from the screen.
base::OneShotTimer hide_timer_;
// The owner of this message which needs to get notified when the message
// closes.
raw_ptr<IdleAppNameNotificationView> owner_;
// True if the widget got already closed.
bool widget_closed_;
};
IdleAppNameNotificationView::IdleAppNameNotificationView(
int message_visibility_time_in_ms,
int animation_time_ms,
const extensions::Extension* extension)
: view_(nullptr) {
ShowMessage(message_visibility_time_in_ms, animation_time_ms, extension);
}
IdleAppNameNotificationView::~IdleAppNameNotificationView() {
CloseMessage();
}
void IdleAppNameNotificationView::CloseMessage() {
if (view_) {
IdleAppNameNotificationDelegateView* view = view_;
view_ = nullptr;
view->Close();
}
}
bool IdleAppNameNotificationView::IsVisible() {
return view_ != nullptr;
}
std::u16string IdleAppNameNotificationView::GetShownTextForTest() {
DCHECK(view_);
return view_->GetViewAccessibility().GetCachedName();
}
void IdleAppNameNotificationView::ShowMessage(
int message_visibility_time_in_ms,
int animation_time_ms,
const extensions::Extension* extension) {
DCHECK(!view_);
std::u16string app_name;
bool error = false;
if (extension &&
!base::ContainsOnlyChars(extension->name(), base::kWhitespaceASCII)) {
app_name = base::UTF8ToUTF16(extension->name());
} else {
error = true;
app_name = l10n_util::GetStringUTF16(
IDS_IDLE_APP_NAME_UNKNOWN_APPLICATION_NOTIFICATION);
}
view_ = new IdleAppNameNotificationDelegateView(
this,
app_name,
error,
message_visibility_time_in_ms + animation_time_ms);
CreateAndShowWidget(view_, animation_time_ms);
}
BEGIN_METADATA(IdleAppNameNotificationDelegateView)
END_METADATA
} // namespace ash
|