FluentUI/src/FramelessView_unix.cpp

145 lines
3.3 KiB
C++
Raw Normal View History

2023-02-26 23:47:07 +08:00
#include "FramelessView.h"
#include <QGuiApplication>
#include <QQuickItem>
#include <QScreen>
#include <QWindow>
2023-03-09 11:50:40 +08:00
#include <FluTheme.h>
2023-02-26 23:47:07 +08:00
class FramelessViewPrivate
{
public:
bool m_isMax = false;
bool m_isFull = false;
2023-03-02 18:21:43 +08:00
bool m_deleteLater = false;
2023-02-26 23:47:07 +08:00
QQuickItem *m_titleItem = nullptr;
};
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
FramelessView::FramelessView(QWindow *parent) : Super(parent), d(new FramelessViewPrivate)
{
2023-03-09 11:50:40 +08:00
refreshWindow();
2023-02-26 23:47:07 +08:00
setIsMax(windowState() == Qt::WindowMaximized);
setIsFull(windowState() == Qt::WindowFullScreen);
connect(this, &QWindow::windowStateChanged, this, [&](Qt::WindowState state) {
(void)state;
setIsMax(windowState() == Qt::WindowMaximized);
setIsFull(windowState() == Qt::WindowFullScreen);
});
2023-03-09 11:50:40 +08:00
connect(FluTheme::getInstance(),&FluTheme::isFramelessChanged,this,[=](){
refreshWindow();
});
}
void FramelessView::refreshWindow(){
if(FluTheme::getInstance()->isFrameless()){
setFlags(Qt::CustomizeWindowHint | Qt::Window | Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
}else{
setFlags(Qt::Window);
}
setResizeMode(SizeViewToRootObject);
setResizeMode(SizeRootObjectToView);
2023-02-26 23:47:07 +08:00
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
FramelessView::~FramelessView()
{
delete d;
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
void FramelessView::showEvent(QShowEvent *e)
{
Super::showEvent(e);
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
QRect FramelessView::calcCenterGeo(const QRect &screenGeo, const QSize &normalSize)
{
int w = normalSize.width();
int h = normalSize.height();
int x = screenGeo.x() + (screenGeo.width() - w) / 2;
int y = screenGeo.y() + (screenGeo.height() - h) / 2;
if (screenGeo.width() < w) {
x = screenGeo.x();
w = screenGeo.width();
}
if (screenGeo.height() < h) {
y = screenGeo.y();
h = screenGeo.height();
}
return { x, y, w, h };
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
void FramelessView::moveToScreenCenter()
{
auto geo = calcCenterGeo(screen()->availableGeometry(), size());
if (minimumWidth() > geo.width() || minimumHeight() > geo.height()) {
setMinimumSize(geo.size());
}
setGeometry(geo);
update();
}
2023-03-09 11:50:40 +08:00
2023-03-02 18:21:43 +08:00
void FramelessView::closeDeleteLater(){
d->m_deleteLater = true;
}
2023-02-26 23:47:07 +08:00
bool FramelessView::isMax() const
{
return d->m_isMax;
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
bool FramelessView::isFull() const
{
return d->m_isFull;
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
QQuickItem *FramelessView::titleItem() const
{
return d->m_titleItem;
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
void FramelessView::setIsMax(bool isMax)
{
if (d->m_isMax == isMax)
return;
d->m_isMax = isMax;
emit isMaxChanged(d->m_isMax);
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
void FramelessView::setIsFull(bool isFull)
{
if(d->m_isFull == isFull)
return;
d->m_isFull = isFull;
emit isFullChanged(d->m_isFull);
}
2023-03-09 11:50:40 +08:00
2023-02-26 23:47:07 +08:00
void FramelessView::setTitleItem(QQuickItem *item)
{
d->m_titleItem = item;
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
bool FramelessView::nativeEvent(const QByteArray &eventType, void *message, qintptr *result)
#else
bool FramelessView::nativeEvent(const QByteArray &eventType, void *message, long *result)
#endif
{
return Super::nativeEvent(eventType, message, result);
}
void FramelessView::resizeEvent(QResizeEvent *e)
{
Super::resizeEvent(e);
}
2023-03-09 11:50:40 +08:00
bool FramelessView::event(QEvent *ev)
{
if (ev->type() == QEvent::Close) {
if(d->m_deleteLater){
deleteLater();
ev->setAccepted(false);
}
}
return QQuickWindow::event(ev);
}