Skip to content

Commit

Permalink
Qt: Implement disable window resize
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and refractionpcsx2 committed Jun 28, 2022
1 parent 9f99e88 commit 6316373
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 14 deletions.
35 changes: 23 additions & 12 deletions pcsx2-qt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// 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);

if (isVisible() != new_state)
setVisible(new_state);
// 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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -1426,7 +1437,7 @@ void MainWindow::onVMStopped()
m_last_fps_status = QString();
updateEmulationActions(false, false);
updateWindowTitle();
updateWindowVisibility();
updateWindowState();
updateStatusBarWidgetVisibility();

if (m_display_widget)
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand All @@ -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<qint32>(width, 1), std::max<qint32>(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<qint32>(width, 1), std::max<qint32>(height + extra_height, 1)));
QtUtils::ResizePotentiallyFixedSizeWindow(this, width, height + extra_height);
}

void MainWindow::destroyDisplay()
Expand Down
2 changes: 1 addition & 1 deletion pcsx2-qt/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private Q_SLOTS:
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();

Expand Down
36 changes: 36 additions & 0 deletions pcsx2-qt/QtUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QScrollBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QStyle>
#include <QtWidgets/QTableView>
#include <QtWidgets/QTreeView>
Expand Down Expand Up @@ -167,4 +168,39 @@ namespace QtUtils
}
}

void SetWindowResizeable(QWidget* widget, bool resizeable)
{
if (QMainWindow* window = qobject_cast<QMainWindow*>(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
6 changes: 6 additions & 0 deletions pcsx2-qt/QtUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion pcsx2-qt/Settings/InterfaceSettingsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 6316373

Please sign in to comment.