Skip to content

Commit

Permalink
Qt: gs_frame position updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Mar 30, 2021
1 parent 66df389 commit bef5df3
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 33 deletions.
4 changes: 2 additions & 2 deletions rpcs3/rpcs3qt/gl_gs_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <QOpenGLContext>
#include <QOffscreenSurface>

gl_gs_frame::gl_gs_frame(const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings)
: gs_frame(geometry, appIcon, gui_settings)
gl_gs_frame::gl_gs_frame(QScreen* screen, const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings)
: gs_frame(screen, geometry, appIcon, gui_settings)
{
setSurfaceType(QSurface::OpenGLSurface);

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/gl_gs_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class gl_gs_frame : public gs_frame
GLContext *m_primary_context = nullptr;

public:
gl_gs_frame(const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings);
explicit gl_gs_frame(QScreen* screen, const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings);

draw_context_t make_context() override;
void set_current(draw_context_t context) override;
Expand Down
18 changes: 11 additions & 7 deletions rpcs3/rpcs3qt/gs_frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <QKeyEvent>
#include <QMessageBox>
#include <QPainter>
#include <QScreen>

#include <string>
#include <thread>
Expand Down Expand Up @@ -51,8 +52,10 @@ extern atomic_t<bool> g_user_asked_for_frame_capture;

constexpr auto qstr = QString::fromStdString;

gs_frame::gs_frame(const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings)
: QWindow(), m_gui_settings(gui_settings)
gs_frame::gs_frame(QScreen* screen, const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings)
: QWindow()
, m_initial_geometry(geometry)
, m_gui_settings(gui_settings)
{
m_disable_mouse = gui_settings->GetValue(gui::gs_disableMouse).toBool();
m_disable_kb_hotkeys = gui_settings->GetValue(gui::gs_disableKbHotkeys).toBool();
Expand All @@ -75,6 +78,7 @@ gs_frame::gs_frame(const QRect& geometry, const QIcon& appIcon, const std::share

setMinimumWidth(160);
setMinimumHeight(90);
setScreen(screen);
setGeometry(geometry);
setTitle(m_window_title);
setVisibility(Hidden);
Expand Down Expand Up @@ -133,11 +137,11 @@ void gs_frame::paintEvent(QPaintEvent *event)
void gs_frame::showEvent(QShowEvent *event)
{
// we have to calculate new window positions, since the frame is only known once the window was created
// the left and right margins are too big on my setup for some reason yet unknown, so we'll have to ignore them
const int x = geometry().left(); //std::max(geometry().left(), frameMargins().left());
const int y = std::max(geometry().top(), frameMargins().top());

setPosition(x, y);
const QRect available_geometry = screen()->availableGeometry();
QPoint pos = m_initial_geometry.topLeft();
pos.setX(std::clamp(pos.x(), available_geometry.left(), available_geometry.width() - frameGeometry().width()));
pos.setY(std::clamp(pos.y(), available_geometry.top(), available_geometry.height() - frameGeometry().height()));
setFramePosition(pos);

QWindow::showEvent(event);
}
Expand Down
4 changes: 3 additions & 1 deletion rpcs3/rpcs3qt/gs_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class gs_frame : public QWindow, public GSFrameBase
void UpdateProgress(int progress, bool disable = false);
#endif

QRect m_initial_geometry;

std::shared_ptr<gui_settings> m_gui_settings;
QTimer m_mousehide_timer;

Expand All @@ -48,7 +50,7 @@ class gs_frame : public QWindow, public GSFrameBase
bool m_flip_showed_frame = false;

public:
gs_frame(const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings);
explicit gs_frame(QScreen* screen, const QRect& geometry, const QIcon& appIcon, const std::shared_ptr<gui_settings>& gui_settings);
~gs_frame();

draw_context_t make_context() override;
Expand Down
9 changes: 5 additions & 4 deletions rpcs3/rpcs3qt/gui_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,9 @@ std::unique_ptr<gs_frame> gui_application::get_gs_frame()
h = m_gui_settings->GetValue(gui::gs_height).toInt();
}

const auto screen_geometry = m_main_window ? m_main_window->geometry() : primaryScreen()->geometry();
const auto frame_geometry = gui::utils::create_centered_window_geometry(screen_geometry, w, h);
const auto screen = m_main_window ? m_main_window->screen() : primaryScreen();
const auto source_geometry = m_main_window ? m_main_window->geometry() : primaryScreen()->geometry();
const auto frame_geometry = gui::utils::create_centered_window_geometry(screen, source_geometry, w, h);
const auto app_icon = m_main_window ? m_main_window->GetAppIcon() : gui::utils::get_app_icon_from_path(Emu.GetBoot(), Emu.GetTitleID());

gs_frame* frame;
Expand All @@ -266,13 +267,13 @@ std::unique_ptr<gs_frame> gui_application::get_gs_frame()
{
case video_renderer::opengl:
{
frame = new gl_gs_frame(frame_geometry, app_icon, m_gui_settings);
frame = new gl_gs_frame(screen, frame_geometry, app_icon, m_gui_settings);
break;
}
case video_renderer::null:
case video_renderer::vulkan:
{
frame = new gs_frame(frame_geometry, app_icon, m_gui_settings);
frame = new gs_frame(screen, frame_geometry, app_icon, m_gui_settings);
break;
}
default: fmt::throw_exception("Invalid video renderer: %s", type);
Expand Down
30 changes: 13 additions & 17 deletions rpcs3/rpcs3qt/qt_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <QProcess>
#include <QScreen>
#include <QUrl>
#include <QDebug>

#include "Emu/System.h"
#include "Utilities/File.h"
Expand All @@ -18,29 +19,24 @@ namespace gui
{
namespace utils
{
QRect create_centered_window_geometry(const QRect& origin, s32 width, s32 height)
QRect create_centered_window_geometry(const QScreen* screen, const QRect& origin, s32 width, s32 height)
{
ensure(screen);

// Get minimum virtual screen x & y for clamping the
// window x & y later while taking the width and height
// into account, so they don't go offscreen
s32 min_screen_x = INT32_MAX;
s32 max_screen_x = INT32_MIN;
s32 min_screen_y = INT32_MAX;
s32 max_screen_y = INT32_MIN;
for (auto screen : QApplication::screens())
{
auto screen_geometry = screen->availableGeometry();
min_screen_x = std::min(min_screen_x, screen_geometry.x());
max_screen_x = std::max(max_screen_x, screen_geometry.x() + screen_geometry.width() - width);
min_screen_y = std::min(min_screen_y, screen_geometry.y());
max_screen_y = std::max(max_screen_y, screen_geometry.y() + screen_geometry.height() - height);
}
const QRect screen_geometry = screen->availableGeometry();
const s32 min_screen_x = screen_geometry.x();
const s32 max_screen_x = screen_geometry.x() + screen_geometry.width() - width;
const s32 min_screen_y = screen_geometry.y();
const s32 max_screen_y = screen_geometry.y() + screen_geometry.height() - height;

s32 frame_x_raw = origin.left() + ((origin.width() - width) / 2);
s32 frame_y_raw = origin.top() + ((origin.height() - height) / 2);
const s32 frame_x_raw = origin.left() + ((origin.width() - width) / 2);
const s32 frame_y_raw = origin.top() + ((origin.height() - height) / 2);

s32 frame_x = std::clamp(frame_x_raw, min_screen_x, max_screen_x);
s32 frame_y = std::clamp(frame_y_raw, min_screen_y, max_screen_y);
const s32 frame_x = std::clamp(frame_x_raw, min_screen_x, max_screen_x);
const s32 frame_y = std::clamp(frame_y_raw, min_screen_y, max_screen_y);

return QRect(frame_x, frame_y, width, height);
}
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/qt_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace gui

// Creates a frame geometry rectangle with given width height that's centered inside the origin,
// while still considering screen boundaries.
QRect create_centered_window_geometry(const QRect& origin, s32 width, s32 height);
QRect create_centered_window_geometry(const QScreen* screen, const QRect& origin, s32 width, s32 height);

// Returns a custom colored QPixmap based on another QPixmap.
// use colorize_all to repaint every opaque pixel with the chosen color
Expand Down

0 comments on commit bef5df3

Please sign in to comment.