qt: Introduce platform specific css sections (#3570)

* qt: Add platform specific css loading to GUIUtil::loadStyleSheet

This commit leads to GUIUtil::loadStyleSheet treating css code between
<os="<os_list>"> and </os> different. It will only become added for
operating systems provided in the list of the sections start tag.

There may be multiple entries per section. Possible entries:

- macosx
- windows
- other

<os_list> must be a combination of the three options above separated by
comma like in "windows,macosx".

Its ok to have multiple <os="...">...</os> sections in a file with
arbitrary OS combinations. They will all become added to the end of the
file though. Means even putting an <os> section in the top of the file
would become appended to the end of the file during loading which should
be kept in mind when adding sections to avoid unexpected overwriting.

Example
------------------------------------------------------------------------

<os="macosx, windows, other">

/* Example section to add styles for all operating systems
   Remove any to exclude it.
*/

</os>

* Respect `-uiplatform` when matching for os-specific styles

* Format osStyleExp to make it a bit easier to see groups

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This commit is contained in:
dustinface 2020-07-20 16:57:23 +02:00 committed by GitHub
parent f43c917191
commit 415e504780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 144 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include <qt/guiutil.h>
#include <qt/bitcoinaddressvalidator.h>
#include <qt/bitcoingui.h>
#include <qt/bitcoinunits.h>
#include <qt/qvalidatedlineedit.h>
#include <qt/walletmodel.h>
@ -1067,6 +1068,7 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
return false;
}
std::string platformName = gArgs.GetArg("-uiplatform", BitcoinGUI::DEFAULT_UIPLATFORM);
stylesheet = std::make_unique<QString>();
for (const auto& file : vecFiles) {
@ -1074,7 +1076,41 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
if (!qFile.open(QFile::ReadOnly)) {
throw std::runtime_error(strprintf("%s: Failed to open file: %s", __func__, file.toStdString()));
}
stylesheet->append(QLatin1String(qFile.readAll()));
QString strStyle = QLatin1String(qFile.readAll());
// Process all <os=...></os> groups in the stylesheet first
QRegularExpressionMatch osStyleMatch;
QRegularExpression osStyleExp(
"^"
"(<os=(?:'|\").+(?:'|\")>)" // group 1
"((?:.|\n)+?)" // group 2
"(</os>?)" // group 3
"$");
osStyleExp.setPatternOptions(QRegularExpression::MultilineOption);
QRegularExpressionMatchIterator it = osStyleExp.globalMatch(strStyle);
// For all <os=...></os> sections
while (it.hasNext() && (osStyleMatch = it.next()).isValid()) {
QStringList listMatches = osStyleMatch.capturedTexts();
// Full match + 3 group matches
if (listMatches.size() % 4) {
throw std::runtime_error(strprintf("%s: Invalid <os=...></os> section in file %s", __func__, file.toStdString()));
}
for (int i = 0; i < listMatches.size(); i += 4) {
if (!listMatches[i + 1].contains(QString::fromStdString(platformName))) {
// If os is not supported for this styles
// just remove the full match
strStyle.replace(listMatches[i], "");
} else {
// If its supported remove the <os=...></os> tags
strStyle.replace(listMatches[i + 1], "");
strStyle.replace(listMatches[i + 3], "");
}
}
}
stylesheet->append(strStyle);
}
return true;
};

View File

@ -784,3 +784,38 @@ TransactionView
******************************************************/
/***** No dark.css specific coloring here yet *****/
/******************************************************
*******************************************************
STYLING OF OS SPECIFIC UI PARTS
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
different. It will only become added for operating systems provided in the list
of the sections start tag.
There may be multiple entries per section. Possible entries:
- macosx
- windows
- other
<os_list> must be a combination of the three options above separated by
comma like in "windows,macosx".
Its ok to have multiple <os="...">...</os> sections in a file with
arbitrary OS combinations. They will all become added to the end of the
file though. Means even putting an <os> section in the top of the file
would become appended to the end of the file during loading which should
be kept in mind when adding sections to avoid unexpected overwriting.
*******************************************************
******************************************************/
<os="macosx, windows, other">
/* Example section to add styles for all operating systems
Remove any to exclude it.
*/
</os>

View File

@ -1844,3 +1844,39 @@ TransactionView QComboBox {
min-height: 30px;
margin-top: 15px;
}
/******************************************************
*******************************************************
STYLING OF OS SPECIFIC UI PARTS
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
different. It will only become added for operating systems provided in the list
of the sections start tag.
There may be multiple entries per section. Possible entries:
- macosx
- windows
- other
<os_list> must be a combination of the three options above separated by
comma like in "windows,macosx".
Its ok to have multiple <os="...">...</os> sections in a file with
arbitrary OS combinations. They will all become added to the end of the
file though. Means even putting an <os> section in the top of the file
would become appended to the end of the file during loading which should
be kept in mind when adding sections to avoid unexpected overwriting.
*******************************************************
******************************************************/
<os="macosx, windows, other">
/* Example section to add styles for all operating systems
Remove any to exclude it.
*/
</os>

View File

@ -766,3 +766,39 @@ TransactionView
******************************************************/
/***** No light.css specific coloring here yet *****/
/******************************************************
*******************************************************
STYLING OF OS SPECIFIC UI PARTS
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
different. It will only become added for operating systems provided in the list
of the sections start tag.
There may be multiple entries per section. Possible entries:
- macosx
- windows
- other
<os_list> must be a combination of the three options above separated by
comma like in "windows,macosx".
Its ok to have multiple <os="...">...</os> sections in a file with
arbitrary OS combinations. They will all become added to the end of the
file though. Means even putting an <os> section in the top of the file
would become appended to the end of the file during loading which should
be kept in mind when adding sections to avoid unexpected overwriting.
*******************************************************
******************************************************/
<os="macosx, windows, other">
/* Example section to add styles for all operating systems
Remove any to exclude it.
*/
</os>