-
Notifications
You must be signed in to change notification settings - Fork 15k
/
client_frame_view_linux.cc
491 lines (402 loc) · 16.7 KB
/
client_frame_view_linux.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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright (c) 2021 Ryan Gonzalez.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/browser/ui/views/client_frame_view_linux.h"
#include <algorithm>
#include "base/strings/utf_string_conversions.h"
#include "cc/paint/paint_filter.h"
#include "cc/paint/paint_flags.h"
#include "shell/browser/native_window_views.h"
#include "shell/browser/ui/electron_desktop_window_tree_host_linux.h"
#include "shell/browser/ui/views/frameless_view.h"
#include "ui/base/hit_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/gfx/skia_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/gtk/gtk_compat.h" // nogncheck
#include "ui/gtk/gtk_util.h" // nogncheck
#include "ui/linux/linux_ui.h"
#include "ui/linux/nav_button_provider.h"
#include "ui/native_theme/native_theme.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/button/image_button.h"
#include "ui/views/style/typography.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/frame_buttons.h"
#include "ui/views/window/window_button_order_provider.h"
namespace electron {
namespace {
// These values should be the same as Chromium uses.
constexpr int kResizeOutsideBorderSize = 10;
constexpr int kResizeInsideBoundsSize = 5;
ui::NavButtonProvider::ButtonState ButtonStateToNavButtonProviderState(
views::Button::ButtonState state) {
switch (state) {
case views::Button::STATE_NORMAL:
return ui::NavButtonProvider::ButtonState::kNormal;
case views::Button::STATE_HOVERED:
return ui::NavButtonProvider::ButtonState::kHovered;
case views::Button::STATE_PRESSED:
return ui::NavButtonProvider::ButtonState::kPressed;
case views::Button::STATE_DISABLED:
return ui::NavButtonProvider::ButtonState::kDisabled;
case views::Button::STATE_COUNT:
default:
NOTREACHED();
return ui::NavButtonProvider::ButtonState::kNormal;
}
}
} // namespace
// static
const char ClientFrameViewLinux::kViewClassName[] = "ClientFrameView";
ClientFrameViewLinux::ClientFrameViewLinux()
: theme_(ui::NativeTheme::GetInstanceForNativeUi()),
nav_button_provider_(ui::LinuxUi::instance()->CreateNavButtonProvider()),
nav_buttons_{
NavButton{ui::NavButtonProvider::FrameButtonDisplayType::kClose,
views::FrameButton::kClose, &views::Widget::Close,
IDS_APP_ACCNAME_CLOSE, HTCLOSE},
NavButton{ui::NavButtonProvider::FrameButtonDisplayType::kMaximize,
views::FrameButton::kMaximize, &views::Widget::Maximize,
IDS_APP_ACCNAME_MAXIMIZE, HTMAXBUTTON},
NavButton{ui::NavButtonProvider::FrameButtonDisplayType::kRestore,
views::FrameButton::kMaximize, &views::Widget::Restore,
IDS_APP_ACCNAME_RESTORE, HTMAXBUTTON},
NavButton{ui::NavButtonProvider::FrameButtonDisplayType::kMinimize,
views::FrameButton::kMinimize, &views::Widget::Minimize,
IDS_APP_ACCNAME_MINIMIZE, HTMINBUTTON},
},
trailing_frame_buttons_{views::FrameButton::kMinimize,
views::FrameButton::kMaximize,
views::FrameButton::kClose} {
for (auto& button : nav_buttons_) {
button.button = new views::ImageButton();
button.button->SetImageVerticalAlignment(views::ImageButton::ALIGN_MIDDLE);
button.button->SetAccessibleName(
l10n_util::GetStringUTF16(button.accessibility_id));
AddChildView(button.button);
}
title_ = new views::Label();
title_->SetSubpixelRenderingEnabled(false);
title_->SetAutoColorReadabilityEnabled(false);
title_->SetHorizontalAlignment(gfx::ALIGN_CENTER);
title_->SetVerticalAlignment(gfx::ALIGN_MIDDLE);
title_->SetTextStyle(views::style::STYLE_TAB_ACTIVE);
AddChildView(title_);
native_theme_observer_.Observe(theme_);
if (ui::LinuxUi* ui = ui::LinuxUi::instance()) {
ui->AddWindowButtonOrderObserver(this);
OnWindowButtonOrderingChange();
}
}
ClientFrameViewLinux::~ClientFrameViewLinux() {
if (ui::LinuxUi* ui = ui::LinuxUi::instance())
ui->RemoveWindowButtonOrderObserver(this);
theme_->RemoveObserver(this);
}
void ClientFrameViewLinux::Init(NativeWindowViews* window,
views::Widget* frame) {
FramelessView::Init(window, frame);
// Unretained() is safe because the subscription is saved into an instance
// member and thus will be cancelled upon the instance's destruction.
paint_as_active_changed_subscription_ =
frame_->RegisterPaintAsActiveChangedCallback(base::BindRepeating(
&ClientFrameViewLinux::PaintAsActiveChanged, base::Unretained(this)));
auto* tree_host = static_cast<ElectronDesktopWindowTreeHostLinux*>(
ElectronDesktopWindowTreeHostLinux::GetHostForWidget(
window->GetAcceleratedWidget()));
host_supports_client_frame_shadow_ = tree_host->SupportsClientFrameShadow();
frame_provider_ = ui::LinuxUi::instance()->GetWindowFrameProvider(
!host_supports_client_frame_shadow_, frame_->IsMaximized());
UpdateWindowTitle();
for (auto& button : nav_buttons_) {
// Unretained() is safe because the buttons are added as children to, and
// thus owned by, this view. Thus, the buttons themselves will be destroyed
// when this view is destroyed, and the frame's life must never outlive the
// view.
button.button->SetCallback(
base::BindRepeating(button.callback, base::Unretained(frame)));
}
UpdateThemeValues();
}
gfx::Insets ClientFrameViewLinux::GetBorderDecorationInsets() const {
return frame_provider_->GetFrameThicknessDip();
}
gfx::Insets ClientFrameViewLinux::GetInputInsets() const {
return gfx::Insets(
host_supports_client_frame_shadow_ ? -kResizeOutsideBorderSize : 0);
}
gfx::Rect ClientFrameViewLinux::GetWindowContentBounds() const {
gfx::Rect content_bounds = bounds();
content_bounds.Inset(GetBorderDecorationInsets());
return content_bounds;
}
SkRRect ClientFrameViewLinux::GetRoundedWindowContentBounds() const {
SkRect rect = gfx::RectToSkRect(GetWindowContentBounds());
SkRRect rrect;
if (!frame_->IsMaximized()) {
SkPoint round_point{theme_values_.window_border_radius,
theme_values_.window_border_radius};
SkPoint radii[] = {round_point, round_point, {}, {}};
rrect.setRectRadii(rect, radii);
} else {
rrect.setRect(rect);
}
return rrect;
}
void ClientFrameViewLinux::OnNativeThemeUpdated(
ui::NativeTheme* observed_theme) {
UpdateThemeValues();
}
void ClientFrameViewLinux::OnWindowButtonOrderingChange() {
auto* provider = views::WindowButtonOrderProvider::GetInstance();
leading_frame_buttons_ = provider->leading_buttons();
trailing_frame_buttons_ = provider->trailing_buttons();
InvalidateLayout();
}
int ClientFrameViewLinux::ResizingBorderHitTest(const gfx::Point& point) {
return ResizingBorderHitTestImpl(
point,
GetBorderDecorationInsets() + gfx::Insets(kResizeInsideBoundsSize));
}
gfx::Rect ClientFrameViewLinux::GetBoundsForClientView() const {
gfx::Rect client_bounds = bounds();
if (!frame_->IsFullscreen()) {
client_bounds.Inset(GetBorderDecorationInsets());
client_bounds.Inset(
gfx::Insets::TLBR(GetTitlebarBounds().height(), 0, 0, 0));
}
return client_bounds;
}
gfx::Rect ClientFrameViewLinux::GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const {
gfx::Insets insets = bounds().InsetsFrom(GetBoundsForClientView());
return gfx::Rect(std::max(0, client_bounds.x() - insets.left()),
std::max(0, client_bounds.y() - insets.top()),
client_bounds.width() + insets.width(),
client_bounds.height() + insets.height());
}
int ClientFrameViewLinux::NonClientHitTest(const gfx::Point& point) {
int component = ResizingBorderHitTest(point);
if (component != HTNOWHERE) {
return component;
}
for (auto& button : nav_buttons_) {
if (button.button->GetVisible() &&
button.button->GetMirroredBounds().Contains(point)) {
return HTCLIENT;
}
}
if (GetTitlebarBounds().Contains(point)) {
return HTCAPTION;
}
return FramelessView::NonClientHitTest(point);
}
void ClientFrameViewLinux::GetWindowMask(const gfx::Size& size,
SkPath* window_mask) {
// Nothing to do here, as transparency is used for decorations, not masks.
}
void ClientFrameViewLinux::UpdateWindowTitle() {
title_->SetText(base::UTF8ToUTF16(window_->GetTitle()));
}
void ClientFrameViewLinux::SizeConstraintsChanged() {
InvalidateLayout();
}
gfx::Size ClientFrameViewLinux::CalculatePreferredSize() const {
return SizeWithDecorations(FramelessView::CalculatePreferredSize());
}
gfx::Size ClientFrameViewLinux::GetMinimumSize() const {
return SizeWithDecorations(FramelessView::GetMinimumSize());
}
gfx::Size ClientFrameViewLinux::GetMaximumSize() const {
return SizeWithDecorations(FramelessView::GetMaximumSize());
}
void ClientFrameViewLinux::Layout() {
FramelessView::Layout();
if (frame_->IsFullscreen()) {
// Just hide everything and return.
for (NavButton& button : nav_buttons_) {
button.button->SetVisible(false);
}
title_->SetVisible(false);
return;
}
frame_provider_ = ui::LinuxUi::instance()->GetWindowFrameProvider(
!host_supports_client_frame_shadow_, frame_->IsMaximized());
UpdateButtonImages();
LayoutButtons();
gfx::Rect title_bounds(GetTitlebarContentBounds());
title_bounds.Inset(theme_values_.title_padding);
title_->SetVisible(true);
title_->SetBounds(title_bounds.x(), title_bounds.y(), title_bounds.width(),
title_bounds.height());
}
void ClientFrameViewLinux::OnPaint(gfx::Canvas* canvas) {
if (!frame_->IsFullscreen()) {
frame_provider_->PaintWindowFrame(canvas, GetLocalBounds(),
GetTitlebarBounds().bottom(),
ShouldPaintAsActive());
}
}
const char* ClientFrameViewLinux::GetClassName() const {
return kViewClassName;
}
void ClientFrameViewLinux::PaintAsActiveChanged() {
UpdateThemeValues();
}
void ClientFrameViewLinux::UpdateThemeValues() {
gtk::GtkCssContext window_context =
gtk::AppendCssNodeToStyleContext({}, "GtkWindow#window.background.csd");
gtk::GtkCssContext headerbar_context = gtk::AppendCssNodeToStyleContext(
{}, "GtkHeaderBar#headerbar.default-decoration.titlebar");
gtk::GtkCssContext title_context = gtk::AppendCssNodeToStyleContext(
headerbar_context, "GtkLabel#label.title");
gtk::GtkCssContext button_context = gtk::AppendCssNodeToStyleContext(
headerbar_context, "GtkButton#button.image-button");
gtk_style_context_set_parent(headerbar_context, window_context);
gtk_style_context_set_parent(title_context, headerbar_context);
gtk_style_context_set_parent(button_context, headerbar_context);
// ShouldPaintAsActive asks the widget, so assume active if the widget is not
// set yet.
if (GetWidget() != nullptr && !ShouldPaintAsActive()) {
gtk_style_context_set_state(window_context, GTK_STATE_FLAG_BACKDROP);
gtk_style_context_set_state(headerbar_context, GTK_STATE_FLAG_BACKDROP);
gtk_style_context_set_state(title_context, GTK_STATE_FLAG_BACKDROP);
gtk_style_context_set_state(button_context, GTK_STATE_FLAG_BACKDROP);
}
theme_values_.window_border_radius = frame_provider_->GetTopCornerRadiusDip();
gtk::GtkStyleContextGet(headerbar_context, "min-height",
&theme_values_.titlebar_min_height, nullptr);
theme_values_.titlebar_padding =
gtk::GtkStyleContextGetPadding(headerbar_context);
theme_values_.title_color = gtk::GtkStyleContextGetColor(title_context);
theme_values_.title_padding = gtk::GtkStyleContextGetPadding(title_context);
gtk::GtkStyleContextGet(button_context, "min-height",
&theme_values_.button_min_size, nullptr);
theme_values_.button_padding = gtk::GtkStyleContextGetPadding(button_context);
title_->SetEnabledColor(theme_values_.title_color);
InvalidateLayout();
SchedulePaint();
}
ui::NavButtonProvider::FrameButtonDisplayType
ClientFrameViewLinux::GetButtonTypeToSkip() const {
return frame_->IsMaximized()
? ui::NavButtonProvider::FrameButtonDisplayType::kMaximize
: ui::NavButtonProvider::FrameButtonDisplayType::kRestore;
}
void ClientFrameViewLinux::UpdateButtonImages() {
nav_button_provider_->RedrawImages(theme_values_.button_min_size,
frame_->IsMaximized(),
ShouldPaintAsActive());
ui::NavButtonProvider::FrameButtonDisplayType skip_type =
GetButtonTypeToSkip();
for (NavButton& button : nav_buttons_) {
if (button.type == skip_type) {
continue;
}
for (size_t state_id = 0; state_id < views::Button::STATE_COUNT;
state_id++) {
views::Button::ButtonState state =
static_cast<views::Button::ButtonState>(state_id);
button.button->SetImage(
state, nav_button_provider_->GetImage(
button.type, ButtonStateToNavButtonProviderState(state)));
}
}
}
void ClientFrameViewLinux::LayoutButtons() {
for (NavButton& button : nav_buttons_) {
button.button->SetVisible(false);
}
gfx::Rect remaining_content_bounds = GetTitlebarContentBounds();
LayoutButtonsOnSide(ButtonSide::kLeading, &remaining_content_bounds);
LayoutButtonsOnSide(ButtonSide::kTrailing, &remaining_content_bounds);
}
void ClientFrameViewLinux::LayoutButtonsOnSide(
ButtonSide side,
gfx::Rect* remaining_content_bounds) {
ui::NavButtonProvider::FrameButtonDisplayType skip_type =
GetButtonTypeToSkip();
std::vector<views::FrameButton> frame_buttons;
switch (side) {
case ButtonSide::kLeading:
frame_buttons = leading_frame_buttons_;
break;
case ButtonSide::kTrailing:
frame_buttons = trailing_frame_buttons_;
// We always lay buttons out going from the edge towards the center, but
// they are given to us as left-to-right, so reverse them.
std::reverse(frame_buttons.begin(), frame_buttons.end());
break;
default:
NOTREACHED();
}
for (views::FrameButton frame_button : frame_buttons) {
auto* button = std::find_if(
nav_buttons_.begin(), nav_buttons_.end(), [&](const NavButton& test) {
return test.type != skip_type && test.frame_button == frame_button;
});
CHECK(button != nav_buttons_.end())
<< "Failed to find frame button: " << static_cast<int>(frame_button);
if (button->type == skip_type) {
continue;
}
button->button->SetVisible(true);
int button_width = theme_values_.button_min_size;
int next_button_offset =
button_width + nav_button_provider_->GetInterNavButtonSpacing();
int x_position = 0;
gfx::Insets inset_after_placement;
switch (side) {
case ButtonSide::kLeading:
x_position = remaining_content_bounds->x();
inset_after_placement.set_left(next_button_offset);
break;
case ButtonSide::kTrailing:
x_position = remaining_content_bounds->right() - button_width;
inset_after_placement.set_right(next_button_offset);
break;
default:
NOTREACHED();
}
button->button->SetBounds(x_position, remaining_content_bounds->y(),
button_width, remaining_content_bounds->height());
remaining_content_bounds->Inset(inset_after_placement);
}
}
gfx::Rect ClientFrameViewLinux::GetTitlebarBounds() const {
if (frame_->IsFullscreen()) {
return gfx::Rect();
}
int font_height = gfx::FontList().GetHeight();
int titlebar_height =
std::max(font_height, theme_values_.titlebar_min_height) +
GetTitlebarContentInsets().height();
gfx::Insets decoration_insets = GetBorderDecorationInsets();
// We add the inset height here, so the .Inset() that follows won't reduce it
// to be too small.
gfx::Rect titlebar(width(), titlebar_height + decoration_insets.height());
titlebar.Inset(decoration_insets);
return titlebar;
}
gfx::Insets ClientFrameViewLinux::GetTitlebarContentInsets() const {
return theme_values_.titlebar_padding +
nav_button_provider_->GetTopAreaSpacing();
}
gfx::Rect ClientFrameViewLinux::GetTitlebarContentBounds() const {
gfx::Rect titlebar(GetTitlebarBounds());
titlebar.Inset(GetTitlebarContentInsets());
return titlebar;
}
gfx::Size ClientFrameViewLinux::SizeWithDecorations(gfx::Size size) const {
gfx::Insets decoration_insets = GetBorderDecorationInsets();
size.Enlarge(0, GetTitlebarBounds().height());
size.Enlarge(decoration_insets.width(), decoration_insets.height());
return size;
}
} // namespace electron