mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
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.
This commit is contained in:
parent
4b34a1302b
commit
23341f35cb
3 changed files with 130 additions and 0 deletions
|
@ -84,6 +84,7 @@ set(SOURCES
|
||||||
ScrollableContainerWidget.cpp
|
ScrollableContainerWidget.cpp
|
||||||
Scrollbar.cpp
|
Scrollbar.cpp
|
||||||
SeparatorWidget.cpp
|
SeparatorWidget.cpp
|
||||||
|
SettingsWindow.cpp
|
||||||
Shortcut.cpp
|
Shortcut.cpp
|
||||||
Slider.cpp
|
Slider.cpp
|
||||||
SortingProxyModel.cpp
|
SortingProxyModel.cpp
|
||||||
|
|
76
Userland/Libraries/LibGUI/SettingsWindow.cpp
Normal file
76
Userland/Libraries/LibGUI/SettingsWindow.cpp
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
* Copyright (c) 2021, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||||
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <LibGUI/Application.h>
|
||||||
|
#include <LibGUI/BoxLayout.h>
|
||||||
|
#include <LibGUI/Button.h>
|
||||||
|
#include <LibGUI/SettingsWindow.h>
|
||||||
|
#include <LibGUI/Widget.h>
|
||||||
|
|
||||||
|
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<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);
|
||||||
|
|
||||||
|
m_tab_widget = main_widget.add<GUI::TabWidget>();
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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) {
|
||||||
|
tab.reset_default_values();
|
||||||
|
tab.apply_settings();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
button_container.layout()->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)
|
||||||
|
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) {
|
||||||
|
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)
|
||||||
|
tab.apply_settings();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
SettingsWindow::~SettingsWindow()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
53
Userland/Libraries/LibGUI/SettingsWindow.h
Normal file
53
Userland/Libraries/LibGUI/SettingsWindow.h
Normal file
|
@ -0,0 +1,53 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2020, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
* Copyright (c) 2021, the SerenityOS developers.
|
||||||
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <LibGUI/Button.h>
|
||||||
|
#include <LibGUI/TabWidget.h>
|
||||||
|
#include <LibGUI/Window.h>
|
||||||
|
|
||||||
|
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<class T, class... Args>
|
||||||
|
T& add_tab(StringView const& title, Args&&... args)
|
||||||
|
{
|
||||||
|
auto& t = m_tab_widget->add_tab<T>(title, forward<Args>(args)...);
|
||||||
|
m_tabs.append(t);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
SettingsWindow(StringView title, ShowDefaultsButton = ShowDefaultsButton::No);
|
||||||
|
|
||||||
|
RefPtr<GUI::TabWidget> m_tab_widget;
|
||||||
|
NonnullRefPtrVector<Tab> m_tabs;
|
||||||
|
|
||||||
|
RefPtr<GUI::Button> m_ok_button;
|
||||||
|
RefPtr<GUI::Button> m_cancel_button;
|
||||||
|
RefPtr<GUI::Button> m_apply_button;
|
||||||
|
RefPtr<GUI::Button> m_reset_button;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue