#include "ui/views/controls/native/native_view_host.h"
#include <memory>
#include <utility>
#include "base/check.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/canvas.h"
#include "ui/views/controls/native/native_view_host_wrapper.h"
#include "ui/views/painter.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
namespace views {
const char kWidgetNativeViewHostKey[] = "WidgetNativeViewHost";
NativeViewHost::NativeViewHost() {
set_suppress_default_focus_handling();
}
NativeViewHost::~NativeViewHost() {
ClearFocus();
}
void NativeViewHost::Attach(gfx::NativeView native_view) {
DCHECK(native_view);
DCHECK(!native_view_);
native_view_ = native_view;
native_wrapper_->AttachNativeView();
InvalidateLayout();
if (!IsDrawn())
native_wrapper_->HideWidget();
Widget* widget = Widget::GetWidgetForNativeView(native_view);
if (widget)
widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, this);
}
void NativeViewHost::Detach() {
Detach(false);
}
void NativeViewHost::SetParentAccessible(gfx::NativeViewAccessible accessible) {
if (!native_wrapper_)
return;
native_wrapper_->SetParentAccessible(accessible);
}
gfx::NativeViewAccessible NativeViewHost::GetParentAccessible() {
if (!native_wrapper_)
return nullptr;
return native_wrapper_->GetParentAccessible();
}
bool NativeViewHost::SetCornerRadii(const gfx::RoundedCornersF& corner_radii) {
return native_wrapper_->SetCornerRadii(corner_radii);
}
bool NativeViewHost::SetCustomMask(std::unique_ptr<ui::LayerOwner> mask) {
DCHECK(native_wrapper_);
return native_wrapper_->SetCustomMask(std::move(mask));
}
void NativeViewHost::SetHitTestTopInset(int top_inset) {
native_wrapper_->SetHitTestTopInset(top_inset);
}
int NativeViewHost::GetHitTestTopInset() const {
return native_wrapper_->GetHitTestTopInset();
}
void NativeViewHost::SetNativeViewSize(const gfx::Size& size) {
if (native_view_size_ == size)
return;
native_view_size_ = size;
InvalidateLayout();
}
gfx::NativeView NativeViewHost::GetNativeViewContainer() const {
return native_view_ ? native_wrapper_->GetNativeViewContainer() : nullptr;
}
void NativeViewHost::NativeViewDestroyed() {
Detach(true);
}
void NativeViewHost::SetBackgroundColorWhenClipped(
absl::optional<SkColor> color) {
background_color_when_clipped_ = color;
}
void NativeViewHost::Layout() {
if (!native_view_ || !native_wrapper_.get())
return;
gfx::Rect vis_bounds = GetVisibleBounds();
bool visible = !vis_bounds.IsEmpty();
if (visible && !fast_resize_) {
if (vis_bounds.size() != size()) {
int x = vis_bounds.x();
int y = vis_bounds.y();
native_wrapper_->InstallClip(x, y, vis_bounds.width(),
vis_bounds.height());
} else if (native_wrapper_->HasInstalledClip()) {
native_wrapper_->UninstallClip();
}
}
if (visible) {
gfx::Rect local_bounds = ConvertRectToWidget(GetContentsBounds());
gfx::Size native_view_size =
native_view_size_.IsEmpty() ? local_bounds.size() : native_view_size_;
native_wrapper_->ShowWidget(local_bounds.x(), local_bounds.y(),
local_bounds.width(), local_bounds.height(),
native_view_size.width(),
native_view_size.height());
} else {
native_wrapper_->HideWidget();
}
}
void NativeViewHost::OnPaint(gfx::Canvas* canvas) {
OnPaintBackground(canvas);
if (native_wrapper_->HasInstalledClip()) {
if (background_color_when_clipped_) {
canvas->FillRect(GetLocalBounds(), *background_color_when_clipped_);
}
}
}
void NativeViewHost::VisibilityChanged(View* starting_from, bool is_visible) {
Layout();
}
bool NativeViewHost::GetNeedsNotificationWhenVisibleBoundsChange() const {
return true;
}
void NativeViewHost::OnVisibleBoundsChanged() {
InvalidateLayout();
}
void NativeViewHost::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
views::Widget* this_widget = GetWidget();
if (details.move_view && this_widget &&
details.move_view->GetWidget() == this_widget) {
return;
}
if (details.is_add && this_widget) {
if (!native_wrapper_.get())
native_wrapper_.reset(NativeViewHostWrapper::CreateWrapper(this));
native_wrapper_->AddedToWidget();
} else if (!details.is_add && native_wrapper_) {
native_wrapper_->RemovedFromWidget();
}
}
void NativeViewHost::OnFocus() {
if (native_view_)
native_wrapper_->SetFocus();
NotifyAccessibilityEvent(ax::mojom::Event::kFocus, true);
}
gfx::NativeViewAccessible NativeViewHost::GetNativeViewAccessible() {
if (native_wrapper_.get()) {
gfx::NativeViewAccessible accessible_view =
native_wrapper_->GetNativeViewAccessible();
if (accessible_view)
return accessible_view;
}
return View::GetNativeViewAccessible();
}
ui::Cursor NativeViewHost::GetCursor(const ui::MouseEvent& event) {
return native_wrapper_->GetCursor(event.x(), event.y());
}
void NativeViewHost::SetVisible(bool visible) {
if (native_view_)
native_wrapper_->SetVisible(visible);
View::SetVisible(visible);
}
bool NativeViewHost::OnMousePressed(const ui::MouseEvent& event) {
return View::OnMousePressed(event);
}
void NativeViewHost::Detach(bool destroyed) {
if (native_view_) {
if (!destroyed) {
Widget* widget = Widget::GetWidgetForNativeView(native_view_);
if (widget)
widget->SetNativeWindowProperty(kWidgetNativeViewHostKey, nullptr);
ClearFocus();
}
native_wrapper_->NativeViewDetaching(destroyed);
native_view_ = nullptr;
}
}
void NativeViewHost::ClearFocus() {
FocusManager* focus_manager = GetFocusManager();
if (!focus_manager || !focus_manager->GetFocusedView())
return;
Widget::Widgets widgets;
Widget::GetAllChildWidgets(native_view(), &widgets);
for (auto* widget : widgets) {
focus_manager->ViewRemoved(widget->GetRootView());
if (!focus_manager->GetFocusedView())
return;
}
}
BEGIN_METADATA(NativeViewHost, View)
END_METADATA
}