// Copyright (c) 2022 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.

#include "libcef/browser/screen_util.h"

#include <algorithm>

namespace {

constexpr int kMinWidth = 0;
constexpr int kMinHeight = 0;

// Makes sure that line segment lies entirely between min and max.
int clamp_segment_start(int start, int len, int min, int max) {
  start = std::clamp(start, min, max);
  const int end = start + len;
  const int excess = end - max;

  if (excess > 0)
    start = start - excess;

  return start;
}

}  // namespace

CefRect MakeVisibleOnScreenRect(const CefRect& rect, const CefRect& screen) {
  const int width = std::clamp(rect.width, kMinWidth, screen.width);
  const int height = std::clamp(rect.height, kMinHeight, screen.height);

  const int right_border = screen.x + screen.width;
  const int x = clamp_segment_start(rect.x, width, screen.x, right_border);

  const int bottom_border = screen.y + screen.height;
  const int y = clamp_segment_start(rect.y, height, screen.y, bottom_border);

  return CefRect(x, y, width, height);
}