Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/kiosk_external_update_notification.h" | |
| 6 | |
| 7 #include "ash/shell.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" | |
| 19 | |
| 20 namespace chromeos { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 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; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class KioskExternalUpdateNotificationView : public views::WidgetDelegateView { | |
| 33 public: | |
| 34 explicit KioskExternalUpdateNotificationView( | |
| 35 KioskExternalUpdateNotification* owner) | |
| 36 : owner_(owner), widget_closed_(false) { | |
| 37 AddLabel(); | |
| 38 SetLayoutManager(new views::FillLayout); | |
| 39 } | |
| 40 | |
| 41 virtual ~KioskExternalUpdateNotificationView() { | |
| 42 widget_closed_ = true; | |
| 43 InformOwnerForDismiss(); | |
| 44 } | |
| 45 | |
| 46 void InformOwnerForDismiss() { | |
| 47 // Inform the |owner_| that we are going away. | |
| 48 if (owner_) { | |
| 49 KioskExternalUpdateNotification* owner = owner_; | |
| 50 owner_ = NULL; | |
| 51 owner->Dismiss(); | |
| 52 } | |
| 53 } | |
| 54 | |
| 55 // Close the widget immediately. This can be called from the owner or from | |
| 56 // this class. | |
| 57 void Close() { | |
| 58 InformOwnerForDismiss(); | |
|
xiyuan
2014/08/27 16:17:03
This is not necessary. Close would cause this view
jennyz
2014/08/28 22:41:07
In our case, both notification and view should go
xiyuan
2014/08/29 02:44:17
InformOwnerForDismiss() code is necessary for hand
jennyz
2014/08/29 18:37:51
Add back InformOwnerForDismiss to handle this corn
| |
| 59 | |
| 60 if (!widget_closed_) { | |
| 61 widget_closed_ = true; | |
| 62 GetWidget()->Close(); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void SetMessage(const base::string16& message) { label_->SetText(message); } | |
| 67 | |
| 68 // views::WidgetDelegateView overrdies. | |
| 69 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
| 70 SkPaint paint; | |
| 71 paint.setStyle(SkPaint::kFill_Style); | |
| 72 paint.setColor(kWindowBackgroundColor); | |
| 73 canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, paint); | |
| 74 views::WidgetDelegateView::OnPaint(canvas); | |
| 75 } | |
| 76 | |
| 77 virtual gfx::Size GetPreferredSize() const OVERRIDE { | |
| 78 return gfx::Size(kPreferredWidth, kPreferredHeight); | |
| 79 } | |
| 80 | |
| 81 private: | |
| 82 void AddLabel() { | |
| 83 label_ = new views::Label; | |
| 84 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 85 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 86 label_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); | |
| 87 label_->SetEnabledColor(kTextColor); | |
| 88 label_->SetDisabledColor(kTextColor); | |
| 89 label_->SetAutoColorReadabilityEnabled(false); | |
| 90 label_->SetMultiLine(true); | |
| 91 AddChildView(label_); | |
| 92 } | |
| 93 | |
| 94 // The owner of this message which needs to get notified when the message | |
| 95 // closes. | |
| 96 KioskExternalUpdateNotification* owner_; | |
| 97 | |
| 98 views::Label* label_; // owned by views hierarchy. | |
| 99 | |
| 100 // True if the widget got already closed. | |
| 101 bool widget_closed_; | |
| 102 | |
| 103 DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdateNotificationView); | |
| 104 }; | |
| 105 | |
| 106 KioskExternalUpdateNotification::KioskExternalUpdateNotification( | |
| 107 const base::string16& message) { | |
| 108 view_ = new KioskExternalUpdateNotificationView(this); | |
| 109 CreateAndShowNotificationView(message); | |
| 110 } | |
| 111 | |
| 112 KioskExternalUpdateNotification::~KioskExternalUpdateNotification() { | |
| 113 Dismiss(); | |
| 114 } | |
| 115 | |
| 116 void KioskExternalUpdateNotification::ShowMessage( | |
| 117 const base::string16& message) { | |
| 118 DCHECK(view_); | |
| 119 view_->SetMessage(message); | |
| 120 } | |
| 121 | |
| 122 void KioskExternalUpdateNotification::CreateAndShowNotificationView( | |
| 123 const base::string16& message) { | |
| 124 view_->SetMessage(message); | |
| 125 | |
| 126 aura::Window* root_window = ash::Shell::GetTargetRootWindow(); | |
| 127 gfx::Size rs = root_window->bounds().size(); | |
| 128 gfx::Size ps = view_->GetPreferredSize(); | |
| 129 gfx::Rect bounds((rs.width() - ps.width()) / 2, | |
| 130 (rs.height() - ps.height()) / 10, | |
| 131 ps.width(), | |
| 132 ps.height()); | |
| 133 views::Widget::InitParams params; | |
| 134 params.type = views::Widget::InitParams::TYPE_POPUP; | |
| 135 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 136 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; | |
| 137 params.accept_events = false; | |
| 138 params.keep_on_top = true; | |
| 139 params.remove_standard_frame = true; | |
| 140 params.delegate = view_; | |
| 141 params.bounds = bounds; | |
| 142 params.parent = ash::Shell::GetContainer( | |
| 143 root_window, ash::kShellWindowId_SettingBubbleContainer); | |
| 144 views::Widget* widget = new views::Widget; | |
| 145 widget->Init(params); | |
| 146 widget->SetContentsView(view_); | |
| 147 gfx::NativeView native_view = widget->GetNativeView(); | |
| 148 native_view->SetName("KioskExternalUpdateNotification"); | |
| 149 widget->Show(); | |
| 150 } | |
| 151 | |
| 152 void KioskExternalUpdateNotification::Dismiss() { | |
| 153 if (view_) { | |
| 154 KioskExternalUpdateNotificationView* view = view_; | |
| 155 view_ = NULL; | |
| 156 view->Close(); | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 } // namespace chromeos | |
| OLD | NEW |