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