2014-05-22 23:01:22 +02:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2015-11-17 19:20:13 +01:00
|
|
|
#include "libcef/browser/native/window_delegate_view.h"
|
2014-05-22 23:01:22 +02:00
|
|
|
|
2018-02-15 01:12:09 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2014-05-22 23:01:22 +02:00
|
|
|
#include "content/public/browser/web_contents.h"
|
|
|
|
#include "ui/views/background.h"
|
|
|
|
#include "ui/views/controls/webview/webview.h"
|
|
|
|
#include "ui/views/layout/fill_layout.h"
|
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
|
2019-07-17 20:47:27 +02:00
|
|
|
CefWindowDelegateView::CefWindowDelegateView(
|
|
|
|
SkColor background_color,
|
|
|
|
bool always_on_top,
|
2023-02-07 00:43:00 +01:00
|
|
|
base::RepeatingClosure on_bounds_changed,
|
|
|
|
base::OnceClosure on_delete)
|
2018-02-28 05:47:36 +01:00
|
|
|
: background_color_(background_color),
|
2019-07-17 20:47:27 +02:00
|
|
|
always_on_top_(always_on_top),
|
2023-02-07 00:43:00 +01:00
|
|
|
on_bounds_changed_(std::move(on_bounds_changed)),
|
|
|
|
on_delete_(std::move(on_delete)) {}
|
2014-05-22 23:01:22 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void CefWindowDelegateView::Init(gfx::AcceleratedWidget parent_widget,
|
|
|
|
content::WebContents* web_contents,
|
|
|
|
const gfx::Rect& bounds) {
|
2014-05-22 23:01:22 +02:00
|
|
|
DCHECK(!web_view_);
|
|
|
|
web_view_ = new views::WebView(web_contents->GetBrowserContext());
|
|
|
|
web_view_->SetWebContents(web_contents);
|
|
|
|
web_view_->SetPreferredSize(bounds.size());
|
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
SetCanResize(true);
|
|
|
|
|
2014-05-22 23:01:22 +02:00
|
|
|
views::Widget* widget = new views::Widget;
|
|
|
|
|
|
|
|
// See CalculateWindowStylesFromInitParams in
|
|
|
|
// ui/views/widget/widget_hwnd_utils.cc for the conversion of |params| to
|
|
|
|
// Windows style flags.
|
|
|
|
views::Widget::InitParams params;
|
|
|
|
params.parent_widget = parent_widget;
|
|
|
|
params.bounds = bounds;
|
|
|
|
params.delegate = this;
|
|
|
|
// Set the WS_CHILD flag.
|
|
|
|
params.child = true;
|
|
|
|
// Set the WS_VISIBLE flag.
|
|
|
|
params.type = views::Widget::InitParams::TYPE_CONTROL;
|
|
|
|
// Don't set the WS_EX_COMPOSITED flag.
|
2020-02-10 18:10:17 +01:00
|
|
|
params.opacity = views::Widget::InitParams::WindowOpacity::kOpaque;
|
2014-11-15 02:19:52 +01:00
|
|
|
// Tell Aura not to draw the window frame on resize.
|
|
|
|
params.remove_standard_frame = true;
|
2015-08-27 23:55:48 +02:00
|
|
|
// Cause WidgetDelegate::CanActivate to return true. See comments in
|
2020-09-22 21:54:02 +02:00
|
|
|
// AlloyBrowserHostImpl::PlatformSetFocus.
|
2021-06-04 03:34:56 +02:00
|
|
|
params.activatable = views::Widget::InitParams::Activatable::kYes;
|
2014-05-22 23:01:22 +02:00
|
|
|
|
2019-09-04 17:13:32 +02:00
|
|
|
params.z_order = always_on_top_ ? ui::ZOrderLevel::kFloatingWindow
|
|
|
|
: ui::ZOrderLevel::kNormal;
|
2018-02-28 05:47:36 +01:00
|
|
|
|
2014-05-22 23:01:22 +02:00
|
|
|
// Results in a call to InitContent().
|
2019-10-01 15:55:16 +02:00
|
|
|
widget->Init(std::move(params));
|
2014-05-22 23:01:22 +02:00
|
|
|
|
|
|
|
// |widget| should now be associated with |this|.
|
|
|
|
DCHECK_EQ(widget, GetWidget());
|
2014-06-12 22:28:58 +02:00
|
|
|
// |widget| must be top-level for focus handling to work correctly.
|
|
|
|
DCHECK(widget->is_top_level());
|
2015-08-27 23:55:48 +02:00
|
|
|
// |widget| must be activatable for focus handling to work correctly.
|
|
|
|
DCHECK(widget->widget_delegate()->CanActivate());
|
2023-02-07 00:43:00 +01:00
|
|
|
|
|
|
|
// WidgetDelegate::DeleteDelegate() will execute the registered callback.
|
|
|
|
RegisterDeleteDelegateCallback(base::BindOnce(
|
|
|
|
&CefWindowDelegateView::DeleteDelegate, base::Unretained(this)));
|
2014-05-22 23:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CefWindowDelegateView::InitContent() {
|
2017-07-27 01:19:27 +02:00
|
|
|
SetBackground(views::CreateSolidBackground(background_color_));
|
2018-02-15 01:12:09 +01:00
|
|
|
SetLayoutManager(std::make_unique<views::FillLayout>());
|
2014-05-22 23:01:22 +02:00
|
|
|
AddChildView(web_view_);
|
|
|
|
}
|
|
|
|
|
2023-02-07 00:43:00 +01:00
|
|
|
void CefWindowDelegateView::DeleteDelegate() {
|
|
|
|
if (!on_delete_.is_null()) {
|
|
|
|
std::move(on_delete_).Run();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 23:01:22 +02:00
|
|
|
void CefWindowDelegateView::ViewHierarchyChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (details.is_add && details.child == this) {
|
2014-05-22 23:01:22 +02:00
|
|
|
InitContent();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2014-05-22 23:01:22 +02:00
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
|
|
|
|
void CefWindowDelegateView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
|
|
|
|
views::WidgetDelegateView::OnBoundsChanged(previous_bounds);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!on_bounds_changed_.is_null()) {
|
2019-07-17 20:47:27 +02:00
|
|
|
on_bounds_changed_.Run();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-17 20:47:27 +02:00
|
|
|
}
|