From cb3547e545e4e366ab77051b0d080fa6ddb06330 Mon Sep 17 00:00:00 2001 From: El Thoro Date: Sun, 22 Jun 2025 19:22:01 +0200 Subject: [PATCH] Center dialogs on current screen (#4024) * Center dialogs on current sreen * Center QuitPrompt --- src/core/flameshot.cpp | 17 +++++++++++++++-- src/widgets/capture/capturewidget.cpp | 10 +++++++++- src/widgets/capturelauncher.cpp | 8 ++++++++ src/widgets/infowindow.cpp | 5 +++-- 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/core/flameshot.cpp b/src/core/flameshot.cpp index 49b12484..99e73d44 100644 --- a/src/core/flameshot.cpp +++ b/src/core/flameshot.cpp @@ -37,8 +37,8 @@ #endif Flameshot::Flameshot() - : m_captureWindow(nullptr) - , m_haveExternalWidget(false) + : m_haveExternalWidget(false) + , m_captureWindow(nullptr) #if defined(Q_OS_MACOS) , m_HotkeyScreenshotCapture(nullptr) , m_HotkeyScreenshotHistory(nullptr) @@ -221,6 +221,12 @@ void Flameshot::config() if (m_configWindow == nullptr) { m_configWindow = new ConfigWindow(); m_configWindow->show(); + // Call show() first, otherwise the correct geometry cannot be fetched + // for centering the window on the screen + QRect position = m_configWindow->frameGeometry(); + QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen(); + position.moveCenter(currentScreen->availableGeometry().center()); + m_configWindow->move(position.topLeft()); #if defined(Q_OS_MACOS) m_configWindow->activateWindow(); m_configWindow->raise(); @@ -249,7 +255,14 @@ void Flameshot::history() historyWidget = nullptr; }); } + historyWidget->show(); + // Call show() first, otherwise the correct geometry cannot be fetched + // for centering the window on the screen + QRect position = historyWidget->frameGeometry(); + QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen(); + position.moveCenter(currentScreen->availableGeometry().center()); + historyWidget->move(position.topLeft()); #if defined(Q_OS_MACOS) historyWidget->activateWindow(); diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index 0662c861..d25f7fde 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -475,7 +475,6 @@ void CaptureWidget::initQuitPrompt() { m_quitPrompt = new QMessageBox; makeChild(m_quitPrompt); - m_quitPrompt->hide(); QString baseSheet = "QDialog { background-color: %1; }" "QLabel, QCheckBox { color: %2 }" @@ -493,6 +492,15 @@ void CaptureWidget::initQuitPrompt() auto* check = new QCheckBox(tr("Do not show this again")); m_quitPrompt->setCheckBox(check); + // Call show() first, otherwise the correct geometry cannot be fetched + // for centering the window on the screen + m_quitPrompt->show(); + QRect position = m_quitPrompt->frameGeometry(); + QScreen* currentScreen = QGuiAppCurrentScreen().currentScreen(); + position.moveCenter(currentScreen->availableGeometry().center()); + m_quitPrompt->move(position.topLeft()); + m_quitPrompt->hide(); + QObject::connect(check, &QCheckBox::clicked, [](bool checked) { ConfigHandler().setShowQuitPrompt(!checked); }); diff --git a/src/widgets/capturelauncher.cpp b/src/widgets/capturelauncher.cpp index 39a6d7c0..69f74296 100644 --- a/src/widgets/capturelauncher.cpp +++ b/src/widgets/capturelauncher.cpp @@ -5,6 +5,7 @@ #include "./ui_capturelauncher.h" #include "src/config/cacheutils.h" #include "src/core/flameshot.h" +#include "src/core/qguiappcurrentscreen.h" #include "src/utils/globalvalues.h" #include "src/utils/screengrabber.h" #include "src/utils/screenshotsaver.h" @@ -84,7 +85,14 @@ CaptureLauncher::CaptureLauncher(QDialog* parent) ui->screenshotY->setText(QString::number(lastRegion.y())); ui->screenshotWidth->setText(QString::number(lastRegion.width())); ui->screenshotHeight->setText(QString::number(lastRegion.height())); + show(); + // Call show() first, otherwise the correct geometry cannot be fetched + // for centering the window on the screen + QRect position = frameGeometry(); + QScreen* screen = QGuiAppCurrentScreen().currentScreen(); + position.moveCenter(screen->availableGeometry().center()); + move(position.topLeft()); } // HACK: diff --git a/src/widgets/infowindow.cpp b/src/widgets/infowindow.cpp index 02630608..88ead9b1 100644 --- a/src/widgets/infowindow.cpp +++ b/src/widgets/infowindow.cpp @@ -23,12 +23,13 @@ InfoWindow::InfoWindow(QWidget* parent) connect( ui->CopyInfoButton, &QPushButton::clicked, this, &InfoWindow::copyInfo); + show(); + // Call show() first, otherwise the correct geometry cannot be fetched for + // centering the window on the screen QRect position = frameGeometry(); QScreen* screen = QGuiAppCurrentScreen().currentScreen(); position.moveCenter(screen->availableGeometry().center()); move(position.topLeft()); - - show(); } InfoWindow::~InfoWindow()