fix: Window being created partially off-screen if monitor is too small

Fixes #2238
This commit is contained in:
WerWolv
2025-05-11 23:54:58 +02:00
parent fa3ed7e618
commit 616f34e210

View File

@@ -985,9 +985,22 @@ namespace hex {
glfwWindowHint(GLFW_MAXIMIZED, initialWindowProperties->maximized);
}
int monitorX, monitorY;
int monitorWidth = std::numeric_limits<int>::max(), monitorHeight = std::numeric_limits<int>::max();
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
if (monitor != nullptr) {
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
if (mode != nullptr) {
glfwGetMonitorPos(monitor, &monitorX, &monitorY);
monitorWidth = mode->width;
monitorHeight = mode->height;
}
}
// Create window
m_windowTitle = "ImHex";
m_window = glfwCreateWindow(1280_scaled, 720_scaled, m_windowTitle.c_str(), nullptr, nullptr);
m_window = glfwCreateWindow(std::min(1280_scaled, monitorWidth - 50_scaled), std::min(720_scaled, monitorHeight - 50_scaled), m_windowTitle.c_str(), nullptr, nullptr);
ImHexApi::System::impl::setMainWindowHandle(m_window);
@@ -1007,18 +1020,11 @@ namespace hex {
glfwSwapInterval(0);
// Center window
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
if (monitor != nullptr) {
const GLFWvidmode *mode = glfwGetVideoMode(monitor);
if (mode != nullptr) {
int monitorX, monitorY;
glfwGetMonitorPos(monitor, &monitorX, &monitorY);
if (monitorWidth != std::numeric_limits<int>::max() && monitorHeight != std::numeric_limits<int>::max()) {
int windowWidth, windowHeight;
glfwGetWindowSize(m_window, &windowWidth, &windowHeight);
int windowWidth, windowHeight;
glfwGetWindowSize(m_window, &windowWidth, &windowHeight);
glfwSetWindowPos(m_window, monitorX + (mode->width - windowWidth) / 2, monitorY + (mode->height - windowHeight) / 2);
}
glfwSetWindowPos(m_window, monitorX + (monitorWidth - windowWidth) / 2, monitorY + (monitorHeight - windowHeight) / 2);
}
// Set up initial window position
@@ -1040,6 +1046,9 @@ namespace hex {
int width = 0, height = 0;
glfwGetWindowSize(m_window, &width, &height);
width = std::min(width, monitorWidth - int(50_scaled));
height = std::min(height, monitorHeight - int(100_scaled));
if (initialWindowProperties.has_value()) {
width = initialWindowProperties->width;
height = initialWindowProperties->height;