OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "ash/system/tray_user.h" |
| 6 |
| 7 #include "ash/shell.h" |
| 8 #include "ash/system/tray/system_tray_delegate.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "grit/ui_resources.h" |
| 11 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/gfx/size.h" |
| 13 #include "ui/views/controls/button/button.h" |
| 14 #include "ui/views/controls/button/text_button.h" |
| 15 #include "ui/views/controls/image_view.h" |
| 16 #include "ui/views/controls/label.h" |
| 17 #include "ui/views/layout/box_layout.h" |
| 18 #include "ui/views/view.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // A custom textbutton with some extra vertical padding, and custom border, |
| 23 // alignment and hover-effects. |
| 24 class TrayButton : public views::TextButton { |
| 25 public: |
| 26 TrayButton(views::ButtonListener* listener, std::string text) |
| 27 : views::TextButton(listener, ASCIIToUTF16(text)), |
| 28 hover_(false), |
| 29 hover_bg_(views::Background::CreateSolidBackground(SkColorSetARGB( |
| 30 10, 0, 0, 0))) { |
| 31 set_alignment(ALIGN_CENTER); |
| 32 set_border(views::Border::CreateSolidSidedBorder( |
| 33 0, 1, 0, 1, SkColorSetARGB(25, 0, 0, 0))); |
| 34 } |
| 35 |
| 36 private: |
| 37 // Overridden from views::View. |
| 38 virtual gfx::Size GetPreferredSize() OVERRIDE { |
| 39 gfx::Size size = views::TextButton::GetPreferredSize(); |
| 40 size.Enlarge(0, 10); |
| 41 return size; |
| 42 } |
| 43 |
| 44 virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE { |
| 45 hover_ = true; |
| 46 SchedulePaint(); |
| 47 } |
| 48 |
| 49 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE { |
| 50 hover_ = false; |
| 51 SchedulePaint(); |
| 52 } |
| 53 |
| 54 virtual void OnPaintBackground(gfx::Canvas* canvas) OVERRIDE { |
| 55 if (hover_) |
| 56 hover_bg_->Paint(canvas, this); |
| 57 else |
| 58 views::TextButton::OnPaintBackground(canvas); |
| 59 } |
| 60 |
| 61 bool hover_; |
| 62 views::Background* hover_bg_; |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(TrayButton); |
| 65 }; |
| 66 |
| 67 class UserView : public views::View, |
| 68 public views::ButtonListener { |
| 69 public: |
| 70 UserView() { |
| 71 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, |
| 72 0, 0, 3)); |
| 73 |
| 74 ash::SystemTrayDelegate* tray = ash::Shell::GetInstance()->tray_delegate(); |
| 75 username_ = new views::Label(ASCIIToUTF16(tray->GetUserDisplayName())); |
| 76 username_->SetFont(username_->font().DeriveFont(2)); |
| 77 username_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 78 AddChildView(username_); |
| 79 |
| 80 email_ = new views::Label(ASCIIToUTF16(tray->GetUserEmail())); |
| 81 email_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 82 email_->SetEnabled(false); |
| 83 AddChildView(email_); |
| 84 |
| 85 views::View* button_container = new views::View; |
| 86 views::BoxLayout *layout = new |
| 87 views::BoxLayout(views::BoxLayout::kHorizontal, 0, 5, 0); |
| 88 layout->set_spread_blank_space(true); |
| 89 button_container->SetLayoutManager(layout); |
| 90 |
| 91 shutdown_ = new TrayButton(this, std::string("Shut down")); |
| 92 signout_ = new TrayButton(this, std::string("Sign out")); |
| 93 lock_ = new TrayButton(this, std::string("Lock")); |
| 94 button_container->AddChildView(shutdown_); |
| 95 button_container->AddChildView(signout_); |
| 96 button_container->AddChildView(lock_); |
| 97 |
| 98 AddChildView(button_container); |
| 99 } |
| 100 |
| 101 private: |
| 102 // Overridden from views::ButtonListener. |
| 103 virtual void ButtonPressed(views::Button* sender, |
| 104 const views::Event& event) OVERRIDE { |
| 105 ash::SystemTrayDelegate* tray = ash::Shell::GetInstance()->tray_delegate(); |
| 106 if (sender == shutdown_) |
| 107 tray->ShutDown(); |
| 108 else if (sender == signout_) |
| 109 tray->SignOut(); |
| 110 else if (sender == lock_) |
| 111 tray->LockScreen(); |
| 112 } |
| 113 |
| 114 views::Label* username_; |
| 115 views::Label* email_; |
| 116 |
| 117 TrayButton* shutdown_; |
| 118 TrayButton* signout_; |
| 119 TrayButton* lock_; |
| 120 |
| 121 DISALLOW_COPY_AND_ASSIGN(UserView); |
| 122 }; |
| 123 |
| 124 } // namespace |
| 125 |
| 126 namespace ash { |
| 127 namespace internal { |
| 128 |
| 129 TrayUser::TrayUser() { |
| 130 } |
| 131 |
| 132 TrayUser::~TrayUser() { |
| 133 } |
| 134 |
| 135 views::View* TrayUser::CreateTrayView() { |
| 136 views::ImageView* avatar = new views::ImageView; |
| 137 avatar->SetImage(ash::Shell::GetInstance()->tray_delegate()->GetUserImage()); |
| 138 avatar->SetImageSize(gfx::Size(32, 32)); |
| 139 return avatar; |
| 140 } |
| 141 |
| 142 views::View* TrayUser::CreateDefaultView() { |
| 143 return new UserView; |
| 144 } |
| 145 |
| 146 views::View* TrayUser::CreateDetailedView() { |
| 147 return NULL; |
| 148 } |
| 149 |
| 150 void TrayUser::DestroyTrayView() { |
| 151 } |
| 152 |
| 153 void TrayUser::DestroyDefaultView() { |
| 154 } |
| 155 |
| 156 void TrayUser::DestroyDetailedView() { |
| 157 } |
| 158 |
| 159 } // namespace internal |
| 160 } // namespace ash |
OLD | NEW |