SettingsWindow.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2020, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2022, the SerenityOS developers.
  4. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  5. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include <LibGUI/Application.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Button.h>
  12. #include <LibGUI/SettingsWindow.h>
  13. #include <LibGUI/Widget.h>
  14. namespace GUI {
  15. ErrorOr<NonnullRefPtr<SettingsWindow>> SettingsWindow::create(String title, ShowDefaultsButton show_defaults_button)
  16. {
  17. auto window = TRY(SettingsWindow::try_create());
  18. window->set_title(move(title));
  19. window->resize(400, 480);
  20. window->set_resizable(false);
  21. window->set_minimizable(false);
  22. auto main_widget = TRY(window->try_set_main_widget<GUI::Widget>());
  23. main_widget->set_fill_with_background_color(true);
  24. (void)TRY(main_widget->try_set_layout<GUI::VerticalBoxLayout>());
  25. main_widget->layout()->set_margins(4);
  26. main_widget->layout()->set_spacing(6);
  27. window->m_tab_widget = TRY(main_widget->try_add<GUI::TabWidget>());
  28. auto button_container = TRY(main_widget->try_add<GUI::Widget>());
  29. button_container->set_shrink_to_fit(true);
  30. (void)TRY(button_container->try_set_layout<GUI::HorizontalBoxLayout>());
  31. button_container->layout()->set_spacing(6);
  32. if (show_defaults_button == ShowDefaultsButton::Yes) {
  33. window->m_reset_button = TRY(button_container->try_add<GUI::Button>("Defaults"));
  34. window->m_reset_button->set_fixed_width(75);
  35. window->m_reset_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
  36. for (auto& [id, tab] : window->m_tabs) {
  37. tab->reset_default_values();
  38. tab->apply_settings();
  39. }
  40. };
  41. }
  42. TRY(button_container->layout()->try_add_spacer());
  43. window->m_ok_button = TRY(button_container->try_add<GUI::Button>("OK"));
  44. window->m_ok_button->set_fixed_width(75);
  45. window->m_ok_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
  46. for (auto& [id, tab] : window->m_tabs)
  47. tab->apply_settings();
  48. GUI::Application::the()->quit();
  49. };
  50. window->m_cancel_button = TRY(button_container->try_add<GUI::Button>("Cancel"));
  51. window->m_cancel_button->set_fixed_width(75);
  52. window->m_cancel_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
  53. for (auto& [id, tab] : window->m_tabs)
  54. tab->cancel_settings();
  55. GUI::Application::the()->quit();
  56. };
  57. window->m_apply_button = TRY(button_container->try_add<GUI::Button>("Apply"));
  58. window->m_apply_button->set_fixed_width(75);
  59. window->m_apply_button->on_click = [window = window->make_weak_ptr<SettingsWindow>()](auto) mutable {
  60. for (auto& [id, tab] : window->m_tabs)
  61. tab->apply_settings();
  62. };
  63. return window;
  64. }
  65. }