From 23341f35cb631b5d57e7ec8d6cb67b075efdab30 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Sat, 20 Nov 2021 14:28:54 +0000 Subject: [PATCH] LibGUI: Add SettingsWindow class The FooSettings apps have quite a lot of boilerplate just around creating a tabbed window with the same styling and the same row of buttons along the bottom. So, let's extract that out into a class we can reuse! :^) You create a SettingsWindow instead of a regular Window, passing a title and a flag to determine if a "Defaults" button is shown. Then call add_tab() to add tabs to it. Tabs are widgets extending SettingsWindow::Tab, which has methods for saving and resetting the values. --- Userland/Libraries/LibGUI/CMakeLists.txt | 1 + Userland/Libraries/LibGUI/SettingsWindow.cpp | 76 ++++++++++++++++++++ Userland/Libraries/LibGUI/SettingsWindow.h | 53 ++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 Userland/Libraries/LibGUI/SettingsWindow.cpp create mode 100644 Userland/Libraries/LibGUI/SettingsWindow.h diff --git a/Userland/Libraries/LibGUI/CMakeLists.txt b/Userland/Libraries/LibGUI/CMakeLists.txt index 21f1c4c8f89..1648dc96342 100644 --- a/Userland/Libraries/LibGUI/CMakeLists.txt +++ b/Userland/Libraries/LibGUI/CMakeLists.txt @@ -84,6 +84,7 @@ set(SOURCES ScrollableContainerWidget.cpp Scrollbar.cpp SeparatorWidget.cpp + SettingsWindow.cpp Shortcut.cpp Slider.cpp SortingProxyModel.cpp diff --git a/Userland/Libraries/LibGUI/SettingsWindow.cpp b/Userland/Libraries/LibGUI/SettingsWindow.cpp new file mode 100644 index 00000000000..463c9c59396 --- /dev/null +++ b/Userland/Libraries/LibGUI/SettingsWindow.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2020, Idan Horowitz + * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Andreas Kling + * Copyright (c) 2021, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include +#include + +namespace GUI { + +SettingsWindow::SettingsWindow(StringView title, ShowDefaultsButton show_defaults_button) +{ + set_title(title); + resize(400, 480); + set_resizable(false); + set_minimizable(false); + + auto& main_widget = set_main_widget(); + main_widget.set_fill_with_background_color(true); + main_widget.set_layout(); + main_widget.layout()->set_margins(4); + main_widget.layout()->set_spacing(6); + + m_tab_widget = main_widget.add(); + + auto& button_container = main_widget.add(); + button_container.set_shrink_to_fit(true); + button_container.set_layout(); + button_container.layout()->set_spacing(6); + + if (show_defaults_button == ShowDefaultsButton::Yes) { + m_reset_button = button_container.add("Defaults"); + m_reset_button->on_click = [&](auto) { + for (auto& tab : m_tabs) { + tab.reset_default_values(); + tab.apply_settings(); + } + }; + } + + button_container.layout()->add_spacer(); + + m_ok_button = button_container.add("OK"); + m_ok_button->set_fixed_width(75); + m_ok_button->on_click = [&](auto) { + for (auto& tab : m_tabs) + tab.apply_settings(); + GUI::Application::the()->quit(); + }; + + m_cancel_button = button_container.add("Cancel"); + m_cancel_button->set_fixed_width(75); + m_cancel_button->on_click = [&](auto) { + GUI::Application::the()->quit(); + }; + + m_apply_button = button_container.add("Apply"); + m_apply_button->set_fixed_width(75); + m_apply_button->on_click = [&](auto) { + for (auto& tab : m_tabs) + tab.apply_settings(); + }; +} + +SettingsWindow::~SettingsWindow() +{ +} + +} diff --git a/Userland/Libraries/LibGUI/SettingsWindow.h b/Userland/Libraries/LibGUI/SettingsWindow.h new file mode 100644 index 00000000000..96e897ac2cc --- /dev/null +++ b/Userland/Libraries/LibGUI/SettingsWindow.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2020, Idan Horowitz + * Copyright (c) 2021, the SerenityOS developers. + * Copyright (c) 2021, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include +#include + +namespace GUI { + +class SettingsWindow : public GUI::Window { + C_OBJECT(SettingsWindow) +public: + class Tab : public GUI::Widget { + public: + virtual void apply_settings() = 0; + virtual void reset_default_values() {}; + }; + + enum class ShowDefaultsButton { + Yes, + No, + }; + + virtual ~SettingsWindow() override; + + template + T& add_tab(StringView const& title, Args&&... args) + { + auto& t = m_tab_widget->add_tab(title, forward(args)...); + m_tabs.append(t); + return t; + } + +private: + SettingsWindow(StringView title, ShowDefaultsButton = ShowDefaultsButton::No); + + RefPtr m_tab_widget; + NonnullRefPtrVector m_tabs; + + RefPtr m_ok_button; + RefPtr m_cancel_button; + RefPtr m_apply_button; + RefPtr m_reset_button; +}; + +}