Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "components/mus/ws/touch_controller.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "components/mus/ws/display.h" | |
| 12 #include "components/mus/ws/display_manager.h" | |
| 13 #include "ui/events/devices/device_data_manager.h" | |
| 14 #include "ui/events/devices/touchscreen_device.h" | |
| 15 #include "ui/gfx/geometry/size.h" | |
| 16 #include "ui/gfx/transform.h" | |
| 17 | |
| 18 namespace mus { | |
| 19 namespace ws { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // Computes the scale ratio for the TouchEvent's radius. | |
| 24 double ComputeTouchResolutionScale(const Display* touch_display, | |
| 25 const ui::TouchscreenDevice& touch_device) { | |
| 26 gfx::Size touch_display_size = touch_display->GetSize(); | |
| 27 | |
| 28 if (touch_device.size.IsEmpty() || touch_display_size.IsEmpty()) | |
| 29 return 1.0; | |
| 30 | |
| 31 double display_area = touch_display_size.GetArea(); | |
| 32 double touch_area = touch_device.size.GetArea(); | |
| 33 double ratio = std::sqrt(display_area / touch_area); | |
| 34 | |
| 35 return ratio; | |
| 36 } | |
| 37 | |
| 38 // Computes a touch transform that maps from window bounds to touchscreen size. | |
| 39 // Assumes scale factor of 1.0. | |
| 40 gfx::Transform ComputeTouchTransform( | |
| 41 const Display* touch_display, | |
| 42 const ui::TouchscreenDevice& touch_device) { | |
| 43 gfx::Size touch_display_size = touch_display->GetSize(); | |
| 44 | |
| 45 gfx::SizeF current_size(touch_display_size); | |
| 46 gfx::SizeF touch_area(touch_device.size); | |
| 47 gfx::Transform transform; | |
| 48 | |
| 49 if (current_size.IsEmpty() || touch_area.IsEmpty()) | |
| 50 return transform; | |
| 51 | |
| 52 // Take care of scaling between touchscreen area and display resolution. | |
| 53 transform.Scale(current_size.width() / touch_area.width(), | |
| 54 current_size.height() / touch_area.height()); | |
| 55 return transform; | |
| 56 } | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 TouchController::TouchController(DisplayManager* display_manager) | |
| 61 : display_manager_(display_manager) { | |
| 62 DCHECK(display_manager_); | |
| 63 ui::DeviceDataManager::GetInstance()->AddObserver(this); | |
| 64 } | |
| 65 | |
| 66 TouchController::~TouchController() { | |
| 67 ui::DeviceDataManager::GetInstance()->RemoveObserver(this); | |
| 68 } | |
| 69 | |
| 70 void TouchController::UpdateTouchTransforms() const { | |
| 71 ui::DeviceDataManager* device_manager = ui::DeviceDataManager::GetInstance(); | |
| 72 device_manager->ClearTouchDeviceAssociations(); | |
| 73 | |
| 74 const std::set<Display*>& touch_displays = display_manager_->displays(); | |
| 75 const std::vector<ui::TouchscreenDevice>& touch_devices = | |
| 76 device_manager->GetTouchscreenDevices(); | |
| 77 | |
| 78 // Since mash can only handle a single display, we're not going to worry about | |
| 79 // other cases yet. This will need to change when multiple displays happen. | |
| 80 if (touch_displays.size() == 1 && touch_devices.size() == 1) { | |
|
sky
2016/06/21 20:09:22
Please add a TODO for the multi-display case.
kylechar
2016/06/22 12:47:45
Done.
| |
| 81 const Display* touch_display = *touch_displays.begin(); | |
| 82 const ui::TouchscreenDevice& touch_device = touch_devices[0]; | |
| 83 | |
| 84 int64_t touch_display_id = touch_display->GetPlatformDisplayId(); | |
| 85 int touch_device_id = touch_device.id; | |
| 86 | |
| 87 if (touch_device_id != ui::InputDevice::kInvalidId) { | |
| 88 device_manager->UpdateTouchRadiusScale( | |
| 89 touch_device_id, | |
| 90 ComputeTouchResolutionScale(touch_display, touch_device)); | |
| 91 | |
| 92 device_manager->UpdateTouchInfoForDisplay( | |
| 93 touch_display_id, touch_device_id, | |
| 94 ComputeTouchTransform(touch_display, touch_device)); | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 void TouchController::OnTouchscreenDeviceConfigurationChanged() { | |
| 100 UpdateTouchTransforms(); | |
| 101 } | |
| 102 | |
| 103 } // namespace ws | |
| 104 } // namespace mus | |
| OLD | NEW |