Qt: Center windows when they don't have saved positions

Stupid Linux decides to spawn it in the top-left corner of the screen
otherwise.
This commit is contained in:
Stenzek
2025-11-02 22:02:02 +10:00
parent b4d71cb66c
commit 70352b69ce
11 changed files with 43 additions and 7 deletions

View File

@@ -77,7 +77,7 @@ static const char* UPDATE_ASSET_FILENAME = SCM_RELEASE_ASSET;
LOG_CHANNEL(Host);
AutoUpdaterWindow::AutoUpdaterWindow(QWidget* parent /* = nullptr */) : QWidget(parent)
AutoUpdaterWindow::AutoUpdaterWindow() : QWidget()
{
m_ui.setupUi(this);
setWindowIcon(QtHost::GetAppIcon());
@@ -91,6 +91,8 @@ AutoUpdaterWindow::AutoUpdaterWindow(QWidget* parent /* = nullptr */) : QWidget(
m_http = HTTPDownloader::Create(Host::GetHTTPUserAgent(), &error);
if (!m_http)
ERROR_LOG("Failed to create HTTP downloader, auto updater will not be available:\n{}", error.GetDescription());
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
AutoUpdaterWindow::~AutoUpdaterWindow() = default;

View File

@@ -26,7 +26,7 @@ class AutoUpdaterWindow final : public QWidget
Q_OBJECT
public:
explicit AutoUpdaterWindow(QWidget* parent = nullptr);
explicit AutoUpdaterWindow();
~AutoUpdaterWindow();
void queueUpdateCheck(bool display_errors);

View File

@@ -104,7 +104,10 @@ ControllerSettingsWindow::ControllerSettingsWindow(INISettingsInterface* game_si
createWidgets();
if (isEditingGlobalSettings())
QtUtils::RestoreWindowGeometry("ControllerSettingsWindow", this);
{
if (!QtUtils::RestoreWindowGeometry("ControllerSettingsWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
}
ControllerSettingsWindow::~ControllerSettingsWindow() = default;

View File

@@ -476,7 +476,8 @@ void DebuggerWindow::setupAdditionalUi()
setCentralWidget(nullptr);
delete m_ui.centralwidget;
QtUtils::RestoreWindowGeometry("DebuggerWindow", this);
if (!QtUtils::RestoreWindowGeometry("DebuggerWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
void DebuggerWindow::connectSignals()

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
#include "isobrowserwindow.h"
#include "mainwindow.h"
#include "qtprogresscallback.h"
#include "qtutils.h"
@@ -39,6 +40,8 @@ ISOBrowserWindow::ISOBrowserWindow(QWidget* parent) : QWidget(parent)
connect(m_ui.fileView, &QTreeWidget::itemSelectionChanged, this, &ISOBrowserWindow::onFileItemSelectionChanged);
connect(m_ui.fileView, &QTreeWidget::customContextMenuRequested, this, &ISOBrowserWindow::onFileContextMenuRequested);
connect(m_ui.close, &QAbstractButton::clicked, this, &ISOBrowserWindow::close);
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
ISOBrowserWindow::~ISOBrowserWindow() = default;

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
#include "memorycardeditorwindow.h"
#include "mainwindow.h"
#include "qtutils.h"
#include "core/host.h"
@@ -166,6 +167,8 @@ MemoryCardEditorWindow::MemoryCardEditorWindow() : QWidget()
m_animation_timer = new QTimer(this);
m_animation_timer->setInterval(MEMORY_CARD_ICON_FRAME_DURATION_MS);
connect(m_animation_timer, &QTimer::timeout, this, &MemoryCardEditorWindow::incrementAnimationFrame);
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
MemoryCardEditorWindow::~MemoryCardEditorWindow() = default;

View File

@@ -399,7 +399,8 @@ void MemoryEditorWindow::setupAdditionalUi()
#endif
m_ui.memoryView->setFont(fixedFont);
QtUtils::RestoreWindowGeometry("MemoryEditorWindow", this);
if (!QtUtils::RestoreWindowGeometry("MemoryEditorWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
// Set minimum width for data inspector.
m_ui.dataInspectorAddress->setFont(fixedFont);

View File

@@ -104,7 +104,8 @@ void MemoryScannerWindow::setupAdditionalUi()
{
QtUtils::SetColumnWidthsForTableView(m_ui.scanTable, {-1, 100, 100, 100});
QtUtils::SetColumnWidthsForTableView(m_ui.watchTable, {-1, 100, 100, 150, 40});
QtUtils::RestoreWindowGeometry("MemoryScannerWindow", this);
if (!QtUtils::RestoreWindowGeometry("MemoryScannerWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
void MemoryScannerWindow::connectUi()

View File

@@ -629,6 +629,22 @@ bool QtUtils::RestoreWindowGeometry(std::string_view window_name, QWidget* widge
return true;
}
void QtUtils::CenterWindowRelativeToParent(QWidget* window, QWidget* parent_window)
{
// la la la, this won't work on fucking wankland, I don't care, it'll appear in the top-left
// corner of the screen or whatever, shit experience is shit
const QRect parent_geometry = (parent_window && parent_window->isVisible()) ?
parent_window->geometry() :
QGuiApplication::primaryScreen()->availableGeometry();
const QPoint parent_center_pos = parent_geometry.center();
QRect window_geometry = window->geometry();
window_geometry.moveCenter(parent_center_pos);
window->setGeometry(window_geometry);
}
bool QtUtils::TryMigrateWindowGeometry(SettingsInterface* si, std::string_view window_name, QWidget* widget)
{
// can we migrate old configuration?

View File

@@ -156,6 +156,9 @@ void SaveWindowGeometry(std::string_view window_name, QWidget* widget, bool auto
/// Restores a window's geometry from configuration. Returns false if it was not found in the configuration.
bool RestoreWindowGeometry(std::string_view window_name, QWidget* widget);
/// Positions a window in the center of its parent or the screen.
void CenterWindowRelativeToParent(QWidget* window, QWidget* parent_window);
/// CPU-friendly way of blocking the UI thread while some predicate holds true.
template<typename Predicate>
inline void ProcessEventsWithSleep(QEventLoop::ProcessEventsFlags flags, const Predicate& pred, int sleep_time_ms = 10)

View File

@@ -48,7 +48,8 @@ SettingsWindow::SettingsWindow() : QWidget()
addPages();
connectUi();
QtUtils::RestoreWindowGeometry("SettingsWindow", this);
if (!QtUtils::RestoreWindowGeometry("SettingsWindow", this))
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
SettingsWindow::SettingsWindow(const GameList::Entry* entry, std::unique_ptr<INISettingsInterface> sif)
@@ -67,6 +68,8 @@ SettingsWindow::SettingsWindow(const GameList::Entry* entry, std::unique_ptr<INI
connectUi();
s_open_game_properties_dialogs.push_back(this);
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
}
SettingsWindow::~SettingsWindow()