blob: 3ac69c9bcb88e4e3e1144e6b24dd60c2de97d514 [file] [log] [blame]
oshima5a296e82016-04-29 01:32:271// 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.
4
5#ifndef ASH_UTILITY_SCREENSHOT_CONTROLLER_H_
6#define ASH_UTILITY_SCREENSHOT_CONTROLLER_H_
7
8#include <stdint.h>
9
10#include <map>
11#include <memory>
12
13#include "ash/ash_export.h"
sky529ff5d2016-06-08 17:00:5214#include "ash/common/shell_observer.h"
oshima5a296e82016-04-29 01:32:2715#include "base/macros.h"
16#include "ui/aura/window_observer.h"
17#include "ui/display/display_observer.h"
18#include "ui/events/event_handler.h"
19#include "ui/gfx/geometry/point.h"
20
21namespace aura {
22class Window;
23}
24
25namespace ui {
26class LocatedEvent;
27}
28
29namespace ash {
30class ScreenshotDelegate;
31
32// This class controls a session of taking partial/window screenshot, i.e.:
33// drawing
34// region rectangles during selection, and changing the mouse cursor to indicate
35// the current mode.
36// This class does not use aura::Window / views::Widget intentionally to avoid
37class ASH_EXPORT ScreenshotController : public ui::EventHandler,
38 public display::DisplayObserver,
39 public aura::WindowObserver {
40 public:
41 ScreenshotController();
42 ~ScreenshotController() override;
43
44 // Starts the UI for taking partial screenshot; dragging to select a region.
45 // ScreenshotController manage their own lifetime so caller must not
46 // delete the returned values.
47 void StartPartialScreenshotSession(ScreenshotDelegate* screenshot_delegate);
48
49 // Starts the UI for taking a window screenshot;
50 void StartWindowScreenshotSession(ScreenshotDelegate* screenshot_delegate);
51
52 private:
53 enum Mode {
54 NONE,
55 PARTIAL,
56 WINDOW,
57 };
58
59 friend class ScreenshotControllerTest;
60
61 class ScopedCursorSetter;
62 class ScreenshotLayer;
63
64 // Starts, ends, cancels, or updates the region selection.
65 void MaybeStart(const ui::LocatedEvent& event);
66 void CompleteWindowScreenshot();
67 void CompletePartialScreenshot();
68 void Cancel();
69 void Update(const ui::LocatedEvent& event);
70 void UpdateSelectedWindow(ui::LocatedEvent* event);
71 void SetSelectedWindow(aura::Window* window);
72
73 // ui::EventHandler:
74 void OnKeyEvent(ui::KeyEvent* event) override;
75 void OnMouseEvent(ui::MouseEvent* event) override;
76 void OnTouchEvent(ui::TouchEvent* event) override;
77
78 // display::DisplayObserver:
79 void OnDisplayAdded(const display::Display& new_display) override;
80 void OnDisplayRemoved(const display::Display& old_display) override;
81 void OnDisplayMetricsChanged(const display::Display& display,
82 uint32_t changed_metrics) override;
83
84 // aura::WindowObserver:
85 void OnWindowDestroying(aura::Window* window) override;
86
87 Mode mode_;
88
89 // The data to build the screenshot region.
90 gfx::Point start_position_;
91 aura::Window* root_window_;
92
93 // Currently selected window in WINDOW mode.
94 aura::Window* selected_;
95
96 // Layers to create the visual effect of region selection or selected window.
97 std::map<aura::Window*, ScreenshotLayer*> layers_;
98
99 // The object to specify the crosshair cursor.
100 std::unique_ptr<ScopedCursorSetter> cursor_setter_;
101
102 // ScreenshotDelegate to take the actual screenshot. No ownership.
103 ScreenshotDelegate* screenshot_delegate_;
104
105 DISALLOW_COPY_AND_ASSIGN(ScreenshotController);
106};
107
108} // namespace ash
109
110#endif // ASH_UTILITY_SCREENSHOT_CONTROLLER_H_