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