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.
5 #include "chrome/browser/chromeos/ui/kiosk_external_update_notification.h"
8 #include "ash/shell_window_ids.h"
9 #include "ui/aura/window.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/compositor/layer.h"
12 #include "ui/compositor/scoped_layer_animation_settings.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/layout/fill_layout.h"
16 #include "ui/views/view.h"
17 #include "ui/views/widget/widget.h"
18 #include "ui/views/widget/widget_delegate.h"
24 const SkColor kTextColor
= SK_ColorBLACK
;
25 const SkColor kWindowBackgroundColor
= SK_ColorWHITE
;
26 const int kWindowCornerRadius
= 4;
27 const int kPreferredWidth
= 600;
28 const int kPreferredHeight
= 250;
32 class KioskExternalUpdateNotificationView
: public views::WidgetDelegateView
{
34 explicit KioskExternalUpdateNotificationView(
35 KioskExternalUpdateNotification
* owner
)
36 : owner_(owner
), widget_closed_(false) {
38 SetLayoutManager(new views::FillLayout
);
41 virtual ~KioskExternalUpdateNotificationView() {
42 widget_closed_
= true;
43 InformOwnerForDismiss();
46 // Closes the widget immediately from |owner_|.
49 if (!widget_closed_
) {
50 widget_closed_
= true;
55 void SetMessage(const base::string16
& message
) { label_
->SetText(message
); }
57 // views::WidgetDelegateView overrides:
58 virtual void OnPaint(gfx::Canvas
* canvas
) override
{
60 paint
.setStyle(SkPaint::kFill_Style
);
61 paint
.setColor(kWindowBackgroundColor
);
62 canvas
->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius
, paint
);
63 views::WidgetDelegateView::OnPaint(canvas
);
66 virtual gfx::Size
GetPreferredSize() const override
{
67 return gfx::Size(kPreferredWidth
, kPreferredHeight
);
72 label_
= new views::Label
;
73 label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
74 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
75 label_
->SetFontList(rb
.GetFontList(ui::ResourceBundle::BoldFont
));
76 label_
->SetEnabledColor(kTextColor
);
77 label_
->SetDisabledColor(kTextColor
);
78 label_
->SetAutoColorReadabilityEnabled(false);
79 label_
->SetMultiLine(true);
83 void InformOwnerForDismiss() {
84 // Inform the |owner_| that we are going away.
86 KioskExternalUpdateNotification
* owner
= owner_
;
92 // The owner of this message which needs to get notified when the message
94 KioskExternalUpdateNotification
* owner_
;
95 views::Label
* label_
; // owned by views hierarchy.
97 // True if the widget got already closed.
100 DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdateNotificationView
);
103 KioskExternalUpdateNotification::KioskExternalUpdateNotification(
104 const base::string16
& message
) {
105 CreateAndShowNotificationView(message
);
108 KioskExternalUpdateNotification::~KioskExternalUpdateNotification() {
112 void KioskExternalUpdateNotification::ShowMessage(
113 const base::string16
& message
) {
115 view_
->SetMessage(message
);
118 void KioskExternalUpdateNotification::CreateAndShowNotificationView(
119 const base::string16
& message
) {
120 view_
= new KioskExternalUpdateNotificationView(this);
121 view_
->SetMessage(message
);
123 aura::Window
* root_window
= ash::Shell::GetTargetRootWindow();
124 gfx::Size rs
= root_window
->bounds().size();
125 gfx::Size ps
= view_
->GetPreferredSize();
126 gfx::Rect
bounds((rs
.width() - ps
.width()) / 2,
127 (rs
.height() - ps
.height()) / 10,
130 views::Widget::InitParams params
;
131 params
.type
= views::Widget::InitParams::TYPE_POPUP
;
132 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
133 params
.ownership
= views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET
;
134 params
.accept_events
= false;
135 params
.keep_on_top
= true;
136 params
.remove_standard_frame
= true;
137 params
.delegate
= view_
;
138 params
.bounds
= bounds
;
139 params
.parent
= ash::Shell::GetContainer(
140 root_window
, ash::kShellWindowId_SettingBubbleContainer
);
141 views::Widget
* widget
= new views::Widget
;
142 widget
->Init(params
);
143 widget
->SetContentsView(view_
);
144 gfx::NativeView native_view
= widget
->GetNativeView();
145 native_view
->SetName("KioskExternalUpdateNotification");
149 void KioskExternalUpdateNotification::Dismiss() {
151 KioskExternalUpdateNotificationView
* view
= view_
;
153 view
->CloseByOwner();
157 } // namespace chromeos