Skip to content

Commit 70352b6

Browse files
committed
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.
1 parent b4d71cb commit 70352b6

11 files changed

+43
-7
lines changed

src/duckstation-qt/autoupdaterwindow.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static const char* UPDATE_ASSET_FILENAME = SCM_RELEASE_ASSET;
7777

7878
LOG_CHANNEL(Host);
7979

80-
AutoUpdaterWindow::AutoUpdaterWindow(QWidget* parent /* = nullptr */) : QWidget(parent)
80+
AutoUpdaterWindow::AutoUpdaterWindow() : QWidget()
8181
{
8282
m_ui.setupUi(this);
8383
setWindowIcon(QtHost::GetAppIcon());
@@ -91,6 +91,8 @@ AutoUpdaterWindow::AutoUpdaterWindow(QWidget* parent /* = nullptr */) : QWidget(
9191
m_http = HTTPDownloader::Create(Host::GetHTTPUserAgent(), &error);
9292
if (!m_http)
9393
ERROR_LOG("Failed to create HTTP downloader, auto updater will not be available:\n{}", error.GetDescription());
94+
95+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
9496
}
9597

9698
AutoUpdaterWindow::~AutoUpdaterWindow() = default;

src/duckstation-qt/autoupdaterwindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class AutoUpdaterWindow final : public QWidget
2626
Q_OBJECT
2727

2828
public:
29-
explicit AutoUpdaterWindow(QWidget* parent = nullptr);
29+
explicit AutoUpdaterWindow();
3030
~AutoUpdaterWindow();
3131

3232
void queueUpdateCheck(bool display_errors);

src/duckstation-qt/controllersettingswindow.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ ControllerSettingsWindow::ControllerSettingsWindow(INISettingsInterface* game_si
104104
createWidgets();
105105

106106
if (isEditingGlobalSettings())
107-
QtUtils::RestoreWindowGeometry("ControllerSettingsWindow", this);
107+
{
108+
if (!QtUtils::RestoreWindowGeometry("ControllerSettingsWindow", this))
109+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
110+
}
108111
}
109112

110113
ControllerSettingsWindow::~ControllerSettingsWindow() = default;

src/duckstation-qt/debuggerwindow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,8 @@ void DebuggerWindow::setupAdditionalUi()
476476
setCentralWidget(nullptr);
477477
delete m_ui.centralwidget;
478478

479-
QtUtils::RestoreWindowGeometry("DebuggerWindow", this);
479+
if (!QtUtils::RestoreWindowGeometry("DebuggerWindow", this))
480+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
480481
}
481482

482483
void DebuggerWindow::connectSignals()

src/duckstation-qt/isobrowserwindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
33

44
#include "isobrowserwindow.h"
5+
#include "mainwindow.h"
56
#include "qtprogresscallback.h"
67
#include "qtutils.h"
78

@@ -39,6 +40,8 @@ ISOBrowserWindow::ISOBrowserWindow(QWidget* parent) : QWidget(parent)
3940
connect(m_ui.fileView, &QTreeWidget::itemSelectionChanged, this, &ISOBrowserWindow::onFileItemSelectionChanged);
4041
connect(m_ui.fileView, &QTreeWidget::customContextMenuRequested, this, &ISOBrowserWindow::onFileContextMenuRequested);
4142
connect(m_ui.close, &QAbstractButton::clicked, this, &ISOBrowserWindow::close);
43+
44+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
4245
}
4346

4447
ISOBrowserWindow::~ISOBrowserWindow() = default;

src/duckstation-qt/memorycardeditorwindow.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
33

44
#include "memorycardeditorwindow.h"
5+
#include "mainwindow.h"
56
#include "qtutils.h"
67

78
#include "core/host.h"
@@ -166,6 +167,8 @@ MemoryCardEditorWindow::MemoryCardEditorWindow() : QWidget()
166167
m_animation_timer = new QTimer(this);
167168
m_animation_timer->setInterval(MEMORY_CARD_ICON_FRAME_DURATION_MS);
168169
connect(m_animation_timer, &QTimer::timeout, this, &MemoryCardEditorWindow::incrementAnimationFrame);
170+
171+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
169172
}
170173

171174
MemoryCardEditorWindow::~MemoryCardEditorWindow() = default;

src/duckstation-qt/memoryeditorwindow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,8 @@ void MemoryEditorWindow::setupAdditionalUi()
399399
#endif
400400
m_ui.memoryView->setFont(fixedFont);
401401

402-
QtUtils::RestoreWindowGeometry("MemoryEditorWindow", this);
402+
if (!QtUtils::RestoreWindowGeometry("MemoryEditorWindow", this))
403+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
403404

404405
// Set minimum width for data inspector.
405406
m_ui.dataInspectorAddress->setFont(fixedFont);

src/duckstation-qt/memoryscannerwindow.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ void MemoryScannerWindow::setupAdditionalUi()
104104
{
105105
QtUtils::SetColumnWidthsForTableView(m_ui.scanTable, {-1, 100, 100, 100});
106106
QtUtils::SetColumnWidthsForTableView(m_ui.watchTable, {-1, 100, 100, 150, 40});
107-
QtUtils::RestoreWindowGeometry("MemoryScannerWindow", this);
107+
if (!QtUtils::RestoreWindowGeometry("MemoryScannerWindow", this))
108+
QtUtils::CenterWindowRelativeToParent(this, g_main_window);
108109
}
109110

110111
void MemoryScannerWindow::connectUi()

src/duckstation-qt/qtutils.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,22 @@ bool QtUtils::RestoreWindowGeometry(std::string_view window_name, QWidget* widge
629629
return true;
630630
}
631631

632+
void QtUtils::CenterWindowRelativeToParent(QWidget* window, QWidget* parent_window)
633+
{
634+
// la la la, this won't work on fucking wankland, I don't care, it'll appear in the top-left
635+
// corner of the screen or whatever, shit experience is shit
636+
637+
const QRect parent_geometry = (parent_window && parent_window->isVisible()) ?
638+
parent_window->geometry() :
639+
QGuiApplication::primaryScreen()->availableGeometry();
640+
const QPoint parent_center_pos = parent_geometry.center();
641+
642+
QRect window_geometry = window->geometry();
643+
window_geometry.moveCenter(parent_center_pos);
644+
645+
window->setGeometry(window_geometry);
646+
}
647+
632648
bool QtUtils::TryMigrateWindowGeometry(SettingsInterface* si, std::string_view window_name, QWidget* widget)
633649
{
634650
// can we migrate old configuration?

src/duckstation-qt/qtutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ void SaveWindowGeometry(std::string_view window_name, QWidget* widget, bool auto
156156
/// Restores a window's geometry from configuration. Returns false if it was not found in the configuration.
157157
bool RestoreWindowGeometry(std::string_view window_name, QWidget* widget);
158158

159+
/// Positions a window in the center of its parent or the screen.
160+
void CenterWindowRelativeToParent(QWidget* window, QWidget* parent_window);
161+
159162
/// CPU-friendly way of blocking the UI thread while some predicate holds true.
160163
template<typename Predicate>
161164
inline void ProcessEventsWithSleep(QEventLoop::ProcessEventsFlags flags, const Predicate& pred, int sleep_time_ms = 10)

0 commit comments

Comments
 (0)