2021-03-01 22:58:58 +03:00
|
|
|
/*
|
2022-02-10 22:28:48 +03:00
|
|
|
* Copyright (c) 2021-2022, the SerenityOS developers.
|
2021-03-01 22:58:58 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-03-01 22:58:58 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-15 18:00:22 +03:00
|
|
|
#include "WelcomeWidget.h"
|
2021-09-01 18:37:05 +03:00
|
|
|
#include <AK/Random.h>
|
2023-01-29 16:59:36 +03:00
|
|
|
#include <AK/String.h>
|
2021-10-18 14:48:26 +03:00
|
|
|
#include <LibConfig/Client.h>
|
2022-09-23 17:52:40 +03:00
|
|
|
#include <LibCore/StandardPaths.h>
|
2021-03-01 22:58:58 +03:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
#include <LibGUI/Button.h>
|
2021-10-18 14:48:26 +03:00
|
|
|
#include <LibGUI/CheckBox.h>
|
2022-09-23 17:54:29 +03:00
|
|
|
#include <LibGUI/Frame.h>
|
2021-03-01 22:58:58 +03:00
|
|
|
#include <LibGUI/Label.h>
|
2021-04-12 01:00:05 +03:00
|
|
|
#include <LibGUI/Painter.h>
|
2022-05-11 21:41:33 +03:00
|
|
|
#include <LibGUI/Process.h>
|
2021-03-01 22:58:58 +03:00
|
|
|
#include <LibGfx/Palette.h>
|
|
|
|
|
2024-01-20 00:09:21 +03:00
|
|
|
namespace Welcome {
|
2024-01-29 21:18:19 +03:00
|
|
|
|
|
|
|
static String tips_file_path = "/usr/share/Welcome/tips.txt"_string;
|
|
|
|
|
2024-01-20 00:09:21 +03:00
|
|
|
ErrorOr<NonnullRefPtr<WelcomeWidget>> WelcomeWidget::create()
|
2021-03-01 22:58:58 +03:00
|
|
|
{
|
2024-01-20 00:09:21 +03:00
|
|
|
auto welcome_widget = TRY(WelcomeWidget::try_create());
|
2023-01-29 16:58:22 +03:00
|
|
|
TRY(welcome_widget->create_widgets());
|
|
|
|
|
|
|
|
return welcome_widget;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorOr<void> WelcomeWidget::create_widgets()
|
|
|
|
{
|
2023-01-29 17:03:12 +03:00
|
|
|
m_banner_widget = find_descendant_of_type_named<GUI::Widget>("welcome_banner");
|
2023-10-17 21:45:22 +03:00
|
|
|
m_banner_font = TRY(Gfx::BitmapFont::try_load_from_uri("resource://fonts/MarietaRegular24.font"sv));
|
2023-01-29 17:03:12 +03:00
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_web_view = find_descendant_of_type_named<WebView::OutOfProcessWebView>("web_view");
|
2023-08-24 13:07:14 +03:00
|
|
|
m_web_view->use_native_user_style_sheet();
|
2023-01-29 16:59:36 +03:00
|
|
|
auto path = TRY(String::formatted("{}/README.md", Core::StandardPaths::home_directory()));
|
2023-12-16 17:19:34 +03:00
|
|
|
m_web_view->load(URL::create_with_file_scheme(path.to_byte_string()));
|
2022-09-29 17:18:11 +03:00
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_tip_label = find_descendant_of_type_named<GUI::Label>("tip_label");
|
|
|
|
m_tip_frame = find_descendant_of_type_named<GUI::Frame>("tip_frame");
|
2021-03-01 22:58:58 +03:00
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_next_button = find_descendant_of_type_named<GUI::Button>("next_button");
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
m_next_button->on_click = [&](auto) {
|
2022-09-23 17:54:29 +03:00
|
|
|
m_web_view->set_visible(false);
|
|
|
|
m_tip_frame->set_visible(true);
|
2021-03-01 22:58:58 +03:00
|
|
|
if (m_tips.is_empty())
|
|
|
|
return;
|
2023-01-29 17:59:11 +03:00
|
|
|
m_tip_index++;
|
|
|
|
if (m_tip_index >= m_tips.size())
|
|
|
|
m_tip_index = 0;
|
2023-04-29 17:41:48 +03:00
|
|
|
m_tip_label->set_text(m_tips[m_tip_index]);
|
2021-03-01 22:58:58 +03:00
|
|
|
};
|
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_help_button = find_descendant_of_type_named<GUI::Button>("help_button");
|
2022-05-11 21:41:33 +03:00
|
|
|
m_help_button->on_click = [&](auto) {
|
|
|
|
GUI::Process::spawn_or_show_error(window(), "/bin/Help"sv);
|
2021-03-01 22:58:58 +03:00
|
|
|
};
|
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_new_button = find_descendant_of_type_named<GUI::Button>("new_button");
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
m_new_button->on_click = [&](auto) {
|
2021-03-01 22:58:58 +03:00
|
|
|
m_web_view->set_visible(!m_web_view->is_visible());
|
2022-09-23 17:54:29 +03:00
|
|
|
m_tip_frame->set_visible(!m_tip_frame->is_visible());
|
2021-03-01 22:58:58 +03:00
|
|
|
};
|
|
|
|
|
2022-09-23 17:54:29 +03:00
|
|
|
m_close_button = find_descendant_of_type_named<GUI::Button>("close_button");
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
m_close_button->on_click = [](auto) {
|
2021-03-01 22:58:58 +03:00
|
|
|
GUI::Application::the()->quit();
|
|
|
|
};
|
|
|
|
|
2022-09-23 16:57:56 +03:00
|
|
|
auto welcome = Config::list_groups("SystemServer"sv).first_matching([](auto& group) { return group == "Welcome"sv; });
|
|
|
|
m_startup_checkbox = find_descendant_of_type_named<GUI::CheckBox>("startup_checkbox");
|
|
|
|
m_startup_checkbox->set_checked(welcome.has_value());
|
2021-10-18 14:48:26 +03:00
|
|
|
m_startup_checkbox->on_checked = [](bool is_checked) {
|
2022-09-23 16:57:56 +03:00
|
|
|
if (is_checked)
|
|
|
|
Config::add_group("SystemServer"sv, "Welcome"sv);
|
|
|
|
else
|
|
|
|
Config::remove_group("SystemServer"sv, "Welcome"sv);
|
2021-10-18 14:48:26 +03:00
|
|
|
};
|
|
|
|
|
2022-09-23 17:52:40 +03:00
|
|
|
if (auto result = open_and_parse_tips_file(); result.is_error()) {
|
2024-01-29 21:18:19 +03:00
|
|
|
auto error = TRY(String::formatted("Opening \"{}\" failed: {}", tips_file_path, result.error()));
|
2023-04-29 17:41:48 +03:00
|
|
|
m_tip_label->set_text(error);
|
2022-09-23 17:52:40 +03:00
|
|
|
warnln(error);
|
|
|
|
}
|
|
|
|
|
2021-03-01 22:58:58 +03:00
|
|
|
set_random_tip();
|
2023-01-29 16:58:22 +03:00
|
|
|
|
|
|
|
return {};
|
2021-03-01 22:58:58 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 17:52:40 +03:00
|
|
|
ErrorOr<void> WelcomeWidget::open_and_parse_tips_file()
|
2021-03-01 22:58:58 +03:00
|
|
|
{
|
2024-01-29 21:18:19 +03:00
|
|
|
auto file = TRY(Core::File::open(tips_file_path, Core::File::OpenMode::Read));
|
2023-05-04 01:45:18 +03:00
|
|
|
auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
|
2022-09-23 17:52:40 +03:00
|
|
|
Array<u8, PAGE_SIZE> buffer;
|
2021-03-01 22:58:58 +03:00
|
|
|
|
2022-09-23 17:52:40 +03:00
|
|
|
while (TRY(buffered_file->can_read_line())) {
|
|
|
|
auto line = TRY(buffered_file->read_line(buffer));
|
|
|
|
if (line.starts_with('#') || line.is_empty())
|
2021-03-01 22:58:58 +03:00
|
|
|
continue;
|
2023-01-29 16:59:36 +03:00
|
|
|
TRY(m_tips.try_append(TRY(String::from_utf8(line))));
|
2021-03-01 22:58:58 +03:00
|
|
|
}
|
2022-09-23 17:52:40 +03:00
|
|
|
|
|
|
|
return {};
|
2021-03-01 22:58:58 +03:00
|
|
|
}
|
|
|
|
|
2021-04-15 18:00:22 +03:00
|
|
|
void WelcomeWidget::set_random_tip()
|
2021-03-01 22:58:58 +03:00
|
|
|
{
|
|
|
|
if (m_tips.is_empty())
|
|
|
|
return;
|
|
|
|
|
2023-01-29 17:59:11 +03:00
|
|
|
m_tip_index = get_random_uniform(m_tips.size());
|
2023-04-29 17:41:48 +03:00
|
|
|
m_tip_label->set_text(m_tips[m_tip_index]);
|
2021-03-01 22:58:58 +03:00
|
|
|
}
|
2021-04-12 01:00:05 +03:00
|
|
|
|
2021-04-15 18:00:22 +03:00
|
|
|
void WelcomeWidget::paint_event(GUI::PaintEvent& event)
|
2021-04-12 01:00:05 +03:00
|
|
|
{
|
|
|
|
GUI::Painter painter(*this);
|
|
|
|
painter.add_clip_rect(event.rect());
|
|
|
|
|
2023-01-29 17:03:12 +03:00
|
|
|
auto rect = m_banner_widget->relative_rect();
|
|
|
|
painter.draw_text(rect, "Welcome to "sv, *m_banner_font, Gfx::TextAlignment::CenterLeft, palette().base_text());
|
|
|
|
rect.set_x(rect.x() + static_cast<int>(ceilf(m_banner_font->width("Welcome to "sv))));
|
|
|
|
painter.draw_text(rect, "Serenity"sv, m_banner_font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().base_text());
|
|
|
|
rect.set_x(rect.x() + static_cast<int>(ceilf(m_banner_font->bold_variant().width("Serenity"sv))));
|
|
|
|
painter.draw_text(rect, "OS"sv, m_banner_font->bold_variant(), Gfx::TextAlignment::CenterLeft, palette().tray_text());
|
2021-04-12 01:00:05 +03:00
|
|
|
}
|
2024-01-29 21:18:19 +03:00
|
|
|
|
2024-01-20 00:09:21 +03:00
|
|
|
}
|