Fluid 1.3.0
Material Design for QtQuick and Qml
Loading...
Searching...
No Matches
style.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
2// SPDX-License-Identifier: MPL-2.0
3
4#include <QPalette>
5
6#include <QtGui/private/qguiapplication_p.h>
7#include <QtGui/qpa/qplatformtheme.h>
8
9#include "../tokens/colordark.h"
10#include "../tokens/colorlight.h"
11#include "../tokens/palette.h"
12#include "style.h"
13
14// Active theme colors resolve the AndroidX Material 3 ColorLightTokens.kt and
15// ColorDarkTokens.kt mappings (VERSION: v0_210) from the pinned token set:
16// https://android.googlesource.com/platform/frameworks/support/+/5ba2cdd61be7b6945db999b238d14f3c626136fb/compose/material3/material3/src/commonMain/kotlin/androidx/compose/material3/tokens/
17
18using namespace Qt::StringLiterals;
19
20// These are the default values used when no value was inherited from a parent
21// or explicitly set on the instance
23static int globalElevation = 0;
24
25FluidStyle::FluidStyle(QObject *parent)
26 : QQuickAttachedPropertyPropagator(parent)
27 , m_systemTheme(globalTheme == FluidStyle::System)
28 , m_theme(effectiveTheme(globalTheme))
29{
30 QQuickAttachedPropertyPropagator::initialize();
31}
32
34{
35 return m_theme;
36}
37
39{
40 // Keep track that the theme was explicitly set on this instance
41 m_explicitTheme = true;
42
43 // System theme:
44 // m_theme is set to system's theme (Dark/Light)
45 // Non-system theme:
46 // m_theme is set to the specified theme (Dark/Light)
47 // and m_systemTheme is false
48
49 m_theme = effectiveTheme(theme);
50 m_systemTheme = (theme == System);
51
52 propagateTheme();
53 Q_EMIT themeChanged();
54}
55
57{
58 if (!m_explicitTheme)
59 return;
60
61 m_explicitTheme = false;
62
63 FluidStyle *style = qobject_cast<FluidStyle *>(attachedParent());
64 inheritTheme(style ? style->theme() : globalTheme);
65}
66
68{
69 return m_elevation;
70}
71
72void FluidStyle::setElevation(int elevation)
73{
74 if (m_elevation != elevation) {
75 m_elevation = elevation;
76 Q_EMIT elevationChanged();
77 }
78}
79
84
85// Font getters
87{
88 return "Roboto"_L1;
89}
90
92{
93 return "Roboto"_L1;
94}
95
96// Color getters
97#define FLUID_STYLE_COLOR(getter, token) \
98 QColor FluidStyle::getter() const \
99 { \
100 return m_theme == Dark ? Fluid::ColorDarkTokens().token() \
101 : Fluid::ColorLightTokens().token(); \
102 }
103
104FLUID_STYLE_COLOR(primaryColor, primary)
105FLUID_STYLE_COLOR(onPrimaryColor, onPrimary)
106FLUID_STYLE_COLOR(primaryContainerColor, primaryContainer)
107FLUID_STYLE_COLOR(onPrimaryContainerColor, onPrimaryContainer)
108FLUID_STYLE_COLOR(primaryFixedColor, primaryFixed)
109FLUID_STYLE_COLOR(primaryFixedDimColor, primaryFixedDim)
110FLUID_STYLE_COLOR(onPrimaryFixedColor, onPrimaryFixed)
111FLUID_STYLE_COLOR(onPrimaryFixedVariantColor, onPrimaryFixedVariant)
112FLUID_STYLE_COLOR(secondaryColor, secondary)
113FLUID_STYLE_COLOR(onSecondaryColor, onSecondary)
114FLUID_STYLE_COLOR(secondaryContainerColor, secondaryContainer)
115FLUID_STYLE_COLOR(onSecondaryContainerColor, onSecondaryContainer)
116FLUID_STYLE_COLOR(secondaryFixedColor, secondaryFixed)
117FLUID_STYLE_COLOR(secondaryFixedDimColor, secondaryFixedDim)
118FLUID_STYLE_COLOR(onSecondaryFixedColor, onSecondaryFixed)
119FLUID_STYLE_COLOR(onSecondaryFixedVariantColor, onSecondaryFixedVariant)
120FLUID_STYLE_COLOR(tertiaryColor, tertiary)
121FLUID_STYLE_COLOR(onTertiaryColor, onTertiary)
122FLUID_STYLE_COLOR(tertiaryContainerColor, tertiaryContainer)
123FLUID_STYLE_COLOR(onTertiaryContainerColor, onTertiaryContainer)
124FLUID_STYLE_COLOR(tertiaryFixedColor, tertiaryFixed)
125FLUID_STYLE_COLOR(tertiaryFixedDimColor, tertiaryFixedDim)
126FLUID_STYLE_COLOR(onTertiaryFixedColor, onTertiaryFixed)
127FLUID_STYLE_COLOR(onTertiaryFixedVariantColor, onTertiaryFixedVariant)
128FLUID_STYLE_COLOR(errorColor, error)
129FLUID_STYLE_COLOR(onErrorColor, onError)
130FLUID_STYLE_COLOR(errorContainerColor, errorContainer)
131FLUID_STYLE_COLOR(onErrorContainerColor, onErrorContainer)
132FLUID_STYLE_COLOR(backgroundColor, background)
133FLUID_STYLE_COLOR(onBackgroundColor, onBackground)
134FLUID_STYLE_COLOR(surfaceColor, surface)
135FLUID_STYLE_COLOR(onSurfaceColor, onSurface)
136FLUID_STYLE_COLOR(surfaceBrightColor, surfaceBright)
137FLUID_STYLE_COLOR(surfaceDimColor, surfaceDim)
138FLUID_STYLE_COLOR(surfaceVariantColor, surfaceVariant)
139FLUID_STYLE_COLOR(onSurfaceVariantColor, onSurfaceVariant)
140FLUID_STYLE_COLOR(surfaceTintColor, surfaceTint)
141FLUID_STYLE_COLOR(surfaceContainerLowestColor, surfaceContainerLowest)
142FLUID_STYLE_COLOR(surfaceContainerLowColor, surfaceContainerLow)
143FLUID_STYLE_COLOR(surfaceContainerColor, surfaceContainer)
144FLUID_STYLE_COLOR(surfaceContainerHighColor, surfaceContainerHigh)
145FLUID_STYLE_COLOR(surfaceContainerHighestColor, surfaceContainerHighest)
146FLUID_STYLE_COLOR(outlineColor, outline)
147FLUID_STYLE_COLOR(outlineVariantColor, outlineVariant)
148FLUID_STYLE_COLOR(inverseSurfaceColor, inverseSurface)
149FLUID_STYLE_COLOR(inverseOnSurfaceColor, inverseOnSurface)
150FLUID_STYLE_COLOR(inversePrimaryColor, inversePrimary)
151FLUID_STYLE_COLOR(scrimColor, scrim)
152
153#undef FLUID_STYLE_COLOR
154
156{
157 return Fluid::PaletteTokens().black();
158}
159
161{
162 return new FluidStyle(object);
163}
164
165void FluidStyle::attachedParentChange(QQuickAttachedPropertyPropagator *newParent,
166 QQuickAttachedPropertyPropagator *oldParent)
167{
168 Q_UNUSED(oldParent);
169
170 FluidStyle *parentStyle = qobject_cast<FluidStyle *>(newParent);
171 if (parentStyle) {
172 inheritTheme(parentStyle->theme());
173 }
174}
175
177{
178 if (const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme()) {
179 if (theme->colorScheme() == Qt::ColorScheme::Unknown)
180 return theme->palette()->windowText().color().lightnessF()
181 > theme->palette()->window().color().lightnessF();
182 return theme->colorScheme() == Qt::ColorScheme::Dark;
183 }
184 return false;
185}
186
193
194void FluidStyle::inheritTheme(FluidStyle::Theme theme)
195{
196 const bool systemThemeChanged = (m_systemTheme != (theme == System));
197 const bool hasThemeChanged = systemThemeChanged || (m_theme != effectiveTheme(theme));
198 if (m_explicitTheme || !hasThemeChanged)
199 return;
200
201 m_theme = effectiveTheme(theme);
202 m_systemTheme = (theme == System);
203
204 propagateTheme();
205 Q_EMIT themeChanged();
206}
207
208void FluidStyle::propagateTheme()
209{
210 const auto styles = attachedChildren();
211 for (QQuickAttachedPropertyPropagator *child : styles) {
212 FluidStyle *childStyle = qobject_cast<FluidStyle *>(child);
213 if (childStyle)
214 childStyle->inheritTheme(m_systemTheme ? System : m_theme);
215 }
216}
Theme theme
Definition style.h:15
QString brandFontFamily
Definition style.h:20
int elevation
Definition style.h:17
static bool isDarkSystemTheme()
Definition style.cpp:176
QColor shadowColor
Definition style.h:101
void resetElevation()
Definition style.cpp:80
void resetTheme()
Definition style.cpp:56
void setTheme(Theme theme)
Definition style.cpp:38
static Theme effectiveTheme(Theme theme)
Definition style.cpp:187
FluidStyle(QObject *parent=nullptr)
Definition style.cpp:25
void attachedParentChange(QQuickAttachedPropertyPropagator *newParent, QQuickAttachedPropertyPropagator *oldParent) override
Definition style.cpp:165
void themeChanged()
QString plainFontFamily
Definition style.h:21
static FluidStyle * qmlAttachedProperties(QObject *object)
Definition style.cpp:160
void elevationChanged()
void setElevation(int elevation)
Definition style.cpp:72
Definition theme.h:9
static FluidStyle::Theme globalTheme
Definition style.cpp:22
#define FLUID_STYLE_COLOR(getter, token)
Definition style.cpp:97
static int globalElevation
Definition style.cpp:23