Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dark UI theme and custom theme support #1124

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
441 changes: 441 additions & 0 deletions src/app/resources/css/darkorange.stylesheet

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/app/resources/zeal.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -244,5 +244,6 @@
<file>icons/logo/128x128.png</file>
<file>icons/logo/icon.png</file>
<file>icons/logo/icon@2x.png</file>
<file>css/darkorange.stylesheet</file>
</qresource>
</RCC>
20 changes: 20 additions & 0 deletions src/libs/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Settings::Settings(QObject *parent) :
QObject(parent)
{
qRegisterMetaTypeStreamOperators<ExternalLinkPolicy>("ExternalLinkPolicy");
qRegisterMetaTypeStreamOperators<UiStyle>("UiStyle");

// Enable local storage due to https://github.com/zealdocs/zeal/issues/872.
QWebSettings *webSettings = QWebSettings::globalSettings();
Expand Down Expand Up @@ -135,6 +136,9 @@ void Settings::load()
darkModeEnabled = settings->value(QStringLiteral("dark_mode"), false).toBool();
highlightOnNavigateEnabled = settings->value(QStringLiteral("highlight_on_navigate"), true).toBool();
customCssFile = settings->value(QStringLiteral("custom_css_file")).toString();
uiStyle = settings->value(QStringLiteral("ui_style"),
QVariant::fromValue(UiStyle::SystemDefault)).value<UiStyle>();
customUiCssFile = settings->value(QStringLiteral("custom_ui_css_file")).toString();
externalLinkPolicy = settings->value(QStringLiteral("external_link_policy"),
QVariant::fromValue(ExternalLinkPolicy::Ask)).value<ExternalLinkPolicy>();
isSmoothScrollingEnabled = settings->value(QStringLiteral("smooth_scrolling"), false).toBool();
Expand Down Expand Up @@ -224,6 +228,8 @@ void Settings::save()
settings->setValue(QStringLiteral("dark_mode"), darkModeEnabled);
settings->setValue(QStringLiteral("highlight_on_navigate"), highlightOnNavigateEnabled);
settings->setValue(QStringLiteral("custom_css_file"), customCssFile);
settings->setValue(QStringLiteral("ui_style"), QVariant::fromValue(uiStyle));
settings->setValue(QStringLiteral("custom_ui_css_file"), customUiCssFile);
settings->setValue(QStringLiteral("external_link_policy"), QVariant::fromValue(externalLinkPolicy));
settings->setValue(QStringLiteral("smooth_scrolling"), isSmoothScrollingEnabled);
settings->endGroup();
Expand Down Expand Up @@ -347,3 +353,17 @@ QDataStream &operator>>(QDataStream &in, Settings::ExternalLinkPolicy &policy)
policy = static_cast<Settings::ExternalLinkPolicy>(value);
return in;
}

QDataStream &operator<<(QDataStream &out, Settings::UiStyle uiStyle)
{
out << static_cast<std::underlying_type<Settings::UiStyle>::type>(uiStyle);
return out;
}

QDataStream &operator>>(QDataStream &in, Settings::UiStyle &uiStyle)
{
std::underlying_type<Settings::UiStyle>::type value;
in >> value;
uiStyle = static_cast<Settings::UiStyle>(value);
return in;
}
9 changes: 9 additions & 0 deletions src/libs/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ class Settings final : public QObject
QString customCssFile;
bool isSmoothScrollingEnabled;

enum class UiStyle {
SystemDefault,
Dark,
CustomCssFile
};
Q_ENUM(UiStyle)
UiStyle uiStyle = UiStyle::SystemDefault;
QString customUiCssFile;

// Network
enum ProxyType : unsigned int {
None = 0,
Expand Down
48 changes: 36 additions & 12 deletions src/libs/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,24 @@ void MainWindow::keyPressEvent(QKeyEvent *keyEvent)
}
}

namespace
{
void addFileContent(QByteArray& destinationArray, const QString& filename)
{
if (filename.isEmpty()) {
return;
}
if (QFileInfo::exists(filename)) {
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
destinationArray += file.readAll();
}
} else {
qWarning("File does not exists: %s", filename.toUtf8().data());
}
}
}

void MainWindow::applySettings()
{
m_globalShortcut->setShortcut(m_settings->showShortcut);
Expand All @@ -525,27 +543,33 @@ void MainWindow::applySettings()
else
removeTrayIcon();

// UI style
QByteArray uiCsssContent;
switch (m_settings->uiStyle) {
case Core::Settings::UiStyle::SystemDefault:
// system style = no css content
break;
case Core::Settings::UiStyle::Dark:
addFileContent(uiCsssContent, ":/css/darkorange.stylesheet");
break;
case Core::Settings::UiStyle::CustomCssFile:
addFileContent(uiCsssContent, m_settings->customUiCssFile);
break;
}
qApp->setStyleSheet(QString::fromUtf8(uiCsssContent));

// Content
QByteArray ba = QByteArrayLiteral("body { background-color: white; }");
if (m_settings->darkModeEnabled) {
QScopedPointer<QFile> file(new QFile(DarkModeCssUrl));
if (file->open(QIODevice::ReadOnly)) {
ba += file->readAll();
}
addFileContent(ba, DarkModeCssUrl);
}

if (m_settings->highlightOnNavigateEnabled) {
QScopedPointer<QFile> file(new QFile(HighlightOnNavigateCssUrl));
if (file->open(QIODevice::ReadOnly)) {
ba += file->readAll();
}
addFileContent(ba, HighlightOnNavigateCssUrl);
}

if (QFileInfo::exists(m_settings->customCssFile)) {
QScopedPointer<QFile> file(new QFile(m_settings->customCssFile));
if (file->open(QIODevice::ReadOnly)) {
ba += file->readAll();
}
addFileContent(ba, m_settings->customCssFile);
}

const QString cssUrl = QLatin1String("data:text/css;charset=utf-8;base64,") + ba.toBase64();
Expand Down
13 changes: 9 additions & 4 deletions src/libs/ui/searchitemdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,19 @@ void SearchItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
const QString elidedText = fm.elidedText(opt.text, opt.textElideMode, textRect.width());

if (!m_highlight.isEmpty()) {
QColor highlightColor = QColor::fromRgb(255, 255, 100);
if(painter->pen().color().toHsv().value() > 127)
highlightColor = QColor::fromRgb(100, 100, 0);;

if(opt.state & (QStyle::State_Selected | QStyle::State_HasFocus))
highlightColor.setAlpha(20);
else
highlightColor.setAlpha(120);

painter->save();
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(QColor::fromRgb(255, 253, 0));

const QColor highlightColor
= (opt.state & (QStyle::State_Selected | QStyle::State_HasFocus))
? QColor::fromRgb(255, 255, 100, 20) : QColor::fromRgb(255, 255, 100, 120);

for (int i = 0;;) {
const int matchIndex = opt.text.indexOf(m_highlight, i, Qt::CaseInsensitive);
if (matchIndex == -1 || matchIndex >= elidedText.length() - 1)
Expand Down
33 changes: 33 additions & 0 deletions src/libs/ui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ void SettingsDialog::chooseCustomCssFile()
ui->customCssFileEdit->setText(QDir::toNativeSeparators(file));
}

void SettingsDialog::chooseCustomUiCssFile()
{
const QString file = QFileDialog::getOpenFileName(this, tr("Choose UI CSS File"),
ui->customCssFileEdit->text(),
tr("CSS Files (*.css);;All Files (*.*)"));
if (file.isEmpty())
return;

ui->customUiCssFileEdit->setText(QDir::toNativeSeparators(file));
}

void SettingsDialog::chooseDocsetStoragePath()
{
QString path = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
Expand Down Expand Up @@ -201,6 +212,19 @@ void SettingsDialog::loadSettings()
ui->highlightOnNavigateCheckBox->setChecked(settings->highlightOnNavigateEnabled);
ui->customCssFileEdit->setText(QDir::toNativeSeparators(settings->customCssFile));

switch (settings->uiStyle) {
case Core::Settings::UiStyle::SystemDefault:
ui->radioUiStyleSystemDefault->setChecked(true);
break;
case Core::Settings::UiStyle::Dark:
ui->radioUiStyleDark->setChecked(true);
break;
case Core::Settings::UiStyle::CustomCssFile:
ui->radioUiStyleCustomCssFile->setChecked(true);
break;
}
ui->customUiCssFileEdit->setText(QDir::toNativeSeparators(settings->customUiCssFile));

switch (settings->externalLinkPolicy) {
case Core::Settings::ExternalLinkPolicy::Ask:
ui->radioExternalLinkAsk->setChecked(true);
Expand Down Expand Up @@ -276,6 +300,15 @@ void SettingsDialog::saveSettings()
settings->highlightOnNavigateEnabled = ui->highlightOnNavigateCheckBox->isChecked();
settings->customCssFile = QDir::fromNativeSeparators(ui->customCssFileEdit->text());

if (ui->radioUiStyleSystemDefault->isChecked()) {
settings->uiStyle = Core::Settings::UiStyle::SystemDefault;
} else if (ui->radioUiStyleDark->isChecked()) {
settings->uiStyle = Core::Settings::UiStyle::Dark;
} else if (ui->radioUiStyleCustomCssFile->isChecked()) {
settings->uiStyle = Core::Settings::UiStyle::CustomCssFile;
}
settings->customUiCssFile = QDir::fromNativeSeparators(ui->customUiCssFileEdit->text());

if (ui->radioExternalLinkAsk->isChecked()) {
settings->externalLinkPolicy = Core::Settings::ExternalLinkPolicy::Ask;
} else if (ui->radioExternalLinkOpen->isChecked()) {
Expand Down
1 change: 1 addition & 0 deletions src/libs/ui/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class SettingsDialog : public QDialog

private slots:
void chooseCustomCssFile();
void chooseCustomUiCssFile();
void chooseDocsetStoragePath();

private:
Expand Down
Loading