1 // Copyright 2017 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/login/ui/login_big_user_view.h"
6
7 #include "ash/public/cpp/login_constants.h"
8 #include "ash/shell.h"
9 #include "ash/style/default_color_constants.h"
10 #include "ash/style/default_colors.h"
11 #include "ash/wallpaper/wallpaper_controller_impl.h"
12 #include "components/account_id/account_id.h"
13 #include "ui/views/background.h"
14 #include "ui/views/layout/fill_layout.h"
15
16 namespace ash {
17
18 namespace {
19
IsPublicAccountUser(const LoginUserInfo & user)20 bool IsPublicAccountUser(const LoginUserInfo& user) {
21 return user.basic_user_info.type == user_manager::USER_TYPE_PUBLIC_ACCOUNT;
22 }
23
24 // Returns true if either a or b have a value, but not both.
OnlyOneSet(views::View * a,views::View * b)25 bool OnlyOneSet(views::View* a, views::View* b) {
26 return !!a ^ !!b;
27 }
28
29 } // namespace
30
TestApi(LoginBigUserView * view)31 LoginBigUserView::TestApi::TestApi(LoginBigUserView* view) : view_(view) {}
32
33 LoginBigUserView::TestApi::~TestApi() = default;
34
Remove()35 void LoginBigUserView::TestApi::Remove() {
36 view_->auth_user_callbacks_.on_remove.Run();
37 }
38
LoginBigUserView(const LoginUserInfo & user,const LoginAuthUserView::Callbacks & auth_user_callbacks,const LoginPublicAccountUserView::Callbacks & public_account_callbacks)39 LoginBigUserView::LoginBigUserView(
40 const LoginUserInfo& user,
41 const LoginAuthUserView::Callbacks& auth_user_callbacks,
42 const LoginPublicAccountUserView::Callbacks& public_account_callbacks)
43 : NonAccessibleView(),
44 auth_user_callbacks_(auth_user_callbacks),
45 public_account_callbacks_(public_account_callbacks) {
46 SetLayoutManager(std::make_unique<views::FillLayout>());
47
48 // Creates either |auth_user_| or |public_account_|.
49 CreateChildView(user);
50
51 observer_.Add(Shell::Get()->wallpaper_controller());
52 // Adding the observer will not run OnWallpaperBlurChanged; run it now to set
53 // the initial state.
54 OnWallpaperBlurChanged();
55 }
56
57 LoginBigUserView::~LoginBigUserView() = default;
58
CreateChildView(const LoginUserInfo & user)59 void LoginBigUserView::CreateChildView(const LoginUserInfo& user) {
60 if (IsPublicAccountUser(user))
61 CreatePublicAccount(user);
62 else
63 CreateAuthUser(user);
64 }
65
UpdateForUser(const LoginUserInfo & user)66 void LoginBigUserView::UpdateForUser(const LoginUserInfo& user) {
67 // Rebuild child view for the following swap case:
68 // 1. Public Account -> Auth User
69 // 2. Auth User -> Public Account
70 if (IsPublicAccountUser(user) != IsPublicAccountUser(GetCurrentUser()))
71 CreateChildView(user);
72
73 DCHECK(OnlyOneSet(public_account_, auth_user_));
74 if (public_account_) {
75 public_account_->UpdateForUser(user);
76 }
77 if (auth_user_)
78 auth_user_->UpdateForUser(user);
79 }
80
GetCurrentUser() const81 const LoginUserInfo& LoginBigUserView::GetCurrentUser() const {
82 DCHECK(OnlyOneSet(public_account_, auth_user_));
83 if (public_account_) {
84 return public_account_->current_user();
85 }
86 return auth_user_->current_user();
87 }
88
GetUserView()89 LoginUserView* LoginBigUserView::GetUserView() {
90 DCHECK(OnlyOneSet(public_account_, auth_user_));
91 if (public_account_) {
92 return public_account_->user_view();
93 }
94 return auth_user_->user_view();
95 }
96
IsAuthEnabled() const97 bool LoginBigUserView::IsAuthEnabled() const {
98 DCHECK(OnlyOneSet(public_account_, auth_user_));
99 if (public_account_) {
100 return public_account_->auth_enabled();
101 }
102 return auth_user_->auth_methods() != LoginAuthUserView::AUTH_NONE;
103 }
104
RequestFocus()105 void LoginBigUserView::RequestFocus() {
106 DCHECK(OnlyOneSet(public_account_, auth_user_));
107 if (public_account_) {
108 return public_account_->RequestFocus();
109 }
110 return auth_user_->RequestFocus();
111 }
112
113
OnWallpaperBlurChanged()114 void LoginBigUserView::OnWallpaperBlurChanged() {
115 if (Shell::Get()->wallpaper_controller()->IsWallpaperBlurredForLockState()) {
116 SetPaintToLayer(ui::LayerType::LAYER_NOT_DRAWN);
117 SetBackground(nullptr);
118 } else {
119 SetPaintToLayer();
120 layer()->SetFillsBoundsOpaquely(false);
121 SetBackground(views::CreateBackgroundFromPainter(
122 views::Painter::CreateSolidRoundRectPainter(
123 AshColorProvider::Get()->GetShieldLayerColor(
124 AshColorProvider::ShieldLayerType::kShield80),
125 login_constants::kNonBlurredWallpaperBackgroundRadiusDp)));
126 }
127 }
128
CreateAuthUser(const LoginUserInfo & user)129 void LoginBigUserView::CreateAuthUser(const LoginUserInfo& user) {
130 DCHECK(!IsPublicAccountUser(user));
131 DCHECK(!auth_user_);
132
133 auth_user_ = new LoginAuthUserView(user, auth_user_callbacks_);
134 delete public_account_;
135 public_account_ = nullptr;
136 AddChildView(auth_user_);
137 }
138
CreatePublicAccount(const LoginUserInfo & user)139 void LoginBigUserView::CreatePublicAccount(const LoginUserInfo& user) {
140 DCHECK(IsPublicAccountUser(user));
141 DCHECK(!public_account_);
142
143 public_account_ =
144 new LoginPublicAccountUserView(user, public_account_callbacks_);
145 delete auth_user_;
146 auth_user_ = nullptr;
147 AddChildView(public_account_);
148 }
149
150 } // namespace ash
151