MauiKit Controls

csdcontrols.cpp
1#include "csdcontrols.h"
2
3#include <QUrl>
4#include <QFile>
5#include <QFileInfo>
6#include <QDir>
7#include <QStandardPaths>
8
9#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
10#include <MauiMan3/thememanager.h>
11#else
12#include <MauiMan4/thememanager.h>
13#endif
14
15Q_GLOBAL_STATIC(CSDControls, csdInstance)
16
17CSDControls::CSDControls(QObject *parent) : QObject (parent)
18 ,m_themeSettings( new MauiMan::ThemeManager(this))
19{
20 connect(m_themeSettings, &MauiMan::ThemeManager::enableCSDChanged, [this](bool enabled)
21 {
22 qDebug() << "CSD ENABLED CHANGED<<<<" << enabled;
23
24 getWindowControlsSettings();
25 });
26
27 connect(m_themeSettings, &MauiMan::ThemeManager::windowControlsThemeChanged, [this](QString style)
28 {
29 m_styleName = style;
30 setStyle();
31
32 Q_EMIT styleNameChanged();
33 Q_EMIT sourceChanged();
34 });
35
36 getWindowControlsSettings();
37}
38
39#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
40
41static QString CSDLookupPath = "org.mauikit.controls/csd.5/%1/config.conf";
42#else
43static QString CSDLookupPath = "org.mauikit.controls/csd.6/%1/config.conf";
44#endif
45
46void CSDControls::setStyle()
47{
48 auto confFile = QStandardPaths::locate (QStandardPaths::GenericDataLocation, QString(CSDLookupPath).arg(m_styleName));
49 QFileInfo file(confFile);
50 if(file.exists ())
51 {
52 const auto dir = QUrl::fromLocalFile (file.dir ().absolutePath ());
53
54 QSettings conf (confFile, QSettings::IniFormat);
55 conf.beginGroup ("Decoration");
56 m_source = dir.toString()+"/"+ conf.value("Source").toString();
57 conf.endGroup ();
58 }
59
60 qDebug() << "CSD QML SOURCXE" << m_source;
61 m_rightWindowControls = QStringList {"I", "A", "X"};
62}
63
64void CSDControls::getWindowControlsSettings()
65{
66 if(m_enabledCSD_blocked)
67 return;
68
69 m_enableCSD = m_themeSettings->enableCSD();
70 Q_EMIT enableCSDChanged();
71
72 /* #if (defined Q_OS_LINUX || defined Q_OS_FREEBSD) && !defined Q_OS_ANDROID
73
74 #ifdef FORMFACTOR_FOUND
75
76 if(m_formFactor->preferredMode() == 0)
77 {
78 m_enableCSD = m_themeSettings->enableCSD();
79 Q_EMIT enableCSDChanged();
80
81 }else
82 {
83 m_enableCSD = false;
84 Q_EMIT enableCSDChanged();
85 return;
86 }
87#else //Fallback in case FormFactor is not found. and then check for the env var QT_QUICK_CONTROLS_MOBILE
88 if (qEnvironmentVariableIsSet("QT_QUICK_CONTROLS_MOBILE"))
89 {
90 if (QByteArrayList {"0", "false"}.contains(qgetenv("QT_QUICK_CONTROLS_MOBILE")))
91 {
92 m_enableCSD = m_themeSettings->enableCSD();
93 Q_EMIT enableCSDChanged();
94 }else
95 {
96 return;
97 }
98 }
99 #endif */
100
101 m_styleName = m_themeSettings->windowControlsTheme();
102 setStyle();
103}
104
105bool CSDControls::enableCSD() const
106{
107 return m_enableCSD;
108}
109
110void CSDControls::setEnableCSD(const bool &value)
111{
112 m_enabledCSD_blocked = true;
113 if (m_enableCSD == value)
114 return;
115
116 m_enableCSD = value;
117 Q_EMIT enableCSDChanged();
118}
119
120void CSDControls::resetEnableCSD()
121{
122 m_enabledCSD_blocked = false;
123 getWindowControlsSettings();
124}
125
127{
128 return m_source;
129}
130
132{
133 return m_styleName;
134}
135
136CSDButton::CSDButton(QObject *parent): QObject(parent)
137{
138 connect(this, &CSDButton::typeChanged, this, &CSDButton::setSources);
139 // connect(this, &CSDButton::styleChanged, this, &CSDButton::setSources);
140 connect(this, &CSDButton::stateChanged, this, &CSDButton::requestCurrentSource);
141 // connect(MauiApp::instance()->controls(), &CSDControls::styleNameChanged, this, &CSDButton::setSources);
142
143 m_style = CSDControls::instance()->styleName();
144 setSources();
145}
146
147void CSDButton::setStyle(const QString& style)
148{
149 if(m_style == style)
150 {
151 return;
152 }
153
154 m_style = style;
155 Q_EMIT styleChanged();
156}
157
159{
160 return m_style;
161}
162
164{
165 return m_source;
166}
167
168void CSDButton::setSources()
169{
170 const auto confFile = QStandardPaths::locate (QStandardPaths::GenericDataLocation, QString(CSDLookupPath).arg(m_style));
171
172 QFileInfo file(confFile);
173 if(file.exists ())
174 {
175 m_dir = QUrl::fromLocalFile (file.dir ().absolutePath ());
176 QSettings conf (confFile, QSettings::IniFormat);
177 m_sources.insert (CSDButtonState::Normal, extractStateValue (conf, CSDButtonState::Normal));
178 m_sources.insert (CSDButtonState::Hover, extractStateValue (conf, CSDButtonState::Hover));
179 m_sources.insert (CSDButtonState::Pressed, extractStateValue (conf, CSDButtonState::Pressed));
180 m_sources.insert (CSDButtonState::Backdrop, extractStateValue (conf, CSDButtonState::Backdrop));
181 m_sources.insert (CSDButtonState::Disabled, extractStateValue (conf, CSDButtonState::Disabled));
182 }
183
184 this->requestCurrentSource ();
185}
186
187CSDButton::CSDButtonState CSDButton::state() const
188{
189 return m_state;
190}
191
192QUrl CSDButton::extractStateValue(QSettings &settings, const CSDButton::CSDButtonState &state)
193{
194 QUrl res;
195
196 settings.beginGroup (mapButtonType (m_type));
197 res = m_dir.toString ()+"/"+settings.value (mapButtonState (state)).toString ();
198 settings.endGroup ();
199
200 if(QFile::exists (res.toLocalFile ()))
201 {
202 return res;
203 }else
204 {
205 return QUrl("dialog-close"); //put here a fallback button
206 }
207}
208
209void CSDButton::requestCurrentSource()
210{
211 m_source = this->m_sources.value (this->m_state);
212 Q_EMIT this->sourceChanged ();
213}
214
215QString CSDButton::mapButtonType(const CSDButtonType &type)
216{
217 switch(type)
218 {
219 case Close: return "Close";
220 case Maximize: return "Maximize";
221 case Minimize: return "Minimize";
222 case Restore: return "Restore";
223 case Fullscreen: return "Fullscreen";
224 default: return "";
225 }
226}
227
228QString CSDButton::mapButtonState(const CSDButtonState &type)
229{
230 switch(type)
231 {
232 case Normal: return "Normal";
233 case Hover: return "Hover";
234 case Pressed: return "Pressed";
235 case Backdrop: return "Backdrop";
236 case Disabled: return "Disabled";
237 default: return "";
238 }
239}
240
241void CSDButton::setState(const CSDButtonState &newState)
242{
243 if (m_state == newState)
244 return;
245 m_state = newState;
246 Q_EMIT stateChanged();
247}
248
249CSDButton::CSDButtonType CSDButton::type() const
250{
251 return m_type;
252}
253
254void CSDButton::setType(CSDButtonType newType)
255{
256 if (m_type == newType)
257 return;
258
259 m_type = newType;
260 Q_EMIT typeChanged();
261}
262
263CSDButton::CSDButtonType CSDButton::mapType(const QString &value)
264{
265 if(value == "X") return CSDButton::CSDButtonType::Close;
266 if(value == "I") return CSDButton::CSDButtonType::Minimize;
267 if(value == "A") return CSDButton::CSDButtonType::Maximize;
268
269 return CSDButtonType::None;
270}
271
272bool CSDButton::isHovered() const
273{
274 return m_isHovered;
275}
276
277void CSDButton::setIsHovered(bool newIsHovered)
278{
279 if (m_isHovered == newIsHovered)
280 return;
281 m_isHovered = newIsHovered;
282 if(m_isHovered)
283 {
284 this->setState (CSDButtonState::Hover);
285 }else
286 {
287 this->setState (m_isFocused ? CSDButtonState::Normal : CSDButtonState::Backdrop);
288 }
289 Q_EMIT isHoveredChanged();
290}
291
292bool CSDButton::isMaximized() const
293{
294 return m_isMaximized;
295}
296
297void CSDButton::setIsMaximized(bool newIsMaximized)
298{
299 if (m_isMaximized == newIsMaximized)
300 return;
301 m_isMaximized = newIsMaximized;
302 if(m_type == CSDButtonType::Maximize && m_isMaximized)
303 {
304 this->setType (CSDButtonType::Restore);
305 }else if(m_type == CSDButtonType::Restore && !m_isMaximized)
306 {
307 this->setType (CSDButtonType::Maximize);
308 }
309 Q_EMIT isMaximizedChanged();
310}
311
312bool CSDButton::isPressed() const
313{
314 return m_isPressed;
315}
316
317void CSDButton::setIsPressed(bool newIsPressed)
318{
319 if (m_isPressed == newIsPressed)
320 return;
321 m_isPressed = newIsPressed;
322 if(m_isPressed)
323 {
324 this->setState (CSDButtonState::Pressed);
325 }else
326 {
327 this->setState (CSDButtonState::Normal);
328 }
329 Q_EMIT isPressedChanged();
330}
331
332bool CSDButton::isFocused() const
333{
334 return m_isFocused;
335}
336
337void CSDButton::setIsFocused(bool newIsFocused)
338{
339 if (m_isFocused == newIsFocused)
340 return;
341 m_isFocused = newIsFocused;
342
343 if(m_isFocused)
344 {
345 this->setState (CSDButtonState::Normal);
346 }
347 else
348 {
349 this->setState (CSDButtonState::Backdrop);
350 }
351 Q_EMIT isFocusedChanged();
352}
353
354CSDControls *CSDControls::qmlAttachedProperties(QObject *object)
355{
356 Q_UNUSED(object)
357 return CSDControls::instance();
358}
359
361{
362 return csdInstance();
363}
CSDButtonType type
The button type.
Definition csdcontrols.h:53
bool isPressed
Whether the button is currently being pressed.
Definition csdcontrols.h:41
bool isFocused
Whether the window is currently focused.
Definition csdcontrols.h:47
QUrl source
The source file path of the theme being used.
Definition csdcontrols.h:58
CSDButton::CSDButtonType mapType(const QString &value)
Maps a based string value convention representing a button type to a CSDButton::CSDButtonType.
QML_ELEMENTbool isHovered
Whether the button is currently being hovered.
Definition csdcontrols.h:29
CSDButtonState
The states of a window control button.
Definition csdcontrols.h:73
@ Disabled
The window or the button are not enabled.
Definition csdcontrols.h:97
@ Normal
The window surface is focused.
Definition csdcontrols.h:77
@ Pressed
The button is being pressed and has not been released.
Definition csdcontrols.h:87
@ Hover
The button is being hovered but has not been activated.
Definition csdcontrols.h:82
@ Backdrop
The window surface is in not focused.
Definition csdcontrols.h:92
QString style
The style to be used for picking up the image assets and config.
Definition csdcontrols.h:65
bool isMaximized
Whether the window is currently maximized.
Definition csdcontrols.h:35
The client-side-decorations manager for the MauiKit application.
static CSDControls * instance()
Retrieves the single instance of MauiApp.
QUrl source
The source file path of the style being used.
QString styleName
The name of the style/theme being used.
KIOCORE_EXPORT QString dir(const QString &fileClass)
bool exists() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key) const const
Q_EMITQ_EMIT
void beginGroup(QAnyStringView prefix)
void endGroup()
QVariant value(QAnyStringView key) const const
QString locate(StandardLocation type, const QString &fileName, LocateOptions options)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:16 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.