#include "ui/views/widget/native_widget_aura.h"
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/notimplemented.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/single_thread_task_runner.h"
#include "base/uuid.h"
#include "build/build_config.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_delegate.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/client/screen_position_client.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/client/window_types.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/class_property.h"
#include "ui/base/data_transfer_policy/data_transfer_endpoint.h"
#include "ui/base/dragdrop/os_exchange_data.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/base/ui_base_types.h"
#include "ui/color/color_provider_key.h"
#include "ui/compositor/layer.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/native_theme/native_theme_aura.h"
#include "ui/views/buildflags.h"
#include "ui/views/drag_utils.h"
#include "ui/views/views_delegate.h"
#include "ui/views/views_features.h"
#include "ui/views/widget/drop_helper.h"
#include "ui/views/widget/focus_manager_event_handler.h"
#include "ui/views/widget/native_widget_delegate.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/tooltip_manager_aura.h"
#include "ui/views/widget/widget_aura_utils.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/window_reorderer.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/core/scoped_animation_disabler.h"
#include "ui/wm/core/shadow_types.h"
#include "ui/wm/core/transient_window_manager.h"
#include "ui/wm/core/window_animations.h"
#include "ui/wm/core/window_properties.h"
#include "ui/wm/core/window_util.h"
#include "ui/wm/public/activation_client.h"
#include "ui/wm/public/window_move_client.h"
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#include "ui/views/widget/desktop_aura/desktop_window_tree_host.h"
#endif
#if BUILDFLAG(ENABLE_DESKTOP_AURA) && BUILDFLAG(IS_OZONE)
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_platform.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "ui/views/widget/desktop_aura/desktop_window_tree_host_win.h"
#endif
DEFINE_UI_CLASS_PROPERTY_TYPE(views::internal::NativeWidgetPrivate*)
namespace views {
namespace {
DEFINE_UI_CLASS_PROPERTY_KEY(internal::NativeWidgetPrivate*,
kNativeWidgetPrivateKey,
nullptr)
void SetRestoreBounds(aura::Window* window, const gfx::Rect& bounds) {
window->SetProperty(aura::client::kRestoreBoundsKey, bounds);
}
void SetIcon(aura::Window* window,
const aura::WindowProperty<gfx::ImageSkia*>* key,
const gfx::ImageSkia& value) {
if (value.isNull()) {
window->ClearProperty(key);
} else {
window->SetProperty(key, value);
}
}
bool FindLayersInOrder(
const std::vector<raw_ptr<ui::Layer, VectorExperimental>>& children,
const ui::Layer** first,
const ui::Layer** second) {
for (const ui::Layer* child : children) {
if (child == *second) {
*second = nullptr;
return *first == nullptr;
}
if (child == *first) {
*first = nullptr;
}
if (FindLayersInOrder(child->children(), first, second)) {
return true;
}
if (!*second) {
return false;
}
}
return false;
}
void ReparentAuraWindow(aura::Window* window, aura::Window* parent) {
if (parent) {
parent->AddChild(window);
} else {
aura::Window* root_window = window->GetRootWindow();
aura::client::ParentWindowWithContext(window, root_window,
root_window->GetBoundsInScreen(),
display::kInvalidDisplayId);
}
}
}
NativeWidgetAura::NativeWidgetAura(internal::NativeWidgetDelegate* delegate)
: delegate_(delegate->AsWidget()->GetWeakPtr()),
window_(new aura::Window(this, aura::client::WINDOW_TYPE_UNKNOWN)) {
aura::client::SetFocusChangeObserver(window_, this);
wm::SetActivationChangeObserver(window_, this);
}
void NativeWidgetAura::RegisterNativeWidgetForWindow(
internal::NativeWidgetPrivate* native_widget,
aura::Window* window) {
window->SetProperty(kNativeWidgetPrivateKey, native_widget);
}
void NativeWidgetAura::AssignIconToAuraWindow(aura::Window* window,
const gfx::ImageSkia& window_icon,
const gfx::ImageSkia& app_icon) {
if (window) {
SetIcon(window, aura::client::kWindowIconKey, window_icon);
SetIcon(window, aura::client::kAppIconKey, app_icon);
}
}
void NativeWidgetAura::SetShadowElevationFromInitParams(
aura::Window* window,
const Widget::InitParams& params) {
if (params.shadow_type == Widget::InitParams::ShadowType::kNone) {
wm::SetShadowElevation(window, wm::kShadowElevationNone);
} else if (params.shadow_type == Widget::InitParams::ShadowType::kDrop &&
params.shadow_elevation) {
wm::SetShadowElevation(window, *params.shadow_elevation);
}
}
void NativeWidgetAura::SetResizeBehaviorFromDelegate(WidgetDelegate* delegate,
aura::Window* window) {
if (!window) {
return;
}
int behavior = aura::client::kResizeBehaviorNone;
if (delegate) {
if (delegate->CanResize()) {
behavior |= aura::client::kResizeBehaviorCanResize;
}
if (delegate->CanMaximize()) {
behavior |= aura::client::kResizeBehaviorCanMaximize;
}
if (delegate->CanMinimize()) {
behavior |= aura::client::kResizeBehaviorCanMinimize;
}
if (delegate->CanFullscreen()) {
behavior |= aura::client::kResizeBehaviorCanFullscreen;
}
}
window->SetProperty(aura::client::kResizeBehaviorKey, behavior);
}
void NativeWidgetAura::InitNativeWidget(Widget::InitParams params) {
DCHECK(params.parent || params.context);
ownership_ = params.ownership;
if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
owned_delegate_ = base::WrapUnique(delegate_.get());
}
window_->AcquireAllPropertiesFrom(
std::move(params.init_properties_container));
RegisterNativeWidgetForWindow(this, window_);
window_->SetType(GetAuraWindowTypeForWidgetType(params.type));
if (params.rounded_corners) {
window_->SetProperty(aura::client::kWindowRoundedCornersKey,
params.rounded_corners.value());
}
window_->SetProperty(aura::client::kShowStateKey, params.show_state);
window_->SetProperty(aura::client::kRemoveStandardFrame,
params.remove_standard_frame);
int desk_index;
if (params.visible_on_all_workspaces) {
window_->SetProperty(aura::client::kWindowWorkspaceKey,
aura::client::kWindowWorkspaceVisibleOnAllWorkspaces);
} else if (const base::Uuid& desk_uuid =
base::Uuid::ParseLowercase(params.workspace);
desk_uuid.is_valid()) {
window_->SetProperty(aura::client::kDeskUuidKey,
desk_uuid.AsLowercaseString());
} else if (base::StringToInt(params.workspace, &desk_index)) {
window_->SetProperty(aura::client::kWindowWorkspaceKey, desk_index);
}
if (params.type == Widget::InitParams::TYPE_BUBBLE) {
wm::SetHideOnDeactivate(window_, true);
}
SetShadowElevationFromInitParams(window_, params);
window_->Init(params.layer_type);
window_->SetTransparent(params.opacity ==
Widget::InitParams::WindowOpacity::kTranslucent);
window_->SetName(params.name.empty() ? "NativeWidgetAura" : params.name);
if (params.type == Widget::InitParams::TYPE_CONTROL) {
window_->Show();
}
delegate_->OnNativeWidgetCreated();
gfx::Rect window_bounds = params.bounds;
gfx::NativeView parent = params.parent;
gfx::NativeView context = params.context;
if (!params.child) {
wm::TransientWindowManager::GetOrCreate(window_)->AddObserver(this);
if (parent && parent->GetType() != aura::client::WINDOW_TYPE_UNKNOWN) {
wm::AddTransientChild(parent, window_);
if (!context) {
context = parent;
}
parent = nullptr;
if (params.type == Widget::InitParams::TYPE_BUBBLE) {
wm::TransientWindowManager::GetOrCreate(window_)
->set_parent_controls_visibility(true);
}
}
SetZOrderLevel(params.EffectiveZOrderLevel());
aura::Window* parent_or_context = parent ? parent : context;
if (parent_or_context && window_bounds == gfx::Rect()) {
gfx::Rect bounds = display::Screen::Get()
->GetDisplayNearestWindow(parent_or_context)
.bounds();
window_bounds.set_origin(bounds.origin());
}
}
OnSizeConstraintsChanged();
std::optional<int64_t> target_display;
#if BUILDFLAG(IS_CHROMEOS)
target_display = params.display_id;
#endif
if (parent) {
parent->AddChild(window_);
} else {
aura::client::ParentWindowWithContext(
window_, context->GetRootWindow(), window_bounds,
target_display.value_or(display::kInvalidDisplayId));
}
window_->AddObserver(this);
if (IsMaximized() || IsMinimized()) {
SetRestoreBounds(window_, window_bounds);
} else {
SetBoundsInternal(window_bounds, target_display);
}
window_->SetEventTargetingPolicy(
params.accept_events ? aura::EventTargetingPolicy::kTargetAndDescendants
: aura::EventTargetingPolicy::kNone);
DCHECK(GetWidget()->GetRootView());
if (params.type != Widget::InitParams::TYPE_TOOLTIP) {
tooltip_manager_ = std::make_unique<views::TooltipManagerAura>(this);
}
drop_helper_ = std::make_unique<DropHelper>(GetWidget()->GetRootView());
if (params.type != Widget::InitParams::TYPE_TOOLTIP &&
params.type != Widget::InitParams::TYPE_POPUP) {
aura::client::SetDragDropDelegate(window_, this);
}
if (params.type == Widget::InitParams::TYPE_WINDOW) {
focus_manager_event_handler_ =
std::make_unique<FocusManagerEventHandler>(GetWidget(), window_);
}
wm::SetActivationDelegate(window_, this);
window_reorderer_ =
std::make_unique<WindowReorderer>(window_, GetWidget()->GetRootView());
}
void NativeWidgetAura::OnWidgetInitDone() {}
void NativeWidgetAura::ReparentNativeViewImpl(gfx::NativeView new_parent) {
ReparentAuraWindow(GetNativeView(), new_parent);
}
std::unique_ptr<FrameView> NativeWidgetAura::CreateFrameView() {
return nullptr;
}
bool NativeWidgetAura::ShouldUseNativeFrame() const {
return false;
}
bool NativeWidgetAura::ShouldWindowContentsBeTransparent() const {
return false;
}
void NativeWidgetAura::FrameTypeChanged() {
GetWidget()->ThemeChanged();
GetWidget()->GetRootView()->SchedulePaint();
}
Widget* NativeWidgetAura::GetWidget() {
return delegate_ ? delegate_->AsWidget() : nullptr;
}
const Widget* NativeWidgetAura::GetWidget() const {
return delegate_ ? delegate_->AsWidget() : nullptr;
}
gfx::NativeView NativeWidgetAura::GetNativeView() const {
return window_;
}
gfx::NativeWindow NativeWidgetAura::GetNativeWindow() const {
return window_;
}
Widget* NativeWidgetAura::GetTopLevelWidget() {
NativeWidgetPrivate* native_widget = GetTopLevelNativeWidget(GetNativeView());
return native_widget ? native_widget->GetWidget() : nullptr;
}
const ui::Compositor* NativeWidgetAura::GetCompositor() const {
return window_ ? window_->layer()->GetCompositor() : nullptr;
}
const ui::Layer* NativeWidgetAura::GetLayer() const {
return window_ ? window_->layer() : nullptr;
}
void NativeWidgetAura::ReorderNativeViews() {
window_reorderer_->ReorderChildWindows();
}
void NativeWidgetAura::ViewRemoved(View* view) {
DCHECK(drop_helper_.get() != nullptr);
drop_helper_->ResetTargetViewIfEquals(view);
}
void NativeWidgetAura::SetNativeWindowProperty(const char* name, void* value) {
if (window_) {
window_->SetNativeWindowProperty(name, value);
}
}
void* NativeWidgetAura::GetNativeWindowProperty(const char* name) const {
return window_ ? window_->GetNativeWindowProperty(name) : nullptr;
}
TooltipManager* NativeWidgetAura::GetTooltipManager() const {
return tooltip_manager_.get();
}
void NativeWidgetAura::SetCapture() {
if (window_) {
window_->SetCapture();
}
}
void NativeWidgetAura::ReleaseCapture() {
if (window_) {
window_->ReleaseCapture();
}
}
bool NativeWidgetAura::HasCapture() const {
return window_ && window_->HasCapture();
}
ui::InputMethod* NativeWidgetAura::GetInputMethod() {
if (!window_) {
return nullptr;
}
aura::Window* root_window = window_->GetRootWindow();
return root_window ? root_window->GetHost()->GetInputMethod() : nullptr;
}
void NativeWidgetAura::CenterWindow(const gfx::Size& size) {
if (!window_) {
return;
}
gfx::Rect parent_bounds(window_->parent()->GetBoundsInRootWindow());
gfx::Rect work_area =
display::Screen::Get()->GetDisplayNearestWindow(window_).work_area();
aura::client::ScreenPositionClient* screen_position_client =
aura::client::GetScreenPositionClient(window_->GetRootWindow());
if (screen_position_client) {
gfx::Point origin = work_area.origin();
screen_position_client->ConvertPointFromScreen(window_->GetRootWindow(),
&origin);
work_area.set_origin(origin);
}
parent_bounds.Intersect(work_area);
gfx::Rect window_bounds = parent_bounds;
if (wm::GetTransientParent(window_)) {
auto* transient_parent = wm::GetTransientParent(window_);
gfx::Rect transient_parent_rect = transient_parent->GetBoundsInRootWindow();
if (transient_parent_rect.height() >= size.height() ||
transient_parent_rect.width() >= size.width()) {
int top_inset =
transient_parent->GetProperty(aura::client::kTopViewInset);
window_bounds = transient_parent_rect;
window_bounds.Inset(gfx::Insets().set_top(top_inset));
}
}
window_bounds.ToCenteredSize(size);
window_bounds.AdjustToFit(parent_bounds);
gfx::Point origin = window_bounds.origin();
aura::Window::ConvertPointToTarget(window_->GetRootWindow(),
window_->parent(), &origin);
window_bounds.set_origin(origin);
window_->SetBounds(window_bounds);
}
void NativeWidgetAura::GetWindowPlacement(
gfx::Rect* bounds,
ui::mojom::WindowShowState* show_state) const {
*bounds = GetRestoredBounds();
*show_state = window_ ? window_->GetProperty(aura::client::kShowStateKey)
: ui::mojom::WindowShowState::kDefault;
}
bool NativeWidgetAura::SetWindowTitle(const std::u16string& title) {
if (!window_) {
return false;
}
if (window_->GetTitle() == title) {
return false;
}
window_->SetTitle(title);
return true;
}
void NativeWidgetAura::SetWindowIcons(const gfx::ImageSkia& window_icon,
const gfx::ImageSkia& app_icon) {
AssignIconToAuraWindow(window_, window_icon, app_icon);
}
void NativeWidgetAura::InitModalType(ui::mojom::ModalType modal_type) {
if (modal_type != ui::mojom::ModalType::kNone) {
window_->SetProperty(aura::client::kModalKey, modal_type);
}
if (modal_type == ui::mojom::ModalType::kWindow) {
wm::TransientWindowManager::GetOrCreate(window_)
->set_parent_controls_visibility(true);
}
}
void NativeWidgetAura::SetBackgroundColor(SkColor background_color) {
}
gfx::Rect NativeWidgetAura::GetWindowBoundsInScreen() const {
return window_ ? window_->GetBoundsInScreen() : gfx::Rect();
}
gfx::Rect NativeWidgetAura::GetClientAreaBoundsInScreen() const {
return window_ ? window_->GetBoundsInScreen() : gfx::Rect();
}
gfx::Rect NativeWidgetAura::GetRestoredBounds() const {
if (!window_) {
return gfx::Rect();
}
if (IsMinimized() || IsMaximized() || IsFullscreen()) {
gfx::Rect* restore_bounds =
window_->GetProperty(aura::client::kRestoreBoundsKey);
if (restore_bounds) {
return *restore_bounds;
}
}
auto* screen_position_client =
aura::client::GetScreenPositionClient(window_->GetRootWindow());
if (screen_position_client) {
gfx::Rect bounds = window_->bounds();
gfx::Point origin = bounds.origin();
screen_position_client->ConvertPointToScreenIgnoringTransforms(
window_->parent(), &origin);
return gfx::Rect(origin, bounds.size());
}
return window_->GetBoundsInScreen();
}
std::string NativeWidgetAura::GetWorkspace() const {
if (!window_) {
return std::string();
}
const int desk_index =
window_->GetProperty(aura::client::kWindowWorkspaceKey);
if (desk_index == aura::client::kWindowWorkspaceUnassignedWorkspace ||
desk_index == aura::client::kWindowWorkspaceVisibleOnAllWorkspaces) {
return std::string();
}
return base::NumberToString(desk_index);
}
void NativeWidgetAura::SetBounds(const gfx::Rect& bounds) {
if (!window_) {
return;
}
SetBoundsInternal(bounds, std::nullopt);
}
void NativeWidgetAura::SetBoundsInternal(const gfx::Rect& bounds,
std::optional<int64_t> display_id) {
display::Display dst_display;
auto* screen = display::Screen::Get();
if (!display_id ||
!screen->GetDisplayWithDisplayId(display_id.value(), &dst_display)) {
dst_display = screen->GetDisplayMatching(bounds);
}
#if BUILDFLAG(IS_CHROMEOS)
CHECK(dst_display.is_valid());
#endif
window_->SetBoundsInScreen(bounds, dst_display);
}
void NativeWidgetAura::SetBoundsConstrained(const gfx::Rect& bounds) {
if (!window_) {
return;
}
gfx::Rect new_bounds(bounds);
if (window_->parent()) {
if (window_->parent()->GetProperty(wm::kUsesScreenCoordinatesKey)) {
new_bounds =
NativeWidgetPrivate::ConstrainBoundsToDisplayWorkArea(new_bounds);
} else {
new_bounds.AdjustToFit(gfx::Rect(window_->parent()->bounds().size()));
}
}
SetBounds(new_bounds);
}
void NativeWidgetAura::SetSize(const gfx::Size& size) {
if (window_) {
window_->SetBounds(gfx::Rect(window_->bounds().origin(), size));
}
}
void NativeWidgetAura::StackAbove(gfx::NativeView native_view) {
if (window_ && window_->parent() &&
window_->parent() == native_view->parent()) {
window_->parent()->StackChildAbove(window_, native_view);
}
}
void NativeWidgetAura::StackAtTop() {
if (window_) {
window_->parent()->StackChildAtTop(window_);
}
}
bool NativeWidgetAura::IsStackedAbove(gfx::NativeView native_view) {
if (!window_) {
return false;
}
if (GetNativeWindow()->GetRootWindow() != native_view->GetRootWindow()) {
return GetTopLevelWidget()->IsStackedAbove(
native_view->GetToplevelWindow());
}
const ui::Layer* first = native_view->layer();
const ui::Layer* second = GetWidget()->GetLayer();
return FindLayersInOrder(
GetNativeWindow()->GetRootWindow()->layer()->children(), &first, &second);
}
void NativeWidgetAura::SetShape(std::unique_ptr<Widget::ShapeRects> shape) {
if (window_) {
window_->layer()->SetAlphaShape(std::move(shape));
}
}
void NativeWidgetAura::Close() {
DCHECK(window_ ||
ownership_ == Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET ||
ownership_ == Widget::InitParams::CLIENT_OWNS_WIDGET);
if (window_) {
Hide();
window_->SetProperty(aura::client::kModalKey, ui::mojom::ModalType::kNone);
}
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&NativeWidgetAura::CloseNow, weak_factory.GetWeakPtr()));
}
void NativeWidgetAura::CloseNow() {
if (window_) {
delete window_;
}
}
void NativeWidgetAura::Show(ui::mojom::WindowShowState show_state,
const gfx::Rect& restore_bounds) {
if (!window_) {
return;
}
if ((show_state == ui::mojom::WindowShowState::kMaximized ||
show_state == ui::mojom::WindowShowState::kMinimized) &&
!restore_bounds.IsEmpty()) {
SetRestoreBounds(window_, restore_bounds);
}
if (show_state == ui::mojom::WindowShowState::kMaximized ||
show_state == ui::mojom::WindowShowState::kFullscreen) {
window_->SetProperty(aura::client::kShowStateKey, show_state);
}
std::optional<wm::ScopedAnimationDisabler> disabler;
if (show_state == ui::mojom::WindowShowState::kMinimized) {
disabler.emplace(window_);
}
window_->Show();
if (delegate_->CanActivate()) {
if (show_state != ui::mojom::WindowShowState::kInactive) {
Activate();
}
SetInitialFocus(IsActive() ? show_state
: ui::mojom::WindowShowState::kInactive);
}
if (show_state == ui::mojom::WindowShowState::kMinimized) {
Minimize();
}
}
void NativeWidgetAura::Hide() {
if (window_) {
window_->Hide();
}
}
bool NativeWidgetAura::IsVisible() const {
return window_ && window_->IsVisible();
}
bool NativeWidgetAura::IsVisibleOnScreen() const {
return IsVisible();
}
void NativeWidgetAura::Activate() {
if (!window_) {
return;
}
if (window_->GetRootWindow()) {
wm::GetActivationClient(window_->GetRootWindow())->ActivateWindow(window_);
}
if (window_->GetProperty(aura::client::kDrawAttentionKey)) {
window_->SetProperty(aura::client::kDrawAttentionKey, false);
}
}
void NativeWidgetAura::Deactivate() {
if (!window_) {
return;
}
wm::GetActivationClient(window_->GetRootWindow())->DeactivateWindow(window_);
}
bool NativeWidgetAura::IsActive() const {
return window_ && wm::IsActiveWindow(window_);
}
void NativeWidgetAura::SetZOrderLevel(ui::ZOrderLevel order) {
if (window_) {
window_->SetProperty(aura::client::kZOrderingKey, order);
}
}
ui::ZOrderLevel NativeWidgetAura::GetZOrderLevel() const {
if (window_) {
return window_->GetProperty(aura::client::kZOrderingKey);
}
return ui::ZOrderLevel::kNormal;
}
void NativeWidgetAura::SetVisibleOnAllWorkspaces(bool always_visible) {
window_->SetProperty(
aura::client::kWindowWorkspaceKey,
always_visible ? aura::client::kWindowWorkspaceVisibleOnAllWorkspaces
: aura::client::kWindowWorkspaceUnassignedWorkspace);
}
bool NativeWidgetAura::IsVisibleOnAllWorkspaces() const {
return window_ && window_->GetProperty(aura::client::kWindowWorkspaceKey) ==
aura::client::kWindowWorkspaceVisibleOnAllWorkspaces;
}
void NativeWidgetAura::Maximize() {
if (window_) {
window_->SetProperty(aura::client::kShowStateKey,
ui::mojom::WindowShowState::kMaximized);
}
}
void NativeWidgetAura::Minimize() {
if (window_) {
window_->SetProperty(aura::client::kShowStateKey,
ui::mojom::WindowShowState::kMinimized);
}
}
bool NativeWidgetAura::IsMaximized() const {
return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
ui::mojom::WindowShowState::kMaximized;
}
bool NativeWidgetAura::IsMinimized() const {
return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
ui::mojom::WindowShowState::kMinimized;
}
void NativeWidgetAura::Restore() {
if (window_) {
wm::Restore(window_);
}
}
void NativeWidgetAura::SetFullscreen(bool fullscreen,
int64_t target_display_id) {
if (!window_) {
return;
}
wm::SetWindowFullscreen(window_, fullscreen, target_display_id);
}
bool NativeWidgetAura::IsFullscreen() const {
return window_ && window_->GetProperty(aura::client::kShowStateKey) ==
ui::mojom::WindowShowState::kFullscreen;
}
void NativeWidgetAura::SetCanAppearInExistingFullscreenSpaces(
bool can_appear_in_existing_fullscreen_spaces) {}
void NativeWidgetAura::SetOpacity(float opacity) {
if (window_) {
window_->layer()->SetOpacity(opacity);
}
}
void NativeWidgetAura::SetAspectRatio(const gfx::SizeF& aspect_ratio,
const gfx::Size& excluded_margin) {
DCHECK(!aspect_ratio.IsEmpty());
if (excluded_margin.width() > 0 || excluded_margin.height() > 0) {
NOTIMPLEMENTED_LOG_ONCE();
}
if (window_) {
window_->SetProperty(aura::client::kAspectRatio, aspect_ratio);
}
}
void NativeWidgetAura::FlashFrame(bool flash) {
if (window_) {
window_->SetProperty(aura::client::kDrawAttentionKey, flash);
}
}
void NativeWidgetAura::RunShellDrag(std::unique_ptr<ui::OSExchangeData> data,
const gfx::Point& location,
int operation,
ui::mojom::DragEventSource source) {
if (window_) {
views::RunShellDrag(window_, std::move(data), location, operation, source);
}
}
void NativeWidgetAura::CancelShellDrag(View* view) {
if (window_) {
views::CancelShellDrag(window_);
}
}
void NativeWidgetAura::SchedulePaintInRect(const gfx::Rect& rect) {
if (window_) {
window_->SchedulePaintInRect(rect);
}
}
void NativeWidgetAura::ScheduleLayout() {
if (window_) {
window_->ScheduleDraw();
}
}
void NativeWidgetAura::SetCursor(const ui::Cursor& cursor) {
cursor_ = cursor;
aura::client::CursorClient* cursor_client =
aura::client::GetCursorClient(window_->GetRootWindow());
if (cursor_client) {
cursor_client->SetCursor(cursor);
}
}
bool NativeWidgetAura::IsMouseEventsEnabled() const {
if (!window_) {
return false;
}
aura::client::CursorClient* cursor_client =
aura::client::GetCursorClient(window_->GetRootWindow());
return cursor_client ? cursor_client->IsMouseEventsEnabled() : true;
}
bool NativeWidgetAura::IsMouseButtonDown() const {
return aura::Env::GetInstance()->IsMouseButtonDown();
}
void NativeWidgetAura::ClearNativeFocus() {
aura::client::FocusClient* client = aura::client::GetFocusClient(window_);
if (window_ && client && window_->Contains(client->GetFocusedWindow())) {
client->ResetFocusWithinActiveWindow(window_);
}
}
gfx::Rect NativeWidgetAura::GetWorkAreaBoundsInScreen() const {
if (!window_) {
return gfx::Rect();
}
return display::Screen::Get()->GetDisplayNearestWindow(window_).work_area();
}
Widget::MoveLoopResult NativeWidgetAura::RunMoveLoop(
const gfx::Vector2d& drag_offset,
Widget::MoveLoopSource source,
Widget::MoveLoopEscapeBehavior escape_behavior) {
if (!window_ || !window_->GetRootWindow()) {
return Widget::MoveLoopResult::kCanceled;
}
wm::WindowMoveClient* move_client =
wm::GetWindowMoveClient(window_->GetRootWindow());
if (!move_client) {
return Widget::MoveLoopResult::kCanceled;
}
SetCapture();
wm::WindowMoveSource window_move_source =
source == Widget::MoveLoopSource::kMouse ? wm::WINDOW_MOVE_SOURCE_MOUSE
: wm::WINDOW_MOVE_SOURCE_TOUCH;
if (move_client->RunMoveLoop(window_, drag_offset, window_move_source) ==
wm::MOVE_SUCCESSFUL) {
return Widget::MoveLoopResult::kSuccessful;
}
return Widget::MoveLoopResult::kCanceled;
}
void NativeWidgetAura::EndMoveLoop() {
if (!window_ || !window_->GetRootWindow()) {
return;
}
wm::WindowMoveClient* move_client =
wm::GetWindowMoveClient(window_->GetRootWindow());
if (move_client) {
move_client->EndMoveLoop();
}
}
void NativeWidgetAura::SetVisibilityChangedAnimationsEnabled(bool value) {
if (window_) {
window_->SetProperty(aura::client::kAnimationsDisabledKey, !value);
}
}
void NativeWidgetAura::SetVisibilityAnimationDuration(
const base::TimeDelta& duration) {
wm::SetWindowVisibilityAnimationDuration(window_, duration);
}
void NativeWidgetAura::SetVisibilityAnimationTransition(
Widget::VisibilityTransition transition) {
wm::WindowVisibilityAnimationTransition wm_transition = wm::ANIMATE_NONE;
switch (transition) {
case Widget::ANIMATE_SHOW:
wm_transition = wm::ANIMATE_SHOW;
break;
case Widget::ANIMATE_HIDE:
wm_transition = wm::ANIMATE_HIDE;
break;
case Widget::ANIMATE_BOTH:
wm_transition = wm::ANIMATE_BOTH;
break;
case Widget::ANIMATE_NONE:
wm_transition = wm::ANIMATE_NONE;
break;
}
wm::SetWindowVisibilityAnimationTransition(window_, wm_transition);
}
ui::GestureRecognizer* NativeWidgetAura::GetGestureRecognizer() {
return aura::Env::GetInstance()->gesture_recognizer();
}
ui::GestureConsumer* NativeWidgetAura::GetGestureConsumer() {
return window_;
}
void NativeWidgetAura::OnSizeConstraintsChanged() {
SetResizeBehaviorFromDelegate(GetWidget()->widget_delegate(), window_);
}
void NativeWidgetAura::OnNativeViewHierarchyWillChange() {}
void NativeWidgetAura::OnNativeViewHierarchyChanged() {}
bool NativeWidgetAura::SetAllowScreenshots(bool allow) {
NOTIMPLEMENTED();
return false;
}
bool NativeWidgetAura::AreScreenshotsAllowed() {
NOTIMPLEMENTED();
return true;
}
bool NativeWidgetAura::IsDesktopNativeWidget() const {
return false;
}
std::string NativeWidgetAura::GetName() const {
return window_ ? window_->GetName() : std::string();
}
base::WeakPtr<internal::NativeWidgetPrivate> NativeWidgetAura::GetWeakPtr() {
return weak_factory.GetWeakPtr();
}
gfx::Size NativeWidgetAura::GetMinimumSize() const {
return delegate_ ? delegate_->GetMinimumSize() : gfx::Size();
}
std::optional<gfx::Size> NativeWidgetAura::GetMaximumSize() const {
const gfx::Size max_size =
delegate_ ? delegate_->GetMaximumSize() : gfx::Size();
return !max_size.IsZero() ? std::make_optional<gfx::Size>(max_size)
: std::nullopt;
}
void NativeWidgetAura::OnBoundsChanged(const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds) {
if (delegate_ &&
(old_bounds.origin() != new_bounds.origin() ||
(old_bounds == gfx::Rect(0, 0, 0, 0) && !new_bounds.IsEmpty()))) {
delegate_->OnNativeWidgetMove();
}
if (delegate_ && (old_bounds.size() != new_bounds.size())) {
delegate_->OnNativeWidgetSizeChanged(new_bounds.size());
}
}
gfx::NativeCursor NativeWidgetAura::GetCursor(const gfx::Point& point) {
return cursor_;
}
int NativeWidgetAura::GetNonClientComponent(const gfx::Point& point) const {
return delegate_ ? delegate_->GetNonClientComponent(point) : 0;
}
bool NativeWidgetAura::ShouldDescendIntoChildForEventHandling(
aura::Window* child,
const gfx::Point& location) {
return delegate_ ? delegate_->ShouldDescendIntoChildForEventHandling(
window_->layer(), child, child->layer(), location)
: false;
}
bool NativeWidgetAura::CanFocus() {
return ShouldActivate();
}
void NativeWidgetAura::OnCaptureLost() {
if (delegate_) {
delegate_->OnMouseCaptureLost();
}
}
void NativeWidgetAura::OnPaint(const ui::PaintContext& context) {
if (delegate_) {
delegate_->OnNativeWidgetPaint(context);
}
}
void NativeWidgetAura::OnDeviceScaleFactorChanged(
float old_device_scale_factor,
float new_device_scale_factor) {
if (Widget* widget = GetWidget()) {
widget->DeviceScaleFactorChanged(old_device_scale_factor,
new_device_scale_factor);
}
}
void NativeWidgetAura::OnWindowDestroying(aura::Window* window) {
window_->RemoveObserver(this);
if (wm::TransientWindowManager::GetIfExists(window_)) {
wm::TransientWindowManager::GetOrCreate(window_)->RemoveObserver(this);
}
if (delegate_) {
delegate_->OnNativeWidgetDestroying();
}
tooltip_manager_.reset();
focus_manager_event_handler_.reset();
}
void NativeWidgetAura::OnWindowDestroyed(aura::Window* window) {
window_ = nullptr;
bool should_delete_this =
(ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) ||
(ownership_ == Widget::InitParams::CLIENT_OWNS_WIDGET);
if (delegate_) {
delegate_->OnNativeWidgetDestroyed();
}
if (should_delete_this) {
delete this;
}
}
void NativeWidgetAura::OnWindowTargetVisibilityChanged(bool visible) {
if (delegate_) {
delegate_->OnNativeWidgetVisibilityChanged(visible);
}
}
bool NativeWidgetAura::HasHitTestMask() const {
return delegate_ ? delegate_->HasHitTestMask() : false;
}
void NativeWidgetAura::GetHitTestMask(SkPath* mask) const {
DCHECK(mask);
if (delegate_) {
delegate_->GetHitTestMask(mask);
}
}
void NativeWidgetAura::UpdateVisualState() {
if (delegate_) {
delegate_->LayoutRootViewIfNecessary();
}
}
void NativeWidgetAura::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
if (delegate_ && key == aura::client::kShowStateKey) {
delegate_->OnNativeWidgetWindowShowStateChanged();
}
if (delegate_ && key == aura::client::kWindowWorkspaceKey) {
delegate_->OnNativeWidgetWorkspaceChanged();
}
}
void NativeWidgetAura::OnResizeLoopStarted(aura::Window* window) {
if (delegate_) {
delegate_->OnNativeWidgetBeginUserBoundsChange();
delegate_->OnNativeWidgetUserResizeStarted();
}
}
void NativeWidgetAura::OnResizeLoopEnded(aura::Window* window) {
if (delegate_) {
delegate_->OnNativeWidgetEndUserBoundsChange();
delegate_->OnNativeWidgetUserResizeEnded();
}
}
void NativeWidgetAura::OnMoveLoopStarted(aura::Window* window) {
if (delegate_) {
delegate_->OnNativeWidgetBeginUserBoundsChange();
}
}
void NativeWidgetAura::OnMoveLoopEnded(aura::Window* window) {
if (delegate_) {
delegate_->OnNativeWidgetEndUserBoundsChange();
}
}
void NativeWidgetAura::OnWindowAddedToRootWindow(aura::Window* window) {
if (delegate_) {
delegate_->OnNativeWidgetAddedToCompositor();
}
}
void NativeWidgetAura::OnWindowRemovingFromRootWindow(aura::Window* window,
aura::Window* new_root) {
if (delegate_) {
delegate_->OnNativeWidgetRemovingFromCompositor();
}
}
void NativeWidgetAura::OnKeyEvent(ui::KeyEvent* event) {
DCHECK(window_);
if (!window_->IsVisible()) {
return;
}
if (delegate_) {
delegate_->OnKeyEvent(event);
}
}
void NativeWidgetAura::OnMouseEvent(ui::MouseEvent* event) {
DCHECK(window_);
DCHECK(window_->IsVisible());
if (delegate_ && event->type() == ui::EventType::kMousewheel) {
delegate_->OnMouseEvent(event);
return;
}
if (tooltip_manager_.get()) {
tooltip_manager_->UpdateTooltip();
}
TooltipManagerAura::UpdateTooltipManagerForCapture(this);
if (delegate_) {
delegate_->OnMouseEvent(event);
}
}
void NativeWidgetAura::OnScrollEvent(ui::ScrollEvent* event) {
if (delegate_) {
delegate_->OnScrollEvent(event);
}
}
void NativeWidgetAura::OnGestureEvent(ui::GestureEvent* event) {
DCHECK(window_);
DCHECK(window_->IsVisible() || event->IsEndingEvent());
if (delegate_) {
delegate_->OnGestureEvent(event);
}
}
bool NativeWidgetAura::ShouldActivate() const {
return delegate_ ? delegate_->CanActivate() : false;
}
void NativeWidgetAura::OnWindowActivated(
wm::ActivationChangeObserver::ActivationReason,
aura::Window* gained_active,
aura::Window* lost_active) {
DCHECK(window_ == gained_active || window_ == lost_active);
if (GetWidget() && GetWidget()->GetFocusManager()) {
if (window_ == gained_active) {
GetWidget()->GetFocusManager()->RestoreFocusedView();
} else if (window_ == lost_active) {
GetWidget()->GetFocusManager()->StoreFocusedView(true);
}
}
if (delegate_) {
delegate_->OnNativeWidgetActivationChanged(window_ == gained_active);
}
}
void NativeWidgetAura::OnWindowFocused(aura::Window* gained_focus,
aura::Window* lost_focus) {
if (delegate_ && window_ == gained_focus) {
delegate_->OnNativeFocus();
} else if (delegate_ && window_ == lost_focus) {
delegate_->OnNativeBlur();
}
}
void NativeWidgetAura::OnDragEntered(const ui::DropTargetEvent& event) {
DCHECK(drop_helper_.get() != nullptr);
last_drop_operation_ = drop_helper_->OnDragOver(
event.data(), event.location(), event.source_operations());
}
aura::client::DragUpdateInfo NativeWidgetAura::OnDragUpdated(
const ui::DropTargetEvent& event) {
DCHECK(drop_helper_.get() != nullptr);
last_drop_operation_ = drop_helper_->OnDragOver(
event.data(), event.location(), event.source_operations());
return aura::client::DragUpdateInfo(
last_drop_operation_,
ui::DataTransferEndpoint(ui::EndpointType::kDefault));
}
void NativeWidgetAura::OnDragExited() {
DCHECK(drop_helper_.get() != nullptr);
drop_helper_->OnDragExit();
}
aura::client::DragDropDelegate::DropCallback NativeWidgetAura::GetDropCallback(
const ui::DropTargetEvent& event) {
DCHECK(drop_helper_);
return drop_helper_->GetDropCallback(event.data(), event.location(),
last_drop_operation_);
}
void NativeWidgetAura::OnTransientParentChanged(aura::Window* new_parent) {
if (delegate_) {
delegate_->OnNativeWidgetParentChanged(new_parent);
}
}
NativeWidgetAura::~NativeWidgetAura() {
if (ownership_ == Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET) {
drop_helper_.reset();
window_reorderer_.reset();
owned_delegate_.reset();
} else {
CloseNow();
}
}
void NativeWidgetAura::SetInitialFocus(ui::mojom::WindowShowState show_state) {
if (!GetWidget()->SetInitialFocus(show_state)) {
window_->Focus();
}
}
namespace {
#if BUILDFLAG(ENABLE_DESKTOP_AURA) && (BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE))
void CloseWindow(aura::Window* window) {
if (window) {
if (Widget* widget = Widget::GetWidgetForNativeView(window)) {
widget->CloseNow();
}
}
}
#endif
#if BUILDFLAG(IS_WIN)
BOOL CALLBACK WindowCallbackProc(HWND hwnd, LPARAM lParam) {
aura::Window* root_window =
DesktopWindowTreeHostWin::GetContentWindowForHWND(hwnd);
CloseWindow(root_window);
return TRUE;
}
#endif
}
void Widget::CloseAllWidgets() {
#if BUILDFLAG(IS_WIN)
EnumThreadWindows(GetCurrentThreadId(), WindowCallbackProc, 0);
#endif
#if BUILDFLAG(ENABLE_DESKTOP_AURA) && BUILDFLAG(IS_OZONE)
DesktopWindowTreeHostPlatform::CleanUpWindowList(CloseWindow);
#endif
}
namespace internal {
NativeWidgetPrivate* NativeWidgetPrivate::CreateNativeWidget(
internal::NativeWidgetDelegate* delegate) {
return new NativeWidgetAura(delegate);
}
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeView(
gfx::NativeView native_view) {
return native_view->GetProperty(kNativeWidgetPrivateKey);
}
NativeWidgetPrivate* NativeWidgetPrivate::GetNativeWidgetForNativeWindow(
gfx::NativeWindow native_window) {
return native_window->GetProperty(kNativeWidgetPrivateKey);
}
NativeWidgetPrivate* NativeWidgetPrivate::GetTopLevelNativeWidget(
gfx::NativeView native_view) {
aura::Window* window = native_view;
while (window) {
NativeWidgetPrivate* native_widget = GetNativeWidgetForNativeView(window);
Widget* widget = native_widget ? native_widget->GetWidget() : nullptr;
if (widget && widget->is_top_level()) {
return native_widget;
}
window = window->parent();
}
return nullptr;
}
Widget::Widgets NativeWidgetPrivate::GetAllChildWidgets(
gfx::NativeView native_view) {
Widget::Widgets children;
NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
GetNativeWidgetForNativeView(native_view));
if (native_widget && native_widget->GetWidget()) {
children.insert(native_widget->GetWidget());
}
for (aura::Window* child_window : native_view->children()) {
children.merge(GetAllChildWidgets(child_window));
}
return children;
}
Widget::Widgets NativeWidgetPrivate::GetAllOwnedWidgets(
gfx::NativeView native_view) {
Widget::Widgets owned;
for (aura::Window* transient_child : wm::GetTransientChildren(native_view)) {
NativeWidgetPrivate* native_widget = static_cast<NativeWidgetPrivate*>(
GetNativeWidgetForNativeView(transient_child));
if (native_widget && native_widget->GetWidget()) {
owned.insert(native_widget->GetWidget());
}
owned.merge(GetAllOwnedWidgets(transient_child));
}
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
NativeWidgetPrivate* native_widget =
GetNativeWidgetForNativeView(native_view);
if (native_widget && native_widget->IsDesktopNativeWidget()) {
owned.merge(static_cast<DesktopNativeWidgetAura*>(native_widget)
->GetOwnedDesktopWidgets());
}
#endif
for (aura::Window* child : native_view->children()) {
owned.merge(GetAllChildWidgets(child));
}
return owned;
}
void NativeWidgetPrivate::ReparentNativeView(gfx::NativeView native_view,
gfx::NativeView new_parent) {
DCHECK(native_view != new_parent);
gfx::NativeView previous_parent = native_view->parent();
if (previous_parent == new_parent) {
return;
}
Widget::Widgets widgets = GetAllChildWidgets(native_view);
for (Widget* widget : widgets) {
widget->NotifyNativeViewHierarchyWillChange();
}
Widget* child_widget = Widget::GetWidgetForNativeView(native_view);
if (child_widget) {
child_widget->native_widget_private()->ReparentNativeViewImpl(new_parent);
} else {
ReparentAuraWindow(native_view, new_parent);
}
for (Widget* widget : widgets) {
widget->NotifyNativeViewHierarchyChanged();
}
}
gfx::NativeView NativeWidgetPrivate::GetGlobalCapture(
gfx::NativeView native_view) {
aura::client::CaptureClient* capture_client =
aura::client::GetCaptureClient(native_view->GetRootWindow());
if (!capture_client) {
return nullptr;
}
return capture_client->GetGlobalCaptureWindow();
}
}
}