Skip to content

Commit f1d106f

Browse files
xdustinfaceUdjinM6
authored andcommitted
qt: Introduce platform specific css sections (dashpay#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>
1 parent 2f69f50 commit f1d106f

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

src/qt/guiutil.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <qt/appearancewidget.h>
1010
#include <qt/bitcoinaddressvalidator.h>
11+
#include <qt/bitcoingui.h>
1112
#include <qt/bitcoinunits.h>
1213
#include <qt/optionsdialog.h>
1314
#include <qt/qvalidatedlineedit.h>
@@ -1104,14 +1105,49 @@ void loadStyleSheet(QWidget* widget, bool fForceUpdate)
11041105
return false;
11051106
}
11061107

1108+
std::string platformName = gArgs.GetArg("-uiplatform", BitcoinGUI::DEFAULT_UIPLATFORM);
11071109
stylesheet = std::make_unique<QString>();
11081110

11091111
for (const auto& file : vecFiles) {
11101112
QFile qFile(file);
11111113
if (!qFile.open(QFile::ReadOnly)) {
11121114
throw std::runtime_error(strprintf("%s: Failed to open file: %s", __func__, file.toStdString()));
11131115
}
1114-
stylesheet->append(QLatin1String(qFile.readAll()));
1116+
1117+
QString strStyle = QLatin1String(qFile.readAll());
1118+
// Process all <os=...></os> groups in the stylesheet first
1119+
QRegularExpressionMatch osStyleMatch;
1120+
QRegularExpression osStyleExp(
1121+
"^"
1122+
"(<os=(?:'|\").+(?:'|\")>)" // group 1
1123+
"((?:.|\n)+?)" // group 2
1124+
"(</os>?)" // group 3
1125+
"$");
1126+
osStyleExp.setPatternOptions(QRegularExpression::MultilineOption);
1127+
QRegularExpressionMatchIterator it = osStyleExp.globalMatch(strStyle);
1128+
1129+
// For all <os=...></os> sections
1130+
while (it.hasNext() && (osStyleMatch = it.next()).isValid()) {
1131+
QStringList listMatches = osStyleMatch.capturedTexts();
1132+
1133+
// Full match + 3 group matches
1134+
if (listMatches.size() % 4) {
1135+
throw std::runtime_error(strprintf("%s: Invalid <os=...></os> section in file %s", __func__, file.toStdString()));
1136+
}
1137+
1138+
for (int i = 0; i < listMatches.size(); i += 4) {
1139+
if (!listMatches[i + 1].contains(QString::fromStdString(platformName))) {
1140+
// If os is not supported for this styles
1141+
// just remove the full match
1142+
strStyle.replace(listMatches[i], "");
1143+
} else {
1144+
// If its supported remove the <os=...></os> tags
1145+
strStyle.replace(listMatches[i + 1], "");
1146+
strStyle.replace(listMatches[i + 3], "");
1147+
}
1148+
}
1149+
}
1150+
stylesheet->append(strStyle);
11151151
}
11161152
return true;
11171153
};

src/qt/res/css/dark.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,38 @@ TransactionView
802802
******************************************************/
803803

804804
/***** No dark.css specific coloring here yet *****/
805+
806+
807+
/******************************************************
808+
*******************************************************
809+
STYLING OF OS SPECIFIC UI PARTS
810+
811+
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
812+
different. It will only become added for operating systems provided in the list
813+
of the sections start tag.
814+
815+
There may be multiple entries per section. Possible entries:
816+
817+
- macosx
818+
- windows
819+
- other
820+
821+
<os_list> must be a combination of the three options above separated by
822+
comma like in "windows,macosx".
823+
824+
Its ok to have multiple <os="...">...</os> sections in a file with
825+
arbitrary OS combinations. They will all become added to the end of the
826+
file though. Means even putting an <os> section in the top of the file
827+
would become appended to the end of the file during loading which should
828+
be kept in mind when adding sections to avoid unexpected overwriting.
829+
*******************************************************
830+
******************************************************/
831+
832+
<os="macosx, windows, other">
833+
834+
/* Example section to add styles for all operating systems
835+
Remove any to exclude it.
836+
*/
837+
838+
</os>
839+

src/qt/res/css/general.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,3 +1919,39 @@ TransactionView QComboBox {
19191919
min-height: 30px;
19201920
margin-top: 15px;
19211921
}
1922+
1923+
1924+
/******************************************************
1925+
*******************************************************
1926+
STYLING OF OS SPECIFIC UI PARTS
1927+
1928+
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
1929+
different. It will only become added for operating systems provided in the list
1930+
of the sections start tag.
1931+
1932+
There may be multiple entries per section. Possible entries:
1933+
1934+
- macosx
1935+
- windows
1936+
- other
1937+
1938+
<os_list> must be a combination of the three options above separated by
1939+
comma like in "windows,macosx".
1940+
1941+
Its ok to have multiple <os="...">...</os> sections in a file with
1942+
arbitrary OS combinations. They will all become added to the end of the
1943+
file though. Means even putting an <os> section in the top of the file
1944+
would become appended to the end of the file during loading which should
1945+
be kept in mind when adding sections to avoid unexpected overwriting.
1946+
*******************************************************
1947+
******************************************************/
1948+
1949+
1950+
<os="macosx, windows, other">
1951+
1952+
/* Example section to add styles for all operating systems
1953+
Remove any to exclude it.
1954+
*/
1955+
1956+
</os>
1957+

src/qt/res/css/light.css

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,3 +784,39 @@ TransactionView
784784
******************************************************/
785785

786786
/***** No light.css specific coloring here yet *****/
787+
788+
789+
/******************************************************
790+
*******************************************************
791+
STYLING OF OS SPECIFIC UI PARTS
792+
793+
NOTE: GUIUtil::loadStyleSheet treats css code between <os="<os_list>"> and </os>
794+
different. It will only become added for operating systems provided in the list
795+
of the sections start tag.
796+
797+
There may be multiple entries per section. Possible entries:
798+
799+
- macosx
800+
- windows
801+
- other
802+
803+
<os_list> must be a combination of the three options above separated by
804+
comma like in "windows,macosx".
805+
806+
Its ok to have multiple <os="...">...</os> sections in a file with
807+
arbitrary OS combinations. They will all become added to the end of the
808+
file though. Means even putting an <os> section in the top of the file
809+
would become appended to the end of the file during loading which should
810+
be kept in mind when adding sections to avoid unexpected overwriting.
811+
*******************************************************
812+
******************************************************/
813+
814+
815+
<os="macosx, windows, other">
816+
817+
/* Example section to add styles for all operating systems
818+
Remove any to exclude it.
819+
*/
820+
821+
</os>
822+

0 commit comments

Comments
 (0)