Skip to content

Commit

Permalink
Add Vector2d classes that represent offsets, instead of using Point.
Browse files Browse the repository at this point in the history
Previously Point served two purposes, to be a position in 2d space, and also
an offset from the origin. This introduces a Vector2d class to represent an
offset, allowing typesafety checks for geometric operations.

The following are now the case:

Point +/- Vector = Point
- A point plus/minus an offset gives you a point at a position offset by the
vector.

Vector +/- Vector = Vector
- Two offsets can be added together to make a new offset.

Point - Point = Vector
- Subtracting one point from another gives you the offset between the two
points.

We add some new methods to perform these operations:

Rect::OffsetFromOrigin() gives the offset between the position of the rect
and the origin.

Point::OffsetFromOrigin() gives the offset between the point and the origin.

PointAtOffsetFromOrigin(Vector2d) converts a Vector2d to a Point at the given
offset away from the origin.

Rect::Offset(), Point::Add(), and Point::Subtract() now receive a Vector2d
instead of a point.

BUG=147395
R=sky

Review URL: https://codereview.chromium.org/11269022

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@165198 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
danakj@chromium.org committed Oct 31, 2012
1 parent 80211f6 commit ceb36f7
Show file tree
Hide file tree
Showing 166 changed files with 1,112 additions and 489 deletions.
3 changes: 2 additions & 1 deletion WATCHLISTS
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@
'filepath': 'ui/gfx/rect|'\
'ui/gfx/point|'\
'ui/gfx/size|'\
'ui/gfx/transform',
'ui/gfx/transform|'\
'ui/gfx/vector2d',
},
'gpu': {
'filepath': 'gpu/',
Expand Down
14 changes: 8 additions & 6 deletions ash/display/multi_display_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ void MultiDisplayManager::EnsurePointerInDisplays() {
return;
gfx::Point location_in_screen = Shell::GetScreen()->GetCursorScreenPoint();
gfx::Point target_location;
int64 closest_distance = -1;
int64 closest_distance_squared = -1;

for (DisplayList::const_iterator iter = displays_.begin();
iter != displays_.end(); ++iter) {
Expand All @@ -522,14 +522,16 @@ void MultiDisplayManager::EnsurePointerInDisplays() {
break;
}
gfx::Point center = display_bounds.CenterPoint();
gfx::Point diff = center.Subtract(location_in_screen);
// Use the distance from the center of the dislay. This is not
// Use the distance squared from the center of the dislay. This is not
// exactly "closest" display, but good enough to pick one
// appropriate (and there are at most two displays).
int64 distance = diff.x() * diff.x() + diff.y() * diff.y();
if (closest_distance < 0 || closest_distance > distance) {
// We don't care about actual distance, only relative to other displays, so
// using the LengthSquared() is cheaper than Length().
int64 distance_squared = (center - location_in_screen).LengthSquared();
if (closest_distance_squared < 0 ||
closest_distance_squared > distance_squared) {
target_location = center;
closest_distance = distance;
closest_distance_squared = distance_squared;
}
}

Expand Down
2 changes: 1 addition & 1 deletion ash/drag_drop/drag_drop_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ASH_EXPORT DragDropController
void Cleanup();

scoped_ptr<DragImageView> drag_image_;
gfx::Point drag_image_offset_;
gfx::Vector2d drag_image_offset_;
const ui::OSExchangeData* drag_data_;
int drag_operation_;

Expand Down
2 changes: 1 addition & 1 deletion ash/launcher/launcher_tooltip_manager_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ TEST_F(LauncherTooltipManagerTest, HideForMouseEvent) {
EXPECT_TRUE(TooltipIsVisible());

// Should hide if the mouse is out of the tooltip.
test_api.set_location(tooltip_rect.origin().Add(gfx::Point(-1, -1)));
test_api.set_location(tooltip_rect.origin().Add(gfx::Vector2d(-1, -1)));
EXPECT_FALSE(event_filter->PreHandleMouseEvent(root_window, &mouse_event));
RunAllPendingInMessageLoop();
EXPECT_FALSE(TooltipIsVisible());
Expand Down
6 changes: 3 additions & 3 deletions ash/launcher/overflow_bubble.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class OverflowBubbleView : public views::BubbleDelegateView {

ShelfAlignment shelf_alignment_;
LauncherView* launcher_view_; // Owned by views hierarchy.
gfx::Point scroll_offset_;
gfx::Vector2d scroll_offset_;

DISALLOW_COPY_AND_ASSIGN(OverflowBubbleView);
};
Expand Down Expand Up @@ -166,8 +166,8 @@ gfx::Size OverflowBubbleView::GetPreferredSize() {
}

void OverflowBubbleView::Layout() {
const gfx::Point origin(-scroll_offset_.x(), -scroll_offset_.y());
launcher_view_->SetBoundsRect(gfx::Rect(origin, GetContentsSize()));
launcher_view_->SetBoundsRect(gfx::Rect(
gfx::PointAtOffsetFromOrigin(-scroll_offset_), GetContentsSize()));
}

void OverflowBubbleView::ChildPreferredSizeChanged(views::View* child) {
Expand Down
5 changes: 2 additions & 3 deletions ash/tooltips/tooltip_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,8 @@ void TooltipController::UpdateIfRequired() {
GetTooltip()->Hide();
} else {
string16 tooltip_text(tooltip_text_);
gfx::Point widget_loc = curr_mouse_loc_;
widget_loc = widget_loc.Add(
tooltip_window_->GetBoundsInScreen().origin());
gfx::Point widget_loc = curr_mouse_loc_.Add(
tooltip_window_->GetBoundsInScreen().OffsetFromOrigin());
GetTooltip()->SetText(tooltip_text, widget_loc);
GetTooltip()->Show();
tooltip_shown_timer_.Start(FROM_HERE,
Expand Down
4 changes: 2 additions & 2 deletions ash/wm/gestures/long_press_affordance_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ class LongPressAffordanceHandler::LongPressAffordanceView
gfx::Transform scale;
scale.SetScale(current_scale_, current_scale_);
// We want to scale from the center.
canvas->Translate(gfx::Point(center.x(), center.y()));
canvas->Translate(center.OffsetFromOrigin());
canvas->Transform(scale);
canvas->Translate(gfx::Point(-center.x(), -center.y()));
canvas->Translate(-center.OffsetFromOrigin());

// Paint affordance glow
int start_radius = kAffordanceInnerRadius - kAffordanceGlowWidth;
Expand Down
2 changes: 1 addition & 1 deletion ash/wm/toplevel_window_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ ui::EventResult ToplevelWindowEventHandler::OnGestureEvent(

aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
aura::Window* source,
const gfx::Point& drag_offset) {
const gfx::Vector2d& drag_offset) {
DCHECK(!in_move_loop_); // Can only handle one nested loop at a time.
in_move_loop_ = true;
move_cancelled_ = false;
Expand Down
2 changes: 1 addition & 1 deletion ash/wm/toplevel_window_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ASH_EXPORT ToplevelWindowEventHandler
// Overridden form aura::client::WindowMoveClient:
virtual aura::client::WindowMoveResult RunMoveLoop(
aura::Window* source,
const gfx::Point& drag_offset) OVERRIDE;
const gfx::Vector2d& drag_offset) OVERRIDE;
virtual void EndMoveLoop() OVERRIDE;

// Overridden form ash::DisplayController::Observer:
Expand Down
17 changes: 7 additions & 10 deletions ash/wm/workspace/frame_maximize_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,11 +337,9 @@ void FrameMaximizeButton::ProcessStartEvent(const ui::LocatedEvent& event) {

void FrameMaximizeButton::ProcessUpdateEvent(const ui::LocatedEvent& event) {
DCHECK(is_snap_enabled_);
int delta_x = event.x() - press_location_.x();
int delta_y = event.y() - press_location_.y();
if (!exceeded_drag_threshold_) {
exceeded_drag_threshold_ =
views::View::ExceededDragThreshold(delta_x, delta_y);
exceeded_drag_threshold_ = views::View::ExceededDragThreshold(
event.location() - press_location_);
}
if (exceeded_drag_threshold_)
UpdateSnap(event.location(), false);
Expand Down Expand Up @@ -449,15 +447,14 @@ void FrameMaximizeButton::UpdateSnap(const gfx::Point& location,
SnapType FrameMaximizeButton::SnapTypeForLocation(
const gfx::Point& location) const {
MaximizeBubbleFrameState maximize_type = GetMaximizeBubbleFrameState();
int delta_x = location.x() - press_location_.x();
int delta_y = location.y() - press_location_.y();
if (!views::View::ExceededDragThreshold(delta_x, delta_y))
gfx::Vector2d delta(location - press_location_);
if (!views::View::ExceededDragThreshold(delta))
return maximize_type != FRAME_STATE_FULL ? SNAP_MAXIMIZE : SNAP_RESTORE;
else if (delta_x < 0 && delta_y > delta_x && delta_y < -delta_x)
if (delta.x() < 0 && delta.y() > delta.x() && delta.y() < -delta.x())
return maximize_type == FRAME_STATE_SNAP_LEFT ? SNAP_RESTORE : SNAP_LEFT;
else if (delta_x > 0 && delta_y > -delta_x && delta_y < delta_x)
if (delta.x() > 0 && delta.y() > -delta.x() && delta.y() < delta.x())
return maximize_type == FRAME_STATE_SNAP_RIGHT ? SNAP_RESTORE : SNAP_RIGHT;
else if (delta_y > 0)
if (delta.y() > 0)
return SNAP_MINIMIZE;
return maximize_type != FRAME_STATE_FULL ? SNAP_MAXIMIZE : SNAP_RESTORE;
}
Expand Down
8 changes: 5 additions & 3 deletions ash/wm/workspace/workspace_event_handler_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ TEST_F(WorkspaceEventHandlerTest, DeleteWhenDragging) {
aura::test::EventGenerator generator(window->GetRootWindow());
generator.MoveMouseToCenterOf(window.get());
generator.PressLeftButton();
generator.MoveMouseTo(generator.current_location().Add(gfx::Point(50, 50)));
generator.MoveMouseTo(
generator.current_location().Add(gfx::Vector2d(50, 50)));
DCHECK_NE(bounds.origin().ToString(), window->bounds().origin().ToString());
window.reset();
generator.MoveMouseTo(generator.current_location().Add(gfx::Point(50, 50)));
generator.MoveMouseTo(
generator.current_location().Add(gfx::Vector2d(50, 50)));
}

// Verifies deleting the window while in a run loop doesn't crash.
Expand All @@ -170,7 +172,7 @@ TEST_F(WorkspaceEventHandlerTest, DeleteWhileInRunLoop) {
ASSERT_TRUE(aura::client::GetWindowMoveClient(window->parent()));
MessageLoop::current()->DeleteSoon(FROM_HERE, window.get());
aura::client::GetWindowMoveClient(window->parent())->RunMoveLoop(
window.release(), gfx::Point());
window.release(), gfx::Vector2d());
}

} // namespace internal
Expand Down
4 changes: 1 addition & 3 deletions cc/gl_renderer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,8 @@ void GLRenderer::drawTileQuad(const DrawingFrame& frame, const TileDrawQuad* qua
float clampX = min(0.5, clampRect.width() / 2.0 - epsilon);
float clampY = min(0.5, clampRect.height() / 2.0 - epsilon);
clampRect.Inset(clampX, clampY, clampX, clampY);
gfx::PointF clampOffset = clampRect.origin() - tileRect.origin();

gfx::PointF textureOffset = quad->textureOffset() + clampOffset +
(tileRect.origin() - quad->quadRect().origin());
gfx::PointF textureOffset = quad->textureOffset() + clampRect.OffsetFromOrigin() - quad->quadRect().OffsetFromOrigin();

// Map clamping rectangle to unit square.
float vertexTexTranslateX = -clampRect.x() / clampRect.width();
Expand Down
10 changes: 5 additions & 5 deletions chrome/browser/automation/testing_automation_provider_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ void TestingAutomationProvider::WindowGetViewBounds(int handle,
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
*bounds = gfx::Rect(allocation.width, allocation.height);
gint x, y;
gfx::Point origin;
if (screen_coordinates) {
gfx::Point point = ui::GetWidgetScreenPosition(widget);
x = point.x();
y = point.y();
origin = gfx::PointAtOffsetFromOrigin(ui::GetWidgetScreenOffset(widget));
} else {
gint x, y;
gtk_widget_translate_coordinates(widget, GTK_WIDGET(window),
0, 0, &x, &y);
origin = gfx::Point(x, y);
}
bounds->set_origin(gfx::Point(x, y));
bounds->set_origin(origin);
}
}

Expand Down
8 changes: 4 additions & 4 deletions chrome/browser/notifications/balloon.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class Balloon {
}
void SetPosition(const gfx::Point& upper_left, bool reposition);

const gfx::Point& offset() { return offset_;}
void set_offset(const gfx::Point& offset) { offset_ = offset; }
void add_offset(const gfx::Point& offset) { offset_ = offset_.Add(offset); }
const gfx::Vector2d& offset() const { return offset_; }
void set_offset(const gfx::Vector2d& offset) { offset_ = offset; }
void add_offset(const gfx::Vector2d& offset) { offset_.Add(offset); }

const gfx::Size& content_size() const { return content_size_; }
void set_content_size(const gfx::Size& size) { content_size_ = size; }
Expand Down Expand Up @@ -126,7 +126,7 @@ class Balloon {

// Temporary offset for balloons that need to be positioned in a non-standard
// position for keeping the close buttons under the mouse cursor.
gfx::Point offset_;
gfx::Vector2d offset_;

// Smallest size for this balloon where scrollbars will be shown.
gfx::Size min_scrollbar_size_;
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/notifications/balloon_collection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) {

Balloons::const_iterator it = balloons.begin();
if (layout_.RequiresOffsets()) {
gfx::Point offset;
gfx::Vector2d offset;
bool apply_offset = false;
while (it != balloons.end()) {
if (*it == source) {
Expand Down Expand Up @@ -249,7 +249,7 @@ void BalloonCollectionImpl::CancelOffsets() {
for (Balloons::const_iterator it = balloons.begin();
it != balloons.end();
++it)
(*it)->set_offset(gfx::Point(0, 0));
(*it)->set_offset(gfx::Vector2d());

PositionBalloons(true);
}
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/gtk/bookmarks/bookmark_bar_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1176,8 +1176,8 @@ void BookmarkBarGtk::OnButtonDragBegin(GtkWidget* button,
gtk_widget_size_request(drag_icon_, &req);
gfx::Rect button_rect = gtk_util::WidgetBounds(button);
gfx::Point drag_icon_relative =
gfx::Rect(req.width, req.height).CenterPoint().Add(
(last_pressed_coordinates_.Subtract(button_rect.CenterPoint())));
gfx::Rect(req.width, req.height).CenterPoint() +
(last_pressed_coordinates_ - button_rect.CenterPoint());
gtk_drag_set_icon_widget(drag_context, drag_icon_,
drag_icon_relative.x(),
drag_icon_relative.y());
Expand Down
6 changes: 1 addition & 5 deletions chrome/browser/ui/gtk/download/download_shelf_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,7 @@ bool DownloadShelfGtk::IsCursorInShelfZone(
if (!realized)
return false;

GtkAllocation allocation;
gtk_widget_get_allocation(shelf_.get(), &allocation);

gfx::Rect bounds(ui::GetWidgetScreenPosition(shelf_.get()),
gfx::Size(allocation.width, allocation.height));
gfx::Rect bounds = ui::GetWidgetScreenBounds(shelf_.get());

// Negative insets expand the rectangle. We only expand the top.
bounds.Inset(gfx::Insets(-kShelfAuraSize, 0, 0, 0));
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/ui/gtk/gtk_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,7 @@ void ConvertWidgetPointToScreen(GtkWidget* widget, gfx::Point* p) {
DCHECK(widget);
DCHECK(p);

gfx::Point position = ui::GetWidgetScreenPosition(widget);
p->SetPoint(p->x() + position.x(), p->y() + position.y());
*p = p->Add(ui::GetWidgetScreenOffset(widget));
}

GtkWidget* CenterWidgetInHBox(GtkWidget* hbox, GtkWidget* widget,
Expand Down
7 changes: 2 additions & 5 deletions chrome/browser/ui/gtk/tabs/dragged_tab_controller_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,7 @@ void DraggedTabControllerGtk::Detach() {

gfx::Point DraggedTabControllerGtk::ConvertScreenPointToTabStripPoint(
TabStripGtk* tabstrip, const gfx::Point& screen_point) {
gfx::Point tabstrip_screen_point =
ui::GetWidgetScreenPosition(tabstrip->tabstrip_.get());
return screen_point.Subtract(tabstrip_screen_point);
return screen_point - ui::GetWidgetScreenOffset(tabstrip->tabstrip_.get());
}

gfx::Rect DraggedTabControllerGtk::GetDraggedViewTabStripBounds(
Expand Down Expand Up @@ -818,8 +816,7 @@ gfx::Rect DraggedTabControllerGtk::GetAnimateBounds() {
gfx::Rect bounds = tab->GetRequisition();
GtkWidget* widget = tab->widget();
GtkWidget* parent = gtk_widget_get_parent(widget);
gfx::Point point = ui::GetWidgetScreenPosition(parent);
bounds.Offset(point);
bounds.Offset(ui::GetWidgetScreenOffset(parent));

return gfx::Rect(bounds.x(), bounds.y(),
dragged_view_->GetTotalWidthInTabStrip(), bounds.height());
Expand Down
12 changes: 3 additions & 9 deletions chrome/browser/ui/gtk/tabs/tab_renderer_gtk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,9 @@ const int kCloseButtonHorzFuzz = 4;
// Gets the bounds of |widget| relative to |parent|.
gfx::Rect GetWidgetBoundsRelativeToParent(GtkWidget* parent,
GtkWidget* widget) {
gfx::Point parent_pos = ui::GetWidgetScreenPosition(parent);
gfx::Point widget_pos = ui::GetWidgetScreenPosition(widget);

GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);

return gfx::Rect(widget_pos.x() - parent_pos.x(),
widget_pos.y() - parent_pos.y(),
allocation.width, allocation.height);
gfx::Rect bounds = ui::GetWidgetScreenBounds(widget);
bounds.Offset(-ui::GetWidgetScreenOffset(parent));
return bounds;
}

// Returns a GdkPixbuf after resizing the SkBitmap as necessary. Caller must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class PanelAndDesktopNotificationTest : public BasePanelBrowserTest {

static void ResizePanelByMouseWithDelta(Panel* panel,
panel::ResizingSides side,
const gfx::Point& delta) {
const gfx::Vector2d& delta) {
PanelManager* panel_manager = PanelManager::GetInstance();
gfx::Point mouse_location = panel->GetBounds().origin();
panel_manager->StartResizingByMouse(panel, mouse_location, side);
Expand Down Expand Up @@ -308,15 +308,15 @@ IN_PROC_BROWSER_TEST_F(PanelAndDesktopNotificationTest, ResizePanelByMouse) {

// Resize the panel to make it taller. Expect that the notification balloon
// moves further up by the amount of enlarge offset.
gfx::Point drag_delta(-50, -100);
gfx::Vector2d drag_delta(-50, -100);
ResizePanelByMouseWithDelta(panel, panel::RESIZE_TOP_LEFT, drag_delta);
MessageLoopForUI::current()->RunAllPending();
int balloon_bottom2 = GetBalloonBottomPosition(balloon);
EXPECT_EQ(balloon_bottom + drag_delta.y(), balloon_bottom2);

// Resize the panel to make it shorter. Expect that the notification balloon
// moves down by the amount of shrink offset.
drag_delta = gfx::Point(0, 60);
drag_delta = gfx::Vector2d(0, 60);
ResizePanelByMouseWithDelta(panel, panel::RESIZE_TOP, drag_delta);
MessageLoopForUI::current()->RunAllPending();
int balloon_bottom3 = GetBalloonBottomPosition(balloon);
Expand Down
2 changes: 1 addition & 1 deletion chrome/browser/ui/panels/panel_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ IN_PROC_BROWSER_TEST_F(PanelBrowserTest, MAYBE_AnimateBounds) {
// dragged.
gfx::Point mouse_location(panel->GetBounds().origin());
panel_testing->PressLeftMouseButtonTitlebar(mouse_location);
panel_testing->DragTitlebar(mouse_location.Add(gfx::Point(-100, 5)));
panel_testing->DragTitlebar(mouse_location.Add(gfx::Vector2d(-100, 5)));
EXPECT_FALSE(panel_testing->IsAnimatingBounds());
panel_testing->FinishDragTitlebar();

Expand Down
Loading

0 comments on commit ceb36f7

Please sign in to comment.