From 350e7468d14e0b88e6b2eeb367d9488b394df8ba Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Mon, 3 Jun 2024 17:56:33 +0200 Subject: Reuse Window::ensureOnScreen Less code duplication. --- src/gui/widgets/window.cpp | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/src/gui/widgets/window.cpp b/src/gui/widgets/window.cpp index 25ca7684..6b36ebe2 100644 --- a/src/gui/widgets/window.cpp +++ b/src/gui/widgets/window.cpp @@ -437,13 +437,7 @@ void Window::mouseDragged(gcn::MouseEvent &event) // Keep guichan window inside screen when it may be moved if (isMovable() && mMoved) - { - int newX = std::max(0, getX()); - int newY = std::max(0, getY()); - newX = std::min(graphics->getWidth() - getWidth(), newX); - newY = std::min(graphics->getHeight() - getHeight(), newY); - setPosition(newX, newY); - } + ensureOnScreen(); if (mouseResize && !mMoved) { @@ -785,19 +779,15 @@ void Window::ensureOnScreen() if (getWidth() == 0 && getHeight() == 0) return; - gcn::Rectangle dimension = getDimension(); + gcn::Rectangle dim = getDimension(); // Check the left and bottom screen boundaries - if (dimension.x + dimension.width > graphics->getWidth()) - dimension.x = graphics->getWidth() - dimension.width; - if (dimension.y + dimension.height > graphics->getHeight()) - dimension.y = graphics->getHeight() - dimension.height; + dim.x = std::min(graphics->getWidth() - dim.width, dim.x); + dim.y = std::min(graphics->getHeight() - dim.height, dim.y); // But never allow the windows to disappear in to the right and top - if (dimension.x < 0) - dimension.x = 0; - if (dimension.y < 0) - dimension.y = 0; + dim.x = std::max(0, dim.x); + dim.y = std::max(0, dim.y); - setDimension(dimension); + setPosition(dim.x, dim.y); } -- cgit v1.2.3-70-g09d2