forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounded_window_corners_aura.cc
157 lines (127 loc) · 4.91 KB
/
rounded_window_corners_aura.cc
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
// Copyright 2019 The Chromium 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 "chromecast/graphics/rounded_window_corners.h"
#include <vector>
#include "base/threading/thread_checker.h"
#include "chromecast/graphics/cast_window_manager.h"
#include "chromecast/ui/mojom/ui_service.mojom.h"
#include "ui/aura/window.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
namespace chromecast {
namespace {
const int kCornerRadius = 10;
// A view that draws a black rounded corner. One should be used for each corner
// of the main view.
class BlackCornerView : public views::View {
public:
METADATA_HEADER(BlackCornerView);
BlackCornerView(int radius, bool on_right, bool on_top)
: radius_(radius), on_right_(on_right), on_top_(on_top) {}
~BlackCornerView() override {}
void SetColorInversion(bool enable) {
// In order to show as black we need to paint white when inversion is on.
color_ = enable ? SK_ColorWHITE : SK_ColorBLACK;
OnPropertyChanged(&color_, views::kPropertyEffectsPaint);
}
bool GetColorInversion() const { return color_ == SK_ColorWHITE; }
private:
void OnPaint(gfx::Canvas* canvas) override {
// Draw a black rectangle over everything.
canvas->DrawColor(color_);
// Then clear out the inner corner.
cc::PaintFlags flags;
flags.setStrokeWidth(0);
flags.setColor(color_);
flags.setStyle(cc::PaintFlags::kFill_Style);
flags.setAntiAlias(true);
flags.setBlendMode(SkBlendMode::kClear);
gfx::PointF center_point(on_right_ ? radius_ : 0, on_top_ ? radius_ : 0);
canvas->DrawCircle(center_point, radius_, flags);
}
void Layout() override {
int x = on_right_ ? 0 : parent()->width() - radius_;
int y = on_top_ ? 0 : parent()->height() - radius_;
SetBounds(x, y, radius_, radius_);
}
SkColor color_ = SK_ColorBLACK;
int radius_;
bool on_right_;
bool on_top_;
};
BEGIN_METADATA(BlackCornerView, views::View)
ADD_PROPERTY_METADATA(bool, ColorInversion)
END_METADATA
// Aura based implementation of RoundedWindowCorners.
class RoundedWindowCornersAura : public RoundedWindowCorners {
public:
explicit RoundedWindowCornersAura(CastWindowManager* window_manager);
RoundedWindowCornersAura(const RoundedWindowCornersAura&) = delete;
RoundedWindowCornersAura& operator=(const RoundedWindowCornersAura&) = delete;
~RoundedWindowCornersAura() override;
void SetEnabled(bool enable) override;
bool IsEnabled() const override;
void SetColorInversion(bool enable) override;
private:
std::unique_ptr<views::Widget> widget_;
views::LayoutProvider layout_provider_;
std::vector<BlackCornerView*> corners_;
THREAD_CHECKER(thread_checker_);
};
RoundedWindowCornersAura::RoundedWindowCornersAura(
CastWindowManager* window_manager) {
auto main_view = std::make_unique<views::View>();
auto add_view = [this, &main_view](int radius, bool on_right, bool on_top) {
BlackCornerView* view = new BlackCornerView(radius, on_right, on_top);
main_view->AddChildView(view);
corners_.push_back(view);
};
add_view(kCornerRadius, false, false);
add_view(kCornerRadius, true, false);
add_view(kCornerRadius, false, true);
add_view(kCornerRadius, true, true);
widget_ = std::make_unique<views::Widget>();
views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.context = window_manager->GetRootWindow();
params.opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
params.bounds = window_manager->GetRootWindow()->GetBoundsInRootWindow();
params.accept_events = false;
widget_->Init(std::move(params));
widget_->SetContentsView(std::move(main_view));
widget_->GetNativeWindow()->SetName("RoundCorners");
window_manager->SetZOrder(widget_->GetNativeView(),
mojom::ZOrder::CORNERS_OVERLAY);
// Remain hidden until explicitly shown. Rounded corners are only needed for
// specific circumstances such as webviews.
widget_->Hide();
}
RoundedWindowCornersAura::~RoundedWindowCornersAura() {
DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
}
void RoundedWindowCornersAura::SetEnabled(bool enable) {
if (enable) {
widget_->Show();
} else {
widget_->Hide();
}
}
bool RoundedWindowCornersAura::IsEnabled() const {
return widget_->IsVisible();
}
void RoundedWindowCornersAura::SetColorInversion(bool enable) {
for (auto* view : corners_)
view->SetColorInversion(enable);
}
} // namespace
// static
std::unique_ptr<RoundedWindowCorners> RoundedWindowCorners::Create(
CastWindowManager* window_manager) {
return std::make_unique<RoundedWindowCornersAura>(window_manager);
}
} // namespace chromecast