From fe21003777718efac8743bf99c5388b3d6477be2 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 4 Apr 2025 19:56:43 +0200 Subject: adw: implement basic StyleManager --- adw/CMakeLists.txt | 2 + adw/include/adwaitamm/enums.hpp | 51 ++++++++++++++++ adw/include/adwaitamm/stylemanager.hpp | 81 +++++++++++++++++++++++++ adw/src/enums.cpp | 49 +++++++++++++++ adw/src/stylemanager.cpp | 106 +++++++++++++++++++++++++++++++++ adw/src/wrap_init.cpp | 3 + app/src/main.cpp | 7 ++- 7 files changed, 296 insertions(+), 3 deletions(-) create mode 100644 adw/include/adwaitamm/enums.hpp create mode 100644 adw/include/adwaitamm/stylemanager.hpp create mode 100644 adw/src/enums.cpp create mode 100644 adw/src/stylemanager.cpp diff --git a/adw/CMakeLists.txt b/adw/CMakeLists.txt index ad979ea..6ca4ad0 100644 --- a/adw/CMakeLists.txt +++ b/adw/CMakeLists.txt @@ -22,9 +22,11 @@ add_library("adwaitamm" "src/applicationwindow.cpp" "src/breakpoint.cpp" "src/dialog.cpp" + "src/enums.cpp" "src/preferencesdialog.cpp" "src/preferencespage.cpp" "src/preferencesrow.cpp" + "src/stylemanager.cpp" "src/switchrow.cpp" "src/toast.cpp" "src/toastoverlay.cpp" diff --git a/adw/include/adwaitamm/enums.hpp b/adw/include/adwaitamm/enums.hpp new file mode 100644 index 0000000..d19ebc7 --- /dev/null +++ b/adw/include/adwaitamm/enums.hpp @@ -0,0 +1,51 @@ +#ifndef LIBADWAITAMM_ENUMS_HPP +#define LIBADWAITAMM_ENUMS_HPP + +#include + +#include + +namespace Adwaita +{ + enum struct AccentColor + { + Blue, + Teal, + Green, + Yellow, + Orange, + Red, + Pink, + Purple, + Slate, + }; + + enum struct ColorScheme + { + Default, + ForceLight, + PreferLight, + PreferDark, + ForceDark, + }; +} // namespace Adwaita + +namespace Glib +{ + +#define VALUE_SPECIALIZATION(Enum) \ + template<> \ + class Value : public Glib::Value_Enum \ + { \ + public: \ + auto static value_type() -> GType; \ + } + + VALUE_SPECIALIZATION(AccentColor); + VALUE_SPECIALIZATION(ColorScheme); + +#undef VALUE_SPECIALIZATION + +} // namespace Glib + +#endif \ No newline at end of file diff --git a/adw/include/adwaitamm/stylemanager.hpp b/adw/include/adwaitamm/stylemanager.hpp new file mode 100644 index 0000000..8c6fd85 --- /dev/null +++ b/adw/include/adwaitamm/stylemanager.hpp @@ -0,0 +1,81 @@ +#ifndef LIBADWAITAMM_STYLE_MANAGER_HPP +#define LIBADWAITAMM_STYLE_MANAGER_HPP + +#include "adwaitamm/helpers/gobj_mixin.hpp" + +#include +#include +#include +#include +#include + +#include + +#include + +#include +#include + +#define _ADWAITA_INSIDE +#include +#undef _ADWAITA_INSIDE + +using AdwStyleManager = struct _AdwStyleManager; + +namespace Adwaita +{ + enum struct AccentColor; + enum struct ColorScheme; + + struct StyleManager : Glib::Object, + helpers::gobj_mixin + { + struct Class : Glib::Class + { + using BaseClassParent = GObjectClass; + using BaseClassType = AdwStyleManagerClass; + using BaseObjectType = AdwStyleManager; + using CppClassParent = struct Glib::Object_Class; + using CppObjectType = StyleManager; + + auto init() -> Glib::Class const &; + auto static class_init_function(void * gclass, void * data) -> void; + auto static wrap_new(GObject * object) -> Glib::ObjectBase *; + }; + + using BaseObjectType = Class::BaseObjectType; + using BaseClassType = Class::BaseClassType; + using CppObjectType = Class::CppObjectType; + using CppClassType = Class; + + using helpers::gobj_mixin::gobj; + using helpers::gobj_mixin::gobj_copy; + + StyleManager(StyleManager const & other) = delete; + StyleManager(StyleManager && other) noexcept = delete; + + auto operator=(StyleManager const & other) noexcept -> StyleManager & = delete; + auto operator=(StyleManager && other) noexcept -> StyleManager & = delete; + + auto static get_default() -> StyleManager *; + auto static for_display(Gdk::Display & display) -> StyleManager *; + + auto static get_type() -> GType; + auto static get_base_type() -> GType; + + auto get_accent_color() const -> AccentColor; + auto set_color_scheme(ColorScheme value) -> void; + + protected: + explicit StyleManager(Glib::ConstructParams const & params); + explicit StyleManager(BaseObjectType * gobj); + explicit StyleManager(); + }; +} // namespace Adwaita + +namespace Glib +{ + auto wrap(AdwStyleManager * object) -> Adwaita::StyleManager *; +} // namespace Glib + +#endif \ No newline at end of file diff --git a/adw/src/enums.cpp b/adw/src/enums.cpp new file mode 100644 index 0000000..02dd74a --- /dev/null +++ b/adw/src/enums.cpp @@ -0,0 +1,49 @@ +#include "adwaitamm/enums.hpp" + +#include + +#include + +#include + +namespace +{ + template + auto constexpr matches = + static_cast>(Wrapped) == static_cast>(Unwrapped); +} // namespace + +namespace Adwaita +{ + + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + static_assert(matches); + +} // namespace Adwaita + +namespace Glib +{ +#define VALUE_SPECIALIZATION(Enum, AdwEnumName) \ + auto Value::value_type() -> GType \ + { \ + return adw_##AdwEnumName##_get_type(); \ + } + + VALUE_SPECIALIZATION(AccentColor, accent_color) + VALUE_SPECIALIZATION(ColorScheme, color_scheme) + +#undef VALUE_SPECIALIZATION +} // namespace Glib \ No newline at end of file diff --git a/adw/src/stylemanager.cpp b/adw/src/stylemanager.cpp new file mode 100644 index 0000000..74fe40a --- /dev/null +++ b/adw/src/stylemanager.cpp @@ -0,0 +1,106 @@ +#include "adwaitamm/stylemanager.hpp" + +#include "adwaitamm/enums.hpp" + +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include +#include +#include + +namespace Adwaita +{ + namespace + { + auto constinit _class = StyleManager::Class{}; + } // namespace + + auto StyleManager::Class::init() -> Glib::Class const & + { + if (!gtype_) + { + class_init_func_ = &StyleManager::Class::class_init_function; + register_derived_type(adw_style_manager_get_type()); + } + return *this; + } + + auto StyleManager::Class::class_init_function(void * gclass, void * data) -> void + { + auto const klass = static_cast(gclass); + Glib::Object_Class::class_init_function(klass, data); + } + + auto StyleManager::Class::wrap_new(GObject * object) -> Glib::ObjectBase * + { + return new StyleManager(ADW_STYLE_MANAGER(object)); + } + + auto StyleManager::get_default() -> StyleManager * + { + return Glib::wrap(adw_style_manager_get_default()); + } + + auto StyleManager::for_display(Gdk::Display & display) -> StyleManager * + { + return Glib::wrap(adw_style_manager_get_for_display(Glib::unwrap(&display))); + } + + auto StyleManager::get_type() -> GType + { + return _class.init().get_type(); + } + + auto StyleManager::get_base_type() -> GType + { + return adw_style_manager_get_type(); + } + + StyleManager::StyleManager(Glib::ConstructParams const & params) + : Glib::Object{params} + { + } + + StyleManager::StyleManager(BaseObjectType * gobj) + : Glib::Object(G_OBJECT(gobj)) + { + } + + StyleManager::StyleManager() + : Glib::ObjectBase{nullptr} + , Glib::Object{Glib::ConstructParams{_class.init()}} + { + adw_init(); + } + + auto StyleManager::get_accent_color() const -> AccentColor + { + return static_cast(adw_style_manager_get_accent_color(const_cast(unwrap(this)))); + } + + auto StyleManager::set_color_scheme(ColorScheme value) -> void + { + adw_style_manager_set_color_scheme(unwrap(this), static_cast(value)); + } + +} // namespace Adwaita + +namespace Glib +{ + auto wrap(AdwStyleManager * object) -> Adwaita::StyleManager * + { + return dynamic_cast(Glib::wrap_auto(G_OBJECT(object))); + } +} // namespace Glib \ No newline at end of file diff --git a/adw/src/wrap_init.cpp b/adw/src/wrap_init.cpp index 8cad77d..f048633 100644 --- a/adw/src/wrap_init.cpp +++ b/adw/src/wrap_init.cpp @@ -9,6 +9,7 @@ #include "adwaitamm/preferencesdialog.hpp" #include "adwaitamm/preferencespage.hpp" #include "adwaitamm/preferencesrow.hpp" +#include "adwaitamm/stylemanager.hpp" #include "adwaitamm/switchrow.hpp" #include "adwaitamm/toast.hpp" #include "adwaitamm/toastoverlay.hpp" @@ -34,6 +35,7 @@ namespace Adwaita WRAP_CLASS(PreferencesDialog, preferences_dialog); WRAP_CLASS(PreferencesPage, preferences_page); WRAP_CLASS(PreferencesRow, preferences_row); + WRAP_CLASS(StyleManager, style_manager); WRAP_CLASS(SwitchRow, switch_row); WRAP_CLASS(Toast, toast); WRAP_CLASS(ToastOverlay, toast_overlay); @@ -47,6 +49,7 @@ namespace Adwaita ENSURE_TYPE(PreferencesDialog); ENSURE_TYPE(PreferencesPage); ENSURE_TYPE(PreferencesRow); + ENSURE_TYPE(StyleManager); ENSURE_TYPE(SwitchRow); ENSURE_TYPE(Toast); ENSURE_TYPE(ToastOverlay); diff --git a/app/src/main.cpp b/app/src/main.cpp index f86eee3..7595dc3 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -13,8 +13,9 @@ #include #include +#include +#include -#include #include #include @@ -45,8 +46,8 @@ auto main(int argc, char * argv[]) -> int turns::core::register_types(); turns::ui::register_types(); - auto style_manager = adw_style_manager_get_default(); - adw_style_manager_set_color_scheme(style_manager, ADW_COLOR_SCHEME_PREFER_LIGHT); + auto style_manager = Adwaita::StyleManager::get_default(); + style_manager->set_color_scheme(Adwaita::ColorScheme::PreferLight); app->add_action("quit", sigc::mem_fun(*app, &Adwaita::Application::quit)); app->set_accel_for_action("app.quit", "q"); -- cgit v1.2.3