add "hideWhenClose" property

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-05-23 21:09:09 +08:00
parent 093040ec93
commit 0cff5ff48e
5 changed files with 68 additions and 2 deletions

View File

@ -51,6 +51,7 @@ class FRAMELESSHELPER_QUICK_API QuickStandardTitleBar : public QQuickRectangle
Q_PROPERTY(QuickStandardSystemButton* closeButton READ closeButton CONSTANT FINAL)
Q_PROPERTY(bool extended READ isExtended WRITE setExtended NOTIFY extendedChanged FINAL)
Q_PROPERTY(bool useAlternativeBackground READ isUsingAlternativeBackground WRITE setUseAlternativeBackground NOTIFY useAlternativeBackgroundChanged FINAL)
Q_PROPERTY(bool hideWhenClose READ isHideWhenClose WRITE setHideWhenClose NOTIFY hideWhenCloseChanged FINAL)
public:
explicit QuickStandardTitleBar(QQuickItem *parent = nullptr);
@ -70,6 +71,9 @@ public:
Q_NODISCARD bool isUsingAlternativeBackground() const;
void setUseAlternativeBackground(const bool value);
Q_NODISCARD bool isHideWhenClose() const;
void setHideWhenClose(const bool value);
protected:
void itemChange(const ItemChange change, const ItemChangeData &value) override;
Q_NODISCARD bool eventFilter(QObject *object, QEvent *event) override;
@ -87,6 +91,7 @@ Q_SIGNALS:
void titleLabelAlignmentChanged();
void extendedChanged();
void useAlternativeBackgroundChanged();
void hideWhenCloseChanged();
private:
void initialize();
@ -104,6 +109,7 @@ private:
QMetaObject::Connection m_windowTitleChangeConnection = {};
bool m_extended = false;
bool m_useAlternativeBackground = false;
bool m_hideWhenClose = false;
};
FRAMELESSHELPER_END_NAMESPACE

View File

@ -60,6 +60,9 @@ public:
Q_NODISCARD bool isUsingAlternativeBackground() const;
void setUseAlternativeBackground(const bool value);
Q_NODISCARD bool isHideWhenClose() const;
void setHideWhenClose(const bool value);
public Q_SLOTS:
void updateMaximizeButton();
void updateTitleBarStyleSheet();
@ -83,6 +86,7 @@ private:
QSpacerItem *m_labelLeftStretch = nullptr;
QSpacerItem *m_labelRightStretch = nullptr;
bool m_useAlternativeBackground = false;
bool m_hideWhenClose = false;
};
FRAMELESSHELPER_END_NAMESPACE

View File

@ -45,6 +45,7 @@ class FRAMELESSHELPER_WIDGETS_API StandardTitleBar : public QWidget
Q_PROPERTY(StandardSystemButton* closeButton READ closeButton CONSTANT FINAL)
Q_PROPERTY(bool extended READ isExtended WRITE setExtended NOTIFY extendedChanged FINAL)
Q_PROPERTY(bool useAlternativeBackground READ isUsingAlternativeBackground WRITE setUseAlternativeBackground NOTIFY useAlternativeBackgroundChanged FINAL)
Q_PROPERTY(bool hideWhenClose READ isHideWhenClose WRITE setHideWhenClose NOTIFY hideWhenCloseChanged FINAL)
public:
explicit StandardTitleBar(QWidget *parent = nullptr);
@ -64,6 +65,9 @@ public:
Q_NODISCARD bool isUsingAlternativeBackground() const;
void setUseAlternativeBackground(const bool value);
Q_NODISCARD bool isHideWhenClose() const;
void setHideWhenClose(const bool value);
protected:
void paintEvent(QPaintEvent *event) override;
@ -71,6 +75,7 @@ Q_SIGNALS:
void extendedChanged();
void titleLabelAlignmentChanged();
void useAlternativeBackgroundChanged();
void hideWhenCloseChanged();
private:
QScopedPointer<StandardTitleBarPrivate> d_ptr;

View File

@ -136,6 +136,20 @@ void QuickStandardTitleBar::setUseAlternativeBackground(const bool value)
Q_EMIT useAlternativeBackgroundChanged();
}
bool QuickStandardTitleBar::isHideWhenClose() const
{
return m_hideWhenClose;
}
void QuickStandardTitleBar::setHideWhenClose(const bool value)
{
if (m_hideWhenClose == value) {
return;
}
m_hideWhenClose = value;
Q_EMIT hideWhenCloseChanged();
}
void QuickStandardTitleBar::updateMaximizeButton()
{
const QQuickWindow * const w = window();
@ -237,7 +251,11 @@ void QuickStandardTitleBar::clickCloseButton()
if (!w) {
return;
}
w->close();
if (m_hideWhenClose) {
w->hide();
} else {
w->close();
}
}
void QuickStandardTitleBar::retranslateUi()

View File

@ -139,6 +139,21 @@ void StandardTitleBarPrivate::setUseAlternativeBackground(const bool value)
Q_EMIT q->useAlternativeBackgroundChanged();
}
bool StandardTitleBarPrivate::isHideWhenClose() const
{
return m_hideWhenClose;
}
void StandardTitleBarPrivate::setHideWhenClose(const bool value)
{
if (m_hideWhenClose == value) {
return;
}
m_hideWhenClose = value;
Q_Q(StandardTitleBar);
Q_EMIT q->hideWhenCloseChanged();
}
void StandardTitleBarPrivate::updateMaximizeButton()
{
const bool max = m_window->isMaximized();
@ -251,7 +266,13 @@ void StandardTitleBarPrivate::initialize()
}
});
m_closeButton.reset(new StandardSystemButton(SystemButtonType::Close, q));
connect(m_closeButton.data(), &StandardSystemButton::clicked, m_window, &QWidget::close);
connect(m_closeButton.data(), &StandardSystemButton::clicked, this, [this](){
if (m_hideWhenClose) {
m_window->hide();
} else {
m_window->close();
}
});
m_labelLeftStretch = new QSpacerItem(0, 0);
m_labelRightStretch = new QSpacerItem(0, 0);
const auto titleLabelLayout = new QHBoxLayout;
@ -355,6 +376,18 @@ void StandardTitleBar::setUseAlternativeBackground(const bool value)
d->setUseAlternativeBackground(value);
}
bool StandardTitleBar::isHideWhenClose() const
{
Q_D(const StandardTitleBar);
return d->isHideWhenClose();
}
void StandardTitleBar::setHideWhenClose(const bool value)
{
Q_D(StandardTitleBar);
d->setHideWhenClose(value);
}
void StandardTitleBar::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);