Index: ash/display/cursor_window_controller.cc |
diff --git a/ash/display/cursor_window_controller.cc b/ash/display/cursor_window_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fcbdadb8a32ef2d19a5a83d06ccd5de16640f470 |
--- /dev/null |
+++ b/ash/display/cursor_window_controller.cc |
@@ -0,0 +1,245 @@ |
+// 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 "ash/display/cursor_window_controller.h" |
+ |
+#include "ash/shell.h" |
+#include "ui/aura/env.h" |
+#include "ui/aura/root_window.h" |
+#include "ui/aura/window_delegate.h" |
+#include "ui/base/cursor/cursors_aura.h" |
+#include "ui/base/hit_test.h" |
+#include "ui/base/resource/resource_bundle.h" |
+#include "ui/compositor/dip_util.h" |
+#include "ui/gfx/canvas.h" |
+#include "ui/gfx/display.h" |
+#include "ui/gfx/image/image_skia.h" |
+#include "ui/gfx/image/image_skia_operations.h" |
+ |
+namespace ash { |
+namespace internal { |
+ |
+class CursorWindowDelegate : public aura::WindowDelegate { |
+ public: |
+ CursorWindowDelegate() : is_mirroring_(false) {} |
+ virtual ~CursorWindowDelegate() {} |
+ |
+ // aura::WindowDelegate overrides: |
+ virtual gfx::Size GetMinimumSize() const OVERRIDE { |
+ return size_; |
+ } |
+ virtual gfx::Size GetMaximumSize() const OVERRIDE { |
+ return size_; |
+ } |
+ virtual void OnBoundsChanged(const gfx::Rect& old_bounds, |
+ const gfx::Rect& new_bounds) OVERRIDE { |
+ } |
+ virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { |
+ return gfx::kNullCursor; |
+ } |
+ virtual int GetNonClientComponent( |
+ const gfx::Point& point) const OVERRIDE { |
+ return HTNOWHERE; |
+ } |
+ virtual bool ShouldDescendIntoChildForEventHandling( |
+ aura::Window* child, |
+ const gfx::Point& location) OVERRIDE { |
+ return false; |
+ } |
+ virtual bool CanFocus() OVERRIDE { |
Daniel Erat
2014/02/05 22:01:32
nit: this file will get shorter and probably easie
hshi1
2014/02/05 22:55:58
Done.
|
+ return false; |
+ } |
+ virtual void OnCaptureLost() OVERRIDE { |
+ } |
+ virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { |
+ canvas->DrawImageInt(cursor_image_, 0, 0); |
+ } |
+ virtual void OnDeviceScaleFactorChanged( |
+ float device_scale_factor) OVERRIDE { |
+ } |
+ virtual void OnWindowDestroying() OVERRIDE {} |
+ virtual void OnWindowDestroyed() OVERRIDE {} |
+ virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE { |
+ } |
+ virtual bool HasHitTestMask() const OVERRIDE { |
+ return false; |
+ } |
+ virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} |
+ virtual void DidRecreateLayer(ui::Layer* old_layer, |
+ ui::Layer* new_layer) OVERRIDE {} |
+ |
+ // Sets mirroring mode on/off. |
+ void SetMirroring(bool is_mirroring) { |
+ is_mirroring_ = is_mirroring; |
+ } |
+ |
+ // Sets the cursor image for the |display|'s scale factor. |
+ void SetCursorImage(const gfx::ImageSkia& image, |
+ const gfx::Display& display) { |
+ float scale_factor = display.device_scale_factor(); |
+ const gfx::ImageSkiaRep& image_rep = image.GetRepresentation(scale_factor); |
+ if (is_mirroring_) { |
+ // Note that mirror window's scale factor is always 1.0f, therefore we |
+ // need to take 2x's image and paint as if it's 1x image. |
+ size_ = image_rep.pixel_size(); |
+ cursor_image_ = gfx::ImageSkia::CreateFrom1xBitmap(image_rep.sk_bitmap()); |
+ } else { |
+ size_ = image.size(); |
+ cursor_image_ = gfx::ImageSkia( |
+ gfx::ImageSkiaRep(image_rep.sk_bitmap(), scale_factor)); |
+ } |
+ } |
+ |
+ const gfx::Size size() const { return size_; } |
+ |
+ private: |
+ bool is_mirroring_; |
+ gfx::ImageSkia cursor_image_; |
+ gfx::Size size_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(CursorWindowDelegate); |
+}; |
+ |
+CursorWindowController::CursorWindowController() |
+ : is_mirroring_(false), |
+ current_container_(NULL), |
+ current_cursor_type_(ui::kCursorNone), |
+ current_cursor_set_(ui::CURSOR_SET_NORMAL), |
+ requested_cursor_set_(ui::CURSOR_SET_NORMAL), |
+ current_cursor_rotation_(gfx::Display::ROTATE_0), |
+ delegate_(new CursorWindowDelegate()) { |
+} |
+ |
+CursorWindowController::~CursorWindowController() { |
+ SetContainer(NULL); |
+} |
+ |
+void CursorWindowController::SetMirroring(bool is_mirroring) { |
+ if (is_mirroring_ != is_mirroring) { |
+ is_mirroring_ = is_mirroring; |
+ delegate_->SetMirroring(is_mirroring); |
+ } |
+} |
+ |
+void CursorWindowController::SetContainer(aura::Window* container) { |
+ if (current_container_ == container) |
+ return; |
+ |
+ current_container_ = container; |
+ if (!container) { |
+ cursor_window_.reset(); |
+ return; |
+ } |
+ |
+ if (!cursor_window_) { |
+ cursor_window_.reset(new aura::Window(delegate_.get())); |
+ cursor_window_->SetTransparent(true); |
+ cursor_window_->Init(aura::WINDOW_LAYER_TEXTURED); |
+ cursor_window_->set_ignore_events(true); |
+ cursor_window_->set_owned_by_parent(false); |
+ } |
+ |
+ container->AddChild(cursor_window_.get()); |
+ cursor_window_->Show(); |
+ SetDisplayBounds(container->bounds()); |
+} |
+ |
+void CursorWindowController::SetDisplayBounds(const gfx::Rect& display_bounds) { |
+ current_display_bounds_ = display_bounds; |
+ UpdateLocation(); |
+} |
+ |
+void CursorWindowController::UpdateLocation() { |
+ if (!cursor_window_) |
+ return; |
+ |
+ gfx::Point point = aura::Env::GetInstance()->last_mouse_location(); |
+ if (is_mirroring_) { |
+ Shell::GetPrimaryRootWindow()->GetDispatcher()->host()->ConvertPointToHost( |
+ &point); |
+ } else { |
+ point.Offset(-current_display_bounds_.x(), -current_display_bounds_.y()); |
+ } |
+ point.Offset(-hot_point_.x(), -hot_point_.y()); |
+ gfx::Rect bounds = cursor_window_->bounds(); |
+ bounds.set_origin(point); |
+ cursor_window_->SetBounds(bounds); |
+} |
+ |
+void CursorWindowController::SetCursor(gfx::NativeCursor cursor) { |
+ const gfx::Display& display = Shell::GetScreen()->GetPrimaryDisplay(); |
+ if (current_cursor_type_ == cursor.native_type() && |
+ current_cursor_set_ == requested_cursor_set_ && |
+ current_cursor_rotation_ == display.rotation()) |
+ return; |
+ current_cursor_type_ = cursor.native_type(); |
+ current_cursor_set_ = requested_cursor_set_; |
+ current_cursor_rotation_ = display.rotation(); |
+ int resource_id; |
+ bool success = ui::GetCursorDataFor( |
+ current_cursor_set_, // Not support custom cursor set. |
Daniel Erat
2014/02/05 22:01:32
nit: s/Not/Don't/ ?
hshi1
2014/02/05 22:55:58
Marked custom cursor support as a TODO. (This was
|
+ current_cursor_type_, |
+ display.device_scale_factor(), |
+ &resource_id, |
+ &hot_point_); |
+ if (!success) |
+ return; |
+ const gfx::ImageSkia* image = |
+ ResourceBundle::GetSharedInstance().GetImageSkiaNamed(resource_id); |
+ gfx::ImageSkia rotated = *image; |
+ if (is_mirroring_) { |
+ switch (current_cursor_rotation_) { |
+ case gfx::Display::ROTATE_0: |
+ break; |
+ case gfx::Display::ROTATE_90: |
+ rotated = gfx::ImageSkiaOperations::CreateRotatedImage( |
+ *image, SkBitmapOperations::ROTATION_90_CW); |
+ hot_point_.SetPoint( |
+ rotated.width() - hot_point_.y(), |
+ hot_point_.x()); |
+ break; |
+ case gfx::Display::ROTATE_180: |
+ rotated = gfx::ImageSkiaOperations::CreateRotatedImage( |
+ *image, SkBitmapOperations::ROTATION_180_CW); |
+ hot_point_.SetPoint( |
+ rotated.height() - hot_point_.x(), |
+ rotated.width() - hot_point_.y()); |
+ break; |
+ case gfx::Display::ROTATE_270: |
+ rotated = gfx::ImageSkiaOperations::CreateRotatedImage( |
+ *image, SkBitmapOperations::ROTATION_270_CW); |
+ hot_point_.SetPoint( |
+ hot_point_.y(), |
+ rotated.height() - hot_point_.x()); |
+ break; |
+ } |
+ } else { |
+ hot_point_ = ui::ConvertPointToDIP(Shell::GetPrimaryRootWindow()->layer(), |
+ hot_point_); |
+ } |
+ delegate_->SetCursorImage(rotated, display); |
+ if (cursor_window_) { |
+ cursor_window_->SetBounds(gfx::Rect(delegate_->size())); |
+ cursor_window_->SchedulePaintInRect( |
+ gfx::Rect(cursor_window_->bounds().size())); |
+ UpdateLocation(); |
+ } |
+} |
+ |
+void CursorWindowController::SetCursorSet(ui::CursorSetType cursor_set) { |
+ requested_cursor_set_ = cursor_set; |
+ SetCursor(gfx::NativeCursor(current_cursor_type_)); |
+} |
+ |
+void CursorWindowController::SetVisibility(bool visible) { |
+ if (!cursor_window_) |
+ return; |
+ if (visible) |
+ cursor_window_->Show(); |
+ else |
+ cursor_window_->Hide(); |
+} |
+ |
+} // namespace internal |
+} // namespace ash |