mirror of
https://github.com/chylex/Nextcloud-Desktop.git
synced 2025-05-13 08:34:11 +02:00
Implement and move some colour-aware helper methods into the Theme class
This introduces a new method to change the colours in the links in QLabel's. Utilizes a custom crafted RegEx function to replace already-coloured links. Moved code is based on stuff from the SettingsDialog class. Signed-off-by: Michael Schuster <michael@schuster.ms>
This commit is contained in:
parent
2dae31486a
commit
be4fc6b887
src
@ -346,7 +346,7 @@ void SettingsDialog::customizeStyle()
|
||||
_toolBar->setStyleSheet(QString::fromLatin1(TOOLBAR_CSS).arg(background, dark, highlightColor, highlightTextColor));
|
||||
|
||||
Q_FOREACH (QAction *a, _actionGroup->actions()) {
|
||||
QIcon icon = createColorAwareIcon(a->property("iconPath").toString());
|
||||
QIcon icon = Theme::createColorAwareIcon(a->property("iconPath").toString(), palette());
|
||||
a->setIcon(icon);
|
||||
QToolButton *btn = qobject_cast<QToolButton *>(_toolBar->widgetForAction(a));
|
||||
if (btn)
|
||||
@ -354,34 +354,6 @@ void SettingsDialog::customizeStyle()
|
||||
}
|
||||
}
|
||||
|
||||
static bool isDarkColor(const QColor &color)
|
||||
{
|
||||
// account for different sensitivity of the human eye to certain colors
|
||||
double treshold = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0;
|
||||
return treshold > 0.5;
|
||||
}
|
||||
|
||||
QIcon SettingsDialog::createColorAwareIcon(const QString &name)
|
||||
{
|
||||
QPalette pal = palette();
|
||||
QImage img(name);
|
||||
QImage inverted(img);
|
||||
inverted.invertPixels(QImage::InvertRgb);
|
||||
|
||||
QIcon icon;
|
||||
if (isDarkColor(pal.color(QPalette::Base))) {
|
||||
icon.addPixmap(QPixmap::fromImage(inverted));
|
||||
} else {
|
||||
icon.addPixmap(QPixmap::fromImage(img));
|
||||
}
|
||||
if (isDarkColor(pal.color(QPalette::HighlightedText))) {
|
||||
icon.addPixmap(QPixmap::fromImage(img), QIcon::Normal, QIcon::On);
|
||||
} else {
|
||||
icon.addPixmap(QPixmap::fromImage(inverted), QIcon::Normal, QIcon::On);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
class ToolButtonAction : public QWidgetAction
|
||||
{
|
||||
public:
|
||||
@ -422,7 +394,7 @@ QAction *SettingsDialog::createActionWithIcon(const QIcon &icon, const QString &
|
||||
QAction *SettingsDialog::createColorAwareAction(const QString &iconPath, const QString &text)
|
||||
{
|
||||
// all buttons must have the same size in order to keep a good layout
|
||||
QIcon coloredIcon = createColorAwareIcon(iconPath);
|
||||
QIcon coloredIcon = Theme::createColorAwareIcon(iconPath, palette());
|
||||
return createActionWithIcon(coloredIcon, text, iconPath);
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,6 @@ private:
|
||||
void customizeStyle();
|
||||
void activityAdded(AccountState *);
|
||||
|
||||
QIcon createColorAwareIcon(const QString &name);
|
||||
QAction *createColorAwareAction(const QString &iconName, const QString &fileName);
|
||||
QAction *createActionWithIcon(const QIcon &icon, const QString &text, const QString &iconPath = QString());
|
||||
|
||||
|
@ -565,4 +565,76 @@ QString Theme::versionSwitchOutput() const
|
||||
return helpText;
|
||||
}
|
||||
|
||||
bool Theme::isDarkColor(const QColor &color)
|
||||
{
|
||||
// account for different sensitivity of the human eye to certain colors
|
||||
double treshold = 1.0 - (0.299 * color.red() + 0.587 * color.green() + 0.114 * color.blue()) / 255.0;
|
||||
return treshold > 0.5;
|
||||
}
|
||||
|
||||
QColor Theme::getBackgroundAwareLinkColor(const QColor &backgroundColor)
|
||||
{
|
||||
return QColor((isDarkColor(backgroundColor) ? QColor("#aabdff") : QGuiApplication::palette().color(QPalette::Link)));
|
||||
}
|
||||
|
||||
QColor Theme::getBackgroundAwareLinkColor()
|
||||
{
|
||||
return getBackgroundAwareLinkColor(QGuiApplication::palette().base().color());
|
||||
}
|
||||
|
||||
void Theme::replaceLinkColorStringBackgroundAware(QString &linkString, const QColor &backgroundColor)
|
||||
{
|
||||
linkString.replace(QRegularExpression("(<a href|<a style='color:#([a-zA-Z0-9]{6});' href)"), QString::fromLatin1("<a style='color:%1;' href").arg(getBackgroundAwareLinkColor(backgroundColor).name()));
|
||||
}
|
||||
|
||||
void Theme::replaceLinkColorStringBackgroundAware(QString &linkString)
|
||||
{
|
||||
replaceLinkColorStringBackgroundAware(linkString, QGuiApplication::palette().color(QPalette::Base));
|
||||
}
|
||||
|
||||
QIcon Theme::createColorAwareIcon(const QString &name, const QPalette &palette)
|
||||
{
|
||||
QImage img(name);
|
||||
QImage inverted(img);
|
||||
inverted.invertPixels(QImage::InvertRgb);
|
||||
|
||||
QIcon icon;
|
||||
if (Theme::isDarkColor(palette.color(QPalette::Base))) {
|
||||
icon.addPixmap(QPixmap::fromImage(inverted));
|
||||
} else {
|
||||
icon.addPixmap(QPixmap::fromImage(img));
|
||||
}
|
||||
if (Theme::isDarkColor(palette.color(QPalette::HighlightedText))) {
|
||||
icon.addPixmap(QPixmap::fromImage(img), QIcon::Normal, QIcon::On);
|
||||
} else {
|
||||
icon.addPixmap(QPixmap::fromImage(inverted), QIcon::Normal, QIcon::On);
|
||||
}
|
||||
return icon;
|
||||
}
|
||||
|
||||
QIcon Theme::createColorAwareIcon(const QString &name)
|
||||
{
|
||||
return createColorAwareIcon(name, QGuiApplication::palette());
|
||||
}
|
||||
|
||||
QPixmap Theme::createColorAwarePixmap(const QString &name, const QPalette &palette)
|
||||
{
|
||||
QImage img(name);
|
||||
QImage inverted(img);
|
||||
inverted.invertPixels(QImage::InvertRgb);
|
||||
|
||||
QPixmap pixmap;
|
||||
if (Theme::isDarkColor(palette.color(QPalette::Base))) {
|
||||
pixmap = QPixmap::fromImage(inverted);
|
||||
} else {
|
||||
pixmap = QPixmap::fromImage(img);
|
||||
}
|
||||
return pixmap;
|
||||
}
|
||||
|
||||
QPixmap Theme::createColorAwarePixmap(const QString &name)
|
||||
{
|
||||
return createColorAwarePixmap(name, QGuiApplication::palette());
|
||||
}
|
||||
|
||||
} // end namespace client
|
||||
|
@ -24,6 +24,7 @@ class QObject;
|
||||
class QPixmap;
|
||||
class QColor;
|
||||
class QPaintDevice;
|
||||
class QPalette;
|
||||
|
||||
namespace OCC {
|
||||
|
||||
@ -355,6 +356,87 @@ public:
|
||||
* (actually 2019/09/13 only systray theming).
|
||||
*/
|
||||
virtual QIcon uiThemeIcon(const QString &iconName, bool uiHasDarkBg) const;
|
||||
|
||||
/**
|
||||
* @brief Perform a calculation to check if a colour is dark or light and accounts for different sensitivity of the human eye.
|
||||
*
|
||||
* @return True if the specified colour is dark.
|
||||
*
|
||||
* 2019/12/08: Moved here from SettingsDialog.
|
||||
*/
|
||||
static bool isDarkColor(const QColor &color);
|
||||
|
||||
/**
|
||||
* @brief Return the colour to be used for HTML links (e.g. used in QLabel), based on the current app palette or given colour (Dark-/Light-Mode switching).
|
||||
*
|
||||
* @return Background-aware colour for HTML links, based on the current app palette or given colour.
|
||||
*
|
||||
* 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
|
||||
*/
|
||||
static QColor getBackgroundAwareLinkColor(const QColor &backgroundColor);
|
||||
|
||||
/**
|
||||
* @brief Return the colour to be used for HTML links (e.g. used in QLabel), based on the current app palette (Dark-/Light-Mode switching).
|
||||
*
|
||||
* @return Background-aware colour for HTML links, based on the current app palette.
|
||||
*
|
||||
* 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
|
||||
*/
|
||||
static QColor getBackgroundAwareLinkColor();
|
||||
|
||||
/**
|
||||
* @brief Appends a CSS-style colour value to all HTML link tags in a given string, based on the current app palette or given colour (Dark-/Light-Mode switching).
|
||||
*
|
||||
* 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
|
||||
*
|
||||
* This way we also avoid having certain strings re-translated on Transifex.
|
||||
*/
|
||||
static void replaceLinkColorStringBackgroundAware(QString &linkString, const QColor &backgroundColor);
|
||||
|
||||
/**
|
||||
* @brief Appends a CSS-style colour value to all HTML link tags in a given string, based on the current app palette (Dark-/Light-Mode switching).
|
||||
*
|
||||
* 2019/12/08: Implemented for the Dark Mode on macOS, because the app palette can not account for that (Qt 5.12.5).
|
||||
*
|
||||
* This way we also avoid having certain strings re-translated on Transifex.
|
||||
*/
|
||||
static void replaceLinkColorStringBackgroundAware(QString &linkString);
|
||||
|
||||
/**
|
||||
* @brief Creates a colour-aware icon based on the specified palette's base colour.
|
||||
*
|
||||
* @return QIcon, colour-aware (inverted on dark backgrounds).
|
||||
*
|
||||
* 2019/12/09: Moved here from SettingsDialog.
|
||||
*/
|
||||
static QIcon createColorAwareIcon(const QString &name, const QPalette &palette);
|
||||
|
||||
/**
|
||||
* @brief Creates a colour-aware icon based on the app palette's base colour (Dark-/Light-Mode switching).
|
||||
*
|
||||
* @return QIcon, colour-aware (inverted on dark backgrounds).
|
||||
*
|
||||
* 2019/12/09: Moved here from SettingsDialog.
|
||||
*/
|
||||
static QIcon createColorAwareIcon(const QString &name);
|
||||
|
||||
/**
|
||||
* @brief Creates a colour-aware pixmap based on the specified palette's base colour.
|
||||
*
|
||||
* @return QPixmap, colour-aware (inverted on dark backgrounds).
|
||||
*
|
||||
* 2019/12/09: Adapted from createColorAwareIcon.
|
||||
*/
|
||||
static QPixmap createColorAwarePixmap(const QString &name, const QPalette &palette);
|
||||
|
||||
/**
|
||||
* @brief Creates a colour-aware pixmap based on the app palette's base colour (Dark-/Light-Mode switching).
|
||||
*
|
||||
* @return QPixmap, colour-aware (inverted on dark backgrounds).
|
||||
*
|
||||
* 2019/12/09: Adapted from createColorAwareIcon.
|
||||
*/
|
||||
static QPixmap createColorAwarePixmap(const QString &name);
|
||||
|
||||
protected:
|
||||
#ifndef TOKEN_AUTH_ONLY
|
||||
|
Loading…
Reference in New Issue
Block a user