Only fsync leveldb's directory when the manifest is being updated.
[chromium-blink-merge.git] / ash / wm / image_cursors.cc
blob63b233565981c46a91fe10e522b24674a6f8fe27
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.
5 #include "ash/wm/image_cursors.h"
7 #include <float.h>
9 #include "base/logging.h"
10 #include "base/string16.h"
11 #include "ui/base/cursor/cursor.h"
12 #include "ui/base/cursor/cursor_loader.h"
13 #include "ui/base/cursor/cursors_aura.h"
14 #include "ui/gfx/display.h"
15 #include "ui/gfx/point.h"
17 namespace ash {
19 const int kImageCursorIds[] = {
20 ui::kCursorNull,
21 ui::kCursorPointer,
22 ui::kCursorNoDrop,
23 ui::kCursorNotAllowed,
24 ui::kCursorCopy,
25 ui::kCursorHand,
26 ui::kCursorMove,
27 ui::kCursorNorthEastResize,
28 ui::kCursorSouthWestResize,
29 ui::kCursorSouthEastResize,
30 ui::kCursorNorthWestResize,
31 ui::kCursorNorthResize,
32 ui::kCursorSouthResize,
33 ui::kCursorEastResize,
34 ui::kCursorWestResize,
35 ui::kCursorIBeam,
36 ui::kCursorAlias,
37 ui::kCursorCell,
38 ui::kCursorContextMenu,
39 ui::kCursorCross,
40 ui::kCursorHelp,
41 ui::kCursorVerticalText,
42 ui::kCursorZoomIn,
43 ui::kCursorZoomOut,
44 ui::kCursorRowResize,
45 ui::kCursorColumnResize,
46 ui::kCursorEastWestResize,
47 ui::kCursorNorthSouthResize,
48 ui::kCursorNorthEastSouthWestResize,
49 ui::kCursorNorthWestSouthEastResize,
50 ui::kCursorGrab,
51 ui::kCursorGrabbing,
54 const int kAnimatedCursorIds[] = {
55 ui::kCursorWait,
56 ui::kCursorProgress
59 ImageCursors::ImageCursors() : scale_(1.f) {
62 ImageCursors::~ImageCursors() {
65 gfx::Display ImageCursors::GetDisplay() const {
66 if (!cursor_loader_) {
67 NOTREACHED();
68 // Returning default on release build as it's not serious enough to crash
69 // even if this ever happens.
70 return gfx::Display();
72 return cursor_loader_->display();
75 bool ImageCursors::SetDisplay(const gfx::Display& display) {
76 float device_scale_factor = display.device_scale_factor();
77 if (!cursor_loader_) {
78 cursor_loader_.reset(ui::CursorLoader::Create());
79 cursor_loader_->set_scale(scale_);
80 } else if (cursor_loader_->display().rotation() == display.rotation() &&
81 cursor_loader_->display().device_scale_factor() ==
82 device_scale_factor) {
83 return false;
86 cursor_loader_->set_display(display);
87 ReloadCursors();
88 return true;
91 void ImageCursors::ReloadCursors() {
92 const gfx::Display& display = cursor_loader_->display();
93 float device_scale_factor = display.device_scale_factor();
95 cursor_loader_->UnloadAll();
97 for (size_t i = 0; i < arraysize(kImageCursorIds); ++i) {
98 int resource_id = -1;
99 gfx::Point hot_point;
100 bool success = ui::GetCursorDataFor(kImageCursorIds[i],
101 device_scale_factor,
102 &resource_id,
103 &hot_point);
104 DCHECK(success);
105 cursor_loader_->LoadImageCursor(kImageCursorIds[i], resource_id, hot_point);
107 for (size_t i = 0; i < arraysize(kAnimatedCursorIds); ++i) {
108 int resource_id = -1;
109 gfx::Point hot_point;
110 bool success = ui::GetAnimatedCursorDataFor(kAnimatedCursorIds[i],
111 device_scale_factor,
112 &resource_id,
113 &hot_point);
114 DCHECK(success);
115 cursor_loader_->LoadAnimatedCursor(kAnimatedCursorIds[i],
116 resource_id,
117 hot_point,
118 ui::kAnimatedCursorFrameDelayMs);
122 void ImageCursors::SetScale(float scale) {
123 if (scale < FLT_EPSILON) {
124 NOTREACHED() << "Scale must be bigger than 0.";
125 scale = 1.0f;
128 scale_ = scale;
130 if (cursor_loader_.get()) {
131 cursor_loader_->set_scale(scale);
132 ReloadCursors();
136 void ImageCursors::SetPlatformCursor(gfx::NativeCursor* cursor) {
137 cursor_loader_->SetPlatformCursor(cursor);
140 void ImageCursors::SetCursorResourceModule(const base::string16& module_name) {
141 cursor_loader_->SetCursorResourceModule(module_name);
144 } // namespace ash