WelcomeWidget.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2021-2022, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "WelcomeWidget.h"
  7. #include <AK/Random.h>
  8. #include <AK/String.h>
  9. #include <Applications/Welcome/WelcomeWindowGML.h>
  10. #include <LibConfig/Client.h>
  11. #include <LibCore/StandardPaths.h>
  12. #include <LibGUI/Application.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/CheckBox.h>
  15. #include <LibGUI/Frame.h>
  16. #include <LibGUI/Label.h>
  17. #include <LibGUI/Painter.h>
  18. #include <LibGUI/Process.h>
  19. #include <LibGfx/Palette.h>
  20. ErrorOr<NonnullRefPtr<WelcomeWidget>> WelcomeWidget::try_create()
  21. {
  22. auto welcome_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WelcomeWidget()));
  23. TRY(welcome_widget->create_widgets());
  24. return welcome_widget;
  25. }
  26. ErrorOr<void> WelcomeWidget::create_widgets()
  27. {
  28. TRY(load_from_gml(welcome_window_gml));
  29. m_banner_widget = find_descendant_of_type_named<GUI::Widget>("welcome_banner");
  30. m_banner_font = TRY(Gfx::BitmapFont::try_load_from_file("/res/fonts/MarietaRegular24.font"sv));
  31. m_web_view = find_descendant_of_type_named<WebView::OutOfProcessWebView>("web_view");
  32. auto path = TRY(String::formatted("{}/README.md", Core::StandardPaths::home_directory()));
  33. m_web_view->load(URL::create_with_file_scheme(path.to_deprecated_string()));
  34. m_tip_label = find_descendant_of_type_named<GUI::Label>("tip_label");
  35. m_tip_frame = find_descendant_of_type_named<GUI::Frame>("tip_frame");
  36. m_next_button = find_descendant_of_type_named<GUI::Button>("next_button");
  37. m_next_button->on_click = [&](auto) {
  38. m_web_view->set_visible(false);
  39. m_tip_frame->set_visible(true);
  40. if (m_tips.is_empty())
  41. return;
  42. m_tip_index++;
  43. if (m_tip_index >= m_tips.size())
  44. m_tip_index = 0;
  45. m_tip_label->set_text(m_tips[m_tip_index]);
  46. };
  47. m_help_button = find_descendant_of_type_named<GUI::Button>("help_button");
  48. m_help_button->on_click = [&](auto) {
  49. GUI::Process::spawn_or_show_error(window(), "/bin/Help"sv);
  50. };
  51. m_new_button = find_descendant_of_type_named<GUI::Button>("new_button");
  52. m_new_button->on_click = [&](auto) {
  53. m_web_view->set_visible(!m_web_view->is_visible());
  54. m_tip_frame->set_visible(!m_tip_frame->is_visible());
  55. };
  56. m_close_button = find_descendant_of_type_named<GUI::Button>("close_button");
  57. m_close_button->on_click = [](auto) {
  58. GUI::Application::the()->quit();
  59. };
  60. auto welcome = Config::list_groups("SystemServer"sv).first_matching([](auto& group) { return group == "Welcome"sv; });
  61. m_startup_checkbox = find_descendant_of_type_named<GUI::CheckBox>("startup_checkbox");
  62. m_startup_checkbox->set_checked(welcome.has_value());
  63. m_startup_checkbox->on_checked = [](bool is_checked) {
  64. if (is_checked)
  65. Config::add_group("SystemServer"sv, "Welcome"sv);
  66. else
  67. Config::remove_group("SystemServer"sv, "Welcome"sv);
  68. };
  69. if (auto result = open_and_parse_tips_file(); result.is_error()) {
  70. auto path = TRY(String::formatted("{}/tips.txt", Core::StandardPaths::documents_directory()));
  71. auto error = TRY(String::formatted("Opening \"{}\" failed: {}", path, result.error()));
  72. m_tip_label->set_text(error);
  73. warnln(error);
  74. }
  75. set_random_tip();
  76. return {};
  77. }
  78. ErrorOr<void> WelcomeWidget::open_and_parse_tips_file()
  79. {
  80. auto path = TRY(String::formatted("{}/tips.txt", Core::StandardPaths::documents_directory()));
  81. auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
  82. auto buffered_file = TRY(Core::BufferedFile::create(move(file)));
  83. Array<u8, PAGE_SIZE> buffer;
  84. while (TRY(buffered_file->can_read_line())) {
  85. auto line = TRY(buffered_file->read_line(buffer));
  86. if (line.starts_with('#') || line.is_empty())
  87. continue;
  88. TRY(m_tips.try_append(TRY(String::from_utf8(line))));
  89. }
  90. return {};
  91. }
  92. void WelcomeWidget::set_random_tip()
  93. {
  94. if (m_tips.is_empty())
  95. return;
  96. m_tip_index = get_random_uniform(m_tips.size());
  97. m_tip_label->set_text(m_tips[m_tip_index]);
  98. }
  99. void WelcomeWidget::paint_event(GUI::PaintEvent& event)
  100. {
  101. GUI::Painter painter(*this);
  102. painter.add_clip_rect(event.rect());
  103. auto rect = m_banner_widget->relative_rect();
  104. painter.draw_text(rect, "Welcome to "sv, *m_banner_font, Gfx::TextAlignment::CenterLeft, palette().base_text());
  105. rect.set_x(rect.x() + static_cast<int>(ceilf(m_banner_font->width("Welcome to "sv))));
  106. painter.draw_text(rect, "Serenity"sv, m_banner_font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base_text());
  107. rect.set_x(rect.x() + static_cast<int>(ceilf(m_banner_font->bold_variant().width("Serenity"sv))));
  108. painter.draw_text(rect, "OS"sv, m_banner_font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().tray_text());
  109. }