Serenity Operating System
at master 88 lines 2.4 kB view raw
1/* 2 * Copyright (c) 2020, Idan Horowitz <idan.horowitz@serenityos.org> 3 * Copyright (c) 2021-2022, the SerenityOS developers. 4 * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org> 5 * 6 * SPDX-License-Identifier: BSD-2-Clause 7 */ 8 9#pragma once 10 11#include <AK/HashMap.h> 12#include <LibGUI/Button.h> 13#include <LibGUI/TabWidget.h> 14#include <LibGUI/Window.h> 15 16namespace GUI { 17 18class SettingsWindow : public GUI::Window { 19 C_OBJECT(SettingsWindow) 20public: 21 class Tab : public GUI::Widget { 22 public: 23 virtual void apply_settings() = 0; 24 virtual void cancel_settings() { } 25 virtual void reset_default_values() { } 26 27 SettingsWindow& settings_window() { return *m_window; } 28 void set_settings_window(SettingsWindow& settings_window) { m_window = settings_window; } 29 30 void set_modified(bool modified) 31 { 32 if (m_window) 33 m_window->set_modified(modified); 34 } 35 36 private: 37 WeakPtr<SettingsWindow> m_window; 38 }; 39 40 enum class ShowDefaultsButton { 41 Yes, 42 No, 43 }; 44 45 static ErrorOr<NonnullRefPtr<SettingsWindow>> create(DeprecatedString title, ShowDefaultsButton = ShowDefaultsButton::No); 46 47 virtual ~SettingsWindow() override = default; 48 49 template<class T, class... Args> 50 ErrorOr<NonnullRefPtr<T>> add_tab(DeprecatedString title, StringView id, Args&&... args) 51 { 52 auto tab = TRY(m_tab_widget->try_add_tab<T>(move(title), forward<Args>(args)...)); 53 TRY(m_tabs.try_set(id, tab)); 54 tab->set_settings_window(*this); 55 return tab; 56 } 57 58 ErrorOr<void> add_tab(NonnullRefPtr<Tab> const& tab, DeprecatedString title, StringView id) 59 { 60 tab->set_title(move(title)); 61 TRY(m_tab_widget->try_add_widget(*tab)); 62 TRY(m_tabs.try_set(id, tab)); 63 tab->set_settings_window(*this); 64 return {}; 65 } 66 67 Optional<NonnullRefPtr<Tab>> get_tab(StringView id) const; 68 void set_active_tab(StringView id); 69 70 void apply_settings(); 71 void cancel_settings(); 72 void reset_default_values(); 73 74 void set_modified(bool); 75 76private: 77 SettingsWindow() = default; 78 79 RefPtr<GUI::TabWidget> m_tab_widget; 80 HashMap<StringView, NonnullRefPtr<Tab>> m_tabs; 81 82 RefPtr<GUI::Button> m_ok_button; 83 RefPtr<GUI::Button> m_cancel_button; 84 RefPtr<GUI::Button> m_apply_button; 85 RefPtr<GUI::Button> m_reset_button; 86}; 87 88}