glanceables_welcome_label.cc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2022 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. #include "ash/glanceables/glanceables_welcome_label.h"
  5. #include <string>
  6. #include "ash/public/cpp/session/session_types.h"
  7. #include "ash/session/session_controller_impl.h"
  8. #include "ash/shell.h"
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "base/check.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "third_party/skia/include/core/SkColor.h"
  13. #include "ui/base/l10n/l10n_util.h"
  14. #include "ui/base/metadata/metadata_impl_macros.h"
  15. #include "ui/gfx/font.h"
  16. #include "ui/gfx/font_list.h"
  17. #include "ui/views/controls/label.h"
  18. namespace ash {
  19. GlanceablesWelcomeLabel::GlanceablesWelcomeLabel() {
  20. SetAutoColorReadabilityEnabled(false);
  21. // TODO(crbug.com/1353488): Make font size customizable.
  22. SetFontList(gfx::FontList({"Google Sans"}, gfx::Font::FontStyle::NORMAL, 48,
  23. gfx::Font::Weight::BOLD));
  24. SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT);
  25. SetMultiLine(false);
  26. // TODO(crbug.com/1353488): Change to dynamic greeting based on system time.
  27. SetText(l10n_util::GetStringFUTF16(IDS_GLANCEABLES_WELCOME_LABEL,
  28. GetUserGivenName()));
  29. }
  30. void GlanceablesWelcomeLabel::OnThemeChanged() {
  31. views::Label::OnThemeChanged();
  32. // TODO(crbug.com/1353488): Use color provider.
  33. SetEnabledColor(SK_ColorWHITE);
  34. }
  35. std::u16string GlanceablesWelcomeLabel::GetUserGivenName() const {
  36. DCHECK(Shell::Get());
  37. const auto* session_controller = Shell::Get()->session_controller();
  38. DCHECK(session_controller);
  39. const auto account_id = session_controller->GetActiveAccountId();
  40. if (account_id.empty()) {
  41. // Prevents failures in `GlanceablesViewTest` and
  42. // `GlanceablesControllerTest`.
  43. // TODO(crbug.com/1353119): Remove this after switching to `AshTestBase`.
  44. return u"";
  45. }
  46. const auto* user_session =
  47. session_controller->GetUserSessionByAccountId(account_id);
  48. DCHECK(user_session);
  49. return base::UTF8ToUTF16(user_session->user_info.given_name);
  50. }
  51. BEGIN_METADATA(GlanceablesWelcomeLabel, views::Label)
  52. END_METADATA
  53. } // namespace ash