Skip to content

Commit

Permalink
Add ScreenPositionClient::ConvertNativePointToScreen.
Browse files Browse the repository at this point in the history
This will be used to convert mouse location properly in RootWindow when X's native input is captured by a drag operation.

BUG=148686


Review URL: https://chromiumcodereview.appspot.com/10911342

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157702 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
mazda@chromium.org committed Sep 20, 2012
1 parent 8c08ca5 commit 9737f8f
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 0 deletions.
1 change: 1 addition & 0 deletions ash/ash.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@
'display/display_controller_unittest.cc',
'display/mouse_cursor_event_filter_unittest.cc',
'display/multi_display_manager_unittest.cc',
'display/screen_position_controller_unittest.cc',
'drag_drop/drag_drop_controller_unittest.cc',
'drag_drop/drag_drop_tracker_unittest.cc',
'extended_desktop_unittest.cc',
Expand Down
10 changes: 10 additions & 0 deletions ash/display/screen_position_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/wm/coordinate_conversion.h"
#include "ash/wm/system_modal_container_layout_manager.h"
#include "ash/wm/window_properties.h"
#include "ash/wm/workspace_controller.h"
Expand Down Expand Up @@ -76,6 +77,15 @@ void ScreenPositionController::ConvertPointFromScreen(
aura::Window::ConvertPointToTarget(root, window, point);
}

void ScreenPositionController::ConvertNativePointToScreen(
aura::Window* window,
gfx::Point* point) {
std::pair<aura::RootWindow*, gfx::Point> pair =
wm::GetRootWindowRelativeToWindow(window, *point);
*point = pair.second;
ConvertPointToScreen(pair.first, point);
}

void ScreenPositionController::SetBounds(aura::Window* window,
const gfx::Rect& bounds,
const gfx::Display& display) {
Expand Down
2 changes: 2 additions & 0 deletions ash/display/screen_position_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ScreenPositionController : public aura::client::ScreenPositionClient {
gfx::Point* point) OVERRIDE;
virtual void ConvertPointFromScreen(const aura::Window* window,
gfx::Point* point) OVERRIDE;
virtual void ConvertNativePointToScreen(aura::Window* window,
gfx::Point* point) OVERRIDE;
virtual void SetBounds(aura::Window* window,
const gfx::Rect& bounds,
const gfx::Display& display) OVERRIDE;
Expand Down
154 changes: 154 additions & 0 deletions ash/display/screen_position_controller_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Copyright (c) 2012 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/display/screen_position_controller.h"

#include "ash/display/display_controller.h"
#include "ash/shell.h"
#include "ash/test/ash_test_base.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/base/layout.h"
#include "ui/gfx/screen.h"

namespace ash {
namespace test {

namespace {
void SetSecondaryDisplayLayout(DisplayLayout::Position position) {
DisplayController* display_controller =
Shell::GetInstance()->display_controller();
DisplayLayout layout = display_controller->default_display_layout();
layout.position = position;
display_controller->SetDefaultDisplayLayout(layout);
}

internal::ScreenPositionController* GetScreenPositionController() {
Shell::TestApi test_api(Shell::GetInstance());
return test_api.screen_position_controller();
}

class ScreenPositionControllerTest : public test::AshTestBase {
public:
ScreenPositionControllerTest() : window_(NULL) {}
virtual ~ScreenPositionControllerTest() {}

virtual void SetUp() OVERRIDE {
AshTestBase::SetUp();
window_.reset(new aura::Window(&window_delegate_));
window_->SetType(aura::client::WINDOW_TYPE_NORMAL);
window_->Init(ui::LAYER_NOT_DRAWN);
window_->SetParent(NULL);
window_->set_id(1);
}

virtual void TearDown() OVERRIDE {
window_.reset();
AshTestBase::TearDown();
}

// Converts a native point (x, y) to screen and returns its string
// representation.
std::string ConvertNativePointToScreen(int x, int y) const {
gfx::Point point(x, y);
GetScreenPositionController()->ConvertNativePointToScreen(
window_.get(), &point);
return point.ToString();
}

protected:
scoped_ptr<aura::Window> window_;
aura::test::TestWindowDelegate window_delegate_;

private:
DISALLOW_COPY_AND_ASSIGN(ScreenPositionControllerTest);
};

} // namespace

TEST_F(ScreenPositionControllerTest, ConvertNativePointToScreen) {
UpdateDisplay("100+100-200x200,100+500-200x200");

Shell::RootWindowList root_windows =
Shell::GetInstance()->GetAllRootWindows();
EXPECT_EQ("100,100", root_windows[0]->GetHostOrigin().ToString());
EXPECT_EQ("200x200", root_windows[0]->GetHostSize().ToString());
EXPECT_EQ("100,500", root_windows[1]->GetHostOrigin().ToString());
EXPECT_EQ("200x200", root_windows[1]->GetHostSize().ToString());

const gfx::Point window_pos(100, 100);
window_->SetBoundsInScreen(gfx::Rect(window_pos, gfx::Size(100, 100)),
gfx::Screen::GetDisplayNearestPoint(window_pos));
SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
// The point is on the primary root window.
EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
// The point is on the secondary display.
EXPECT_EQ("350,100", ConvertNativePointToScreen(50, 400));

SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
// The point is on the primary root window.
EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
// The point is on the secondary display.
EXPECT_EQ("150,300", ConvertNativePointToScreen(50, 400));

SetSecondaryDisplayLayout(DisplayLayout::LEFT);
// The point is on the primary root window.
EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
// The point is on the secondary display.
EXPECT_EQ("-50,100", ConvertNativePointToScreen(50, 400));

SetSecondaryDisplayLayout(DisplayLayout::TOP);
// The point is on the primary root window.
EXPECT_EQ("150,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,350", ConvertNativePointToScreen(250, 250));
// The point is on the secondary display.
EXPECT_EQ("150,-100", ConvertNativePointToScreen(50, 400));


SetSecondaryDisplayLayout(DisplayLayout::RIGHT);
const gfx::Point window_pos2(300, 100);
window_->SetBoundsInScreen(gfx::Rect(window_pos2, gfx::Size(100, 100)),
gfx::Screen::GetDisplayNearestPoint(window_pos2));
// The point is on the secondary display.
EXPECT_EQ("350,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("550,350", ConvertNativePointToScreen(250, 250));
// The point is on the primary root window.
EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

SetSecondaryDisplayLayout(DisplayLayout::BOTTOM);
// The point is on the secondary display.
EXPECT_EQ("150,350", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,550", ConvertNativePointToScreen(250, 250));
// The point is on the primary root window.
EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

SetSecondaryDisplayLayout(DisplayLayout::LEFT);
// The point is on the secondary display.
EXPECT_EQ("-50,150", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("150,350", ConvertNativePointToScreen(250, 250));
// The point is on the primary root window.
EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));

SetSecondaryDisplayLayout(DisplayLayout::TOP);
// The point is on the secondary display.
EXPECT_EQ("150,-50", ConvertNativePointToScreen(50, 50));
// The point is out of the all root windows.
EXPECT_EQ("350,150", ConvertNativePointToScreen(250, 250));
// The point is on the primary root window.
EXPECT_EQ("150,100", ConvertNativePointToScreen(50, -400));
}

} // namespace test
} // namespace ash
5 changes: 5 additions & 0 deletions ash/shell.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ internal::WorkspaceController* Shell::TestApi::workspace_controller() {
return shell_->GetPrimaryRootWindowController()->workspace_controller();
}

internal::ScreenPositionController*
Shell::TestApi::screen_position_controller() {
return shell_->screen_position_controller_.get();
}

////////////////////////////////////////////////////////////////////////////////
// Shell, public:

Expand Down
1 change: 1 addition & 0 deletions ash/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class ASH_EXPORT Shell : ash::CursorDelegate {
aura::shared::InputMethodEventFilter* input_method_event_filter();
internal::SystemGestureEventFilter* system_gesture_event_filter();
internal::WorkspaceController* workspace_controller();
internal::ScreenPositionController* screen_position_controller();

private:
Shell* shell_; // not owned
Expand Down
7 changes: 7 additions & 0 deletions ui/aura/client/screen_position_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ class AURA_EXPORT ScreenPositionClient {
gfx::Point* point) = 0;
virtual void ConvertPointFromScreen(const Window* window,
gfx::Point* point) = 0;
// Converts the |screen_point| from a given |window|'s native screen
// coordinate space into screen coordinate space.
// A typical example of using this function instead of ConvertPointToScreen is
// when X's native input is captured by a drag operation.
// See the comments in ash::wm::GetRootWindowRelativeToWindow for details.
virtual void ConvertNativePointToScreen(Window* window,
gfx::Point* point) = 0;
// Sets the bounds of the window. The implementation is responsible
// for finding out and translating the right coordinates for the |window|.
virtual void SetBounds(Window* window,
Expand Down
5 changes: 5 additions & 0 deletions ui/views/widget/desktop_screen_position_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void DesktopScreenPositionClient::ConvertPointFromScreen(
aura::Window::ConvertPointToTarget(root_window, window, point);
}

void DesktopScreenPositionClient::ConvertNativePointToScreen(
aura::Window* window, gfx::Point* point) {
ConvertPointToScreen(window, point);
}

void DesktopScreenPositionClient::SetBounds(
aura::Window* window,
const gfx::Rect& bounds,
Expand Down
2 changes: 2 additions & 0 deletions ui/views/widget/desktop_screen_position_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class VIEWS_EXPORT DesktopScreenPositionClient
gfx::Point* point) OVERRIDE;
virtual void ConvertPointFromScreen(const aura::Window* window,
gfx::Point* point) OVERRIDE;
virtual void ConvertNativePointToScreen(aura::Window* window,
gfx::Point* point) OVERRIDE;
virtual void SetBounds(aura::Window* window,
const gfx::Rect& bounds,
const gfx::Display& display) OVERRIDE;
Expand Down

0 comments on commit 9737f8f

Please sign in to comment.