blob: 4462f7d9fea2777d6bd539b3900f5021f8ec8874 [file] [log] [blame]
// Copyright 2015 The Chromium 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 "ash/wm/non_client_frame_controller.h"
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "ash/frame/detached_title_area_renderer.h"
#include "ash/frame/non_client_frame_view_ash.h"
#include "ash/public/cpp/ash_constants.h"
#include "ash/public/cpp/ash_layout_constants.h"
#include "ash/public/cpp/immersive/immersive_fullscreen_controller_delegate.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/shell.h"
#include "ash/wm/property_util.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/window_util.h"
#include "ash/ws/window_service_owner.h"
#include "base/macros.h"
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "services/ui/public/interfaces/window_manager.mojom.h"
#include "services/ui/ws2/window_properties.h"
#include "services/ui/ws2/window_service.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/transient_window_client.h"
#include "ui/aura/mus/property_converter.h"
#include "ui/aura/mus/property_utils.h"
#include "ui/aura/mus/window_port_mus.h"
#include "ui/aura/window.h"
#include "ui/base/class_property.h"
#include "ui/base/hit_test.h"
#include "ui/compositor/layer.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/views/mus/ax_remote_host.h"
#include "ui/views/widget/native_widget_aura.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
DEFINE_UI_CLASS_PROPERTY_TYPE(ash::NonClientFrameController*);
namespace ash {
namespace {
DEFINE_UI_CLASS_PROPERTY_KEY(NonClientFrameController*,
kNonClientFrameControllerKey,
nullptr);
// This class supports draggable app windows that paint their own custom frames.
// It uses empty insets, doesn't paint anything, and hit tests return HTCAPTION.
class EmptyDraggableNonClientFrameView : public views::NonClientFrameView {
public:
EmptyDraggableNonClientFrameView() = default;
~EmptyDraggableNonClientFrameView() override = default;
// views::NonClientFrameView:
gfx::Rect GetBoundsForClientView() const override { return bounds(); }
gfx::Rect GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const override {
return bounds();
}
int NonClientHitTest(const gfx::Point& point) override { return HTCAPTION; }
void GetWindowMask(const gfx::Size& size, gfx::Path* window_mask) override {}
void ResetWindowControls() override {}
void UpdateWindowIcon() override {}
void UpdateWindowTitle() override {}
void SizeConstraintsChanged() override {}
private:
DISALLOW_COPY_AND_ASSIGN(EmptyDraggableNonClientFrameView);
};
// Creates a Window to host the top container when in immersive mode. The
// top container contains a DetachedTitleAreaRenderer, which handles drawing and
// events.
class ImmersiveFullscreenControllerDelegateMus
: public ImmersiveFullscreenControllerDelegate {
public:
ImmersiveFullscreenControllerDelegateMus(views::Widget* frame,
aura::Window* frame_window)
: frame_(frame), frame_window_(frame_window) {}
~ImmersiveFullscreenControllerDelegateMus() override {
DestroyTitleAreaWindow();
}
// WmImmersiveFullscreenControllerDelegate:
void OnImmersiveRevealStarted() override {
CreateTitleAreaWindow();
SetVisibleFraction(0);
}
void OnImmersiveRevealEnded() override { DestroyTitleAreaWindow(); }
void OnImmersiveFullscreenEntered() override {}
void OnImmersiveFullscreenExited() override { DestroyTitleAreaWindow(); }
void SetVisibleFraction(double visible_fraction) override {
aura::Window* title_area_window = GetTitleAreaWindow();
if (!title_area_window)
return;
gfx::Rect bounds = title_area_window->bounds();
bounds.set_y(frame_window_->bounds().y() - bounds.height() +
visible_fraction * bounds.height());
title_area_window->SetBounds(bounds);
}
std::vector<gfx::Rect> GetVisibleBoundsInScreen() const override {
std::vector<gfx::Rect> result;
const aura::Window* title_area_window = GetTitleAreaWindow();
if (!title_area_window)
return result;
// Clip the bounds of the title area to that of the |frame_window_|.
gfx::Rect visible_bounds = title_area_window->bounds();
visible_bounds.Intersect(frame_window_->bounds());
// The intersection is in the coordinates of |title_area_window|'s parent,
// convert to be in |title_area_window| and then to screen.
visible_bounds -= title_area_window->bounds().origin().OffsetFromOrigin();
// TODO: this needs updating when parent of |title_area_window| is changed,
// DCHECK is to ensure when parent changes this code is updated.
// http://crbug.com/640392.
DCHECK_EQ(frame_window_->parent(), title_area_window->parent());
::wm::ConvertRectToScreen(title_area_window, &visible_bounds);
result.push_back(visible_bounds);
return result;
}
private:
void CreateTitleAreaWindow() {
if (GetTitleAreaWindow())
return;
// TODO(sky): bounds aren't right here. Need to convert to display.
gfx::Rect bounds = frame_window_->bounds();
// Use the preferred size as when fullscreen the client area is generally
// set to 0.
bounds.set_height(
NonClientFrameController::GetPreferredClientAreaInsets().top());
bounds.set_y(bounds.y() - bounds.height());
title_area_renderer_ =
std::make_unique<DetachedTitleAreaRendererForInternal>(frame_);
title_area_renderer_->widget()->SetBounds(bounds);
title_area_renderer_->widget()->ShowInactive();
}
void DestroyTitleAreaWindow() { title_area_renderer_.reset(); }
aura::Window* GetTitleAreaWindow() {
return const_cast<aura::Window*>(
const_cast<const ImmersiveFullscreenControllerDelegateMus*>(this)
->GetTitleAreaWindow());
}
const aura::Window* GetTitleAreaWindow() const {
return title_area_renderer_
? title_area_renderer_->widget()->GetNativeView()
: nullptr;
}
// The Widget immersive mode is operating on.
views::Widget* frame_;
// The ui::Window associated with |frame_|.
aura::Window* frame_window_;
std::unique_ptr<DetachedTitleAreaRendererForInternal> title_area_renderer_;
DISALLOW_COPY_AND_ASSIGN(ImmersiveFullscreenControllerDelegateMus);
};
class WmNativeWidgetAura : public views::NativeWidgetAura {
public:
WmNativeWidgetAura(views::internal::NativeWidgetDelegate* delegate,
bool remove_standard_frame,
bool enable_immersive,
mojom::WindowStyle window_style)
// The NativeWidget is mirroring the real Widget created in client code.
// |is_parallel_widget_in_window_manager| is used to indicate this
: views::NativeWidgetAura(delegate,
true /* is_parallel_widget_in_window_manager */,
Shell::Get()->aura_env()),
remove_standard_frame_(remove_standard_frame),
enable_immersive_(enable_immersive),
window_style_(window_style) {}
~WmNativeWidgetAura() override = default;
void SetHeaderHeight(int height) {
if (custom_frame_view_)
custom_frame_view_->SetHeaderHeight({height});
}
void set_cursor(const ui::Cursor& cursor) { cursor_ = cursor; }
// views::NativeWidgetAura:
views::NonClientFrameView* CreateNonClientFrameView() override {
// TODO(sky): investigate why we have this. Seems this should be the same
// as not specifying client area insets.
if (remove_standard_frame_)
return new EmptyDraggableNonClientFrameView();
aura::Window* window = GetNativeView();
immersive_delegate_ =
std::make_unique<ImmersiveFullscreenControllerDelegateMus>(GetWidget(),
window);
// See description for details on ownership.
custom_frame_view_ =
new NonClientFrameViewAsh(GetWidget(), immersive_delegate_.get(),
enable_immersive_, window_style_);
// Only the header actually paints any content. So the rest of the region is
// marked as transparent content (see below in NonClientFrameController()
// ctor). So, it is necessary to provide a texture-layer for the header
// view.
views::View* header_view = custom_frame_view_->GetHeaderView();
header_view->SetPaintToLayer(ui::LAYER_TEXTURED);
header_view->layer()->SetFillsBoundsOpaquely(false);
return custom_frame_view_;
}
gfx::NativeCursor GetCursor(const gfx::Point& point) override {
return cursor_;
}
private:
const bool remove_standard_frame_;
const bool enable_immersive_;
const mojom::WindowStyle window_style_;
std::unique_ptr<ImmersiveFullscreenControllerDelegateMus> immersive_delegate_;
// Not used for panels or if |remove_standard_frame_| is true. This is owned
// by the Widget's view hierarchy (e.g. it's a child of Widget's root View).
NonClientFrameViewAsh* custom_frame_view_ = nullptr;
// The cursor for this widget. CompoundEventFilter will retrieve this cursor
// via GetCursor and update the CursorManager's active cursor as appropriate
// (i.e. when the mouse pointer is over this widget).
ui::Cursor cursor_;
DISALLOW_COPY_AND_ASSIGN(WmNativeWidgetAura);
};
// ContentsViewMus links the ash Widget's accessibility node tree with the one
// inside a remote process app. This is needed to support focus; ash needs to
// have "focus" on a leaf node in its Widget/View hierarchy in order for the
// accessibility subsystem to see focused nodes in the remote app.
class ContentsViewMus : public views::View {
public:
ContentsViewMus() = default;
~ContentsViewMus() override = default;
// views::View:
const char* GetClassName() const override { return "ContentsViewMus"; }
void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
node_data->AddIntAttribute(ax::mojom::IntAttribute::kChildTreeId,
views::AXRemoteHost::kRemoteAXTreeID);
node_data->role = ax::mojom::Role::kClient;
}
private:
DISALLOW_COPY_AND_ASSIGN(ContentsViewMus);
};
class ClientViewMus : public views::ClientView {
public:
ClientViewMus(views::Widget* widget,
views::View* contents_view,
NonClientFrameController* frame_controller)
: views::ClientView(widget, contents_view),
frame_controller_(frame_controller) {}
~ClientViewMus() override = default;
// views::ClientView:
bool CanClose() override {
// CanClose() is called when the user requests the window to close (such as
// clicking the close button). As this window is managed by a remote client
// pass the request to the remote client and return false (to cancel the
// close). If the remote client wants the window to close, it will close it
// in a way that does not reenter this code path.
Shell::Get()->window_service_owner()->window_service()->RequestClose(
frame_controller_->window());
return false;
}
// views::View:
const char* GetClassName() const override { return "ClientViewMus"; }
private:
NonClientFrameController* frame_controller_;
DISALLOW_COPY_AND_ASSIGN(ClientViewMus);
};
} // namespace
NonClientFrameController::NonClientFrameController(
aura::Window* parent,
aura::Window* context,
const gfx::Rect& bounds,
ui::mojom::WindowType window_type,
aura::PropertyConverter* property_converter,
std::map<std::string, std::vector<uint8_t>>* properties)
: widget_(new views::Widget), window_(nullptr) {
// To simplify things this code creates a Widget. While a Widget is created
// we need to ensure we don't inadvertently change random properties of the
// underlying ui::Window. For example, showing the Widget shouldn't change
// the bounds of the ui::Window in anyway.
//
// Assertions around InitParams::Type matching ui::mojom::WindowType exist in
// MusClient.
views::Widget::InitParams params(
static_cast<views::Widget::InitParams::Type>(window_type));
DCHECK((parent && !context) || (!parent && context));
params.parent = parent;
params.context = context;
// TODO: properly set |params.activatable|. Should key off whether underlying
// (mus) window can have focus.
params.delegate = this;
params.bounds = bounds;
params.opacity = views::Widget::InitParams::OPAQUE_WINDOW;
params.layer_type = ui::LAYER_SOLID_COLOR;
WmNativeWidgetAura* native_widget = new WmNativeWidgetAura(
widget_, ShouldRemoveStandardFrame(*properties),
ShouldEnableImmersive(*properties), GetWindowStyle(*properties));
window_ = native_widget->GetNativeView();
window_->SetProperty(kNonClientFrameControllerKey, this);
window_->SetProperty(kWidgetCreationTypeKey, WidgetCreationType::FOR_CLIENT);
window_->AddObserver(this);
params.native_widget = native_widget;
aura::SetWindowType(window_, window_type);
for (auto& property_pair : *properties) {
property_converter->SetPropertyFromTransportValue(
window_, property_pair.first, &property_pair.second);
}
// Applying properties will have set the show state if specified.
// NativeWidgetAura resets the show state from |params|, so we need to update
// |params|.
params.show_state = window_->GetProperty(aura::client::kShowStateKey);
widget_->Init(params);
did_init_native_widget_ = true;
// Only the caption draws any content. So the caption has its own layer (see
// above in WmNativeWidgetAura::CreateNonClientFrameView()). The rest of the
// region needs to take part in occlusion in the compositor, but not generate
// any content to draw. So the layer is marked as opaque and to draw
// solid-color (but the color is transparent, so nothing is actually drawn).
ui::Layer* layer = widget_->GetNativeWindow()->layer();
layer->SetColor(SK_ColorTRANSPARENT);
layer->SetFillsBoundsOpaquely(true);
aura::client::GetTransientWindowClient()->AddObserver(this);
}
// static
NonClientFrameController* NonClientFrameController::Get(aura::Window* window) {
return window->GetProperty(kNonClientFrameControllerKey);
}
// static
gfx::Insets NonClientFrameController::GetPreferredClientAreaInsets() {
return gfx::Insets(
GetAshLayoutSize(AshLayoutSize::kNonBrowserCaption).height(), 0, 0, 0);
}
// static
int NonClientFrameController::GetMaxTitleBarButtonWidth() {
return GetAshLayoutSize(AshLayoutSize::kNonBrowserCaption).width() * 3;
}
void NonClientFrameController::SetClientArea(const gfx::Insets& insets) {
static_cast<WmNativeWidgetAura*>(widget_->native_widget())
->SetHeaderHeight(insets.top());
}
void NonClientFrameController::StoreCursor(const ui::Cursor& cursor) {
static_cast<WmNativeWidgetAura*>(widget_->native_widget())
->set_cursor(cursor);
}
base::string16 NonClientFrameController::GetWindowTitle() const {
if (!window_)
return base::string16();
base::string16 title = window_->GetTitle();
if (window_->GetProperty(kWindowIsJanky))
title += base::ASCIIToUTF16(" !! Not responding !!");
return title;
}
bool NonClientFrameController::CanResize() const {
return window_ && (window_->GetProperty(aura::client::kResizeBehaviorKey) &
ui::mojom::kResizeBehaviorCanResize) != 0;
}
bool NonClientFrameController::CanMaximize() const {
return window_ && (window_->GetProperty(aura::client::kResizeBehaviorKey) &
ui::mojom::kResizeBehaviorCanMaximize) != 0;
}
bool NonClientFrameController::CanMinimize() const {
return window_ && (window_->GetProperty(aura::client::kResizeBehaviorKey) &
ui::mojom::kResizeBehaviorCanMinimize) != 0;
}
bool NonClientFrameController::CanActivate() const {
// kCanFocus is used for both focus and activation.
return window_ && window_->GetProperty(ui::ws2::kCanFocus) &&
views::WidgetDelegate::CanActivate();
}
bool NonClientFrameController::ShouldShowWindowTitle() const {
return window_ && window_->GetProperty(aura::client::kTitleShownKey);
}
void NonClientFrameController::DeleteDelegate() {
delete this;
}
views::Widget* NonClientFrameController::GetWidget() {
return widget_;
}
const views::Widget* NonClientFrameController::GetWidget() const {
return widget_;
}
views::View* NonClientFrameController::GetContentsView() {
return contents_view_;
}
views::ClientView* NonClientFrameController::CreateClientView(
views::Widget* widget) {
DCHECK(!contents_view_);
contents_view_ = new ContentsViewMus(); // Owned by views hierarchy.
return new ClientViewMus(widget, contents_view_, this);
}
void NonClientFrameController::OnWindowPropertyChanged(aura::Window* window,
const void* key,
intptr_t old) {
// Properties are applied before the call to InitNativeWidget(). Ignore
// processing changes in this case as the Widget is not in a state where we
// can use it yet.
if (!did_init_native_widget_)
return;
if (key == kWindowIsJanky || key == aura::client::kTitleKey ||
key == aura::client::kTitleShownKey) {
widget_->UpdateWindowTitle();
widget_->non_client_view()->frame_view()->SchedulePaint();
} else if (key == aura::client::kResizeBehaviorKey) {
widget_->OnSizeConstraintsChanged();
}
}
void NonClientFrameController::OnWindowDestroyed(aura::Window* window) {
window_->RemoveObserver(this);
window_ = nullptr;
}
void NonClientFrameController::OnTransientChildWindowAdded(
aura::Window* parent,
aura::Window* transient_child) {
if (parent != window_ ||
!transient_child->GetProperty(kRenderTitleAreaProperty)) {
return;
}
DetachedTitleAreaRendererForClient* renderer =
DetachedTitleAreaRendererForClient::ForWindow(transient_child);
if (!renderer || renderer->is_attached())
return;
renderer->Attach(widget_);
}
void NonClientFrameController::OnTransientChildWindowRemoved(
aura::Window* parent,
aura::Window* transient_child) {
if (parent != window_)
return;
DetachedTitleAreaRendererForClient* renderer =
DetachedTitleAreaRendererForClient::ForWindow(transient_child);
if (renderer)
renderer->Detach();
}
NonClientFrameController::~NonClientFrameController() {
aura::client::GetTransientWindowClient()->RemoveObserver(this);
if (window_)
window_->RemoveObserver(this);
}
} // namespace ash