feat: allow dragging frameless window

This commit is contained in:
talksik
2026-01-28 18:50:10 -08:00
parent a82aef0ae5
commit 62c262e0a9
2 changed files with 52 additions and 2 deletions
+43 -2
View File
@@ -12,6 +12,7 @@
#include <QItemSelectionModel>
#include <QLabel>
#include <QMenu>
#include <QMouseEvent>
#include <QRandomGenerator>
#include <QSystemTrayIcon>
#include <QTimer>
@@ -19,13 +20,16 @@
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), m_trayIcon(nullptr), m_authManager(new AuthManager(this)), m_settingsDialog(nullptr),
m_authDialog(nullptr), m_data(nullptr), m_networkStreamModel(nullptr), m_particleListModel(nullptr),
m_selectedParticleRow(-1)
m_selectedParticleRow(-1), m_isDragging(false)
{
m_authDialog = new AuthDialog(m_authManager, this);
ui = new Ui::MainWindow;
ui->setupUi(this);
// this->setWindowFlags(Qt::FramelessWindowHint);
// Enable frameless window for edge-to-edge experience
setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
setAttribute(Qt::WA_TranslucentBackground, false);
// Style for grey status bar text
QString statusBarLabelStyle = "QLabel { color: #808080; }";
@@ -359,3 +363,40 @@ void MainWindow::updatePingIndicator()
m_pingLabel->setText("connected " + QString::number(currentPing) + "ms");
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
// Check if the click is within the top bar widget area
QWidget *topBar = ui->widget; // The top bar widget from the UI
if (topBar && topBar->geometry().contains(event->pos()))
{
m_isDragging = true;
m_dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
event->accept();
return;
}
}
QMainWindow::mousePressEvent(event);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if (m_isDragging && (event->buttons() & Qt::LeftButton))
{
move(event->globalPosition().toPoint() - m_dragPosition);
event->accept();
return;
}
QMainWindow::mouseMoveEvent(event);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
m_isDragging = false;
}
QMainWindow::mouseReleaseEvent(event);
}
+9
View File
@@ -42,6 +42,11 @@ private slots:
void onTreeSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
void onParticleSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
protected:
void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
private:
void setupTrayIcon();
void setupModels();
@@ -70,6 +75,10 @@ private:
QLabel *m_storageLabel;
QLabel *m_pingLabel;
QTimer *m_pingTimer;
// Window dragging
QPoint m_dragPosition;
bool m_isDragging;
};
#endif // LLINK_MAINWINDOW_H