Skip to content

Commit

Permalink
修复了windows在深色模式下ui显示不清楚的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost-him committed Aug 7, 2024
1 parent b0d3768 commit 3bdfc6c
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 deletions.
2 changes: 1 addition & 1 deletion controller/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ SettingWindowConfigure buildClassWithJson(const QJsonObject &json)

QString getProgramVersion()
{
return "ZeroLaunch 0.7.1";
return "ZeroLaunch 0.7.2";
}

QString getPinyinConfigPath()
Expand Down
27 changes: 12 additions & 15 deletions ui/resultframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <QScrollBar>
#include <QPropertyAnimation>
#include <QSize>
#include "uiutils.h"

ResultFrame::ResultFrame(QWidget *parent)
: QWidget(parent)
Expand Down Expand Up @@ -55,32 +56,28 @@ ResultFrame::ResultFrame(QWidget *parent)
"}"
"QListWidget {"
" border-radius: 10px;" // 设置 QListWidget 的圆角边框
" background-color: white;" // 设置背景色为透明
" border: 1px solid gray;"
" background-color: %1;" // 设置背景色为透明
" border: 1px solid %2;"
"}"
"QListWidget::item:selected {"
" background-color: %1;" // 设置选中项的背景颜色
" color: %2;" // 设置选中项的文字颜色
" background-color: %3;" // 设置选中项的背景颜色
" color: %4;" // 设置选中项的文字颜色
" border-radius: 10px;"
" font-weight: bold;" // 设置选中项的文字加粗
"}"
"QListWidget::item:selected:!active {"
" background-color: %3;" // 设置非活动状态下选中项的背景颜色
" color: %2;" // 设置非活动状态下选中项的文字颜色
" border-radius: 10px;"
" font-weight: bold;" // 设置非活动状态下选中项的文字加粗
"}"
"QListWidget::item {"
" padding-left: 4px;" // 设置左侧填充,使标志更宽
" padding-right: 4px;" // 设置右侧填充,使标志更宽
" border-radius: 10px;"
" color: black;" // 设置普通项的文字颜色
" background-color: white;" // 设置普通项的背景颜色
" color: %5;" // 设置普通项的文字颜色
" background-color: %1;" // 设置普通项的背景颜色
"}"
).arg(
palette.color(QPalette::Highlight).name(), // 获取系统主题的高亮颜色
palette.color(QPalette::HighlightedText).name(), // 获取系统主题的高亮文字颜色
palette.color(QPalette::Inactive, QPalette::Highlight).name() // 获取系统主题的非活动状态高亮颜色
Color::backgroundColor(),
Color::borderColor(),
Color::selectedBackgroundColor(),
Color::selectedTextColor(),
Color::textColor()
));
}

Expand Down
8 changes: 6 additions & 2 deletions ui/searchbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ SearchBar::SearchBar() {

this->setStyleSheet(QString(
"QLineEdit{"
"background-color: white;"
"background-color: %1;"
"color: %2;"
"border: 1px solid gray;"
"border-radius: 10px;" // 设置圆角半径
"padding: 8px;"
"}"));
"}").arg(
Color::backgroundColor(),
Color::textColor()
));
}

void SearchBar::focusOnSearchBar()
Expand Down
60 changes: 60 additions & 0 deletions ui/uiutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "../controller/uicontroller.h"
#include "searchbar.h"
#include <QMessageBox>
#include "singleapplication.h"

QAction* createExitAction(QObject *parent)
{
Expand Down Expand Up @@ -62,3 +63,62 @@ ESC:当搜索栏中有文字时,则清屏;没有文字时,则隐藏搜
});
return showHelp;
}

QPalette* Color::palette = nullptr;

bool Color::isDarkMode()
{
if (!palette) {
palette = new QPalette(SingleApplication::palette());
}


static std::optional<bool> ret;
if (ret.has_value()) {
return ret.value();
}
#ifdef Q_OS_WIN
HKEY hKey;
DWORD type;
DWORD value;
DWORD size = sizeof(value);

// 打开注册表项
if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Themes\\Personalize"), 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
// 读取 AppsUseLightTheme 值
if (RegQueryValueEx(hKey, TEXT("AppsUseLightTheme"), NULL, &type, reinterpret_cast<LPBYTE>(&value), &size) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
ret = static_cast<bool>(value == 0);
}
RegCloseKey(hKey);
}
#endif
return ret.value_or(false); // 如果无法确定,默认返回false(非深色模式)
}

QString Color::textColor()
{
return isDarkMode() ? "#E0E0E0" : "black";
}

QString Color::selectedBackgroundColor()
{
return isDarkMode() ? "#3F3F46" : palette->color(QPalette::Highlight).name();
}

QString Color::selectedTextColor()
{
return isDarkMode() ? "#FFFFFF" : palette->color(QPalette::HighlightedText).name();
}

QString Color::backgroundColor()
{
return isDarkMode() ? "#2D2D30" : "white";
}

QString Color::borderColor()
{
return isDarkMode() ? "#555555" : "gray";
}
15 changes: 15 additions & 0 deletions ui/uiutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define UIUTILS_H

#include <QAction>
#include <QPalette>


// 获取Action
Expand All @@ -13,5 +14,19 @@ QAction* createReloadSettingAction(QObject *parent);

QAction* createShowHelpAction(QObject *parent);

class Color {
public:
static bool isDarkMode();
static QString textColor();
static QString selectedBackgroundColor();
static QString selectedTextColor();
static QString backgroundColor();
static QString borderColor();
private:
static QPalette* palette;
};




#endif // UIUTILS_H

0 comments on commit 3bdfc6c

Please sign in to comment.