Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ash/wm/ash_native_cursor_manager.h" |
| 6 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 7 | #include "ash/display/cursor_window_controller.h" |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 8 | #include "ash/display/display_controller.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 9 | #include "ash/shell.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "ui/aura/env.h" |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 12 | #include "ui/aura/window_event_dispatcher.h" |
Torne (Richard Coles) | 23730a6 | 2014-03-21 14:25:57 +0000 | [diff] [blame] | 13 | #include "ui/aura/window_tree_host.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 14 | #include "ui/base/cursor/cursor.h" |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 15 | #include "ui/base/cursor/image_cursors.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 16 | |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 17 | namespace ash { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 20 | void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) { |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 21 | aura::Window::Windows root_windows = |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 22 | Shell::GetInstance()->GetAllRootWindows(); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 23 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 24 | iter != root_windows.end(); ++iter) |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 25 | (*iter)->GetHost()->SetCursor(cursor); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 26 | #if defined(OS_CHROMEOS) |
Ben Murdoch | 7dbb3d5 | 2013-07-17 14:55:54 +0100 | [diff] [blame] | 27 | Shell::GetInstance()->display_controller()-> |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 28 | cursor_window_controller()->SetCursor(cursor); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 29 | #endif |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void NotifyCursorVisibilityChange(bool visible) { |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 33 | aura::Window::Windows root_windows = |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 34 | Shell::GetInstance()->GetAllRootWindows(); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 35 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 36 | iter != root_windows.end(); ++iter) |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 37 | (*iter)->GetHost()->OnCursorVisibilityChanged(visible); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 38 | #if defined(OS_CHROMEOS) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 39 | Shell::GetInstance()->display_controller()->cursor_window_controller()-> |
| 40 | SetVisibility(visible); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 41 | #endif |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void NotifyMouseEventsEnableStateChange(bool enabled) { |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 45 | aura::Window::Windows root_windows = |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 46 | Shell::GetInstance()->GetAllRootWindows(); |
Torne (Richard Coles) | f2477e0 | 2013-11-28 11:55:43 +0000 | [diff] [blame] | 47 | for (aura::Window::Windows::iterator iter = root_windows.begin(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 48 | iter != root_windows.end(); ++iter) |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 49 | (*iter)->GetHost()->dispatcher()->OnMouseEventsEnableStateChanged(enabled); |
Torne (Richard Coles) | 90dce4d | 2013-05-29 14:40:03 +0100 | [diff] [blame] | 50 | // Mirror window never process events. |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | } // namespace |
| 54 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 55 | AshNativeCursorManager::AshNativeCursorManager() |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 56 | : native_cursor_enabled_(true), |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 57 | image_cursors_(new ui::ImageCursors) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | AshNativeCursorManager::~AshNativeCursorManager() { |
| 61 | } |
| 62 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 63 | |
| 64 | void AshNativeCursorManager::SetNativeCursorEnabled(bool enabled) { |
| 65 | native_cursor_enabled_ = enabled; |
| 66 | |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 67 | ::wm::CursorManager* cursor_manager = |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 68 | Shell::GetInstance()->cursor_manager(); |
| 69 | SetCursor(cursor_manager->GetCursor(), cursor_manager); |
| 70 | } |
| 71 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 72 | void AshNativeCursorManager::SetDisplay( |
| 73 | const gfx::Display& display, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 74 | ::wm::NativeCursorManagerDelegate* delegate) { |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 75 | DCHECK(display.is_valid()); |
| 76 | // Use the platform's device scale factor instead of the display's, which |
| 77 | // might have been adjusted for the UI scale. |
| 78 | const float scale_factor = Shell::GetInstance()->display_manager()-> |
| 79 | GetDisplayInfo(display.id()).device_scale_factor(); |
| 80 | if (image_cursors_->SetDisplay(display, scale_factor)) |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 81 | SetCursor(delegate->GetCursor(), delegate); |
| 82 | #if defined(OS_CHROMEOS) |
| 83 | Shell::GetInstance()->display_controller()->cursor_window_controller()-> |
| 84 | SetDisplay(display); |
| 85 | #endif |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void AshNativeCursorManager::SetCursor( |
| 89 | gfx::NativeCursor cursor, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 90 | ::wm::NativeCursorManagerDelegate* delegate) { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 91 | if (native_cursor_enabled_) { |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 92 | image_cursors_->SetPlatformCursor(&cursor); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 93 | } else { |
| 94 | gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 95 | image_cursors_->SetPlatformCursor(&invisible_cursor); |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 96 | if (cursor == ui::kCursorCustom) { |
| 97 | cursor = invisible_cursor; |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 98 | } else { |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 99 | cursor.SetPlatformCursor(invisible_cursor.platform()); |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 102 | cursor.set_device_scale_factor(image_cursors_->GetScale()); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 103 | |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 104 | delegate->CommitCursor(cursor); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 105 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 106 | if (delegate->IsCursorVisible()) |
Bo Liu | 5c02ac1 | 2014-05-01 10:37:37 -0700 | [diff] [blame] | 107 | SetCursorOnAllRootWindows(cursor); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 110 | void AshNativeCursorManager::SetCursorSet( |
| 111 | ui::CursorSetType cursor_set, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 112 | ::wm::NativeCursorManagerDelegate* delegate) { |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 113 | image_cursors_->SetCursorSet(cursor_set); |
| 114 | delegate->CommitCursorSet(cursor_set); |
| 115 | |
| 116 | // Sets the cursor to reflect the scale change immediately. |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 117 | if (delegate->IsCursorVisible()) |
| 118 | SetCursor(delegate->GetCursor(), delegate); |
| 119 | |
| 120 | #if defined(OS_CHROMEOS) |
| 121 | Shell::GetInstance()->display_controller()->cursor_window_controller()-> |
| 122 | SetCursorSet(cursor_set); |
| 123 | #endif |
Torne (Richard Coles) | 424c4d7 | 2013-08-30 15:14:49 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 126 | void AshNativeCursorManager::SetVisibility( |
| 127 | bool visible, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 128 | ::wm::NativeCursorManagerDelegate* delegate) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 129 | delegate->CommitVisibility(visible); |
| 130 | |
| 131 | if (visible) { |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 132 | SetCursor(delegate->GetCursor(), delegate); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 133 | } else { |
| 134 | gfx::NativeCursor invisible_cursor(ui::kCursorNone); |
| 135 | image_cursors_->SetPlatformCursor(&invisible_cursor); |
| 136 | SetCursorOnAllRootWindows(invisible_cursor); |
| 137 | } |
| 138 | |
| 139 | NotifyCursorVisibilityChange(visible); |
| 140 | } |
| 141 | |
| 142 | void AshNativeCursorManager::SetMouseEventsEnabled( |
| 143 | bool enabled, |
Torne (Richard Coles) | a140131 | 2014-03-18 10:20:56 +0000 | [diff] [blame] | 144 | ::wm::NativeCursorManagerDelegate* delegate) { |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 145 | delegate->CommitMouseEventsEnabled(enabled); |
| 146 | |
| 147 | if (enabled) { |
| 148 | aura::Env::GetInstance()->set_last_mouse_location( |
| 149 | disabled_cursor_location_); |
| 150 | } else { |
| 151 | disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location(); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Torne (Richard Coles) | 5d1f7b1 | 2014-02-21 12:16:55 +0000 | [diff] [blame] | 154 | SetVisibility(delegate->IsCursorVisible(), delegate); |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 155 | NotifyMouseEventsEnableStateChange(enabled); |
| 156 | } |
| 157 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 158 | } // namespace ash |