From 63163737c23691bfc305343430cf8104e9c8fe8d Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Tue, 28 Jun 2022 22:16:45 +1000 Subject: [PATCH] Qt: Implement disable window resize --- pcsx2-qt/MainWindow.cpp | 35 +++++++++++------- pcsx2-qt/MainWindow.h | 2 +- pcsx2-qt/QtUtils.cpp | 36 +++++++++++++++++++ pcsx2-qt/QtUtils.h | 6 ++++ pcsx2-qt/Settings/InterfaceSettingsWidget.cpp | 2 +- 5 files changed, 67 insertions(+), 14 deletions(-) diff --git a/pcsx2-qt/MainWindow.cpp b/pcsx2-qt/MainWindow.cpp index 95e295ed47..d9a83737fd 100644 --- a/pcsx2-qt/MainWindow.cpp +++ b/pcsx2-qt/MainWindow.cpp @@ -728,14 +728,25 @@ void MainWindow::updateWindowTitle() } } -void MainWindow::updateWindowVisibility() +void MainWindow::updateWindowState(bool force_visible) { - // Need to test both valid and display widget because of startup (vm invalid while window is created). const bool hide_window = !g_emu_thread->isRenderingToMain() && Host::GetBaseBoolSettingValue("UI", "HideMainWindowWhenRunning", false); - const bool new_state = !hide_window || (!m_vm_valid && !m_display_widget); + const bool disable_resize = Host::GetBaseBoolSettingValue("UI", "DisableWindowResize", false); + const bool has_window = m_vm_valid || m_display_widget; - if (isVisible() != new_state) - setVisible(new_state); + // Need to test both valid and display widget because of startup (vm invalid while window is created). + const bool visible = force_visible || !hide_window || !has_window; + if (isVisible() != visible) + setVisible(visible); + + // No point changing realizability if we're not visible. + const bool resizeable = force_visible || !disable_resize || !has_window; + if (visible) + QtUtils::SetWindowResizeable(this, resizeable); + + // Update the display widget too if rendering separately. + if (m_display_widget && !isRenderingToMain()) + QtUtils::SetWindowResizeable(getDisplayContainer(), resizeable); } void MainWindow::setProgressBar(int current, int total) @@ -896,7 +907,7 @@ bool MainWindow::requestShutdown(bool allow_confirm /* = true */, bool allow_sav // would briefly show and then hide the main window. So instead, we do it on shutdown, here. Except if we're in // batch mode, when we're going to exit anyway. if (!isRenderingToMain() && isHidden() && !QtHost::InBatchMode()) - show(); + updateWindowState(true); // Now we can actually shut down the VM. g_emu_thread->shutdownVM(save_state); @@ -931,7 +942,7 @@ void MainWindow::checkForSettingChanges() if (m_display_widget) m_display_widget->updateRelativeMode(m_vm_valid && !m_vm_paused); - updateWindowVisibility(); + updateWindowState(); } void Host::InvalidateSaveStateCache() @@ -1426,7 +1437,7 @@ void MainWindow::onVMStopped() m_last_fps_status = QString(); updateEmulationActions(false, false); updateWindowTitle(); - updateWindowVisibility(); + updateWindowState(); updateStatusBarWidgetVisibility(); if (m_display_widget) @@ -1608,7 +1619,7 @@ DisplayWidget* MainWindow::createDisplay(bool fullscreen, bool render_to_main) setDisplayFullscreen(fullscreen_mode); updateWindowTitle(); - updateWindowVisibility(); + updateWindowState(); m_display_widget->setFocus(); m_display_widget->setShouldHideCursor(shouldHideMouseCursor()); @@ -1741,7 +1752,7 @@ DisplayWidget* MainWindow::updateDisplay(bool fullscreen, bool render_to_main, b setDisplayFullscreen(fullscreen_mode); updateWindowTitle(); - updateWindowVisibility(); + updateWindowState(); m_display_widget->setFocus(); m_display_widget->setShouldHideCursor(shouldHideMouseCursor()); @@ -1766,13 +1777,13 @@ void MainWindow::displayResizeRequested(qint32 width, qint32 height) if (m_display_container || !m_display_widget->parent()) { // no parent - rendering to separate window. easy. - getDisplayContainer()->resize(QSize(std::max(width, 1), std::max(height, 1))); + QtUtils::ResizePotentiallyFixedSizeWindow(getDisplayContainer(), width, height); return; } // we are rendering to the main window. we have to add in the extra height from the toolbar/status bar. const s32 extra_height = this->height() - m_display_widget->height(); - resize(QSize(std::max(width, 1), std::max(height + extra_height, 1))); + QtUtils::ResizePotentiallyFixedSizeWindow(this, width, height + extra_height); } void MainWindow::destroyDisplay() diff --git a/pcsx2-qt/MainWindow.h b/pcsx2-qt/MainWindow.h index 90c6bda22a..5e5d8c4978 100644 --- a/pcsx2-qt/MainWindow.h +++ b/pcsx2-qt/MainWindow.h @@ -179,7 +179,7 @@ private: void updateEmulationActions(bool starting, bool running); void updateStatusBarWidgetVisibility(); void updateWindowTitle(); - void updateWindowVisibility(); + void updateWindowState(bool force_visible = false); void setProgressBar(int current, int total); void clearProgressBar(); diff --git a/pcsx2-qt/QtUtils.cpp b/pcsx2-qt/QtUtils.cpp index a3cceb9584..cea0d06bdd 100644 --- a/pcsx2-qt/QtUtils.cpp +++ b/pcsx2-qt/QtUtils.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -167,4 +168,39 @@ namespace QtUtils } } + void SetWindowResizeable(QWidget* widget, bool resizeable) + { + if (QMainWindow* window = qobject_cast(widget); window) + { + // update status bar grip if present + if (QStatusBar* sb = window->statusBar(); sb) + sb->setSizeGripEnabled(resizeable); + } + + if ((widget->sizePolicy().horizontalPolicy() == QSizePolicy::Preferred) != resizeable) + { + if (resizeable) + { + // Min/max numbers come from uic. + widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); + widget->setMinimumSize(1, 1); + widget->setMaximumSize(16777215, 16777215); + } + else + { + widget->setFixedSize(widget->size()); + widget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed); + } + } + } + + void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height) + { + width = std::max(width, 1); + height = std::max(height, 1); + if (widget->sizePolicy().horizontalPolicy() == QSizePolicy::Fixed) + widget->setFixedSize(width, height); + + widget->resize(width, height); + } } // namespace QtUtils diff --git a/pcsx2-qt/QtUtils.h b/pcsx2-qt/QtUtils.h index 8f1ae38c42..3a35daf0f3 100644 --- a/pcsx2-qt/QtUtils.h +++ b/pcsx2-qt/QtUtils.h @@ -68,4 +68,10 @@ namespace QtUtils /// Sets a widget to italics if the setting value is inherited. void SetWidgetFontForInheritedSetting(QWidget* widget, bool inherited); + + /// Changes whether a window is resizable. + void SetWindowResizeable(QWidget* widget, bool resizeable); + + /// Adjusts the fixed size for a window if it's not resizeable. + void ResizePotentiallyFixedSizeWindow(QWidget* widget, int width, int height); } // namespace QtUtils \ No newline at end of file diff --git a/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp b/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp index 8e860b9e37..cdb15182a6 100644 --- a/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp +++ b/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp @@ -62,6 +62,7 @@ InterfaceSettingsWidget::InterfaceSettingsWidget(SettingsDialog* dialog, QWidget SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.hideMouseCursor, "UI", "HideMouseCursor", false); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.renderToSeparateWindow, "UI", "RenderToSeparateWindow", false); SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.hideMainWindow, "UI", "HideMainWindowWhenRunning", false); + SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.disableWindowResizing, "UI", "DisableWindowResize", false); connect(m_ui.renderToSeparateWindow, &QCheckBox::stateChanged, this, &InterfaceSettingsWidget::onRenderToSeparateWindowChanged); SettingWidgetBinder::BindWidgetToEnumSetting(sif, m_ui.theme, "UI", "Theme", THEME_NAMES, THEME_VALUES, @@ -118,7 +119,6 @@ InterfaceSettingsWidget::InterfaceSettingsWidget(SettingsDialog* dialog, QWidget // Not yet used, disable the options m_ui.pauseOnStart->setDisabled(true); m_ui.pauseOnFocusLoss->setDisabled(true); - m_ui.disableWindowResizing->setDisabled(true); m_ui.language->setDisabled(true); onRenderToSeparateWindowChanged();