2021-08-28 03:55:15 +02:00
|
|
|
// Copyright (c) 2021 The Chromium Embedded Framework 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 "tests/cefclient/browser/views_overlay_controls.h"
|
|
|
|
|
2021-09-22 10:14:49 +02:00
|
|
|
#include <algorithm>
|
2023-02-28 19:34:12 +01:00
|
|
|
#include <array>
|
2021-08-28 03:55:15 +02:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "include/views/cef_box_layout.h"
|
|
|
|
#include "include/views/cef_window.h"
|
|
|
|
#include "tests/cefclient/browser/views_style.h"
|
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr int kInsets = 4;
|
|
|
|
constexpr int kLocationBarPadding = 100;
|
|
|
|
|
|
|
|
// White with 80% opacity.
|
|
|
|
constexpr auto kBackgroundColor = CefColorSetARGB(255 * .80, 255, 255, 255);
|
|
|
|
|
|
|
|
std::string GetLabel(ViewsOverlayControls::Command command, bool maximized) {
|
|
|
|
switch (command) {
|
|
|
|
case ViewsOverlayControls::Command::kMinimize:
|
|
|
|
return "-";
|
|
|
|
case ViewsOverlayControls::Command::kMaximize:
|
|
|
|
return maximized ? "O" : "o";
|
|
|
|
case ViewsOverlayControls::Command::kClose:
|
|
|
|
return "X";
|
|
|
|
}
|
2021-09-21 11:39:21 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return std::string();
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 19:34:12 +01:00
|
|
|
std::array<ViewsOverlayControls::Command, 3> GetButtons() {
|
|
|
|
#if defined(OS_MAC)
|
|
|
|
return {ViewsOverlayControls::Command::kClose,
|
|
|
|
ViewsOverlayControls::Command::kMaximize,
|
|
|
|
ViewsOverlayControls::Command::kMinimize};
|
|
|
|
#else
|
|
|
|
return {ViewsOverlayControls::Command::kMinimize,
|
|
|
|
ViewsOverlayControls::Command::kMaximize,
|
|
|
|
ViewsOverlayControls::Command::kClose};
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:17:02 +02:00
|
|
|
cef_docking_mode_t GetPanelDockingMode(bool use_bottom_controls) {
|
2023-02-28 19:34:12 +01:00
|
|
|
#if defined(OS_MAC)
|
2023-06-30 14:17:02 +02:00
|
|
|
return use_bottom_controls ? CEF_DOCKING_MODE_BOTTOM_LEFT
|
|
|
|
: CEF_DOCKING_MODE_TOP_LEFT;
|
2023-02-28 19:34:12 +01:00
|
|
|
#else
|
2023-06-30 14:17:02 +02:00
|
|
|
return use_bottom_controls ? CEF_DOCKING_MODE_BOTTOM_RIGHT
|
|
|
|
: CEF_DOCKING_MODE_TOP_RIGHT;
|
2023-02-28 19:34:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
2023-06-30 14:17:02 +02:00
|
|
|
cef_docking_mode_t GetMenuDockingMode(bool use_bottom_controls) {
|
2023-02-28 19:34:12 +01:00
|
|
|
#if defined(OS_MAC)
|
2023-06-30 14:17:02 +02:00
|
|
|
return use_bottom_controls ? CEF_DOCKING_MODE_BOTTOM_RIGHT
|
|
|
|
: CEF_DOCKING_MODE_TOP_RIGHT;
|
2023-02-28 19:34:12 +01:00
|
|
|
#else
|
2023-06-30 14:17:02 +02:00
|
|
|
return use_bottom_controls ? CEF_DOCKING_MODE_BOTTOM_LEFT
|
|
|
|
: CEF_DOCKING_MODE_TOP_LEFT;
|
2023-02-28 19:34:12 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-28 03:55:15 +02:00
|
|
|
} // namespace
|
|
|
|
|
2023-06-30 14:17:02 +02:00
|
|
|
ViewsOverlayControls::ViewsOverlayControls(bool with_window_buttons,
|
|
|
|
bool use_bottom_controls)
|
|
|
|
: with_window_buttons_(with_window_buttons),
|
|
|
|
use_bottom_controls_(use_bottom_controls) {}
|
2021-08-28 03:55:15 +02:00
|
|
|
|
|
|
|
void ViewsOverlayControls::Initialize(CefRefPtr<CefWindow> window,
|
|
|
|
CefRefPtr<CefMenuButton> menu_button,
|
|
|
|
CefRefPtr<CefView> location_bar,
|
|
|
|
bool is_chrome_toolbar) {
|
|
|
|
DCHECK(!window_);
|
|
|
|
DCHECK(menu_button);
|
|
|
|
DCHECK(location_bar);
|
|
|
|
|
|
|
|
window_ = window;
|
|
|
|
window_maximized_ = window_->IsMaximized();
|
|
|
|
|
2023-06-30 14:17:02 +02:00
|
|
|
CefInsets insets;
|
|
|
|
if (use_bottom_controls_) {
|
|
|
|
insets.Set(0, kInsets, kInsets, kInsets);
|
|
|
|
} else {
|
|
|
|
insets.Set(kInsets, kInsets, 0, kInsets);
|
|
|
|
}
|
|
|
|
|
2023-02-28 19:34:12 +01:00
|
|
|
if (with_window_buttons_) {
|
|
|
|
// Window control buttons. These controls are currently text which means
|
|
|
|
// that we can't use a transparent background because subpixel text
|
|
|
|
// rendering will break.
|
|
|
|
// See comments on the related DCHECK in Label::PaintText.
|
2024-03-23 00:51:00 +01:00
|
|
|
panel_ = CefPanel::CreatePanel(this);
|
2023-02-28 19:34:12 +01:00
|
|
|
|
|
|
|
// Use a horizontal box layout.
|
|
|
|
CefBoxLayoutSettings panel_layout_settings;
|
|
|
|
panel_layout_settings.horizontal = true;
|
|
|
|
panel_->SetToBoxLayout(panel_layout_settings);
|
|
|
|
|
|
|
|
for (auto button : GetButtons()) {
|
|
|
|
panel_->AddChildView(CreateButton(button));
|
|
|
|
}
|
2023-06-30 14:17:02 +02:00
|
|
|
panel_controller_ = window->AddOverlayView(
|
2023-10-25 11:16:57 +02:00
|
|
|
panel_, GetPanelDockingMode(use_bottom_controls_),
|
|
|
|
/*can_activate=*/false);
|
2023-06-30 14:17:02 +02:00
|
|
|
panel_controller_->SetInsets(insets);
|
2023-02-28 19:34:12 +01:00
|
|
|
panel_controller_->SetVisible(true);
|
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
|
|
|
|
// Menu button.
|
|
|
|
menu_button->SetBackgroundColor(kBackgroundColor);
|
2023-06-30 14:17:02 +02:00
|
|
|
menu_controller_ = window_->AddOverlayView(
|
2023-10-25 11:16:57 +02:00
|
|
|
menu_button, GetMenuDockingMode(use_bottom_controls_),
|
|
|
|
/*can_activate=*/false);
|
2023-06-30 14:17:02 +02:00
|
|
|
menu_controller_->SetInsets(insets);
|
2021-08-28 03:55:15 +02:00
|
|
|
menu_controller_->SetVisible(true);
|
|
|
|
|
|
|
|
// Location bar. Will be made visible in UpdateControls().
|
|
|
|
location_bar_ = location_bar;
|
|
|
|
is_chrome_toolbar_ = is_chrome_toolbar;
|
|
|
|
// Use a 100% transparent background for the Chrome toolbar.
|
|
|
|
location_bar_->SetBackgroundColor(is_chrome_toolbar_ ? 0 : kBackgroundColor);
|
2023-10-25 11:16:57 +02:00
|
|
|
location_controller_ = window_->AddOverlayView(
|
|
|
|
location_bar_, CEF_DOCKING_MODE_CUSTOM, /*can_activate=*/false);
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ViewsOverlayControls::Destroy() {
|
|
|
|
window_ = nullptr;
|
|
|
|
panel_ = nullptr;
|
2023-02-28 19:34:12 +01:00
|
|
|
if (panel_controller_) {
|
|
|
|
panel_controller_->Destroy();
|
|
|
|
panel_controller_ = nullptr;
|
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
menu_controller_->Destroy();
|
|
|
|
menu_controller_ = nullptr;
|
|
|
|
location_bar_ = nullptr;
|
|
|
|
location_controller_->Destroy();
|
|
|
|
location_controller_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewsOverlayControls::UpdateControls() {
|
|
|
|
// Update location bar size, position and visibility.
|
2023-06-30 14:17:02 +02:00
|
|
|
const auto window_bounds = window_->GetBounds();
|
|
|
|
auto bounds = window_bounds;
|
2021-08-28 03:55:15 +02:00
|
|
|
bounds.x = kLocationBarPadding;
|
|
|
|
bounds.width -= kLocationBarPadding * 2;
|
2023-06-30 14:17:02 +02:00
|
|
|
|
2021-08-28 03:55:15 +02:00
|
|
|
if (is_chrome_toolbar_) {
|
|
|
|
// Fit the standard Chrome toolbar.
|
|
|
|
const auto preferred_size = location_bar_->GetPreferredSize();
|
|
|
|
bounds.height =
|
|
|
|
std::max(menu_controller_->GetSize().height, preferred_size.height);
|
|
|
|
} else {
|
|
|
|
bounds.height = menu_controller_->GetSize().height;
|
|
|
|
}
|
2023-06-30 14:17:02 +02:00
|
|
|
|
|
|
|
if (use_bottom_controls_) {
|
|
|
|
bounds.y = window_bounds.height - bounds.height - kInsets;
|
|
|
|
} else {
|
|
|
|
bounds.y = kInsets;
|
|
|
|
}
|
|
|
|
|
2021-08-28 03:55:15 +02:00
|
|
|
if (bounds.width < kLocationBarPadding * 2) {
|
|
|
|
// Not enough space.
|
|
|
|
location_controller_->SetVisible(false);
|
|
|
|
} else {
|
|
|
|
location_bar_->SetSize(CefSize(bounds.width, bounds.height));
|
|
|
|
location_controller_->SetBounds(bounds);
|
|
|
|
location_controller_->SetVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
MaybeUpdateMaximizeButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewsOverlayControls::UpdateDraggableRegions(
|
|
|
|
std::vector<CefDraggableRegion>& window_regions) {
|
|
|
|
if (panel_controller_ && panel_controller_->IsVisible()) {
|
2024-01-20 03:22:56 +01:00
|
|
|
window_regions.emplace_back(panel_controller_->GetBounds(),
|
|
|
|
/*draggable=*/false);
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (menu_controller_ && menu_controller_->IsVisible()) {
|
2024-01-20 03:22:56 +01:00
|
|
|
window_regions.emplace_back(menu_controller_->GetBounds(),
|
|
|
|
/*draggable=*/false);
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (location_controller_ && location_controller_->IsVisible()) {
|
2024-01-20 03:22:56 +01:00
|
|
|
window_regions.emplace_back(location_controller_->GetBounds(),
|
|
|
|
/*draggable=*/false);
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewsOverlayControls::OnButtonPressed(CefRefPtr<CefButton> button) {
|
|
|
|
auto command = static_cast<Command>(button->GetID());
|
|
|
|
switch (command) {
|
|
|
|
case ViewsOverlayControls::Command::kMinimize:
|
|
|
|
window_->Minimize();
|
|
|
|
break;
|
|
|
|
case ViewsOverlayControls::Command::kMaximize:
|
2023-01-02 23:59:03 +01:00
|
|
|
if (window_->IsMaximized()) {
|
2021-08-28 03:55:15 +02:00
|
|
|
window_->Restore();
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2021-08-28 03:55:15 +02:00
|
|
|
window_->Maximize();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
break;
|
|
|
|
case ViewsOverlayControls::Command::kClose:
|
|
|
|
window_->Close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly reset button state because the button may have moved and it
|
|
|
|
// won't receive the corresponding mouse move events.
|
|
|
|
button->SetState(CEF_BUTTON_STATE_NORMAL);
|
|
|
|
button->SetInkDropEnabled(false);
|
|
|
|
button->SetInkDropEnabled(true);
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (command == Command::kMaximize) {
|
2021-08-28 03:55:15 +02:00
|
|
|
MaybeUpdateMaximizeButton();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
|
|
|
|
2024-03-23 00:51:00 +01:00
|
|
|
void ViewsOverlayControls::OnThemeChanged(CefRefPtr<CefView> view) {
|
|
|
|
// Apply colors when the theme changes.
|
2024-03-29 17:48:33 +01:00
|
|
|
views_style::OnThemeChanged(view);
|
2024-03-23 00:51:00 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 03:55:15 +02:00
|
|
|
CefRefPtr<CefLabelButton> ViewsOverlayControls::CreateButton(Command command) {
|
|
|
|
CefRefPtr<CefLabelButton> button = CefLabelButton::CreateLabelButton(
|
|
|
|
this, GetLabel(command, window_maximized_));
|
|
|
|
button->SetID(static_cast<int>(command));
|
|
|
|
button->SetInkDropEnabled(true);
|
|
|
|
button->SetFocusable(false); // Don't give focus to the button.
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ViewsOverlayControls::MaybeUpdateMaximizeButton() {
|
2023-02-28 19:34:12 +01:00
|
|
|
if (!with_window_buttons_ || window_->IsMaximized() == window_maximized_) {
|
2021-08-28 03:55:15 +02:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
window_maximized_ = !window_maximized_;
|
|
|
|
|
|
|
|
auto max_button = panel_->GetChildViewAt(1);
|
|
|
|
auto command = static_cast<Command>(max_button->GetID());
|
|
|
|
DCHECK(command == Command::kMaximize);
|
|
|
|
max_button->AsButton()->AsLabelButton()->SetText(
|
|
|
|
GetLabel(command, window_maximized_));
|
|
|
|
|
|
|
|
// Adjust overlay size and placement due to layout changing.
|
|
|
|
panel_controller_->SizeToPreferredSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace client
|