Adapt math and IPA panels to dark theme (#5636)

This commit is contained in:
Juergen Spitzmueller 2020-12-11 09:08:41 +01:00
parent b2ab394832
commit a06cdfa073
2 changed files with 75 additions and 15 deletions

View File

@ -86,6 +86,7 @@
#include <tuple> #include <tuple>
#include <QByteArray> #include <QByteArray>
#include <QBitmap>
#include <QDateTime> #include <QDateTime>
#include <QDesktopWidget> #include <QDesktopWidget>
#include <QEvent> #include <QEvent>
@ -102,6 +103,7 @@
#include <QMenuBar> #include <QMenuBar>
#include <QMimeData> #include <QMimeData>
#include <QObject> #include <QObject>
#include <QPainter>
#include <QPixmap> #include <QPixmap>
#include <QRegExp> #include <QRegExp>
#include <QSessionManager> #include <QSessionManager>
@ -475,9 +477,10 @@ QString themeIconName(QString const & action)
} }
// the returned bool is true if the icon needs to be flipped IconInfo iconInfo(FuncRequest const & f, bool unknown, bool rtl)
pair<QString,bool> iconName(FuncRequest const & f, bool unknown, bool rtl)
{ {
IconInfo res;
QStringList names; QStringList names;
QString lfunname = toqstr(lyxaction.getActionName(f.action())); QString lfunname = toqstr(lyxaction.getActionName(f.action()));
@ -539,16 +542,61 @@ pair<QString,bool> iconName(FuncRequest const & f, bool unknown, bool rtl)
for (QString const & suffix : suffixes) { for (QString const & suffix : suffixes) {
QString id = imagedir; QString id = imagedir;
FileName fname = imageLibFileSearch(id, name + suffix, "svgz,png", mode); FileName fname = imageLibFileSearch(id, name + suffix, "svgz,png", mode);
if (fname.exists()) if (fname.exists()) {
return make_pair(toqstr(fname.absFileName()), docstring const fpath = fname.absoluteFilePath();
rtl && suffix.isEmpty()); res.filepath = toqstr(fname.absFileName());
// these icons are subject to inversion in dark mode
res.invert = (contains(fpath, from_ascii("math")) || contains(fpath, from_ascii("ert-insert"))
|| suffixIs(fname.onlyPath().absoluteFilePath(), from_ascii("ipa")));
res.swap = rtl && suffix.isEmpty();
return res;
}
} }
LYXERR(Debug::GUI, "Cannot find icon for command \"" LYXERR(Debug::GUI, "Cannot find icon for command \""
<< lyxaction.getActionName(f.action()) << lyxaction.getActionName(f.action())
<< '(' << to_utf8(f.argument()) << ")\""); << '(' << to_utf8(f.argument()) << ")\"");
return make_pair(QString(), false); return res;
}
QPixmap prepareForDarkMode(QPixmap pixmap)
{
QPalette palette = QPalette();
QColor text_color = palette.color(QPalette::Active, QPalette::WindowText);
QColor bg_color = palette.color(QPalette::Active, QPalette::Window);
// guess whether we are in dark mode
if (text_color.black() > bg_color.black())
// not in dark mode, do nothing
return pixmap;
// create a layer with black text turned to QPalette::WindowText
QPixmap black_overlay(pixmap.size());
black_overlay.fill(text_color);
black_overlay.setMask(pixmap.createMaskFromColor(Qt::black, Qt::MaskOutColor));
// create a layer with blue text turned to lighter blue
QPixmap blue_overlay(pixmap.size());
QColor math_blue(0, 0, 255);
blue_overlay.fill(math_blue.lighter());
blue_overlay.setMask(pixmap.createMaskFromColor(math_blue, Qt::MaskOutColor));
// create a layer with ("latex") red text turned to lighter red
QPixmap red_overlay(pixmap.size());
QColor math_red(128, 0, 0);
QColor math_red_light(233, 175, 175);
red_overlay.fill(math_red_light);
red_overlay.setMask(pixmap.createMaskFromColor(math_red, Qt::MaskOutColor));
// put layers on top of existing pixmap
QPainter painter(&pixmap);
painter.drawPixmap(pixmap.rect(), black_overlay);
painter.drawPixmap(pixmap.rect(), blue_overlay);
painter.drawPixmap(pixmap.rect(), red_overlay);
return pixmap;
} }
@ -559,8 +607,11 @@ QPixmap getPixmap(QString const & path, QString const & name, QString const & ex
QString fpath = toqstr(fname.absFileName()); QString fpath = toqstr(fname.absFileName());
QPixmap pixmap = QPixmap(); QPixmap pixmap = QPixmap();
if (pixmap.load(fpath)) if (pixmap.load(fpath)) {
if (fpath.contains("math") || fpath.contains("ipa"))
return prepareForDarkMode(pixmap);
return pixmap; return pixmap;
}
bool const list = ext.contains(","); bool const list = ext.contains(",");
LYXERR(Debug::GUI, "Cannot load pixmap \"" LYXERR(Debug::GUI, "Cannot load pixmap \""
@ -588,20 +639,21 @@ QIcon getIcon(FuncRequest const & f, bool unknown, bool rtl)
} }
#endif #endif
QString icon; IconInfo icondata = iconInfo(f, unknown, rtl);
bool flip; if (icondata.filepath.isEmpty())
tie(icon, flip) = iconName(f, unknown, rtl);
if (icon.isEmpty())
return QIcon(); return QIcon();
//LYXERR(Debug::GUI, "Found icon: " << icon); //LYXERR(Debug::GUI, "Found icon: " << icon);
QPixmap pixmap = QPixmap(); QPixmap pixmap = QPixmap();
if (!pixmap.load(icon)) { if (!pixmap.load(icondata.filepath)) {
LYXERR0("Cannot load icon " << icon << "."); LYXERR0("Cannot load icon " << icondata.filepath << ".");
return QIcon(); return QIcon();
} }
if (flip) if (icondata.invert)
pixmap = prepareForDarkMode(pixmap);
if (icondata.swap)
return QIcon(pixmap.transformed(QTransform().scale(-1, 1))); return QIcon(pixmap.transformed(QTransform().scale(-1, 1)));
else else
return QIcon(pixmap); return QIcon(pixmap);
@ -1129,7 +1181,7 @@ void GuiApplication::clearSession()
docstring Application::iconName(FuncRequest const & f, bool unknown) docstring Application::iconName(FuncRequest const & f, bool unknown)
{ {
return qstring_to_ucs4(lyx::frontend::iconName(f, unknown, false).first); return qstring_to_ucs4(lyx::frontend::iconInfo(f, unknown, false).filepath);
} }

View File

@ -264,6 +264,14 @@ private:
extern GuiApplication * guiApp; extern GuiApplication * guiApp;
struct IconInfo {
/// Absolute path to icon file
QString filepath;
/// Swap the icon in RTL mode
bool swap;
/// Invert the icon in dark mode
bool invert;
};
/// \return the pixmap for the given path, name and extension. /// \return the pixmap for the given path, name and extension.
/// in case of errors a warning is produced and an empty pixmap is returned. /// in case of errors a warning is produced and an empty pixmap is returned.