1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
// Copyright 2014 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/browser_frame_header_chromeos.h"
#include "base/check.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view_chromeos.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chromeos/ash/experiences/system_web_apps/types/system_web_app_delegate.h"
#include "chromeos/ui/base/window_properties.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "chromeos/ui/frame/frame_utils.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/ui_base_features.h"
#include "ui/color/color_id.h"
#include "ui/display/screen.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/scoped_canvas.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/caption_button_layout_constants.h"
namespace {
void PaintThemedFrame(gfx::Canvas* canvas,
const gfx::ImageSkia& frame_image,
const gfx::ImageSkia& frame_overlay_image,
SkColor background_color,
const gfx::Rect& bounds,
int image_inset_x,
int image_inset_y) {
SkColor opaque_background_color =
SkColorSetA(background_color, SK_AlphaOPAQUE);
// When no images are used, just draw a color.
if (frame_image.isNull() && frame_overlay_image.isNull()) {
canvas->DrawColor(opaque_background_color);
return;
}
// This handles the case where blending is required between one or more images
// and the background color. In this case we use a SaveLayerWithFlags() call
// to draw all 2-3 components into a single layer.
const bool blending_required =
!frame_image.isNull() && !frame_overlay_image.isNull();
if (blending_required) {
cc::PaintFlags flags;
// We use kPlus blending mode so that between the active and inactive
// background colors, the result is 255 alpha (i.e. opaque).
flags.setBlendMode(SkBlendMode::kPlus);
canvas->SaveLayerWithFlags(flags);
}
// Images can be transparent and we expect the background color to be present
// behind them.
canvas->DrawColor(opaque_background_color);
if (!frame_image.isNull()) {
canvas->TileImageInt(frame_image, image_inset_x, image_inset_y, 0, 0,
bounds.width(), bounds.height(), 1.0f,
SkTileMode::kRepeat, SkTileMode::kMirror);
}
if (!frame_overlay_image.isNull()) {
canvas->DrawImageInt(frame_overlay_image, 0, 0);
}
if (blending_required) {
canvas->Restore();
}
}
// Returns the frame path with the given |bounds| and |corner_radius|
// for the rounded corner of the frame header.
SkPath GetFrameHeaderPath(const gfx::Rect& bounds, int corner_radius) {
const SkScalar sk_corner_radius = SkIntToScalar(corner_radius);
const SkScalar radii[8] = {sk_corner_radius,
sk_corner_radius, // top-left
sk_corner_radius,
sk_corner_radius, // top-right
0,
0, // bottom-right
0,
0}; // bottom-left
SkPath frame_path;
frame_path.addRoundRect(gfx::RectToSkRect(bounds), radii,
SkPathDirection::kCW);
return frame_path;
}
// Tiles |frame_image| and |frame_overlay_image| into an area, rounding the top
// corners.
void PaintFrameImagesInRoundRect(gfx::Canvas* canvas,
const gfx::ImageSkia& frame_image,
const gfx::ImageSkia& frame_overlay_image,
SkColor background_color,
const gfx::Rect& bounds,
int image_inset_x,
int image_inset_y,
int corner_radius) {
bool antialias = corner_radius > 0;
gfx::ScopedCanvas scoped_save(canvas);
canvas->ClipPath(GetFrameHeaderPath(bounds, corner_radius), antialias);
PaintThemedFrame(canvas, frame_image, frame_overlay_image, background_color,
bounds, image_inset_x, image_inset_y);
}
} // namespace
///////////////////////////////////////////////////////////////////////////////
// BrowserFrameHeaderChromeOS, public:
BrowserFrameHeaderChromeOS::BrowserFrameHeaderChromeOS(
views::Widget* target_widget,
views::View* view,
AppearanceProvider* appearance_provider,
chromeos::FrameCaptionButtonContainerView* caption_button_container)
: FrameHeader(target_widget, view) {
DCHECK(appearance_provider);
DCHECK(caption_button_container);
appearance_provider_ = appearance_provider;
SetCaptionButtonContainer(caption_button_container);
}
BrowserFrameHeaderChromeOS::~BrowserFrameHeaderChromeOS() = default;
///////////////////////////////////////////////////////////////////////////////
// BrowserFrameHeaderChromeOS, protected:
void BrowserFrameHeaderChromeOS::DoPaintHeader(gfx::Canvas* canvas) {
PaintFrameImages(canvas);
PaintTitleBar(canvas);
}
views::CaptionButtonLayoutSize BrowserFrameHeaderChromeOS::GetButtonLayoutSize()
const {
if (display::Screen::GetScreen()->InTabletMode()) {
return views::CaptionButtonLayoutSize::kBrowserCaptionMaximized;
}
return chromeos::ShouldUseRestoreFrame(target_widget())
? views::CaptionButtonLayoutSize::kBrowserCaptionRestored
: views::CaptionButtonLayoutSize::kBrowserCaptionMaximized;
}
SkColor BrowserFrameHeaderChromeOS::GetTitleColor() const {
return appearance_provider_->GetTitleColor();
}
SkColor BrowserFrameHeaderChromeOS::GetCurrentFrameColor() const {
return appearance_provider_->GetFrameHeaderColor(mode() == MODE_ACTIVE);
}
void BrowserFrameHeaderChromeOS::UpdateFrameColors() {
SetPaintAsActive(target_widget()->ShouldPaintAsActive());
std::optional<ui::ColorId> button_colors;
auto* browser_non_client_frame_view =
static_cast<BrowserNonClientFrameViewChromeOS*>(view());
web_app::AppBrowserController* app_browser_controller =
browser_non_client_frame_view->browser_view()
->browser()
->app_controller();
// Please note, `app_browser_controller` may be null for non-PWA windows.
if (!app_browser_controller ||
(app_browser_controller->system_app() &&
app_browser_controller->system_app()->UseSystemThemeColor())) {
button_colors = mode() == MODE_ACTIVE
? ui::kColorSysPrimary
: ui::kColorFrameCaptionButtonUnfocused;
}
UpdateCaptionButtonColors(button_colors);
view()->SchedulePaint();
}
///////////////////////////////////////////////////////////////////////////////
// BrowserFrameHeaderChromeOS, private:
void BrowserFrameHeaderChromeOS::PaintFrameImages(gfx::Canvas* canvas) {
const bool active = mode() == Mode::MODE_ACTIVE;
gfx::ImageSkia frame_image =
appearance_provider_->GetFrameHeaderImage(active);
gfx::ImageSkia frame_overlay_image =
appearance_provider_->GetFrameHeaderOverlayImage(active);
PaintFrameImagesInRoundRect(canvas, frame_image, frame_overlay_image,
appearance_provider_->GetFrameHeaderColor(active),
GetPaintedBounds(), /*image_inset_x=*/0,
appearance_provider_->GetFrameHeaderImageYInset(),
header_corner_radius());
}
|