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>
2021-04-15 22:00:35 +03:00
# include <Applications/Welcome/WelcomeWindowGML.h>
2021-10-18 14:48:26 +03:00
# include <LibConfig/Client.h>
2021-03-01 22:58:58 +03:00
# include <LibCore/File.h>
# include <LibGUI/Application.h>
# include <LibGUI/Button.h>
2021-10-18 14:48:26 +03:00
# include <LibGUI/CheckBox.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>
2022-04-09 10:28:38 +03:00
# include <LibGfx/Font/BitmapFont.h>
2021-03-01 22:58:58 +03:00
# include <LibGfx/Palette.h>
# include <LibMarkdown/Document.h>
2022-04-30 11:46:33 +03:00
# include <LibWebView/OutOfProcessWebView.h>
2021-03-01 22:58:58 +03:00
# include <serenity.h>
2021-04-15 18:00:22 +03:00
WelcomeWidget : : WelcomeWidget ( )
2021-03-01 22:58:58 +03:00
{
2021-04-15 22:00:35 +03:00
load_from_gml ( welcome_window_gml ) ;
2021-03-01 22:58:58 +03:00
auto & tip_frame = * find_descendant_of_type_named < GUI : : Frame > ( " tip_frame " ) ;
tip_frame . set_background_role ( Gfx : : ColorRole : : Base ) ;
tip_frame . set_fill_with_background_color ( true ) ;
auto & light_bulb_label = * find_descendant_of_type_named < GUI : : Label > ( " light_bulb_label " ) ;
2022-07-11 20:32:29 +03:00
light_bulb_label . set_icon ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/32x32/app-welcome.png " sv ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
2021-03-01 22:58:58 +03:00
2022-04-30 11:46:33 +03:00
m_web_view = * find_descendant_of_type_named < WebView : : OutOfProcessWebView > ( " web_view " ) ;
2021-03-01 22:58:58 +03:00
m_tip_label = * find_descendant_of_type_named < GUI : : Label > ( " tip_label " ) ;
m_next_button = * find_descendant_of_type_named < GUI : : Button > ( " next_button " ) ;
2022-07-11 20:32:29 +03:00
m_next_button - > set_icon ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/go-forward.png " sv ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
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 ) {
2021-03-01 22:58:58 +03:00
if ( ! tip_frame . is_visible ( ) ) {
m_web_view - > set_visible ( false ) ;
tip_frame . set_visible ( true ) ;
}
if ( m_tips . is_empty ( ) )
return ;
m_initial_tip_index + + ;
if ( m_initial_tip_index > = m_tips . size ( ) )
m_initial_tip_index = 0 ;
m_tip_label - > set_text ( m_tips [ m_initial_tip_index ] ) ;
} ;
m_help_button = * find_descendant_of_type_named < GUI : : Button > ( " help_button " ) ;
2022-07-11 20:32:29 +03:00
m_help_button - > set_icon ( Gfx : : Bitmap : : try_load_from_file ( " /res/icons/16x16/book-open.png " sv ) . release_value_but_fixme_should_propagate_errors ( ) ) ;
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
} ;
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 ( ) ) ;
tip_frame . set_visible ( ! tip_frame . is_visible ( ) ) ;
} ;
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-07-11 20:32:29 +03:00
auto exec_path = Config : : read_string ( " SystemServer " sv , " Welcome " sv , " Executable " sv , { } ) ;
2021-10-18 14:48:26 +03:00
m_startup_checkbox = * find_descendant_of_type_named < GUI : : CheckBox > ( " startup_checkbox " ) ;
m_startup_checkbox - > set_checked ( ! exec_path . is_empty ( ) ) ;
m_startup_checkbox - > on_checked = [ ] ( bool is_checked ) {
2022-07-11 20:32:29 +03:00
Config : : write_string ( " SystemServer " sv , " Welcome " sv , " Executable " sv , is_checked ? " /bin/Welcome " sv : " " sv ) ;
2021-10-18 14:48:26 +03:00
} ;
2021-03-01 22:58:58 +03:00
open_and_parse_readme_file ( ) ;
open_and_parse_tips_file ( ) ;
set_random_tip ( ) ;
}
2021-04-15 18:00:22 +03:00
void WelcomeWidget : : open_and_parse_tips_file ( )
2021-03-01 22:58:58 +03:00
{
auto file = Core : : File : : construct ( " /home/anon/Documents/tips.txt " ) ;
2021-05-12 12:26:43 +03:00
if ( ! file - > open ( Core : : OpenMode : : ReadOnly ) ) {
2021-03-01 22:58:58 +03:00
m_tip_label - > set_text ( " ~/Documents/tips.txt has gone missing! " ) ;
return ;
}
while ( file - > can_read_line ( ) ) {
auto line = file - > read_line ( ) ;
auto * ch = line . characters ( ) ;
switch ( * ch ) {
case ' \n ' :
case ' \r ' :
case ' \0 ' :
case ' # ' :
continue ;
}
m_tips . append ( line ) ;
}
}
2021-04-15 18:00:22 +03:00
void WelcomeWidget : : open_and_parse_readme_file ( )
2021-03-01 22:58:58 +03:00
{
2021-04-11 11:52:25 +03:00
auto file = Core : : File : : construct ( " /home/anon/README.md " ) ;
2021-05-12 12:26:43 +03:00
if ( ! file - > open ( Core : : OpenMode : : ReadOnly ) )
2021-03-01 22:58:58 +03:00
return ;
auto document = Markdown : : Document : : parse ( file - > read_all ( ) ) ;
if ( document ) {
auto html = document - > render_to_html ( ) ;
2021-04-11 11:52:25 +03:00
m_web_view - > load_html ( html , URL : : create_with_file_protocol ( " /home/anon/README.md " ) ) ;
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 ;
2021-09-01 18:37:05 +03:00
m_initial_tip_index = get_random_uniform ( m_tips . size ( ) ) ;
m_tip_label - > set_text ( m_tips [ m_initial_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 ( ) ) ;
2022-07-11 20:32:29 +03:00
static auto font = Gfx : : BitmapFont : : load_from_file ( " /res/fonts/MarietaRegular24.font " sv ) ;
painter . draw_text ( { 12 , 4 , 1 , 30 } , " Welcome to " sv , * font , Gfx : : TextAlignment : : CenterLeft , palette ( ) . base_text ( ) ) ;
painter . draw_text ( { 12 + font - > width ( " Welcome to " sv ) , 4 , 1 , 30 } , " Serenity " sv , font - > bold_variant ( ) , Gfx : : TextAlignment : : CenterLeft , palette ( ) . base_text ( ) ) ;
painter . draw_text ( { 12 + font - > width ( " Welcome to " sv ) + font - > bold_variant ( ) . width ( " Serenity " sv ) , 4 , 1 , 30 } , " OS " sv , font - > bold_variant ( ) , Gfx : : TextAlignment : : CenterLeft , palette ( ) . base ( ) = = palette ( ) . window ( ) ? palette ( ) . base_text ( ) : palette ( ) . base ( ) ) ;
2021-04-12 01:00:05 +03:00
}