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

KDE's Doxygen guidelines are available online.