// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/frame/multi_contents_background_view.h"

#include "chrome/browser/ui/views/frame/themed_background.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer_solid_color.h"
#include "ui/gfx/geometry/rounded_corners_f.h"

MultiContentsBackgroundView::MultiContentsBackgroundView(
    BrowserView* browser_view)
    : browser_view_(browser_view) {
  SetPaintToLayer(ui::LAYER_SOLID_COLOR);
}

MultiContentsBackgroundView::~MultiContentsBackgroundView() = default;

void MultiContentsBackgroundView::SetRoundedCorners(
    const gfx::RoundedCornersF& radii) {
  layer()->SetRoundedCornerRadius(radii);
  layer()->SetIsFastRoundedCorner(!radii.IsEmpty());
}

const gfx::RoundedCornersF& MultiContentsBackgroundView::GetRoundedCorners()
    const {
  return layer()->rounded_corner_radii();
}

void MultiContentsBackgroundView::OnThemeChanged() {
  views::View::OnThemeChanged();
  if (auto new_type = CalculateLayerType(); new_type != layer()->type()) {
    SetPaintToLayer(new_type);
  }

  if (layer()->AsSolidColor()) {
    UpdateSolidLayerColor();
  } else {
    SchedulePaint();
  }
}

void MultiContentsBackgroundView::OnPaint(gfx::Canvas* canvas) {
  CHECK(layer()->AsTextured());
  ThemedBackground::PaintBackground(canvas, this, browser_view_);
}

ui::LayerType MultiContentsBackgroundView::CalculateLayerType() const {
  const bool has_custom_image =
      !ThemedBackground::GetBackgroundColor(this, browser_view_).has_value();
  return has_custom_image ? ui::LAYER_TEXTURED : ui::LAYER_SOLID_COLOR;
}

void MultiContentsBackgroundView::UpdateSolidLayerColor() {
  CHECK(layer()->AsSolidColor());
  if (auto color = ThemedBackground::GetBackgroundColor(this, browser_view_)) {
    layer()->AsSolidColor()->SetColor(SkColor4f::FromColor(*color));
  }
}

BEGIN_METADATA(MultiContentsBackgroundView)
ADD_PROPERTY_METADATA(gfx::RoundedCornersF, RoundedCorners)
END_METADATA
