2021-11-20 14:28:54 +00:00
/*
* Copyright ( c ) 2020 , Idan Horowitz < idan . horowitz @ serenityos . org >
2022-02-26 10:50:04 -07:00
* Copyright ( c ) 2021 - 2022 , the SerenityOS developers .
2021-11-20 14:28:54 +00:00
* Copyright ( c ) 2021 , Andreas Kling < kling @ serenityos . org >
2022-04-21 15:12:09 +01:00
* Copyright ( c ) 2021 - 2022 , Sam Atkins < atkinssj @ serenityos . org >
2021-11-20 14:28:54 +00:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
# include <LibGUI/Application.h>
# include <LibGUI/BoxLayout.h>
# include <LibGUI/Button.h>
2022-04-29 17:00:24 +01:00
# include <LibGUI/MessageBox.h>
2021-11-20 14:28:54 +00:00
# include <LibGUI/SettingsWindow.h>
# include <LibGUI/Widget.h>
namespace GUI {
2022-04-29 17:00:24 +01:00
void SettingsWindow : : set_modified ( bool modified )
{
Window : : set_modified ( modified ) ;
if ( m_apply_button )
m_apply_button - > set_enabled ( modified ) ;
}
2021-11-28 08:34:04 +01:00
ErrorOr < NonnullRefPtr < SettingsWindow > > SettingsWindow : : create ( String title , ShowDefaultsButton show_defaults_button )
2021-11-20 14:28:54 +00:00
{
2021-11-28 08:34:04 +01:00
auto window = TRY ( SettingsWindow : : try_create ( ) ) ;
2021-11-20 14:28:54 +00:00
2021-11-28 08:34:04 +01:00
window - > set_title ( move ( title ) ) ;
window - > resize ( 400 , 480 ) ;
window - > set_resizable ( false ) ;
window - > set_minimizable ( false ) ;
2021-11-20 14:28:54 +00:00
2021-11-28 08:34:04 +01:00
auto main_widget = TRY ( window - > try_set_main_widget < GUI : : Widget > ( ) ) ;
main_widget - > set_fill_with_background_color ( true ) ;
2021-12-02 12:53:02 +00:00
( void ) TRY ( main_widget - > try_set_layout < GUI : : VerticalBoxLayout > ( ) ) ;
2021-11-28 08:34:04 +01:00
main_widget - > layout ( ) - > set_margins ( 4 ) ;
main_widget - > layout ( ) - > set_spacing ( 6 ) ;
2021-11-20 14:28:54 +00:00
2021-11-28 08:34:04 +01:00
window - > m_tab_widget = TRY ( main_widget - > try_add < GUI : : TabWidget > ( ) ) ;
auto button_container = TRY ( main_widget - > try_add < GUI : : Widget > ( ) ) ;
2022-02-22 20:01:20 +01:00
button_container - > set_preferred_size ( { SpecialDimension : : Grow , SpecialDimension : : Fit } ) ;
2021-12-02 12:53:02 +00:00
( void ) TRY ( button_container - > try_set_layout < GUI : : HorizontalBoxLayout > ( ) ) ;
2021-11-28 08:34:04 +01:00
button_container - > layout ( ) - > set_spacing ( 6 ) ;
2021-11-20 14:28:54 +00:00
if ( show_defaults_button = = ShowDefaultsButton : : Yes ) {
2022-06-10 23:03:11 +02:00
window - > m_reset_button = TRY ( button_container - > try_add < GUI : : DialogButton > ( " Defaults " ) ) ;
2021-11-28 08:34:04 +01:00
window - > m_reset_button - > on_click = [ window = window - > make_weak_ptr < SettingsWindow > ( ) ] ( auto ) mutable {
2022-04-29 17:00:24 +01:00
window - > reset_default_values ( ) ;
2021-11-20 14:28:54 +00:00
} ;
}
2021-11-28 08:34:04 +01:00
TRY ( button_container - > layout ( ) - > try_add_spacer ( ) ) ;
2021-11-20 14:28:54 +00:00
2022-06-10 23:03:11 +02:00
window - > m_ok_button = TRY ( button_container - > try_add < GUI : : DialogButton > ( " OK " ) ) ;
2021-11-28 08:34:04 +01:00
window - > m_ok_button - > on_click = [ window = window - > make_weak_ptr < SettingsWindow > ( ) ] ( auto ) mutable {
2022-04-29 17:00:24 +01:00
window - > apply_settings ( ) ;
2021-11-20 14:28:54 +00:00
GUI : : Application : : the ( ) - > quit ( ) ;
} ;
2022-06-10 23:03:11 +02:00
window - > m_cancel_button = TRY ( button_container - > try_add < GUI : : DialogButton > ( " Cancel " ) ) ;
2021-11-28 08:34:04 +01:00
window - > m_cancel_button - > on_click = [ window = window - > make_weak_ptr < SettingsWindow > ( ) ] ( auto ) mutable {
2022-04-29 17:00:24 +01:00
window - > cancel_settings ( ) ;
2021-11-20 14:28:54 +00:00
GUI : : Application : : the ( ) - > quit ( ) ;
} ;
2022-06-10 23:03:11 +02:00
window - > m_apply_button = TRY ( button_container - > try_add < GUI : : DialogButton > ( " Apply " ) ) ;
2021-11-28 08:34:04 +01:00
window - > m_apply_button - > on_click = [ window = window - > make_weak_ptr < SettingsWindow > ( ) ] ( auto ) mutable {
2022-04-29 17:00:24 +01:00
window - > apply_settings ( ) ;
} ;
window - > on_close_request = [ window = window - > make_weak_ptr < SettingsWindow > ( ) ] ( ) mutable - > Window : : CloseRequestDecision {
if ( ! window - > is_modified ( ) )
return Window : : CloseRequestDecision : : Close ;
auto result = MessageBox : : show ( window , " Apply these settings before closing? " , " Unsaved changes " , MessageBox : : Type : : Warning , MessageBox : : InputType : : YesNoCancel ) ;
switch ( result ) {
2022-05-13 13:10:27 +01:00
case MessageBox : : ExecResult : : Yes :
2022-04-29 17:00:24 +01:00
window - > apply_settings ( ) ;
return Window : : CloseRequestDecision : : Close ;
2022-05-13 13:10:27 +01:00
case MessageBox : : ExecResult : : No :
2022-04-29 17:00:24 +01:00
window - > cancel_settings ( ) ;
return Window : : CloseRequestDecision : : Close ;
default :
return Window : : CloseRequestDecision : : StayOpen ;
}
2021-11-20 14:28:54 +00:00
} ;
2021-11-28 08:34:04 +01:00
return window ;
}
2022-04-21 15:34:53 +01:00
Optional < NonnullRefPtr < SettingsWindow : : Tab > > SettingsWindow : : get_tab ( StringView id ) const
{
auto tab = m_tabs . find ( id ) ;
if ( tab = = m_tabs . end ( ) )
return { } ;
return tab - > value ;
}
void SettingsWindow : : set_active_tab ( StringView id )
{
if ( auto tab = get_tab ( id ) ; tab . has_value ( ) )
m_tab_widget - > set_active_widget ( tab . value ( ) ) ;
}
2022-04-29 17:00:24 +01:00
void SettingsWindow : : apply_settings ( )
{
for ( auto & [ id , tab ] : m_tabs )
tab - > apply_settings ( ) ;
set_modified ( false ) ;
}
void SettingsWindow : : cancel_settings ( )
{
for ( auto & [ id , tab ] : m_tabs )
tab - > cancel_settings ( ) ;
}
void SettingsWindow : : reset_default_values ( )
{
for ( auto & [ id , tab ] : m_tabs ) {
tab - > reset_default_values ( ) ;
tab - > apply_settings ( ) ;
}
set_modified ( false ) ;
}
2021-11-20 14:28:54 +00:00
}