OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/system/logout_button/logout_button_tray.h" | 5 #include "ash/system/session/logout_button_tray.h" |
6 | 6 |
7 #include "ash/shelf/shelf_types.h" | 7 #include "ash/shelf/shelf_types.h" |
8 #include "ash/shell.h" | 8 #include "ash/shell.h" |
| 9 #include "ash/system/session/logout_confirmation_controller.h" |
9 #include "ash/system/status_area_widget.h" | 10 #include "ash/system/status_area_widget.h" |
10 #include "ash/system/tray/system_tray_delegate.h" | 11 #include "ash/system/tray/system_tray_delegate.h" |
11 #include "ash/system/tray/system_tray_notifier.h" | 12 #include "ash/system/tray/system_tray_notifier.h" |
12 #include "ash/system/tray/tray_constants.h" | 13 #include "ash/system/tray/tray_constants.h" |
13 #include "ash/system/tray/tray_utils.h" | 14 #include "ash/system/tray/tray_utils.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/scoped_ptr.h" |
15 #include "grit/ash_resources.h" | 17 #include "grit/ash_resources.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 18 #include "third_party/skia/include/core/SkColor.h" |
17 #include "ui/events/event.h" | 19 #include "ui/events/event.h" |
18 #include "ui/gfx/geometry/insets.h" | 20 #include "ui/gfx/geometry/insets.h" |
19 #include "ui/gfx/geometry/size.h" | 21 #include "ui/gfx/geometry/size.h" |
20 #include "ui/views/bubble/tray_bubble_view.h" | 22 #include "ui/views/bubble/tray_bubble_view.h" |
21 #include "ui/views/controls/button/label_button.h" | 23 #include "ui/views/controls/button/label_button.h" |
22 #include "ui/views/controls/button/label_button_border.h" | 24 #include "ui/views/controls/button/label_button_border.h" |
23 #include "ui/views/painter.h" | 25 #include "ui/views/painter.h" |
24 | 26 |
(...skipping 30 matching lines...) Expand all Loading... |
55 | 57 |
56 class LogoutButton : public views::LabelButton { | 58 class LogoutButton : public views::LabelButton { |
57 public: | 59 public: |
58 LogoutButton(views::ButtonListener* listener); | 60 LogoutButton(views::ButtonListener* listener); |
59 virtual ~LogoutButton(); | 61 virtual ~LogoutButton(); |
60 | 62 |
61 private: | 63 private: |
62 DISALLOW_COPY_AND_ASSIGN(LogoutButton); | 64 DISALLOW_COPY_AND_ASSIGN(LogoutButton); |
63 }; | 65 }; |
64 | 66 |
65 class LogoutConfirmationDialogDelegate | |
66 : public LogoutConfirmationDialogView::Delegate { | |
67 | |
68 public: | |
69 LogoutConfirmationDialogDelegate() {} | |
70 virtual ~LogoutConfirmationDialogDelegate() {} | |
71 | |
72 virtual void LogoutCurrentUser() OVERRIDE; | |
73 virtual base::TimeTicks GetCurrentTime() const OVERRIDE; | |
74 virtual void ShowDialog(views::DialogDelegate* dialog) OVERRIDE; | |
75 | |
76 private: | |
77 DISALLOW_COPY_AND_ASSIGN(LogoutConfirmationDialogDelegate); | |
78 }; | |
79 | |
80 } // namespace | 67 } // namespace |
81 | 68 |
82 LogoutButton::LogoutButton(views::ButtonListener* listener) | 69 LogoutButton::LogoutButton(views::ButtonListener* listener) |
83 : views::LabelButton(listener, base::string16()) { | 70 : views::LabelButton(listener, base::string16()) { |
84 SetupLabelForTray(label()); | 71 SetupLabelForTray(label()); |
85 SetFontList(label()->font_list()); | 72 SetFontList(label()->font_list()); |
86 for (size_t state = 0; state < views::Button::STATE_COUNT; ++state) | 73 for (size_t state = 0; state < views::Button::STATE_COUNT; ++state) |
87 SetTextColor(static_cast<views::Button::ButtonState>(state), SK_ColorWHITE); | 74 SetTextColor(static_cast<views::Button::ButtonState>(state), SK_ColorWHITE); |
88 | 75 |
89 scoped_ptr<views::LabelButtonBorder> border( | 76 scoped_ptr<views::LabelButtonBorder> border( |
(...skipping 10 matching lines...) Expand all Loading... |
100 border->set_insets(insets); | 87 border->set_insets(insets); |
101 SetBorder(border.PassAs<views::Border>()); | 88 SetBorder(border.PassAs<views::Border>()); |
102 set_animate_on_state_change(false); | 89 set_animate_on_state_change(false); |
103 | 90 |
104 set_min_size(gfx::Size(0, GetShelfItemHeight())); | 91 set_min_size(gfx::Size(0, GetShelfItemHeight())); |
105 } | 92 } |
106 | 93 |
107 LogoutButton::~LogoutButton() { | 94 LogoutButton::~LogoutButton() { |
108 } | 95 } |
109 | 96 |
110 void LogoutConfirmationDialogDelegate::LogoutCurrentUser() { | |
111 Shell::GetInstance()->system_tray_delegate()->SignOut(); | |
112 } | |
113 | |
114 base::TimeTicks LogoutConfirmationDialogDelegate::GetCurrentTime() const { | |
115 return base::TimeTicks::Now(); | |
116 } | |
117 | |
118 void LogoutConfirmationDialogDelegate::ShowDialog( | |
119 views::DialogDelegate *dialog) { | |
120 views::DialogDelegate::CreateDialogWidget( | |
121 dialog, ash::Shell::GetPrimaryRootWindow(), NULL); | |
122 dialog->GetWidget()->Show(); | |
123 } | |
124 | |
125 LogoutButtonTray::LogoutButtonTray(StatusAreaWidget* status_area_widget) | 97 LogoutButtonTray::LogoutButtonTray(StatusAreaWidget* status_area_widget) |
126 : TrayBackgroundView(status_area_widget), | 98 : TrayBackgroundView(status_area_widget), |
127 button_(NULL), | 99 button_(NULL), |
128 login_status_(user::LOGGED_IN_NONE), | 100 login_status_(user::LOGGED_IN_NONE), |
129 show_logout_button_in_tray_(false), | 101 show_logout_button_in_tray_(false) { |
130 confirmation_dialog_(NULL), | |
131 confirmation_delegate_(new LogoutConfirmationDialogDelegate) { | |
132 button_ = new LogoutButton(this); | 102 button_ = new LogoutButton(this); |
133 tray_container()->AddChildView(button_); | 103 tray_container()->AddChildView(button_); |
134 tray_container()->SetBorder(views::Border::NullBorder()); | 104 tray_container()->SetBorder(views::Border::NullBorder()); |
135 // The Shell may not exist in some unit tests. | 105 Shell::GetInstance()->system_tray_notifier()->AddLogoutButtonObserver(this); |
136 if (Shell::HasInstance()) { | |
137 Shell::GetInstance()->system_tray_notifier()-> | |
138 AddLogoutButtonObserver(this); | |
139 } | |
140 } | 106 } |
141 | 107 |
142 LogoutButtonTray::~LogoutButtonTray() { | 108 LogoutButtonTray::~LogoutButtonTray() { |
143 EnsureConfirmationDialogIsClosed(); | 109 Shell::GetInstance()->system_tray_notifier()-> |
144 // The Shell may not exist in some unit tests. | 110 RemoveLogoutButtonObserver(this); |
145 if (Shell::HasInstance()) { | |
146 Shell::GetInstance()->system_tray_notifier()-> | |
147 RemoveLogoutButtonObserver(this); | |
148 } | |
149 } | |
150 | |
151 bool LogoutButtonTray::IsConfirmationDialogShowing() const { | |
152 return confirmation_dialog_ != NULL; | |
153 } | |
154 | |
155 void LogoutButtonTray::EnsureConfirmationDialogIsShowing() { | |
156 if (!confirmation_dialog_) { | |
157 confirmation_dialog_ = new LogoutConfirmationDialogView( | |
158 this, confirmation_delegate_.get()); | |
159 confirmation_dialog_->Show(dialog_duration_); | |
160 } | |
161 } | |
162 | |
163 void LogoutButtonTray::EnsureConfirmationDialogIsClosed() { | |
164 if (confirmation_dialog_) | |
165 confirmation_dialog_->Close(); | |
166 } | 111 } |
167 | 112 |
168 void LogoutButtonTray::SetShelfAlignment(ShelfAlignment alignment) { | 113 void LogoutButtonTray::SetShelfAlignment(ShelfAlignment alignment) { |
169 TrayBackgroundView::SetShelfAlignment(alignment); | 114 TrayBackgroundView::SetShelfAlignment(alignment); |
170 tray_container()->SetBorder(views::Border::NullBorder()); | 115 tray_container()->SetBorder(views::Border::NullBorder()); |
171 } | 116 } |
172 | 117 |
173 base::string16 LogoutButtonTray::GetAccessibleNameForTray() { | 118 base::string16 LogoutButtonTray::GetAccessibleNameForTray() { |
174 return button_->GetText(); | 119 return button_->GetText(); |
175 } | 120 } |
176 | 121 |
177 void LogoutButtonTray::HideBubbleWithView( | 122 void LogoutButtonTray::HideBubbleWithView( |
178 const views::TrayBubbleView* bubble_view) { | 123 const views::TrayBubbleView* bubble_view) { |
179 } | 124 } |
180 | 125 |
181 bool LogoutButtonTray::ClickedOutsideBubble() { | 126 bool LogoutButtonTray::ClickedOutsideBubble() { |
182 return false; | 127 return false; |
183 } | 128 } |
184 | 129 |
185 void LogoutButtonTray::OnShowLogoutButtonInTrayChanged(bool show) { | 130 void LogoutButtonTray::OnShowLogoutButtonInTrayChanged(bool show) { |
186 show_logout_button_in_tray_ = show; | 131 show_logout_button_in_tray_ = show; |
187 UpdateVisibility(); | 132 UpdateVisibility(); |
188 } | 133 } |
189 | 134 |
190 void LogoutButtonTray::OnLogoutDialogDurationChanged(base::TimeDelta duration) { | 135 void LogoutButtonTray::OnLogoutDialogDurationChanged(base::TimeDelta duration) { |
191 dialog_duration_ = duration; | 136 dialog_duration_ = duration; |
192 if (confirmation_dialog_) | |
193 confirmation_dialog_->UpdateDialogDuration(dialog_duration_); | |
194 } | 137 } |
195 | 138 |
196 void LogoutButtonTray::ButtonPressed(views::Button* sender, | 139 void LogoutButtonTray::ButtonPressed(views::Button* sender, |
197 const ui::Event& event) { | 140 const ui::Event& event) { |
198 DCHECK_EQ(sender, button_); | 141 DCHECK_EQ(sender, button_); |
199 // Sign out immediately if |dialog_duration_| is non-positive. | 142 if (dialog_duration_ <= base::TimeDelta()) { |
200 if (dialog_duration_ <= base::TimeDelta()) | 143 // Sign out immediately if |dialog_duration_| is non-positive. |
201 confirmation_delegate_->LogoutCurrentUser(); | 144 Shell::GetInstance()->system_tray_delegate()->SignOut(); |
202 else | 145 } else if (Shell::GetInstance()->logout_confirmation_controller()) { |
203 EnsureConfirmationDialogIsShowing(); | 146 Shell::GetInstance()->logout_confirmation_controller()->ConfirmLogout( |
| 147 base::TimeTicks::Now() + dialog_duration_); |
| 148 } |
204 } | 149 } |
205 | 150 |
206 void LogoutButtonTray::UpdateAfterLoginStatusChange( | 151 void LogoutButtonTray::UpdateAfterLoginStatusChange( |
207 user::LoginStatus login_status) { | 152 user::LoginStatus login_status) { |
208 login_status_ = login_status; | 153 login_status_ = login_status; |
209 const base::string16 title = | 154 const base::string16 title = |
210 GetLocalizedSignOutStringForStatus(login_status, false); | 155 GetLocalizedSignOutStringForStatus(login_status, false); |
211 button_->SetText(title); | 156 button_->SetText(title); |
212 button_->SetAccessibleName(title); | 157 button_->SetAccessibleName(title); |
213 UpdateVisibility(); | 158 UpdateVisibility(); |
214 } | 159 } |
215 | 160 |
216 void LogoutButtonTray::ReleaseConfirmationDialog() { | |
217 confirmation_dialog_ = NULL; | |
218 } | |
219 | |
220 void LogoutButtonTray::SetDelegateForTest( | |
221 scoped_ptr<LogoutConfirmationDialogView::Delegate> delegate) { | |
222 confirmation_delegate_ = delegate.Pass(); | |
223 } | |
224 | |
225 void LogoutButtonTray::UpdateVisibility() { | 161 void LogoutButtonTray::UpdateVisibility() { |
226 SetVisible(show_logout_button_in_tray_ && | 162 SetVisible(show_logout_button_in_tray_ && |
227 login_status_ != user::LOGGED_IN_NONE && | 163 login_status_ != user::LOGGED_IN_NONE && |
228 login_status_ != user::LOGGED_IN_LOCKED); | 164 login_status_ != user::LOGGED_IN_LOCKED); |
229 if (!show_logout_button_in_tray_) | |
230 EnsureConfirmationDialogIsClosed(); | |
231 } | 165 } |
232 | 166 |
233 } // namespace internal | 167 } // namespace internal |
234 } // namespace ash | 168 } // namespace ash |
OLD | NEW |