#include "ui/views/window/default_frame_view.h"
#include <algorithm>
#include <utility>
#include <vector>
#include "base/containers/adapters.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "third_party/skia/include/core/SkPath.h"
#include "ui/base/hit_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/image/image.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/resources/grit/views_resources.h"
#include "ui/views/style/typography_provider.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/window/client_view.h"
#include "ui/views/window/frame_background.h"
#include "ui/views/window/window_button_order_provider.h"
#include "ui/views/window/window_resources.h"
#include "ui/views/window/window_shape.h"
#if BUILDFLAG(IS_WIN)
#include "ui/display/win/screen_win.h"
#endif
namespace views {
namespace {
constexpr int kFrameShadowThickness = 1;
constexpr int kFrameBorderThickness = 4;
constexpr int kResizeAreaCornerSize = 16;
constexpr int kCaptionButtonHeightWithPadding = 19;
constexpr int kTitlebarTopAndBottomEdgeThickness = 2;
constexpr int kIconLeftSpacing = 2;
constexpr int kTitleIconOffsetX = 4;
constexpr int kTitleCaptionSpacing = 5;
constexpr int kClientEdgeThickness = 1;
void LayoutButton(ImageButton* button, const gfx::Rect& bounds) {
button->SetVisible(true);
button->SetImageVerticalAlignment(ImageButton::ALIGN_BOTTOM);
button->SetBoundsRect(bounds);
}
}
DefaultFrameView::DefaultFrameView(Widget* widget)
: widget_(widget), frame_background_(new FrameBackground()) {
close_button_ = InitWindowCaptionButton(
base::BindRepeating(&Widget::CloseWithReason, base::Unretained(widget_),
views::Widget::ClosedReason::kCloseButtonClicked),
IDS_APP_ACCNAME_CLOSE, IDR_CLOSE, IDR_CLOSE_H, IDR_CLOSE_P);
minimize_button_ = InitWindowCaptionButton(
base::BindRepeating(&Widget::Minimize, base::Unretained(widget_)),
IDS_APP_ACCNAME_MINIMIZE, IDR_MINIMIZE, IDR_MINIMIZE_H, IDR_MINIMIZE_P);
maximize_button_ = InitWindowCaptionButton(
base::BindRepeating(&Widget::Maximize, base::Unretained(widget_)),
IDS_APP_ACCNAME_MAXIMIZE, IDR_MAXIMIZE, IDR_MAXIMIZE_H, IDR_MAXIMIZE_P);
restore_button_ = InitWindowCaptionButton(
base::BindRepeating(&Widget::Restore, base::Unretained(widget_)),
IDS_APP_ACCNAME_RESTORE, IDR_RESTORE, IDR_RESTORE_H, IDR_RESTORE_P);
if (widget_->widget_delegate()->ShouldShowWindowIcon()) {
window_icon_ =
AddChildView(std::make_unique<ImageButton>(Button::PressedCallback()));
window_icon_->SetFocusBehavior(FocusBehavior::NEVER);
}
}
DefaultFrameView::~DefaultFrameView() = default;
gfx::Rect DefaultFrameView::GetBoundsForClientView() const {
return client_view_bounds_;
}
gfx::Rect DefaultFrameView::GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const {
int top_height = NonClientTopBorderHeight();
int border_thickness = NonClientBorderThickness();
return gfx::Rect(client_bounds.x() - border_thickness,
client_bounds.y() - top_height,
client_bounds.width() + (2 * border_thickness),
client_bounds.height() + top_height + border_thickness);
}
int DefaultFrameView::NonClientHitTest(const gfx::Point& point) {
if (!bounds().Contains(point)) {
return HTNOWHERE;
}
int frame_component = widget_->client_view()->NonClientHitTest(point);
gfx::Rect sysmenu_rect(IconBounds());
if (widget_->IsMaximized()) {
sysmenu_rect.SetRect(0, 0, sysmenu_rect.right(), sysmenu_rect.bottom());
}
sysmenu_rect.set_x(GetMirroredXForRect(sysmenu_rect));
if (sysmenu_rect.Contains(point)) {
return (frame_component == HTCLIENT) ? HTCLIENT : HTSYSMENU;
}
if (frame_component != HTNOWHERE) {
return frame_component;
}
if (close_button_->GetMirroredBounds().Contains(point)) {
return HTCLOSE;
}
if (restore_button_->GetMirroredBounds().Contains(point)) {
return HTMAXBUTTON;
}
if (maximize_button_->GetMirroredBounds().Contains(point)) {
return HTMAXBUTTON;
}
if (minimize_button_->GetMirroredBounds().Contains(point)) {
return HTMINBUTTON;
}
if (window_icon_ && window_icon_->GetMirroredBounds().Contains(point)) {
return HTSYSMENU;
}
gfx::Insets resize_border(NonClientBorderThickness());
resize_border.set_top(FrameBorderThickness());
int window_component = GetHTComponentForFrame(
point, resize_border, kResizeAreaCornerSize, kResizeAreaCornerSize,
widget_->widget_delegate()->CanResize());
return (window_component == HTNOWHERE) ? HTCAPTION : window_component;
}
void DefaultFrameView::GetWindowMask(const gfx::Size& size,
SkPath* window_mask) {
DCHECK(window_mask);
if (widget_->IsMaximized() || !ShouldShowTitleBarAndBorder()) {
return;
}
GetDefaultWindowMask(size, window_mask);
}
void DefaultFrameView::ResetWindowControls() {
restore_button_->SetState(Button::STATE_NORMAL);
minimize_button_->SetState(Button::STATE_NORMAL);
maximize_button_->SetState(Button::STATE_NORMAL);
}
void DefaultFrameView::UpdateWindowIcon() {
if (window_icon_) {
window_icon_->SchedulePaint();
}
}
void DefaultFrameView::UpdateWindowTitle() {
if (widget_->widget_delegate()->ShouldShowWindowTitle() &&
maximum_title_bar_x_ > -1) {
LayoutTitleBar();
SchedulePaintInRect(title_bounds_);
}
}
void DefaultFrameView::SizeConstraintsChanged() {
ResetWindowControls();
LayoutWindowControls();
}
void DefaultFrameView::OnPaint(gfx::Canvas* canvas) {
if (!ShouldShowTitleBarAndBorder()) {
return;
}
frame_background_->set_frame_color(GetFrameColor());
frame_background_->set_use_custom_frame(true);
frame_background_->set_is_active(ShouldPaintAsActive());
const gfx::ImageSkia frame_image = GetFrameImage();
frame_background_->set_theme_image(frame_image);
frame_background_->set_top_area_height(frame_image.height());
if (widget_->IsMaximized()) {
PaintMaximizedFrameBorder(canvas);
} else {
PaintRestoredFrameBorder(canvas);
}
PaintTitleBar(canvas);
if (ShouldShowClientEdge()) {
PaintRestoredClientEdge(canvas);
}
}
void DefaultFrameView::Layout(PassKey) {
if (ShouldShowTitleBarAndBorder()) {
LayoutWindowControls();
LayoutTitleBar();
}
LayoutClientView();
LayoutSuperclass<FrameView>(this);
}
gfx::Size DefaultFrameView::CalculatePreferredSize(
const SizeBounds& available_size) const {
return widget_->non_client_view()
->GetWindowBoundsForClientBounds(
gfx::Rect(widget_->client_view()->GetPreferredSize(available_size)))
.size();
}
gfx::Size DefaultFrameView::GetMinimumSize() const {
return widget_->non_client_view()
->GetWindowBoundsForClientBounds(
gfx::Rect(widget_->client_view()->GetMinimumSize()))
.size();
}
gfx::Size DefaultFrameView::GetMaximumSize() const {
gfx::Size max_size = widget_->client_view()->GetMaximumSize();
gfx::Size converted_size =
widget_->non_client_view()
->GetWindowBoundsForClientBounds(gfx::Rect(max_size))
.size();
return gfx::Size(max_size.width() == 0 ? 0 : converted_size.width(),
max_size.height() == 0 ? 0 : converted_size.height());
}
int DefaultFrameView::FrameBorderThickness() const {
return widget_->IsMaximized() ? 0 : kFrameBorderThickness;
}
int DefaultFrameView::NonClientBorderThickness() const {
return FrameBorderThickness() +
(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
}
int DefaultFrameView::NonClientTopBorderHeight() const {
return std::max(FrameBorderThickness() + IconSize(),
CaptionButtonY() + kCaptionButtonHeightWithPadding) +
TitlebarBottomThickness();
}
int DefaultFrameView::CaptionButtonY() const {
#if BUILDFLAG(IS_LINUX)
return FrameBorderThickness();
#else
return widget_->IsMaximized() ? FrameBorderThickness()
: kFrameShadowThickness;
#endif
}
int DefaultFrameView::TitlebarBottomThickness() const {
return kTitlebarTopAndBottomEdgeThickness +
(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
}
int DefaultFrameView::IconSize() const {
#if BUILDFLAG(IS_WIN)
return display::win::GetScreenWin()->GetSystemMetricsInDIP(SM_CYSMICON);
#else
constexpr int kIconMinimumSize = 16;
return std::max(
TypographyProvider::Get().GetWindowTitleFontList().GetHeight(),
kIconMinimumSize);
#endif
}
gfx::Rect DefaultFrameView::IconBounds() const {
int size = IconSize();
int frame_thickness = FrameBorderThickness();
int unavailable_px_at_top = widget_->IsMaximized()
? frame_thickness
: kTitlebarTopAndBottomEdgeThickness;
int y = unavailable_px_at_top +
(NonClientTopBorderHeight() - unavailable_px_at_top - size -
TitlebarBottomThickness() + 1) /
2;
return gfx::Rect(frame_thickness + kIconLeftSpacing + minimum_title_bar_x_, y,
size, size);
}
bool DefaultFrameView::ShouldShowTitleBarAndBorder() const {
return !widget_->IsFullscreen() &&
!ViewsDelegate::GetInstance()->WindowManagerProvidesTitleBar(
widget_->IsMaximized());
}
bool DefaultFrameView::ShouldShowClientEdge() const {
return !widget_->IsMaximized() && ShouldShowTitleBarAndBorder();
}
void DefaultFrameView::PaintRestoredFrameBorder(gfx::Canvas* canvas) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
frame_background_->SetCornerImages(
rb.GetImageNamed(IDR_WINDOW_TOP_LEFT_CORNER).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_TOP_RIGHT_CORNER).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_BOTTOM_LEFT_CORNER).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_BOTTOM_RIGHT_CORNER).ToImageSkia());
frame_background_->SetSideImages(
rb.GetImageNamed(IDR_WINDOW_LEFT_SIDE).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_TOP_CENTER).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_RIGHT_SIDE).ToImageSkia(),
rb.GetImageNamed(IDR_WINDOW_BOTTOM_CENTER).ToImageSkia());
frame_background_->PaintRestored(canvas, this);
}
void DefaultFrameView::PaintMaximizedFrameBorder(gfx::Canvas* canvas) {
frame_background_->PaintMaximized(canvas, this);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
const gfx::ImageSkia* titlebar_bottom =
rb.GetImageNamed(IDR_APP_TOP_CENTER).ToImageSkia();
int edge_height = titlebar_bottom->height() -
(ShouldShowClientEdge() ? kClientEdgeThickness : 0);
canvas->TileImageInt(*titlebar_bottom, 0,
widget_->client_view()->y() - edge_height, width(),
edge_height);
}
void DefaultFrameView::PaintTitleBar(gfx::Canvas* canvas) {
WidgetDelegate* delegate = widget_->widget_delegate();
if (!delegate || !delegate->ShouldShowWindowTitle()) {
return;
}
gfx::Rect rect = title_bounds_;
rect.set_x(GetMirroredXForRect(title_bounds_));
canvas->DrawStringRect(
delegate->GetWindowTitle(),
TypographyProvider::Get().GetWindowTitleFontList(),
GetColorProvider()->GetColor(ui::kColorCustomFrameCaptionForeground),
rect);
}
void DefaultFrameView::PaintRestoredClientEdge(gfx::Canvas* canvas) {
gfx::Rect client_area_bounds = widget_->client_view()->bounds();
gfx::Rect shadowed_area_bounds = client_area_bounds;
shadowed_area_bounds.Inset(gfx::Insets(1));
int shadowed_area_top = shadowed_area_bounds.y();
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
const gfx::ImageSkia* top_left = rb.GetImageSkiaNamed(IDR_APP_TOP_LEFT);
const gfx::ImageSkia* top_center = rb.GetImageSkiaNamed(IDR_APP_TOP_CENTER);
const gfx::ImageSkia* top_right = rb.GetImageSkiaNamed(IDR_APP_TOP_RIGHT);
int top_edge_y = shadowed_area_top - top_center->height();
canvas->DrawImageInt(*top_left, shadowed_area_bounds.x() - top_left->width(),
top_edge_y);
canvas->TileImageInt(*top_center, shadowed_area_bounds.x(), top_edge_y,
shadowed_area_bounds.width(), top_center->height());
canvas->DrawImageInt(*top_right, shadowed_area_bounds.right(), top_edge_y);
const gfx::ImageSkia* right = rb.GetImageSkiaNamed(IDR_CONTENT_RIGHT_SIDE);
int shadowed_area_bottom =
std::max(shadowed_area_top, shadowed_area_bounds.bottom());
int shadowed_area_height = shadowed_area_bottom - shadowed_area_top;
canvas->TileImageInt(*right, shadowed_area_bounds.right(), shadowed_area_top,
right->width(), shadowed_area_height);
const gfx::ImageSkia* bottom_left =
rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_LEFT_CORNER);
const gfx::ImageSkia* bottom_center =
rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_CENTER);
const gfx::ImageSkia* bottom_right =
rb.GetImageSkiaNamed(IDR_CONTENT_BOTTOM_RIGHT_CORNER);
canvas->DrawImageInt(*bottom_left,
shadowed_area_bounds.x() - bottom_left->width(),
shadowed_area_bottom);
canvas->TileImageInt(*bottom_center, shadowed_area_bounds.x(),
shadowed_area_bottom, shadowed_area_bounds.width(),
bottom_right->height());
canvas->DrawImageInt(*bottom_right, shadowed_area_bounds.right(),
shadowed_area_bottom);
const gfx::ImageSkia* left = rb.GetImageSkiaNamed(IDR_CONTENT_LEFT_SIDE);
canvas->TileImageInt(*left, shadowed_area_bounds.x() - left->width(),
shadowed_area_top, left->width(), shadowed_area_height);
}
SkColor DefaultFrameView::GetFrameColor() const {
return GetColorProvider()->GetColor(
widget_->IsActive() ? ui::kColorFrameActive : ui::kColorFrameInactive);
}
gfx::ImageSkia DefaultFrameView::GetFrameImage() const {
return *ui::ResourceBundle::GetSharedInstance()
.GetImageNamed(widget_->IsActive() ? IDR_FRAME
: IDR_FRAME_INACTIVE)
.ToImageSkia();
}
void DefaultFrameView::LayoutWindowControls() {
minimum_title_bar_x_ = 0;
maximum_title_bar_x_ = width();
if (bounds().IsEmpty()) {
return;
}
int caption_y = CaptionButtonY();
bool is_maximized = widget_->IsMaximized();
int extra_width =
is_maximized ? (kFrameBorderThickness - kFrameShadowThickness) : 0;
int next_button_x = FrameBorderThickness();
bool is_restored = !is_maximized && !widget_->IsMinimized();
ImageButton* invisible_button =
is_restored ? restore_button_.get() : maximize_button_.get();
invisible_button->SetVisible(false);
WindowButtonOrderProvider* button_order =
WindowButtonOrderProvider::GetInstance();
const std::vector<views::FrameButton>& leading_buttons =
button_order->leading_buttons();
const std::vector<views::FrameButton>& trailing_buttons =
button_order->trailing_buttons();
ImageButton* button = nullptr;
for (auto frame_button : leading_buttons) {
button = GetImageButton(frame_button);
if (!button) {
continue;
}
gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
button->GetPreferredSize({}));
if (frame_button == leading_buttons.front()) {
target_bounds.set_width(target_bounds.width() + extra_width);
}
LayoutButton(button, target_bounds);
next_button_x += button->width();
minimum_title_bar_x_ = std::min(width(), next_button_x);
}
next_button_x = width() - FrameBorderThickness();
for (auto frame_button : base::Reversed(trailing_buttons)) {
button = GetImageButton(frame_button);
if (!button) {
continue;
}
gfx::Rect target_bounds(gfx::Point(next_button_x, caption_y),
button->GetPreferredSize({}));
if (frame_button == trailing_buttons.back()) {
target_bounds.set_width(target_bounds.width() + extra_width);
}
target_bounds.Offset(-target_bounds.width(), 0);
LayoutButton(button, target_bounds);
next_button_x = button->x();
maximum_title_bar_x_ = std::max(minimum_title_bar_x_, next_button_x);
}
}
void DefaultFrameView::LayoutTitleBar() {
DCHECK_GE(maximum_title_bar_x_, 0);
gfx::Rect icon_bounds(IconBounds());
bool show_window_icon = window_icon_ != nullptr;
if (show_window_icon) {
window_icon_->SetBoundsRect(icon_bounds);
}
if (!widget_->widget_delegate()->ShouldShowWindowTitle()) {
return;
}
int title_x = show_window_icon ? icon_bounds.right() + kTitleIconOffsetX
: icon_bounds.x();
int title_height =
TypographyProvider::Get().GetWindowTitleFontList().GetHeight();
title_bounds_.SetRect(
title_x,
icon_bounds.y() + ((icon_bounds.height() - title_height - 1) / 2),
std::max(0, maximum_title_bar_x_ - kTitleCaptionSpacing - title_x),
title_height);
}
void DefaultFrameView::LayoutClientView() {
if (!ShouldShowTitleBarAndBorder()) {
client_view_bounds_ = bounds();
return;
}
int top_height = NonClientTopBorderHeight();
int border_thickness = NonClientBorderThickness();
client_view_bounds_.SetRect(
border_thickness, top_height,
std::max(0, width() - (2 * border_thickness)),
std::max(0, height() - top_height - border_thickness));
}
ImageButton* DefaultFrameView::InitWindowCaptionButton(
Button::PressedCallback callback,
int accessibility_string_id,
int normal_image_id,
int hot_image_id,
int pushed_image_id) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
ImageButton* button =
AddChildView(std::make_unique<ImageButton>(std::move(callback)));
button->SetFocusBehavior(FocusBehavior::ACCESSIBLE_ONLY);
button->GetViewAccessibility().SetName(
l10n_util::GetStringUTF16(accessibility_string_id));
button->SetImageModel(
Button::STATE_NORMAL,
ui::ImageModel::FromImage(rb.GetImageNamed(normal_image_id)));
button->SetImageModel(
Button::STATE_HOVERED,
ui::ImageModel::FromImage(rb.GetImageNamed(hot_image_id)));
button->SetImageModel(
Button::STATE_PRESSED,
ui::ImageModel::FromImage(rb.GetImageNamed(pushed_image_id)));
return button;
}
ImageButton* DefaultFrameView::GetImageButton(views::FrameButton frame_button) {
ImageButton* button = nullptr;
switch (frame_button) {
case views::FrameButton::kMinimize: {
button = minimize_button_;
bool should_show = widget_->widget_delegate()->CanMinimize();
button->SetVisible(should_show);
if (!should_show) {
return nullptr;
}
break;
}
case views::FrameButton::kMaximize: {
bool is_restored = !widget_->IsMaximized() && !widget_->IsMinimized();
button = is_restored ? maximize_button_.get() : restore_button_.get();
bool should_show = widget_->widget_delegate()->CanMaximize();
button->SetVisible(should_show);
if (!should_show) {
return nullptr;
}
break;
}
case views::FrameButton::kClose: {
button = close_button_;
break;
}
}
return button;
}
}