forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay_util.cc
203 lines (175 loc) · 8.09 KB
/
display_util.cc
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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/display_util.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "ash/display/extended_mouse_warp_controller.h"
#include "ash/display/null_mouse_warp_controller.h"
#include "ash/display/unified_mouse_warp_controller.h"
#include "ash/host/ash_window_tree_host.h"
#include "ash/new_window_controller.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/sys_info.h"
#include "ui/aura/env.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/display/display.h"
#include "ui/display/manager/display_manager.h"
#include "ui/display/manager/managed_display_info.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/notification_list.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/message_center/public/cpp/notification_delegate.h"
#include "ui/wm/core/coordinate_conversion.h"
namespace ash {
namespace {
const char kDisplayErrorNotificationId[] = "chrome://settings/display/error";
const char kNotifierDisplayError[] = "ash.display.error";
// A notification delegate that will start the feedback app when the notication
// is clicked.
class DisplayErrorNotificationDelegate
: public message_center::NotificationDelegate {
public:
DisplayErrorNotificationDelegate() = default;
// message_center::NotificationDelegate:
void ButtonClick(int index) override {
DCHECK_EQ(0, index);
Shell::Get()->new_window_controller()->OpenFeedbackPage();
}
private:
// Private destructor since NotificationDelegate is ref-counted.
~DisplayErrorNotificationDelegate() override = default;
DISALLOW_COPY_AND_ASSIGN(DisplayErrorNotificationDelegate);
};
void ConvertPointFromScreenToNative(aura::WindowTreeHost* host,
gfx::Point* point) {
::wm::ConvertPointFromScreen(host->window(), point);
host->ConvertDIPToScreenInPixels(point);
}
} // namespace
std::unique_ptr<MouseWarpController> CreateMouseWarpController(
display::DisplayManager* manager,
aura::Window* drag_source) {
if (manager->IsInUnifiedMode() && manager->num_connected_displays() >= 2)
return std::make_unique<UnifiedMouseWarpController>();
// Extra check for |num_connected_displays()| is for SystemDisplayApiTest
// that injects MockScreen.
if (manager->GetNumDisplays() < 2 || manager->num_connected_displays() < 2)
return std::make_unique<NullMouseWarpController>();
return std::make_unique<ExtendedMouseWarpController>(drag_source);
}
gfx::Rect GetNativeEdgeBounds(AshWindowTreeHost* ash_host,
const gfx::Rect& bounds_in_screen) {
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
gfx::Rect native_bounds = host->GetBoundsInPixels();
native_bounds.Inset(ash_host->GetHostInsets());
gfx::Point start_in_native = bounds_in_screen.origin();
gfx::Point end_in_native = bounds_in_screen.bottom_right();
ConvertPointFromScreenToNative(host, &start_in_native);
ConvertPointFromScreenToNative(host, &end_in_native);
if (std::abs(start_in_native.x() - end_in_native.x()) <
std::abs(start_in_native.y() - end_in_native.y())) {
// vertical in native
int x = std::abs(native_bounds.x() - start_in_native.x()) <
std::abs(native_bounds.right() - start_in_native.x())
? native_bounds.x()
: native_bounds.right() - 1;
return gfx::Rect(x, std::min(start_in_native.y(), end_in_native.y()), 1,
std::abs(end_in_native.y() - start_in_native.y()));
} else {
// horizontal in native
int y = std::abs(native_bounds.y() - start_in_native.y()) <
std::abs(native_bounds.bottom() - start_in_native.y())
? native_bounds.y()
: native_bounds.bottom() - 1;
return gfx::Rect(std::min(start_in_native.x(), end_in_native.x()), y,
std::abs(end_in_native.x() - start_in_native.x()), 1);
}
}
// Moves the cursor to the point inside the root that is closest to
// the point_in_screen, which is outside of the root window.
void MoveCursorTo(AshWindowTreeHost* ash_host,
const gfx::Point& point_in_screen,
bool update_last_location_now) {
aura::WindowTreeHost* host = ash_host->AsWindowTreeHost();
gfx::Point point_in_native = point_in_screen;
::wm::ConvertPointFromScreen(host->window(), &point_in_native);
host->ConvertDIPToScreenInPixels(&point_in_native);
// now fit the point inside the native bounds.
gfx::Rect native_bounds = host->GetBoundsInPixels();
gfx::Point native_origin = native_bounds.origin();
native_bounds.Inset(ash_host->GetHostInsets());
// Shrink further so that the mouse doesn't warp on the
// edge. The right/bottom needs to be shrink by 2 to subtract
// the 1 px from width/height value.
native_bounds.Inset(1, 1, 2, 2);
// Ensure that |point_in_native| is inside the |native_bounds|.
point_in_native.SetToMax(native_bounds.origin());
point_in_native.SetToMin(native_bounds.bottom_right());
gfx::Point point_in_host = point_in_native;
point_in_host.Offset(-native_origin.x(), -native_origin.y());
host->MoveCursorToLocationInPixels(point_in_host);
if (update_last_location_now) {
gfx::Point new_point_in_screen = point_in_native;
host->ConvertScreenInPixelsToDIP(&new_point_in_screen);
::wm::ConvertPointToScreen(host->window(), &new_point_in_screen);
if (Shell::Get()->display_manager()->IsInUnifiedMode()) {
// In unified desktop mode, the mirroring host converts the point to the
// unified host's pixel coordinates, so we also need to apply the unified
// host transform to get a point in the unified screen coordinates to take
// into account any device scale factors or ui scaling.
Shell::GetPrimaryRootWindow()->GetHost()->ConvertScreenInPixelsToDIP(
&new_point_in_screen);
}
aura::Env::GetInstance()->set_last_mouse_location(new_point_in_screen);
}
}
void ShowDisplayErrorNotification(const base::string16& message,
bool allow_feedback) {
// Always remove the notification to make sure the notification appears
// as a popup in any situation.
message_center::MessageCenter::Get()->RemoveNotification(
kDisplayErrorNotificationId, false /* by_user */);
message_center::RichNotificationData data;
if (allow_feedback) {
message_center::ButtonInfo send_button(
l10n_util::GetStringUTF16(IDS_ASH_DISPLAY_FAILURE_SEND_FEEDBACK));
data.buttons.push_back(send_button);
}
std::unique_ptr<message_center::Notification> notification =
message_center::Notification::CreateSystemNotification(
message_center::NOTIFICATION_TYPE_SIMPLE, kDisplayErrorNotificationId,
base::string16(), // title
message, gfx::Image(),
base::string16(), // display_source
GURL(),
message_center::NotifierId(
message_center::NotifierId::SYSTEM_COMPONENT,
kNotifierDisplayError),
data, new DisplayErrorNotificationDelegate,
kNotificationMonitorWarningIcon,
message_center::SystemNotificationWarningLevel::WARNING);
notification->set_priority(message_center::SYSTEM_PRIORITY);
message_center::MessageCenter::Get()->AddNotification(
std::move(notification));
}
base::string16 GetDisplayErrorNotificationMessageForTest() {
message_center::NotificationList::Notifications notifications =
message_center::MessageCenter::Get()->GetVisibleNotifications();
for (auto* const notification : notifications) {
if (notification->id() == kDisplayErrorNotificationId)
return notification->message();
}
return base::string16();
}
} // namespace ash