Commit ed5b1b34 authored by Avery Musbach's avatar Avery Musbach Committed by Commit Bot

wm: Simplify DragWindowController and usage thereof

As a first step toward fixing Issue 965665, the present CL just
simplifies the relevant code.

Test: manual
Bug: 965665
Change-Id: I371a293e8ace81e2b4e325a4e6c36b576e0a46bc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1623130Reviewed-by: default avatarMitsuru Oshima <oshima@chromium.org>
Commit-Queue: Avery Musbach <amusbach@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661977}
parent b07027a8
......@@ -32,6 +32,13 @@
namespace ash {
namespace {
// The maximum opacity of the drag phantom window.
constexpr float kDragPhantomMaxOpacity = 0.8f;
} // namespace
// This keeps track of the drag window's state. It creates/destroys/updates
// bounds and opacity based on the current bounds.
class DragWindowController::DragWindowDetails : public aura::WindowDelegate {
......@@ -46,10 +53,10 @@ class DragWindowController::DragWindowDetails : public aura::WindowDelegate {
}
void Update(aura::Window* original_window,
const gfx::Rect& bounds_in_screen,
const gfx::Point& drag_location_in_screen) {
gfx::Rect root_bounds_in_screen = root_window_->GetBoundsInScreen();
if (!root_bounds_in_screen.Intersects(bounds_in_screen)) {
const float opacity = GetDragWindowOpacity(root_window_, original_window,
drag_location_in_screen);
if (opacity == 0.f) {
delete drag_window_;
// Make sure drag_window_ is reset so that new drag window will be created
// when it becomes necessary again.
......@@ -58,32 +65,26 @@ class DragWindowController::DragWindowDetails : public aura::WindowDelegate {
return;
}
if (!drag_window_)
CreateDragWindow(original_window, bounds_in_screen);
gfx::Rect bounds_in_root = bounds_in_screen;
::wm::ConvertRectFromScreen(drag_window_->parent(), &bounds_in_root);
drag_window_->SetBounds(bounds_in_root);
if (root_bounds_in_screen.Contains(drag_location_in_screen)) {
SetOpacity(original_window, 1.f);
} else {
drag_window_->SetBounds(bounds_in_root);
gfx::Rect visible_bounds = root_bounds_in_screen;
visible_bounds.Intersect(bounds_in_screen);
SetOpacity(original_window,
GetDragWindowOpacity(bounds_in_screen, visible_bounds));
}
CreateDragWindow(original_window);
gfx::Rect bounds = original_window->bounds();
aura::Window::ConvertRectToTarget(original_window->parent(),
drag_window_->parent(), &bounds);
drag_window_->SetBounds(bounds);
SetOpacity(original_window, opacity);
}
private:
friend class DragWindowController;
void CreateDragWindow(aura::Window* original_window,
const gfx::Rect& bounds_in_screen) {
void CreateDragWindow(aura::Window* original_window) {
DCHECK(!drag_window_);
original_window_ = original_window;
drag_window_ = window_factory::NewWindow(this).release();
int parent_id = original_window->parent()->id();
aura::Window* container = root_window_->GetChildById(parent_id);
gfx::Rect bounds = original_window->bounds();
::wm::ConvertRectToScreen(original_window->parent(), &bounds);
drag_window_->SetType(aura::client::WINDOW_TYPE_POPUP);
drag_window_->SetTransparent(true);
......@@ -92,7 +93,7 @@ class DragWindowController::DragWindowDetails : public aura::WindowDelegate {
drag_window_->set_id(kShellWindowId_PhantomWindow);
drag_window_->SetProperty(aura::client::kAnimationsDisabledKey, true);
container->AddChild(drag_window_);
drag_window_->SetBounds(bounds_in_screen);
drag_window_->SetBounds(bounds);
::wm::SetShadowElevation(drag_window_, ::wm::kShadowElevationActiveWindow);
RecreateWindowLayers(original_window);
......@@ -171,13 +172,19 @@ class DragWindowController::DragWindowDetails : public aura::WindowDelegate {
// static
float DragWindowController::GetDragWindowOpacity(
const gfx::Rect& window_bounds,
const gfx::Rect& visible_bounds) {
// The maximum opacity of the drag phantom window.
static const float kMaxOpacity = 0.8f;
return kMaxOpacity * visible_bounds.size().GetArea() /
window_bounds.size().GetArea();
aura::Window* root_window,
aura::Window* dragged_window,
const gfx::Point& drag_location_in_screen) {
const gfx::Rect root_bounds = root_window->GetBoundsInScreen();
if (root_bounds.Contains(drag_location_in_screen))
return 1.f;
gfx::Rect dragged_window_bounds = dragged_window->bounds();
::wm::ConvertRectToScreen(dragged_window->parent(), &dragged_window_bounds);
gfx::Rect visible_bounds = root_bounds;
visible_bounds.Intersect(dragged_window_bounds);
return kDragPhantomMaxOpacity * visible_bounds.size().GetArea() /
dragged_window_bounds.size().GetArea();
}
DragWindowController::DragWindowController(aura::Window* window)
......@@ -196,10 +203,9 @@ DragWindowController::DragWindowController(aura::Window* window)
DragWindowController::~DragWindowController() = default;
void DragWindowController::Update(const gfx::Rect& bounds_in_screen,
const gfx::Point& drag_location_in_screen) {
void DragWindowController::Update(const gfx::Point& drag_location_in_screen) {
for (std::unique_ptr<DragWindowDetails>& details : drag_windows_)
details->Update(window_, bounds_in_screen, drag_location_in_screen);
details->Update(window_, drag_location_in_screen);
}
int DragWindowController::GetDragWindowsCountForTest() const {
......
......@@ -27,10 +27,12 @@ namespace ash {
// while dragging a window across displays.
class ASH_EXPORT DragWindowController {
public:
// Computes the opacity for drag window based on how much of the area
// of the window is visible.
static float GetDragWindowOpacity(const gfx::Rect& window_bounds,
const gfx::Rect& visible_bounds);
// Returns 1 if |drag_location_in_screen| is contained in |root_window|.
// Otherwise, returns an opacity value based on what fraction of
// |dragged_window| is contained in |root_window|.
static float GetDragWindowOpacity(aura::Window* root_window,
aura::Window* dragged_window,
const gfx::Point& drag_location_in_screen);
explicit DragWindowController(aura::Window* window);
virtual ~DragWindowController();
......@@ -38,8 +40,7 @@ class ASH_EXPORT DragWindowController {
// This is used to update the bounds and opacity for the drag window
// immediately.
// This also creates/destorys the drag window when necessary.
void Update(const gfx::Rect& bounds_in_screen,
const gfx::Point& drag_location_in_screen);
void Update(const gfx::Point& drag_location_in_screen);
private:
class DragWindowDetails;
......
......@@ -62,7 +62,7 @@ void DragWindowResizer::Drag(const gfx::Point& location, int event_flags) {
if (display::Screen::GetScreen()->GetNumDisplays() > 1) {
gfx::Point location_in_screen = location;
::wm::ConvertPointToScreen(GetTarget()->parent(), &location_in_screen);
UpdateDragWindow(GetTarget()->bounds(), location_in_screen);
UpdateDragWindow(location_in_screen);
} else {
drag_window_controller_.reset();
}
......@@ -105,7 +105,6 @@ DragWindowResizer::DragWindowResizer(
}
void DragWindowResizer::UpdateDragWindow(
const gfx::Rect& bounds_in_parent,
const gfx::Point& drag_location_in_screen) {
if (details().window_component != HTCAPTION || !ShouldAllowMouseWarp())
return;
......@@ -113,20 +112,9 @@ void DragWindowResizer::UpdateDragWindow(
if (!drag_window_controller_)
drag_window_controller_.reset(new DragWindowController(GetTarget()));
gfx::Rect bounds_in_screen = bounds_in_parent;
::wm::ConvertRectToScreen(GetTarget()->parent(), &bounds_in_screen);
gfx::Rect root_bounds_in_screen =
GetTarget()->GetRootWindow()->GetBoundsInScreen();
float opacity = 1.0f;
if (!root_bounds_in_screen.Contains(drag_location_in_screen)) {
gfx::Rect visible_bounds = root_bounds_in_screen;
visible_bounds.Intersect(bounds_in_screen);
opacity = DragWindowController::GetDragWindowOpacity(bounds_in_screen,
visible_bounds);
}
GetTarget()->layer()->SetOpacity(opacity);
drag_window_controller_->Update(bounds_in_screen, drag_location_in_screen);
GetTarget()->layer()->SetOpacity(DragWindowController::GetDragWindowOpacity(
GetTarget()->GetRootWindow(), GetTarget(), drag_location_in_screen));
drag_window_controller_->Update(drag_location_in_screen);
}
bool DragWindowResizer::ShouldAllowMouseWarp() {
......
......@@ -45,8 +45,7 @@ class ASH_EXPORT DragWindowResizer : public WindowResizer {
DragWindowControllerAcrossThreeDisplays);
// Updates the bounds of the drag window for window dragging.
void UpdateDragWindow(const gfx::Rect& bounds_in_parent,
const gfx::Point& drag_location_in_screen);
void UpdateDragWindow(const gfx::Point& drag_location_in_screen);
// Returns true if we should allow the mouse pointer to warp.
bool ShouldAllowMouseWarp();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment