diff options
author | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-28 23:46:20 +0000 |
---|---|---|
committer | derat@chromium.org <derat@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-28 23:46:20 +0000 |
commit | 68bd9289ad689ff35a4efe5755ea06d9e4230297 (patch) | |
tree | d08291c13ede612446b5d3509662498315fe8c32 /ui/base/cursor | |
parent | 2f264fc0b3981e9c72965a4d3d18be75c935d133 (diff) | |
download | chromium_src-68bd9289ad689ff35a4efe5755ea06d9e4230297.zip chromium_src-68bd9289ad689ff35a4efe5755ea06d9e4230297.tar.gz chromium_src-68bd9289ad689ff35a4efe5755ea06d9e4230297.tar.bz2 |
app_shell: Add support for mouse cursors.
Move ImageCursors from ash/wm/ to ui/base/cursors/ and add a
NativeCursorManager implementation for
ShellDesktopController to use.
BUG=364290
TEST=cursor is visible when running app_shell on a Chrome OS
device and changes after mousing over a link
Review URL: https://codereview.chromium.org/258893002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266694 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/cursor')
-rw-r--r-- | ui/base/cursor/image_cursors.cc | 150 | ||||
-rw-r--r-- | ui/base/cursor/image_cursors.h | 52 |
2 files changed, 202 insertions, 0 deletions
diff --git a/ui/base/cursor/image_cursors.cc b/ui/base/cursor/image_cursors.cc new file mode 100644 index 0000000..b4c19fc --- /dev/null +++ b/ui/base/cursor/image_cursors.cc @@ -0,0 +1,150 @@ +// Copyright 2014 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 "ui/base/cursor/image_cursors.h" + +#include <float.h> + +#include "base/logging.h" +#include "base/strings/string16.h" +#include "ui/base/cursor/cursor.h" +#include "ui/base/cursor/cursor_loader.h" +#include "ui/base/cursor/cursors_aura.h" +#include "ui/gfx/display.h" +#include "ui/gfx/point.h" + +namespace ui { + +namespace { + +const int kImageCursorIds[] = { + kCursorNull, + kCursorPointer, + kCursorNoDrop, + kCursorNotAllowed, + kCursorCopy, + kCursorHand, + kCursorMove, + kCursorNorthEastResize, + kCursorSouthWestResize, + kCursorSouthEastResize, + kCursorNorthWestResize, + kCursorNorthResize, + kCursorSouthResize, + kCursorEastResize, + kCursorWestResize, + kCursorIBeam, + kCursorAlias, + kCursorCell, + kCursorContextMenu, + kCursorCross, + kCursorHelp, + kCursorVerticalText, + kCursorZoomIn, + kCursorZoomOut, + kCursorRowResize, + kCursorColumnResize, + kCursorEastWestResize, + kCursorNorthSouthResize, + kCursorNorthEastSouthWestResize, + kCursorNorthWestSouthEastResize, + kCursorGrab, + kCursorGrabbing, +}; + +const int kAnimatedCursorIds[] = { + kCursorWait, + kCursorProgress +}; + +} // namespace + +ImageCursors::ImageCursors() : cursor_set_(CURSOR_SET_NORMAL) { +} + +ImageCursors::~ImageCursors() { +} + +float ImageCursors::GetScale() const { + if (!cursor_loader_) { + NOTREACHED(); + // Returning default on release build as it's not serious enough to crash + // even if this ever happens. + return 1.0f; + } + return cursor_loader_->scale(); +} + +gfx::Display::Rotation ImageCursors::GetRotation() const { + if (!cursor_loader_) { + NOTREACHED(); + // Returning default on release build as it's not serious enough to crash + // even if this ever happens. + return gfx::Display::ROTATE_0; + } + return cursor_loader_->rotation(); +} + +bool ImageCursors::SetDisplay(const gfx::Display& display, + float scale_factor) { + if (!cursor_loader_) { + cursor_loader_.reset(CursorLoader::Create()); + } else if (cursor_loader_->rotation() == display.rotation() && + cursor_loader_->scale() == scale_factor) { + return false; + } + + cursor_loader_->set_rotation(display.rotation()); + cursor_loader_->set_scale(scale_factor); + ReloadCursors(); + return true; +} + +void ImageCursors::ReloadCursors() { + float device_scale_factor = cursor_loader_->scale(); + + cursor_loader_->UnloadAll(); + + for (size_t i = 0; i < arraysize(kImageCursorIds); ++i) { + int resource_id = -1; + gfx::Point hot_point; + bool success = GetCursorDataFor(cursor_set_, + kImageCursorIds[i], + device_scale_factor, + &resource_id, + &hot_point); + DCHECK(success); + cursor_loader_->LoadImageCursor(kImageCursorIds[i], resource_id, hot_point); + } + for (size_t i = 0; i < arraysize(kAnimatedCursorIds); ++i) { + int resource_id = -1; + gfx::Point hot_point; + bool success = GetAnimatedCursorDataFor(cursor_set_, + kAnimatedCursorIds[i], + device_scale_factor, + &resource_id, + &hot_point); + DCHECK(success); + cursor_loader_->LoadAnimatedCursor(kAnimatedCursorIds[i], + resource_id, + hot_point, + kAnimatedCursorFrameDelayMs); + } +} + +void ImageCursors::SetCursorSet(CursorSetType cursor_set) { + if (cursor_set_ == cursor_set) + return; + + cursor_set_ = cursor_set; + + if (cursor_loader_.get()) + ReloadCursors(); +} + +void ImageCursors::SetPlatformCursor(gfx::NativeCursor* cursor) { + cursor_loader_->SetPlatformCursor(cursor); +} + +} // namespace ui diff --git a/ui/base/cursor/image_cursors.h b/ui/base/cursor/image_cursors.h new file mode 100644 index 0000000..25676ca --- /dev/null +++ b/ui/base/cursor/image_cursors.h @@ -0,0 +1,52 @@ +// Copyright 2014 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. + +#ifndef UI_BASE_CURSOR_IMAGE_CURSORS_H_ +#define UI_BASE_CURSOR_IMAGE_CURSORS_H_ + +#include "base/memory/scoped_ptr.h" +#include "base/strings/string16.h" +#include "ui/base/cursor/cursor.h" +#include "ui/base/ui_base_export.h" +#include "ui/gfx/display.h" +#include "ui/gfx/native_widget_types.h" + +namespace ui { + +class CursorLoader; + +// A utility class that provides cursors for NativeCursors for which we have +// image resources. +class UI_BASE_EXPORT ImageCursors { + public: + ImageCursors(); + ~ImageCursors(); + + // Returns the scale and rotation of the currently loaded cursor. + float GetScale() const; + gfx::Display::Rotation GetRotation() const; + + // Sets the display the cursors are loaded for. |scale_factor| determines the + // size of the image to load. Returns true if the cursor image is reloaded. + bool SetDisplay(const gfx::Display& display, float scale_factor); + + // Sets the type of the mouse cursor icon. + void SetCursorSet(CursorSetType cursor_set); + + // Sets the platform cursor based on the native type of |cursor|. + void SetPlatformCursor(gfx::NativeCursor* cursor); + + private: + // Reloads the all loaded cursors in the cursor loader. + void ReloadCursors(); + + scoped_ptr<CursorLoader> cursor_loader_; + CursorSetType cursor_set_; + + DISALLOW_COPY_AND_ASSIGN(ImageCursors); +}; + +} // namespace ui + +#endif // UI_BASE_CURSOR_IMAGE_CURSORS_H_ |