LibGUI: Move GUI::SettingsWindow setup out of the constructor

In order to propagate errors that occur during UI setup, we have to move
all that logic out of widget/window subclass constructors. This is a
first attempt at doing that, for GUI::SettingsWindow.
This commit is contained in:
Andreas Kling 2021-11-28 08:34:04 +01:00
parent 56c4f0aff5
commit 60c132d7d3
Notes: sideshowbarker 2024-07-18 05:01:22 +09:00
8 changed files with 48 additions and 38 deletions

View File

@ -24,7 +24,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-browser");
auto window = TRY(GUI::SettingsWindow::try_create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
auto window = TRY(GUI::SettingsWindow::create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
window->set_icon(app_icon.bitmap_for_size(16));
TRY(window->add_tab<BrowserSettingsWidget>("Browser"));

View File

@ -26,7 +26,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-display-settings");
auto window = TRY(GUI::SettingsWindow::try_create("Display Settings"));
auto window = TRY(GUI::SettingsWindow::create("Display Settings"));
TRY(window->add_tab<DisplaySettings::BackgroundSettingsWidget>("Background"));
TRY(window->add_tab<DisplaySettings::FontSettingsWidget>("Fonts"));
TRY(window->add_tab<DisplaySettings::MonitorSettingsWidget>("Monitor"));

View File

@ -29,7 +29,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-keyboard-settings");
auto window = TRY(GUI::SettingsWindow::try_create("Keyboard Settings"));
auto window = TRY(GUI::SettingsWindow::create("Keyboard Settings"));
window->set_icon(app_icon.bitmap_for_size(16));
TRY(window->add_tab<KeyboardSettingsWidget>("Keyboard"));

View File

@ -27,7 +27,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-mail");
auto window = GUI::SettingsWindow::construct("Mail Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes);
auto window = TRY(GUI::SettingsWindow::create("Mail Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
TRY(window->add_tab<MailSettingsWidget>("Mail"));
window->set_icon(app_icon.bitmap_for_size(16));

View File

@ -25,7 +25,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-mouse");
auto window = TRY(GUI::SettingsWindow::try_create("Mouse Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
auto window = TRY(GUI::SettingsWindow::create("Mouse Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
TRY(window->add_tab<MouseWidget>("Mouse"));
TRY(window->add_tab<ThemeWidget>("Cursor Theme"));
window->set_icon(app_icon.bitmap_for_size(16));

View File

@ -26,7 +26,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto app_icon = GUI::Icon::default_icon("app-terminal");
auto window = TRY(GUI::SettingsWindow::try_create("Terminal Settings"));
auto window = TRY(GUI::SettingsWindow::create("Terminal Settings"));
window->set_icon(app_icon.bitmap_for_size(16));
TRY(window->add_tab<TerminalSettingsMainWidget>("Terminal"));
TRY(window->add_tab<TerminalSettingsViewWidget>("View"));

View File

@ -15,60 +15,68 @@
namespace GUI {
SettingsWindow::SettingsWindow(StringView title, ShowDefaultsButton show_defaults_button)
ErrorOr<NonnullRefPtr<SettingsWindow>> SettingsWindow::create(String title, ShowDefaultsButton show_defaults_button)
{
set_title(title);
resize(400, 480);
set_resizable(false);
set_minimizable(false);
auto window = TRY(SettingsWindow::try_create());
auto& main_widget = set_main_widget<GUI::Widget>();
main_widget.set_fill_with_background_color(true);
main_widget.set_layout<GUI::VerticalBoxLayout>();
main_widget.layout()->set_margins(4);
main_widget.layout()->set_spacing(6);
window->set_title(move(title));
window->resize(400, 480);
window->set_resizable(false);
window->set_minimizable(false);
m_tab_widget = main_widget.add<GUI::TabWidget>();
auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
main_widget->set_fill_with_background_color(true);
TRY(main_widget->try_set_layout<GUI::VerticalBoxLayout>());
main_widget->layout()->set_margins(4);
main_widget->layout()->set_spacing(6);
auto& button_container = main_widget.add<GUI::Widget>();
button_container.set_shrink_to_fit(true);
button_container.set_layout<GUI::HorizontalBoxLayout>();
button_container.layout()->set_spacing(6);
window->m_tab_widget = TRY(main_widget->try_add<GUI::TabWidget>());
auto button_container = TRY(main_widget->try_add<GUI::Widget>());
button_container->set_shrink_to_fit(true);
TRY(button_container->try_set_layout<GUI::HorizontalBoxLayout>());
button_container->layout()->set_spacing(6);
if (show_defaults_button == ShowDefaultsButton::Yes) {
m_reset_button = button_container.add<GUI::Button>("Defaults");
m_reset_button->on_click = [&](auto) {
for (auto& tab : m_tabs) {
window->m_reset_button = TRY(button_container->try_add<GUI::Button>("Defaults"));
window->m_reset_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
for (auto& tab : window->m_tabs) {
tab.reset_default_values();
tab.apply_settings();
}
};
}
button_container.layout()->add_spacer();
TRY(button_container->layout()->try_add_spacer());
m_ok_button = button_container.add<GUI::Button>("OK");
m_ok_button->set_fixed_width(75);
m_ok_button->on_click = [&](auto) {
for (auto& tab : m_tabs)
window->m_ok_button = TRY(button_container->try_add<GUI::Button>("OK"));
window->m_ok_button->set_fixed_width(75);
window->m_ok_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
for (auto& tab : window->m_tabs)
tab.apply_settings();
GUI::Application::the()->quit();
};
m_cancel_button = button_container.add<GUI::Button>("Cancel");
m_cancel_button->set_fixed_width(75);
m_cancel_button->on_click = [&](auto) {
for (auto& tab : m_tabs)
window->m_cancel_button = TRY(button_container->try_add<GUI::Button>("Cancel"));
window->m_cancel_button->set_fixed_width(75);
window->m_cancel_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
for (auto& tab : window->m_tabs)
tab.cancel_settings();
GUI::Application::the()->quit();
};
m_apply_button = button_container.add<GUI::Button>("Apply");
m_apply_button->set_fixed_width(75);
m_apply_button->on_click = [&](auto) {
for (auto& tab : m_tabs)
window->m_apply_button = TRY(button_container->try_add<GUI::Button>("Apply"));
window->m_apply_button->set_fixed_width(75);
window->m_apply_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
for (auto& tab : window->m_tabs)
tab.apply_settings();
};
return window;
}
SettingsWindow::SettingsWindow()
{
}
SettingsWindow::~SettingsWindow()

View File

@ -29,6 +29,8 @@ public:
No,
};
static ErrorOr<NonnullRefPtr<SettingsWindow>> create(String title, ShowDefaultsButton = ShowDefaultsButton::No);
virtual ~SettingsWindow() override;
template<class T, class... Args>
@ -40,7 +42,7 @@ public:
}
private:
SettingsWindow(StringView title, ShowDefaultsButton = ShowDefaultsButton::No);
SettingsWindow();
RefPtr<GUI::TabWidget> m_tab_widget;
NonnullRefPtrVector<Tab> m_tabs;