1 // Copyright 2014 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/utility/partial_screenshot_controller.h"
9 #include "ash/display/mouse_cursor_event_filter.h"
10 #include "ash/screenshot_delegate.h"
11 #include "ash/shell.h"
12 #include "ash/shell_window_ids.h"
13 #include "base/stl_util.h"
14 #include "ui/compositor/paint_recorder.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_handler.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/wm/core/cursor_manager.h"
24 // The size to increase the invalidated area in the layer to repaint. The area
25 // should be slightly bigger than the actual region because the region indicator
26 // rectangles are drawn outside of the selected region.
27 const int kInvalidateRegionAdditionalSize
= 3;
29 // This will prevent the user from taking a screenshot across multiple
30 // monitors. it will stop the mouse at the any edge of the screen. must
31 // swtich back on when the screenshot is complete.
32 void EnableMouseWarp(bool enable
) {
33 Shell::GetInstance()->mouse_cursor_filter()->set_mouse_warp_enabled(enable
);
38 class PartialScreenshotController::PartialScreenshotLayer
39 : public ui::LayerOwner
,
40 public ui::LayerDelegate
{
42 PartialScreenshotLayer(ui::Layer
* parent
) {
43 SetLayer(new ui::Layer(ui::LAYER_TEXTURED
));
44 layer()->SetFillsBoundsOpaquely(false);
45 layer()->SetBounds(parent
->bounds());
47 parent
->StackAtTop(layer());
48 layer()->SetVisible(true);
49 layer()->set_delegate(this);
51 ~PartialScreenshotLayer() override
{}
53 const gfx::Rect
& region() const { return region_
; }
55 void SetRegion(const gfx::Rect
& region
) {
56 // Invalidates the region which covers the current and new region.
57 gfx::Rect
union_rect(region_
);
58 union_rect
.Union(region
);
59 union_rect
.Inset(-kInvalidateRegionAdditionalSize
,
60 -kInvalidateRegionAdditionalSize
);
61 union_rect
.Intersects(layer()->bounds());
64 layer()->SchedulePaint(union_rect
);
69 void OnPaintLayer(const ui::PaintContext
& context
) override
{
70 if (region_
.IsEmpty())
73 // Screenshot area representation: black rectangle with white
74 // rectangle inside. To avoid capturing these rectangles when mouse
75 // release, they should be outside of the actual capturing area.
76 gfx::Rect
rect(region_
);
77 ui::PaintRecorder
recorder(context
, layer()->size());
79 recorder
.canvas()->DrawRect(rect
, SK_ColorWHITE
);
81 recorder
.canvas()->DrawRect(rect
, SK_ColorBLACK
);
84 void OnDelegatedFrameDamage(const gfx::Rect
& damage_rect_in_dip
) override
{}
86 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
88 base::Closure
PrepareForLayerBoundsChange() override
{
89 return base::Closure();
94 DISALLOW_COPY_AND_ASSIGN(PartialScreenshotLayer
);
97 class PartialScreenshotController::ScopedCursorSetter
{
99 ScopedCursorSetter(::wm::CursorManager
* cursor_manager
,
100 gfx::NativeCursor cursor
)
101 : cursor_manager_(nullptr) {
102 if (cursor_manager
->IsCursorLocked())
104 gfx::NativeCursor original_cursor
= cursor_manager
->GetCursor();
105 cursor_manager_
= cursor_manager
;
106 cursor_manager_
->SetCursor(cursor
);
107 if (!cursor_manager_
->IsCursorVisible())
108 cursor_manager_
->ShowCursor();
109 cursor_manager_
->LockCursor();
110 // SetCursor does not make any effects at this point but it sets back to
111 // the original cursor when unlocked.
112 cursor_manager_
->SetCursor(original_cursor
);
115 ~ScopedCursorSetter() {
117 cursor_manager_
->UnlockCursor();
121 ::wm::CursorManager
* cursor_manager_
;
123 DISALLOW_COPY_AND_ASSIGN(ScopedCursorSetter
);
126 PartialScreenshotController::PartialScreenshotController()
127 : root_window_(nullptr), screenshot_delegate_(nullptr) {
128 Shell
* shell
= Shell::GetInstance();
129 shell
->PrependPreTargetHandler(this);
132 PartialScreenshotController::~PartialScreenshotController() {
133 if (screenshot_delegate_
)
135 Shell::GetInstance()->RemovePreTargetHandler(this);
138 void PartialScreenshotController::StartPartialScreenshotSession(
139 ScreenshotDelegate
* screenshot_delegate
) {
140 // Already in a screenshot session.
141 if (screenshot_delegate_
) {
142 DCHECK_EQ(screenshot_delegate_
, screenshot_delegate
);
146 screenshot_delegate_
= screenshot_delegate
;
147 Shell::GetScreen()->AddObserver(this);
148 for (aura::Window
* root
: Shell::GetAllRootWindows()) {
149 layers_
[root
] = new PartialScreenshotLayer(
150 Shell::GetContainer(root
, kShellWindowId_OverlayContainer
)->layer());
153 cursor_setter_
.reset(new ScopedCursorSetter(
154 Shell::GetInstance()->cursor_manager(), ui::kCursorCross
));
156 EnableMouseWarp(false);
159 void PartialScreenshotController::MaybeStart(const ui::LocatedEvent
& event
) {
160 aura::Window
* current_root
=
161 static_cast<aura::Window
*>(event
.target())->GetRootWindow();
163 // It's already started. This can happen when the second finger touches
164 // the screen, or combination of the touch and mouse. We should grab the
165 // partial screenshot instead of restarting.
166 if (current_root
== root_window_
) {
171 root_window_
= current_root
;
172 start_position_
= event
.root_location();
176 void PartialScreenshotController::Complete() {
177 const gfx::Rect
& region
= layers_
[root_window_
]->region();
178 if (!region
.IsEmpty()) {
179 screenshot_delegate_
->HandleTakePartialScreenshot(
180 root_window_
, gfx::IntersectRects(root_window_
->bounds(), region
));
185 void PartialScreenshotController::Cancel() {
186 root_window_
= nullptr;
187 screenshot_delegate_
= nullptr;
188 Shell::GetScreen()->RemoveObserver(this);
189 STLDeleteValues(&layers_
);
190 cursor_setter_
.reset();
191 EnableMouseWarp(true);
194 void PartialScreenshotController::Update(const ui::LocatedEvent
& event
) {
195 // Update may happen without MaybeStart() if the partial screenshot session
196 // starts when dragging.
200 DCHECK(layers_
.find(root_window_
) != layers_
.end());
201 layers_
[root_window_
]->SetRegion(
202 gfx::Rect(std::min(start_position_
.x(), event
.root_location().x()),
203 std::min(start_position_
.y(), event
.root_location().y()),
204 ::abs(start_position_
.x() - event
.root_location().x()),
205 ::abs(start_position_
.y() - event
.root_location().y())));
208 void PartialScreenshotController::OnKeyEvent(ui::KeyEvent
* event
) {
209 if (!screenshot_delegate_
)
211 if (event
->type() == ui::ET_KEY_RELEASED
&&
212 event
->key_code() == ui::VKEY_ESCAPE
) {
216 // Intercepts all key events.
217 event
->StopPropagation();
220 void PartialScreenshotController::OnMouseEvent(ui::MouseEvent
* event
) {
221 if (!screenshot_delegate_
)
223 switch (event
->type()) {
224 case ui::ET_MOUSE_PRESSED
:
227 case ui::ET_MOUSE_DRAGGED
:
230 case ui::ET_MOUSE_RELEASED
:
237 event
->StopPropagation();
240 void PartialScreenshotController::OnTouchEvent(ui::TouchEvent
* event
) {
241 if (!screenshot_delegate_
)
243 switch (event
->type()) {
244 case ui::ET_TOUCH_PRESSED
:
247 case ui::ET_TOUCH_MOVED
:
250 case ui::ET_TOUCH_RELEASED
:
257 event
->StopPropagation();
260 void PartialScreenshotController::OnDisplayAdded(
261 const gfx::Display
& new_display
) {
262 if (!screenshot_delegate_
)
267 void PartialScreenshotController::OnDisplayRemoved(
268 const gfx::Display
& old_display
) {
269 if (!screenshot_delegate_
)
274 void PartialScreenshotController::OnDisplayMetricsChanged(
275 const gfx::Display
& display
,
276 uint32_t changed_metrics
) {