Skip to content

Commit

Permalink
views: Account for device scale factor when drawing drag images for v…
Browse files Browse the repository at this point in the history
…iews.

BUG=128820
TEST=manual


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@143052 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
varunjain@chromium.org committed Jun 19, 2012
1 parent 539df6c commit c600edb
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 13 deletions.
8 changes: 5 additions & 3 deletions chrome/browser/ui/views/bookmarks/bookmark_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/drag_utils.h"
#include "ui/views/metrics.h"
#include "ui/views/view_constants.h"
#include "ui/views/widget/tooltip_manager.h"
Expand Down Expand Up @@ -940,9 +941,10 @@ void BookmarkBarView::WriteDragDataForView(View* sender,
for (int i = 0; i < GetBookmarkButtonCount(); ++i) {
if (sender == GetBookmarkButton(i)) {
views::TextButton* button = GetBookmarkButton(i);
gfx::Canvas canvas(button->size(), false);
button->PaintButton(&canvas, views::TextButton::PB_FOR_DRAG);
drag_utils::SetDragImageOnDataObject(canvas, button->size(), press_pt,
scoped_ptr<gfx::Canvas> canvas(
views::GetCanvasForDragImage(button->GetWidget(), button->size()));
button->PaintButton(canvas.get(), views::TextButton::PB_FOR_DRAG);
drag_utils::SetDragImageOnDataObject(*canvas, button->size(), press_pt,
data);
WriteBookmarkDragData(model_->bookmark_bar_node()->GetChild(i), data);
return;
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/ui/views/location_bar/location_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1191,7 +1191,8 @@ void LocationBarView::WriteDragDataForView(views::View* sender,
tab_contents->web_contents()->GetURL(),
tab_contents->web_contents()->GetTitle(),
tab_contents->favicon_tab_helper()->GetFavicon(),
data);
data,
sender->GetWidget());
}

int LocationBarView::GetDragOperationsForView(views::View* sender,
Expand Down
3 changes: 2 additions & 1 deletion chrome/browser/ui/views/omnibox/omnibox_view_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2524,7 +2524,8 @@ void OmniboxViewWin::StartDragIfNecessary(const CPoint& point) {
SkBitmap favicon;
if (is_all_selected)
model_->GetDataForURLExport(&url, &title, &favicon);
button_drag_utils::SetURLAndDragImage(url, title, favicon, &data);
button_drag_utils::SetURLAndDragImage(url, title, favicon, &data,
native_view_host_->GetWidget());
supported_modes |= DROPEFFECT_LINK;
content::RecordAction(UserMetricsAction("Omnibox_DragURL"));
} else {
Expand Down
11 changes: 7 additions & 4 deletions ui/views/button_drag_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/button/text_button.h"
#include "ui/views/drag_utils.h"

namespace button_drag_utils {

Expand All @@ -22,7 +23,8 @@ static const int kLinkDragImageMaxWidth = 200;
void SetURLAndDragImage(const GURL& url,
const string16& title,
const gfx::ImageSkia& icon,
ui::OSExchangeData* data) {
ui::OSExchangeData* data,
views::Widget* widget) {
DCHECK(url.is_valid() && data);

data->SetURL(url, title);
Expand All @@ -41,9 +43,10 @@ void SetURLAndDragImage(const GURL& url,
button.SetBounds(0, 0, prefsize.width(), prefsize.height());

// Render the image.
gfx::Canvas canvas(prefsize, false);
button.PaintButton(&canvas, views::TextButton::PB_FOR_DRAG);
drag_utils::SetDragImageOnDataObject(canvas, prefsize,
scoped_ptr<gfx::Canvas> canvas(
views::GetCanvasForDragImage(widget, prefsize));
button.PaintButton(canvas.get(), views::TextButton::PB_FOR_DRAG);
drag_utils::SetDragImageOnDataObject(*canvas, prefsize,
gfx::Point(prefsize.width() / 2, prefsize.height() / 2), data);
}

Expand Down
7 changes: 6 additions & 1 deletion ui/views/button_drag_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ namespace ui {
class OSExchangeData;
}

namespace views {
class Widget;
}

namespace button_drag_utils {

// Sets url and title on data as well as setting a suitable image for dragging.
// The image looks like that of the bookmark buttons.
VIEWS_EXPORT void SetURLAndDragImage(const GURL& url,
const string16& title,
const gfx::ImageSkia& icon,
ui::OSExchangeData* data);
ui::OSExchangeData* data,
views::Widget* widget);

} // namespace drag_utils

Expand Down
8 changes: 5 additions & 3 deletions ui/views/controls/menu/menu_controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "ui/views/controls/menu/menu_controller_delegate.h"
#include "ui/views/controls/menu/menu_scroll_view_container.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/drag_utils.h"
#include "ui/views/view_constants.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/root_view.h"
Expand Down Expand Up @@ -849,12 +850,13 @@ void MenuController::StartDrag(SubmenuView* source,
View::ConvertPointToView(NULL, item, &press_loc);
gfx::Point widget_loc(press_loc);
View::ConvertPointToWidget(item, &widget_loc);
gfx::Canvas canvas(gfx::Size(item->width(), item->height()), false);
item->PaintButton(&canvas, MenuItemView::PB_FOR_DRAG);
scoped_ptr<gfx::Canvas> canvas(views::GetCanvasForDragImage(
source->GetWidget(), gfx::Size(item->width(), item->height())));
item->PaintButton(canvas.get(), MenuItemView::PB_FOR_DRAG);

OSExchangeData data;
item->GetDelegate()->WriteDragData(item, &data);
drag_utils::SetDragImageOnDataObject(canvas, item->size(), press_loc,
drag_utils::SetDragImageOnDataObject(*canvas, item->size(), press_loc,
&data);
StopScrolling();
int drag_ops = item->GetDelegate()->GetDragOperations(item);
Expand Down
30 changes: 30 additions & 0 deletions ui/views/drag_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

#include "ui/views/drag_utils.h"

#include "ui/gfx/canvas.h"
#include "ui/gfx/size.h"

#if defined(USE_AURA)
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/root_window.h"
#include "ui/aura/window.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/views/widget/widget.h"
#elif defined(OS_WIN)
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/drag_source.h"
Expand All @@ -16,6 +22,21 @@
#error
#endif

float GetDeviceScaleFactorForNativeView(views::Widget* widget) {
float device_scale_factor = 1;
#if defined(USE_AURA)
// The following code should work on other platforms as well. But we do not
// yet care about device scale factor on other platforms. So to keep drag and
// drop behavior on other platforms un-touched, we wrap this in the #if guard.
if (widget && widget->GetNativeView()) {
gfx::Display display = gfx::Screen::GetDisplayNearestWindow(
widget->GetNativeView());
device_scale_factor = display.device_scale_factor();
}
#endif
return device_scale_factor;
}

namespace views {

void RunShellDrag(gfx::NativeView view,
Expand All @@ -40,4 +61,13 @@ void RunShellDrag(gfx::NativeView view,
#endif
}

gfx::Canvas* GetCanvasForDragImage(views::Widget* widget,
const gfx::Size& canvas_size) {
float device_scale_factor = GetDeviceScaleFactorForNativeView(widget);
gfx::Size scaled_canvas_size = canvas_size.Scale(device_scale_factor);
gfx::Canvas* canvas = new gfx::Canvas(scaled_canvas_size, false);
canvas->Scale(device_scale_factor, device_scale_factor);
return canvas;
}

} // namespace views
8 changes: 8 additions & 0 deletions ui/views/drag_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,29 @@
#include "ui/views/views_export.h"

namespace gfx {
class Canvas;
class Point;
class Size;
}

namespace ui {
class OSExchangeData;
}

namespace views {
class Widget;

// Starts a drag operation. This blocks until the drag operation completes.
VIEWS_EXPORT void RunShellDrag(gfx::NativeView view,
const ui::OSExchangeData& data,
const gfx::Point& location,
int operation);

// Returns a canvas that can be used to draw the drag image. Caller owns the
// returned object. |widget| is Widget hosting the view being dragged.
VIEWS_EXPORT gfx::Canvas* GetCanvasForDragImage(Widget* widget,
const gfx::Size& canvas_size);

} // namespace views

#endif // UI_VIEWS_DRAG_UTILS_H_

0 comments on commit c600edb

Please sign in to comment.