Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_controller.cc

Issue 2596553003: Implements Touch calibrator controller and a skeleton for touch calibrator view. (Closed)
Patch Set: Resolved comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_cont roller.h"
6
7 #include "ash/shell.h"
8 #include "ash/touch/touch_transformer_controller.h"
9 #include "base/memory/ptr_util.h"
10 #include "chrome/browser/chromeos/display/touch_calibrator/touch_calibrator_view .h"
11 #include "ui/display/screen.h"
12 #include "ui/events/event.h"
13 #include "ui/events/event_constants.h"
14
15 namespace chromeos {
16
17 // Time interval after a touch event during which all other touch events are
18 // ignored during calibration.
19 const base::TimeDelta TouchCalibratorController::kTouchIntervalThreshold =
20 base::TimeDelta::FromMilliseconds(200);
21
22 TouchCalibratorController::TouchCalibratorController()
23 : last_touch_timestamp_(base::Time::Now()) {
24 ash::Shell::GetInstance()->window_tree_host_manager()->AddObserver(this);
25 }
26
27 TouchCalibratorController::~TouchCalibratorController() {
28 ash::Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this);
29 touch_calibrator_views_.clear();
30 StopCalibration();
31 }
32
33 void TouchCalibratorController::OnDisplayConfigurationChanged() {
34 touch_calibrator_views_.clear();
35 StopCalibration();
36 }
37
38 void TouchCalibratorController::StartCalibration(
39 const display::Display& target_display) {
40 is_calibrating_ = true;
41 target_display_ = target_display;
42
43 // Clear all touch calibrator views used in any previous calibration.
44 touch_calibrator_views_.clear();
45
46 // Reset the calibration data.
47 touch_point_quad_.fill(std::make_pair(gfx::Point(0, 0), gfx::Point(0, 0)));
48
49 std::vector<display::Display> displays =
50 display::Screen::GetScreen()->GetAllDisplays();
51
52 for (const display::Display& display : displays) {
53 bool is_primary_view = display.id() == target_display_.id();
54 touch_calibrator_views_[display.id()] =
55 base::MakeUnique<TouchCalibratorView>(display, is_primary_view);
56 }
57
58 // TODO(malaykeshav): Call TouchTransformerController::SetForCalibration()
59
60 // Add self as an event handler target.
61 ash::Shell::GetInstance()->AddPreTargetHandler(this);
62 }
63
64 void TouchCalibratorController::StopCalibration() {
65 if (!is_calibrating_)
66 return;
67 is_calibrating_ = false;
68
69 // TODO(malaykeshav): Call TouchTransformerController::SetForCalibration()
70
71 // Remove self as the event handler.
72 ash::Shell::GetInstance()->RemovePreTargetHandler(this);
73
74 // Transition all touch calibrator views to their final state for a graceful
75 // exit.
76 for (const auto& it : touch_calibrator_views_)
77 it.second->SkipToFinalState();
78 }
79
80 // ui::EventHandler:
81 void TouchCalibratorController::OnKeyEvent(ui::KeyEvent* key) {
82 if (!is_calibrating_)
83 return;
84 // Detect ESC key press.
85 if (key->type() == ui::ET_KEY_PRESSED && key->key_code() == ui::VKEY_ESCAPE)
86 StopCalibration();
87 key->StopPropagation();
88 }
89
90 void TouchCalibratorController::OnTouchEvent(ui::TouchEvent* touch) {
91 if (!is_calibrating_)
92 return;
93 if (touch->type() != ui::ET_TOUCH_RELEASED)
94 return;
95 if (base::Time::Now() - last_touch_timestamp_ < kTouchIntervalThreshold)
96 return;
97
98 last_touch_timestamp_ = base::Time::Now();
99
100 TouchCalibratorView* target_screen_calibration_view =
101 touch_calibrator_views_[target_display_.id()].get();
102
103 int state_index;
104 // Maps the state to an integer value. Assigns a non negative integral value
105 // for a state in which the user can interact with the the interface.
106 switch (target_screen_calibration_view->state()) {
107 case TouchCalibratorView::DISPLAY_POINT_1:
108 state_index = 0;
109 break;
110 case TouchCalibratorView::DISPLAY_POINT_2:
111 state_index = 1;
112 break;
113 case TouchCalibratorView::DISPLAY_POINT_3:
114 state_index = 2;
115 break;
116 case TouchCalibratorView::DISPLAY_POINT_4:
117 state_index = 3;
118 break;
119 default:
120 // Return early if the interface is in a state that does not allow user
121 // interaction.
122 return;
123 }
124
125 // Store touch point corresponding to its display point.
126 gfx::Point display_point;
127 if (target_screen_calibration_view->GetDisplayPointLocation(&display_point)) {
128 touch_point_quad_[state_index] =
129 std::make_pair(display_point, touch->location());
130 }
131
132 // If this is the final state, then store all calibration data and stop
133 // calibration.
134 if (target_screen_calibration_view->state() ==
135 TouchCalibratorView::CALIBRATION_COMPLETE) {
136 ash::Shell::GetInstance()->display_manager()->SetTouchCalibrationData(
137 target_display_.id(), touch_point_quad_,
138 target_screen_calibration_view->size());
139 StopCalibration();
140 return;
141 }
142
143 target_screen_calibration_view->AdvanceToNextState();
144 }
145
146 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698