Minor improvements.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-10-21 21:21:56 +08:00
parent 19da037a77
commit 6a12cb1a30
4 changed files with 71 additions and 48 deletions

View File

@ -50,6 +50,7 @@ namespace {
const char useNativeTitleBar[] = "WNEF_USE_NATIVE_TITLE_BAR"; const char useNativeTitleBar[] = "WNEF_USE_NATIVE_TITLE_BAR";
const char preserveWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME"; const char preserveWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME";
const char forceUseAcrylicEffect[] = "WNEF_FORCE_ACRYLIC_ON_WIN10"; const char forceUseAcrylicEffect[] = "WNEF_FORCE_ACRYLIC_ON_WIN10";
const char dontExtendFrame[] = "WNEF_DO_NOT_EXTEND_FRAME";
const QLatin1String systemButtonsStyleSheet(R"( const QLatin1String systemButtonsStyleSheet(R"(
#iconButton, #minimizeButton, #maximizeButton, #closeButton { #iconButton, #minimizeButton, #maximizeButton, #closeButton {
@ -89,7 +90,7 @@ void updateWindow(QWidget *widget)
Q_ASSERT(widget); Q_ASSERT(widget);
if (widget->isTopLevel()) { if (widget->isTopLevel()) {
void *handle = getRawHandle(widget); void *handle = getRawHandle(widget);
//WinNativeEventFilter::updateFrameMargins(handle); WinNativeEventFilter::updateFrameMargins(handle);
WinNativeEventFilter::updateWindow(handle, true, true); WinNativeEventFilter::updateWindow(handle, true, true);
widget->update(); widget->update();
} }
@ -100,7 +101,7 @@ bool isWin10OrGreater()
return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10; return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10;
} }
bool isGreaterThanWin10_1803() bool isWin101803OrGreater()
{ {
return QOperatingSystemVersion::current() return QOperatingSystemVersion::current()
>= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, 17134); >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, 17134);
@ -147,17 +148,14 @@ QColor getThemeColor()
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{ {
m_bIsWin10OrGreater = isWin10OrGreater(); m_bIsWin10OrGreater = isWin10OrGreater();
m_bIsWin101803OrGreater = isWin101803OrGreater();
m_cThemeColor = getThemeColor(); m_cThemeColor = getThemeColor();
ui->setupUi(this); ui->setupUi(this);
ui->forceAcrylicCB->setEnabled(m_bIsWin101803OrGreater);
ui->forceAcrylicCB->setEnabled(isGreaterThanWin10_1803());
if (shouldDrawBorder()) { if (shouldDrawBorder()) {
layout()->setContentsMargins(1, 1, 1, 1); layout()->setContentsMargins(1, 1, 1, 1);
} }
updateTitleBar(); updateTitleBar();
connect(ui->iconButton, &QPushButton::clicked, this, [this]() { connect(ui->iconButton, &QPushButton::clicked, this, [this]() {
@ -166,7 +164,6 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
const auto hwnd = reinterpret_cast<HWND>(getRawHandle(this)); const auto hwnd = reinterpret_cast<HWND>(getRawHandle(this));
SendMessageW(hwnd, WM_CONTEXTMENU, reinterpret_cast<WPARAM>(hwnd), MAKELPARAM(pos.x, pos.y)); SendMessageW(hwnd, WM_CONTEXTMENU, reinterpret_cast<WPARAM>(hwnd), MAKELPARAM(pos.x, pos.y));
}); });
connect(ui->minimizeButton, &QPushButton::clicked, this, &Widget::showMinimized); connect(ui->minimizeButton, &QPushButton::clicked, this, &Widget::showMinimized);
connect(ui->maximizeButton, &QPushButton::clicked, this, [this]() { connect(ui->maximizeButton, &QPushButton::clicked, this, [this]() {
if (isMaximized()) { if (isMaximized()) {
@ -176,17 +173,14 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
} }
}); });
connect(ui->closeButton, &QPushButton::clicked, this, &Widget::close); connect(ui->closeButton, &QPushButton::clicked, this, &Widget::close);
connect(ui->moveCenterButton, &QPushButton::clicked, this, [this]() { connect(ui->moveCenterButton, &QPushButton::clicked, this, [this]() {
WinNativeEventFilter::moveWindowToDesktopCenter(getRawHandle(this)); WinNativeEventFilter::moveWindowToDesktopCenter(getRawHandle(this));
}); });
connect(this, &Widget::windowTitleChanged, ui->titleLabel, &QLabel::setText); connect(this, &Widget::windowTitleChanged, ui->titleLabel, &QLabel::setText);
connect(this, &Widget::windowIconChanged, ui->iconButton, &QPushButton::setIcon); connect(this, &Widget::windowIconChanged, ui->iconButton, &QPushButton::setIcon);
connect(ui->customizeTitleBarCB, &QCheckBox::stateChanged, this, [this](int state) { connect(ui->customizeTitleBarCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked; const bool enable = state == Qt::Checked;
ui->removeWindowFrameCB->setEnabled(enable); ui->preserveWindowFrameCB->setEnabled(enable);
WinNativeEventFilter::updateQtFrame(windowHandle(), WinNativeEventFilter::updateQtFrame(windowHandle(),
enable ? WinNativeEventFilter::getSystemMetric( enable ? WinNativeEventFilter::getSystemMetric(
getRawHandle(this), getRawHandle(this),
@ -200,25 +194,28 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
} }
updateWindow(this); updateWindow(this);
}); });
connect(ui->removeWindowFrameCB, &QCheckBox::stateChanged, this, [this](int state) { connect(ui->preserveWindowFrameCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked; const bool enable = state == Qt::Checked;
if (enable) { if (enable) {
qunsetenv(preserveWindowFrame);
} else {
qputenv(preserveWindowFrame, "1"); qputenv(preserveWindowFrame, "1");
qputenv(dontExtendFrame, "1");
} else {
qunsetenv(preserveWindowFrame);
qunsetenv(dontExtendFrame);
} }
if (enable && shouldDrawBorder()) { if (!enable && shouldDrawBorder()) {
layout()->setContentsMargins(1, 1, 1, 1); layout()->setContentsMargins(1, 1, 1, 1);
} else { } else {
layout()->setContentsMargins(0, 0, 0, 0); layout()->setContentsMargins(0, 0, 0, 0);
} }
updateTitleBar();
updateWindow(this); updateWindow(this);
}); });
connect(ui->blurEffectCB, &QCheckBox::stateChanged, this, [this](int state) { connect(ui->blurEffectCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked; const bool enable = state == Qt::Checked;
QColor color = Qt::white; QColor color = Qt::white;
if (isGreaterThanWin10_1803() && ui->forceAcrylicCB->isChecked()) { if (m_bIsWin101803OrGreater && ui->forceAcrylicCB->isChecked()) {
if (enable) { if (enable && m_bShowColorDialog) {
color = QColorDialog::getColor(color, color = QColorDialog::getColor(color,
this, this,
tr("Please select a gradient color"), tr("Please select a gradient color"),
@ -233,6 +230,9 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
updateTitleBar(); updateTitleBar();
}); });
connect(ui->forceAcrylicCB, &QCheckBox::stateChanged, this, [this](int state) { connect(ui->forceAcrylicCB, &QCheckBox::stateChanged, this, [this](int state) {
if (!m_bIsWin101803OrGreater) {
return;
}
if (state == Qt::Checked) { if (state == Qt::Checked) {
qputenv(forceUseAcrylicEffect, "1"); qputenv(forceUseAcrylicEffect, "1");
} else { } else {
@ -255,6 +255,8 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
WinNativeEventFilter::addFramelessWindow(this, &data); WinNativeEventFilter::addFramelessWindow(this, &data);
installEventFilter(this); installEventFilter(this);
initWindow();
} }
Widget::~Widget() Widget::~Widget()
@ -270,7 +272,7 @@ bool Widget::isNormaled() const
bool Widget::shouldDrawBorder(const bool ignoreWindowState) const bool Widget::shouldDrawBorder(const bool ignoreWindowState) const
{ {
return m_bIsWin10OrGreater && (ignoreWindowState ? true : isNormaled()) return m_bIsWin10OrGreater && (ignoreWindowState ? true : isNormaled())
&& ui->removeWindowFrameCB->isChecked() && ui->customizeTitleBarCB->isChecked(); && !ui->preserveWindowFrameCB->isChecked() && ui->customizeTitleBarCB->isChecked();
} }
bool Widget::shouldDrawThemedBorder(const bool ignoreWindowState) const bool Widget::shouldDrawThemedBorder(const bool ignoreWindowState) const
@ -405,28 +407,53 @@ void Widget::updateTitleBar()
ui->maximizeButton->setIcon(QIcon(QLatin1String(":/images/button_maximize_black.svg"))); ui->maximizeButton->setIcon(QIcon(QLatin1String(":/images/button_maximize_black.svg")));
} }
} }
const QColor titleBarColor = m_bExtendToTitleBar ? Qt::transparent const QColor titleBarBackgroundColor = m_bExtendToTitleBar
? Qt::transparent
: (themedTitleBar ? m_cThemeColor : Qt::white); : (themedTitleBar ? m_cThemeColor : Qt::white);
const QColor titleTextColor = isActiveWindow() const QColor titleBarTextColor = isActiveWindow()
? ((!themedTitleBar || m_bExtendToTitleBar) ? Qt::black ? ((!themedTitleBar || m_bExtendToTitleBar) ? Qt::black
: Qt::white) : Qt::white)
: QColor("#999999"); : QColor("#999999");
const QColor titleBarBorderColor = (!m_bIsWin10OrGreater || shouldDrawBorder() || isMaximized()
|| isFullScreen())
? Qt::transparent
: borderColor();
ui->titleBarWidget->setStyleSheet(systemButtonsStyleSheet ui->titleBarWidget->setStyleSheet(systemButtonsStyleSheet
+ QLatin1String(R"( + QLatin1String(R"(
#titleLabel { #titleLabel {
color: rgb(%1, %2, %3); color: rgb(%1, %2, %3);
} }
)") )")
.arg(QString::number(titleTextColor.red()), .arg(QString::number(titleBarTextColor.red()),
QString::number(titleTextColor.green()), QString::number(titleBarTextColor.green()),
QString::number(titleTextColor.blue())) QString::number(titleBarTextColor.blue()))
+ QLatin1String(R"( + QLatin1String(R"(
#titleBarWidget { #titleBarWidget {
background-color: rgba(%1, %2, %3, %4); background-color: rgba(%1, %2, %3, %4);
border-top: 1px solid rgba(%5, %6, %7, %8);
} }
)") )")
.arg(QString::number(titleBarColor.red()), .arg(QString::number(titleBarBackgroundColor.red()),
QString::number(titleBarColor.green()), QString::number(titleBarBackgroundColor.green()),
QString::number(titleBarColor.blue()), QString::number(titleBarBackgroundColor.blue()),
QString::number(titleBarColor.alpha()))); QString::number(titleBarBackgroundColor.alpha()),
QString::number(titleBarBorderColor.red()),
QString::number(titleBarBorderColor.green()),
QString::number(titleBarBorderColor.blue()),
QString::number(titleBarBorderColor.alpha())));
}
void Widget::initWindow()
{
if (m_bIsWin10OrGreater) {
ui->preserveWindowFrameCB->click();
if (m_bIsWin101803OrGreater) {
ui->forceAcrylicCB->click();
}
}
ui->customizeTitleBarCB->click();
ui->extendToTitleBarCB->click();
ui->blurEffectCB->click();
ui->resizableCB->click();
m_bShowColorDialog = true;
} }

View File

@ -71,10 +71,12 @@ protected:
private: private:
void updateTitleBar(); void updateTitleBar();
void initWindow();
private: private:
Ui::Widget *ui = nullptr; Ui::Widget *ui = nullptr;
bool m_bIsWin10OrGreater = false, m_bExtendToTitleBar = false; bool m_bIsWin10OrGreater = false, m_bIsWin101803OrGreater = false, m_bExtendToTitleBar = false,
m_bShowColorDialog = false;
const QColor m_cDefaultActiveBorderColor = {"#707070"} /*Qt::darkGray*/; const QColor m_cDefaultActiveBorderColor = {"#707070"} /*Qt::darkGray*/;
const QColor m_cDefaultInactiveBorderColor = {"#aaaaaa"} /*Qt::gray*/; const QColor m_cDefaultInactiveBorderColor = {"#aaaaaa"} /*Qt::gray*/;
QColor m_cThemeColor = Qt::white; QColor m_cThemeColor = Qt::white;

View File

@ -280,13 +280,10 @@
<property name="text"> <property name="text">
<string>Enable customized title bar</string> <string>Enable customized title bar</string>
</property> </property>
<property name="checked">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QCheckBox" name="removeWindowFrameCB"> <widget class="QCheckBox" name="preserveWindowFrameCB">
<property name="font"> <property name="font">
<font> <font>
<pointsize>15</pointsize> <pointsize>15</pointsize>
@ -295,10 +292,7 @@
</font> </font>
</property> </property>
<property name="text"> <property name="text">
<string>Remove window frame</string> <string>Preserve window frame</string>
</property>
<property name="checked">
<bool>true</bool>
</property> </property>
</widget> </widget>
</item> </item>
@ -356,9 +350,6 @@
<property name="text"> <property name="text">
<string>Resizable</string> <string>Resizable</string>
</property> </property>
<property name="checked">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>

View File

@ -662,6 +662,7 @@ const char envVarUseNativeTitleBar[] = "WNEF_USE_NATIVE_TITLE_BAR";
const char envVarPreserveWindowFrame[] = "WNEF_PRESERVE_WINDOW_FRAME"; const char envVarPreserveWindowFrame[] = "WNEF_PRESERVE_WINDOW_FRAME";
const char envVarForceWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME"; const char envVarForceWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME";
const char envVarForceAcrylic[] = "WNEF_FORCE_ACRYLIC_ON_WIN10"; const char envVarForceAcrylic[] = "WNEF_FORCE_ACRYLIC_ON_WIN10";
const char envVarNoExtendFrame[] = "WNEF_DO_NOT_EXTEND_FRAME";
bool shouldUseNativeTitleBar() bool shouldUseNativeTitleBar()
{ {
@ -699,6 +700,11 @@ bool forceEnableAcrylicOnWin10()
return qEnvironmentVariableIsSet(envVarForceAcrylic); return qEnvironmentVariableIsSet(envVarForceAcrylic);
} }
bool dontExtendFrame()
{
return qEnvironmentVariableIsSet(envVarNoExtendFrame);
}
BOOL IsDwmCompositionEnabled() BOOL IsDwmCompositionEnabled()
{ {
// Since Win8, DWM composition is always enabled and can't be disabled. // Since Win8, DWM composition is always enabled and can't be disabled.
@ -999,7 +1005,7 @@ void UpdateFrameMarginsForWindow(const HWND handle)
} else { } else {
margins.cyTopHeight = 1; margins.cyTopHeight = 1;
} }
if (shouldUseNativeTitleBar()) { if (shouldUseNativeTitleBar() || dontExtendFrame()) {
// If we are going to use the native title bar, // If we are going to use the native title bar,
// we should use the original window frame as well. // we should use the original window frame as well.
margins = {0, 0, 0, 0}; margins = {0, 0, 0, 0};
@ -1621,9 +1627,6 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// area. // area.
*result = 0; *result = 0;
} }
if (shouldHaveWindowFrame()) {
*result = 0;
}
/* /*
// It does solve the flickering issue indeed, however, it also // It does solve the flickering issue indeed, however, it also
// causes a lot of new issues when we are trying to draw // causes a lot of new issues when we are trying to draw