summaryrefslogtreecommitdiffstats
path: root/ash/wm/ash_native_cursor_manager.cc
blob: be9c6b7b617062bb7c91c11e69786530b886b66b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (c) 2012 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/wm/ash_native_cursor_manager.h"

#include "ash/shell.h"
#include "ash/wm/image_cursors.h"
#include "base/logging.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/base/cursor/cursor.h"

namespace  {

// The coordinate of the cursor used when the mouse events are disabled.
const int kDisabledCursorLocationX = -10000;
const int kDisabledCursorLocationY = -10000;

void SetCursorOnAllRootWindows(gfx::NativeCursor cursor) {
  ash::Shell::RootWindowList root_windows =
      ash::Shell::GetInstance()->GetAllRootWindows();
  for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter)
    (*iter)->SetCursor(cursor);
}

void NotifyCursorVisibilityChange(bool visible) {
  ash::Shell::RootWindowList root_windows =
      ash::Shell::GetInstance()->GetAllRootWindows();
  for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter)
    (*iter)->OnCursorVisibilityChanged(visible);
}

void NotifyMouseEventsEnableStateChange(bool enabled) {
  ash::Shell::RootWindowList root_windows =
      ash::Shell::GetInstance()->GetAllRootWindows();
  for (ash::Shell::RootWindowList::iterator iter = root_windows.begin();
       iter != root_windows.end(); ++iter)
    (*iter)->OnMouseEventsEnableStateChanged(enabled);
}

}  // namespace

namespace ash {

AshNativeCursorManager::AshNativeCursorManager()
    : image_cursors_(new ImageCursors) {
}

AshNativeCursorManager::~AshNativeCursorManager() {
}

void AshNativeCursorManager::SetDisplay(
    const gfx::Display& display,
    views::corewm::NativeCursorManagerDelegate* delegate) {
  if (image_cursors_->SetDisplay(display))
    SetCursor(delegate->GetCurrentCursor(), delegate);
}

void AshNativeCursorManager::SetCursor(
    gfx::NativeCursor cursor,
    views::corewm::NativeCursorManagerDelegate* delegate) {
  gfx::NativeCursor new_cursor = cursor;
  image_cursors_->SetPlatformCursor(&new_cursor);
  new_cursor.set_device_scale_factor(
      image_cursors_->GetDisplay().device_scale_factor());

  delegate->CommitCursor(new_cursor);

  if (delegate->GetCurrentVisibility())
    SetCursorOnAllRootWindows(new_cursor);
}

void AshNativeCursorManager::SetVisibility(
    bool visible,
    views::corewm::NativeCursorManagerDelegate* delegate) {
  delegate->CommitVisibility(visible);

  if (visible) {
    SetCursor(delegate->GetCurrentCursor(), delegate);
  } else {
    gfx::NativeCursor invisible_cursor(ui::kCursorNone);
    image_cursors_->SetPlatformCursor(&invisible_cursor);
    SetCursorOnAllRootWindows(invisible_cursor);
  }

  NotifyCursorVisibilityChange(visible);
}

void AshNativeCursorManager::SetMouseEventsEnabled(
    bool enabled,
    views::corewm::NativeCursorManagerDelegate* delegate) {
  delegate->CommitMouseEventsEnabled(enabled);

  if (enabled) {
    aura::Env::GetInstance()->set_last_mouse_location(
        disabled_cursor_location_);
  } else {
    disabled_cursor_location_ = aura::Env::GetInstance()->last_mouse_location();
    aura::Env::GetInstance()->set_last_mouse_location(
        gfx::Point(kDisabledCursorLocationX, kDisabledCursorLocationY));
  }

  SetVisibility(delegate->GetCurrentVisibility(), delegate);
  NotifyMouseEventsEnableStateChange(enabled);
}

void AshNativeCursorManager::SetCursorResourceModule(
    const base::string16& module_name) {
  image_cursors_->SetCursorResourceModule(module_name);
}


}  // namespace ash