void (@NotNull Rectangle r) { Rectangle screen = getScreenRectangle(r.x, r.y); int xOverdraft = r.x + r.width - screen.x - screen.width; if (xOverdraft > 0) { int shift = Math.min(xOverdraft, r.x - screen.x); xOverdraft -= shift; r.x -= shift; if (xOverdraft > 0) { r.width -= xOverdraft; } } int yOverdraft = r.y + r.height - screen.y - screen.height; if (yOverdraft > 0) { int shift = Math.min(yOverdraft, r.y - screen.y); yOverdraft -= shift; r.y -= shift; if (yOverdraft > 0) { r.height -= yOverdraft; } } }