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 Close(); | |
|
xiyuan
2014/08/23 18:11:58
Suggest to not call Close() here. It does not feel
jennyz
2014/08/27 00:58:42
Done.
| |
| 44 } | |
| 45 | |
| 46 // Close the widget immediately. This can be called from the owner or from | |
| 47 // this class. | |
| 48 void Close() { | |
| 49 // Inform the |owner_| that we are going away. | |
| 50 if (owner_) { | |
| 51 KioskExternalUpdateNotification* owner = owner_; | |
| 52 owner_ = NULL; | |
| 53 owner->Dismiss(); | |
| 54 } | |
| 55 | |
| 56 if (!widget_closed_) { | |
| 57 widget_closed_ = true; | |
| 58 GetWidget()->Close(); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void SetMessage(const base::string16& message) { label_->SetText(message); } | |
| 63 | |
| 64 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
|
xiyuan
2014/08/23 18:11:58
nit: // views::WidgetDelegateView:
jennyz
2014/08/27 00:58:42
Done.
| |
| 65 SkPaint paint; | |
| 66 paint.setStyle(SkPaint::kFill_Style); | |
| 67 paint.setColor(kWindowBackgroundColor); | |
| 68 canvas->DrawRoundRect(GetLocalBounds(), kWindowCornerRadius, paint); | |
| 69 views::WidgetDelegateView::OnPaint(canvas); | |
| 70 } | |
| 71 | |
| 72 virtual gfx::Size GetPreferredSize() const OVERRIDE { | |
| 73 return gfx::Size(kPreferredWidth, kPreferredHeight); | |
| 74 } | |
| 75 | |
| 76 private: | |
| 77 void AddLabel() { | |
| 78 label_ = new views::Label; | |
| 79 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 80 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 81 label_->SetFontList(rb.GetFontList(ui::ResourceBundle::BoldFont)); | |
| 82 label_->SetEnabledColor(kTextColor); | |
| 83 label_->SetDisabledColor(kTextColor); | |
| 84 label_->SetAutoColorReadabilityEnabled(false); | |
| 85 label_->SetMultiLine(true); | |
| 86 AddChildView(label_); | |
| 87 } | |
| 88 | |
| 89 // The owner of this message which needs to get notified when the message | |
| 90 // closes. | |
| 91 KioskExternalUpdateNotification* owner_; | |
| 92 | |
| 93 views::Label* label_; // owned by views hierarchy. | |
| 94 | |
| 95 // True if the widget got already closed. | |
| 96 bool widget_closed_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(KioskExternalUpdateNotificationView); | |
| 99 }; | |
| 100 | |
| 101 KioskExternalUpdateNotification::KioskExternalUpdateNotification( | |
| 102 const base::string16& message) { | |
| 103 view_ = new KioskExternalUpdateNotificationView(this); | |
| 104 CreateAndShowNotificationView(message); | |
| 105 } | |
| 106 | |
| 107 KioskExternalUpdateNotification::~KioskExternalUpdateNotification() { | |
| 108 Dismiss(); | |
| 109 } | |
| 110 | |
| 111 void KioskExternalUpdateNotification::ShowMessage( | |
| 112 const base::string16& message) { | |
| 113 DCHECK(view_); | |
| 114 view_->SetMessage(message); | |
| 115 } | |
| 116 | |
| 117 void KioskExternalUpdateNotification::CreateAndShowNotificationView( | |
| 118 const base::string16& message) { | |
| 119 view_->SetMessage(message); | |
| 120 | |
| 121 aura::Window* root_window = ash::Shell::GetTargetRootWindow(); | |
| 122 gfx::Size rs = root_window->bounds().size(); | |
| 123 gfx::Size ps = view_->GetPreferredSize(); | |
| 124 gfx::Rect bounds((rs.width() - ps.width()) / 2, | |
| 125 (rs.height() - ps.height()) / 10, | |
| 126 ps.width(), | |
| 127 ps.height()); | |
| 128 views::Widget::InitParams params; | |
| 129 params.type = views::Widget::InitParams::TYPE_POPUP; | |
| 130 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; | |
| 131 params.ownership = views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET; | |
| 132 params.accept_events = false; | |
| 133 params.keep_on_top = true; | |
| 134 params.remove_standard_frame = true; | |
| 135 params.delegate = view_; | |
| 136 params.bounds = bounds; | |
| 137 params.parent = ash::Shell::GetContainer( | |
| 138 root_window, ash::kShellWindowId_SettingBubbleContainer); | |
| 139 views::Widget* widget = new views::Widget; | |
| 140 widget->Init(params); | |
| 141 widget->SetContentsView(view_); | |
| 142 gfx::NativeView native_view = widget->GetNativeView(); | |
| 143 native_view->SetName("KioskExternalUpdateNotification"); | |
| 144 widget->Show(); | |
| 145 } | |
| 146 | |
| 147 void KioskExternalUpdateNotification::Dismiss() { | |
| 148 if (view_) { | |
| 149 KioskExternalUpdateNotificationView* view = view_; | |
| 150 view_ = NULL; | |
| 151 view->Close(); | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 } // namespace chromeos | |
| OLD | NEW |