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/logout_button/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/status_area_widget.h" | 9 #include "ash/system/status_area_widget.h" |
10 #include "ash/system/tray/system_tray_delegate.h" | 10 #include "ash/system/tray/system_tray_delegate.h" |
11 #include "ash/system/tray/system_tray_notifier.h" | 11 #include "ash/system/tray/system_tray_notifier.h" |
12 #include "ash/system/tray/tray_constants.h" | 12 #include "ash/system/tray/tray_constants.h" |
13 #include "ash/system/tray/tray_utils.h" | 13 #include "ash/system/tray/tray_utils.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "grit/ash_resources.h" | 15 #include "grit/ash_resources.h" |
16 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
17 #include "ui/events/event.h" | 17 #include "ui/events/event.h" |
18 #include "ui/gfx/font.h" | 18 #include "ui/gfx/font.h" |
19 #include "ui/gfx/insets.h" | 19 #include "ui/gfx/insets.h" |
20 #include "ui/gfx/size.h" | 20 #include "ui/gfx/size.h" |
21 #include "ui/views/bubble/tray_bubble_view.h" | 21 #include "ui/views/bubble/tray_bubble_view.h" |
22 #include "ui/views/controls/button/label_button.h" | 22 #include "ui/views/controls/button/label_button.h" |
23 #include "ui/views/controls/button/label_button_border.h" | 23 #include "ui/views/controls/button/label_button_border.h" |
24 #include "ui/views/painter.h" | 24 #include "ui/views/painter.h" |
25 | 25 |
26 namespace ash { | 26 namespace ash { |
27 | |
28 namespace internal { | 27 namespace internal { |
29 | 28 |
30 namespace { | 29 namespace { |
31 | 30 |
32 const int kLogoutButtonHorizontalExtraPadding = 7; | 31 const int kLogoutButtonHorizontalExtraPadding = 7; |
33 | 32 |
34 const int kLogoutButtonNormalImages[] = { | 33 const int kLogoutButtonNormalImages[] = { |
35 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP_LEFT, | 34 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP_LEFT, |
36 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP, | 35 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP, |
37 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP_RIGHT, | 36 IDR_AURA_UBER_TRAY_LOGOUT_BUTTON_NORMAL_TOP_RIGHT, |
(...skipping 19 matching lines...) Expand all Loading... | |
57 | 56 |
58 class LogoutButton : public views::LabelButton { | 57 class LogoutButton : public views::LabelButton { |
59 public: | 58 public: |
60 LogoutButton(views::ButtonListener* listener); | 59 LogoutButton(views::ButtonListener* listener); |
61 virtual ~LogoutButton(); | 60 virtual ~LogoutButton(); |
62 | 61 |
63 private: | 62 private: |
64 DISALLOW_COPY_AND_ASSIGN(LogoutButton); | 63 DISALLOW_COPY_AND_ASSIGN(LogoutButton); |
65 }; | 64 }; |
66 | 65 |
66 class LogoutConfirmationDialogDelegate | |
67 : public LogoutConfirmationDialogView::Delegate { | |
68 | |
69 public: | |
70 LogoutConfirmationDialogDelegate() {} | |
71 virtual ~LogoutConfirmationDialogDelegate() {} | |
72 | |
73 virtual void LogoutCurrentUser() OVERRIDE; | |
74 virtual base::TimeTicks GetCurrentTime() OVERRIDE const; | |
75 virtual void ShowDialog(views::DialogDelegate* dialog) OVERRIDE; | |
76 | |
77 private: | |
78 DISALLOW_COPY_AND_ASSIGN(LogoutConfirmationDialogDelegate); | |
79 }; | |
stevenjb
2013/12/19 18:57:44
WS
binjin
2013/12/20 13:35:49
Done.
| |
67 } // namespace | 80 } // namespace |
68 | 81 |
69 LogoutButton::LogoutButton(views::ButtonListener* listener) | 82 LogoutButton::LogoutButton(views::ButtonListener* listener) |
70 : views::LabelButton(listener, base::string16()) { | 83 : views::LabelButton(listener, base::string16()) { |
71 SetupLabelForTray(label()); | 84 SetupLabelForTray(label()); |
72 SetFont(GetFont().DeriveFont(0, gfx::Font::NORMAL)); | 85 SetFont(GetFont().DeriveFont(0, gfx::Font::NORMAL)); |
73 for (size_t state = 0; state < views::Button::STATE_COUNT; ++state) | 86 for (size_t state = 0; state < views::Button::STATE_COUNT; ++state) |
74 SetTextColor(static_cast<views::Button::ButtonState>(state), SK_ColorWHITE); | 87 SetTextColor(static_cast<views::Button::ButtonState>(state), SK_ColorWHITE); |
75 | 88 |
76 views::LabelButtonBorder* border = | 89 views::LabelButtonBorder* border = |
(...skipping 10 matching lines...) Expand all Loading... | |
87 border->set_insets(insets); | 100 border->set_insets(insets); |
88 set_border(border); | 101 set_border(border); |
89 set_animate_on_state_change(false); | 102 set_animate_on_state_change(false); |
90 | 103 |
91 set_min_size(gfx::Size(0, GetShelfItemHeight())); | 104 set_min_size(gfx::Size(0, GetShelfItemHeight())); |
92 } | 105 } |
93 | 106 |
94 LogoutButton::~LogoutButton() { | 107 LogoutButton::~LogoutButton() { |
95 } | 108 } |
96 | 109 |
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 | |
97 LogoutButtonTray::LogoutButtonTray(StatusAreaWidget* status_area_widget) | 125 LogoutButtonTray::LogoutButtonTray(StatusAreaWidget* status_area_widget) |
98 : TrayBackgroundView(status_area_widget), | 126 : TrayBackgroundView(status_area_widget), |
99 button_(NULL), | 127 button_(NULL), |
100 login_status_(user::LOGGED_IN_NONE), | 128 login_status_(user::LOGGED_IN_NONE), |
101 show_logout_button_in_tray_(false) { | 129 show_logout_button_in_tray_(false), |
130 confirmation_dialog_(NULL), | |
131 confirmation_delegate_(new LogoutConfirmationDialogDelegate) { | |
102 button_ = new LogoutButton(this); | 132 button_ = new LogoutButton(this); |
103 tray_container()->AddChildView(button_); | 133 tray_container()->AddChildView(button_); |
104 tray_container()->set_border(NULL); | 134 tray_container()->set_border(NULL); |
105 Shell::GetInstance()->system_tray_notifier()->AddLogoutButtonObserver(this); | 135 // The Shell may not exist in some unit tests. |
136 if (Shell::HasInstance()) { | |
137 Shell::GetInstance()->system_tray_notifier()-> | |
138 AddLogoutButtonObserver(this); | |
139 } | |
106 } | 140 } |
107 | 141 |
108 LogoutButtonTray::~LogoutButtonTray() { | 142 LogoutButtonTray::~LogoutButtonTray() { |
109 Shell::GetInstance()->system_tray_notifier()-> | 143 EnsureConfirmationDialogIsClosed(); |
110 RemoveLogoutButtonObserver(this); | 144 // The Shell may not exist in some unit tests. |
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_; | |
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(); | |
111 } | 166 } |
112 | 167 |
113 void LogoutButtonTray::SetShelfAlignment(ShelfAlignment alignment) { | 168 void LogoutButtonTray::SetShelfAlignment(ShelfAlignment alignment) { |
114 TrayBackgroundView::SetShelfAlignment(alignment); | 169 TrayBackgroundView::SetShelfAlignment(alignment); |
115 tray_container()->set_border(NULL); | 170 tray_container()->set_border(NULL); |
116 } | 171 } |
117 | 172 |
118 base::string16 LogoutButtonTray::GetAccessibleNameForTray() { | 173 base::string16 LogoutButtonTray::GetAccessibleNameForTray() { |
119 return button_->GetText(); | 174 return button_->GetText(); |
120 } | 175 } |
121 | 176 |
122 void LogoutButtonTray::HideBubbleWithView( | 177 void LogoutButtonTray::HideBubbleWithView( |
123 const views::TrayBubbleView* bubble_view) { | 178 const views::TrayBubbleView* bubble_view) { |
124 } | 179 } |
125 | 180 |
126 bool LogoutButtonTray::ClickedOutsideBubble() { | 181 bool LogoutButtonTray::ClickedOutsideBubble() { |
127 return false; | 182 return false; |
128 } | 183 } |
129 | 184 |
130 void LogoutButtonTray::OnShowLogoutButtonInTrayChanged(bool show) { | 185 void LogoutButtonTray::OnShowLogoutButtonInTrayChanged(bool show) { |
131 show_logout_button_in_tray_ = show; | 186 show_logout_button_in_tray_ = show; |
132 UpdateVisibility(); | 187 UpdateVisibility(); |
133 } | 188 } |
134 | 189 |
190 void LogoutButtonTray::OnLogoutDialogDurationChanged(base::TimeDelta duration) { | |
191 dialog_duration_ = duration; | |
192 if (confirmation_dialog_) | |
193 confirmation_dialog_->UpdateDialogDuration(dialog_duration_); | |
194 } | |
195 | |
135 void LogoutButtonTray::ButtonPressed(views::Button* sender, | 196 void LogoutButtonTray::ButtonPressed(views::Button* sender, |
136 const ui::Event& event) { | 197 const ui::Event& event) { |
137 DCHECK_EQ(sender, button_); | 198 DCHECK_EQ(sender, button_); |
138 Shell::GetInstance()->system_tray_delegate()->SignOut(); | 199 // Sign out immediately if |dialog_duration_| is non-positive. |
200 if (dialog_duration_ <= base::TimeDelta()) | |
201 confirmation_delegate_->LogoutCurrentUser(); | |
202 else | |
203 EnsureConfirmationDialogIsShowing(); | |
139 } | 204 } |
140 | 205 |
141 void LogoutButtonTray::UpdateAfterLoginStatusChange( | 206 void LogoutButtonTray::UpdateAfterLoginStatusChange( |
142 user::LoginStatus login_status) { | 207 user::LoginStatus login_status) { |
143 login_status_ = login_status; | 208 login_status_ = login_status; |
144 const base::string16 title = | 209 const base::string16 title = |
145 GetLocalizedSignOutStringForStatus(login_status, false); | 210 GetLocalizedSignOutStringForStatus(login_status, false); |
146 button_->SetText(title); | 211 button_->SetText(title); |
147 button_->SetAccessibleName(title); | 212 button_->SetAccessibleName(title); |
148 UpdateVisibility(); | 213 UpdateVisibility(); |
149 } | 214 } |
150 | 215 |
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 | |
151 void LogoutButtonTray::UpdateVisibility() { | 225 void LogoutButtonTray::UpdateVisibility() { |
152 SetVisible(show_logout_button_in_tray_ && | 226 SetVisible(show_logout_button_in_tray_ && |
153 login_status_ != user::LOGGED_IN_NONE && | 227 login_status_ != user::LOGGED_IN_NONE && |
154 login_status_ != user::LOGGED_IN_LOCKED); | 228 login_status_ != user::LOGGED_IN_LOCKED); |
229 if (!show_logout_button_in_tray_) | |
230 EnsureConfirmationDialogIsClosed(); | |
155 } | 231 } |
156 | 232 |
157 } // namespace internal | 233 } // namespace internal |
158 } // namespace ash | 234 } // namespace ash |
OLD | NEW |