summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/SettingsWindow.h
blob: 1d2471ae85f019b016db43fcd21c3523f41da5b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (c) 2020, Idan Horowitz <idan.horowitz@serenityos.org>
 * Copyright (c) 2021-2022, the SerenityOS developers.
 * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/HashMap.h>
#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 cancel_settings() { }
        virtual void reset_default_values() { }

        SettingsWindow& settings_window() { return *m_window; }
        void set_settings_window(SettingsWindow& settings_window) { m_window = settings_window; }

        void set_modified(bool modified)
        {
            if (m_window)
                m_window->set_modified(modified);
        }

    private:
        WeakPtr<SettingsWindow> m_window;
    };

    enum class ShowDefaultsButton {
        Yes,
        No,
    };

    static ErrorOr<NonnullRefPtr<SettingsWindow>> create(String title, ShowDefaultsButton = ShowDefaultsButton::No);

    virtual ~SettingsWindow() override = default;

    template<class T, class... Args>
    ErrorOr<NonnullRefPtr<T>> add_tab(String title, StringView id, Args&&... args)
    {
        auto tab = TRY(m_tab_widget->try_add_tab<T>(move(title), forward<Args>(args)...));
        TRY(m_tabs.try_set(id, tab));
        tab->set_settings_window(*this);
        return tab;
    }

    Optional<NonnullRefPtr<Tab>> get_tab(StringView id) const;
    void set_active_tab(StringView id);

    void apply_settings();
    void cancel_settings();
    void reset_default_values();

    void set_modified(bool);

private:
    SettingsWindow() = default;

    RefPtr<GUI::TabWidget> m_tab_widget;
    HashMap<StringView, NonnullRefPtr<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;
};

}