Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "ash/touch/touch_exploration_controller.h" | |
| 6 | |
| 7 #include "ash/root_window_settings.h" | |
| 8 #include "ash/shell.h" | |
| 9 #include "ash/wm/coordinate_conversion.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "ui/aura/client/cursor_client.h" | |
| 12 #include "ui/aura/window_tree_host.h" | |
| 13 #include "ui/events/event.h" | |
| 14 #include "ui/gfx/point.h" | |
| 15 | |
| 16 namespace ash { | |
| 17 | |
| 18 TouchExplorationController::TouchExplorationController( | |
| 19 aura::Window* root_window) : | |
|
Daniel Erat
2014/04/18 00:44:08
see the style guide: http://google-styleguide.goog
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 20 display_id_(GetRootWindowSettings(root_window)->display_id), | |
| 21 root_window_(root_window) { | |
| 22 Shell::GetInstance()->display_controller()->AddObserver(this); | |
| 23 ui::EventSource* event_source = root_window->GetHost()->GetEventSource(); | |
| 24 event_source->AddEventRewriter(this); | |
|
Daniel Erat
2014/04/18 00:44:08
nit: combine this line with the previous one as in
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 25 } | |
| 26 | |
| 27 TouchExplorationController::~TouchExplorationController() { | |
| 28 Shell::GetInstance()->display_controller()->RemoveObserver(this); | |
| 29 if (root_window_) | |
|
Daniel Erat
2014/04/18 00:44:08
why does the c'tor assume that root_window is non-
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 30 root_window_->GetHost()->GetEventSource()->RemoveEventRewriter(this); | |
| 31 } | |
| 32 | |
| 33 ui::EventRewriteStatus TouchExplorationController::RewriteEvent( | |
| 34 const ui::Event& event, | |
| 35 scoped_ptr<ui::Event>* rewritten_event) { | |
| 36 if (!event.IsTouchEvent()) { | |
|
Daniel Erat
2014/04/18 00:44:08
nit: leave out these curly brackets since the stat
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 37 return ui::EVENT_REWRITE_CONTINUE; | |
| 38 } | |
| 39 ui::EventRewriteStatus result; | |
| 40 const ui::TouchEvent& touch_event = | |
| 41 static_cast<const ui::TouchEvent&> (event); | |
|
Daniel Erat
2014/04/18 00:44:08
get rid of space before '('
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 42 ui::EventType type = touch_event.type(); | |
| 43 gfx::Point location = touch_event.location(); | |
| 44 int touch_id = touch_event.touch_id(); | |
| 45 int flags = touch_event.flags(); | |
|
Daniel Erat
2014/04/18 00:44:08
nit: make all of the above types be const
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 46 | |
| 47 if (type == ui::ET_TOUCH_PRESSED) { | |
| 48 LOG(ERROR) << "ET_TOUCH_PRESSED"; | |
|
Daniel Erat
2014/04/18 00:44:08
remove debug messages here and below (i.e. switch
mfomitchev
2014/04/23 18:27:37
I am going to leave the logging for now, since thi
| |
| 49 // If this is the first and only finger touching rewrite the touch as a | |
| 50 // mouse move. | |
| 51 if (touch_ids_.size() == 0) { | |
|
Daniel Erat
2014/04/18 00:44:08
touch_ids_.empty()
| |
| 52 result = ui::EVENT_REWRITE_REWRITTEN; | |
| 53 rewritten_event->reset(CreateMouseMoveEvent(location, flags)); | |
| 54 EnterMouseMoveMode(); | |
| 55 | |
| 56 // Otherwise let the touch go through. | |
| 57 } else { | |
| 58 result = ui::EVENT_REWRITE_CONTINUE; | |
| 59 } | |
| 60 touch_ids_.push_back(touch_id); | |
| 61 touch_locations_.insert(std::pair<int, gfx::Point>(touch_id, location)); | |
|
Daniel Erat
2014/04/18 00:44:08
please make this method return early whenever it c
mfomitchev
2014/04/23 18:27:37
Done.. kind of
| |
| 62 } else if ((type == ui::ET_TOUCH_RELEASED || | |
| 63 type == ui::ET_TOUCH_CANCELLED)) { | |
|
Daniel Erat
2014/04/18 00:44:08
fix indenting and get rid of extra parentheses
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 64 LOG(ERROR) << (type == ui::ET_TOUCH_RELEASED ? | |
| 65 "ET_TOUCH_RELEASED" : "ET_TOUCH_CANCELLED"); | |
| 66 std::vector<int>::iterator it = | |
| 67 std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); | |
| 68 if (it != touch_ids_.end()) { | |
| 69 if (it != touch_ids_.begin()) { | |
| 70 // If it wasn't the first finger that was lifted, dispatch the event as | |
| 71 // is. If there were exactly two fingers on the screen - enter the mouse | |
| 72 // move mode and generate a mouse move corresponding to the location of | |
| 73 // the first finger. | |
| 74 if (touch_ids_.size() != 2) { | |
| 75 result = ui::EVENT_REWRITE_CONTINUE; | |
| 76 } else { | |
| 77 LOG(ERROR) << "Dispatching release, entering mouse move mode."; | |
| 78 result = ui::EVENT_REWRITE_DISPATCH_ANOTHER; | |
| 79 // Dispatch the release event as is. | |
| 80 rewritten_event->reset(new ui::TouchEvent(touch_event)); | |
| 81 int first_touch_id = touch_ids_.at(0); | |
| 82 gfx::Point first_touch_location = touch_locations_[first_touch_id]; | |
| 83 next_dispatch_event_.reset( | |
| 84 CreateMouseMoveEvent(first_touch_location, flags)); | |
| 85 EnterMouseMoveMode(); | |
| 86 } | |
| 87 } else { | |
| 88 // If it was the first finger that was lifted, we need to do a few | |
| 89 // things depending on how many fingers were on the screen: | |
| 90 // 1. If only one finger was on the screen, this is simply equivalent to | |
| 91 // the end of the mouse move. | |
| 92 // 2. If there were multiple fingers on the screen, this is equivalent | |
| 93 // to lifting the _second_ finger. Also, if there were exactly two | |
| 94 // fingers touching, we are only left with one, so we are entering | |
| 95 // the mouse move mode, and so we should also generate a mouse move. | |
| 96 if (touch_ids_.size() == 1) { | |
| 97 result = ui::EVENT_REWRITE_REWRITTEN; | |
| 98 rewritten_event->reset(CreateMouseMoveEvent(location, flags)); | |
| 99 } else { | |
| 100 LOG(ERROR) << "Rewriting as a touch release event"; | |
| 101 // Rewrite the release of the first finger as a release of the | |
| 102 // second finger. | |
| 103 int second_touch_id = touch_ids_.at(1); | |
| 104 gfx::Point second_touch_location = touch_locations_[second_touch_id]; | |
| 105 ui::TouchEvent* touch_release_event = new ui::TouchEvent( | |
| 106 ui::ET_TOUCH_RELEASED, | |
| 107 second_touch_location, | |
| 108 second_touch_id, | |
| 109 event.time_stamp()); | |
| 110 rewritten_event->reset(touch_release_event); | |
| 111 if (touch_ids_.size() == 2) { | |
| 112 // If there were exactly two fingers touching - enter the mouse move | |
| 113 // mode and dispatch a mouse move corresponding to the location of | |
| 114 // the second finger. | |
| 115 result = ui::EVENT_REWRITE_DISPATCH_ANOTHER; | |
| 116 next_dispatch_event_.reset( | |
| 117 CreateMouseMoveEvent(second_touch_location, flags)); | |
| 118 EnterMouseMoveMode(); | |
| 119 } else { | |
| 120 result = ui::EVENT_REWRITE_REWRITTEN; | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 touch_ids_.erase(it); | |
| 125 touch_locations_.erase(touch_id); | |
| 126 } else { | |
| 127 NOTREACHED(); | |
| 128 result = ui::EVENT_REWRITE_CONTINUE; | |
| 129 } | |
| 130 } else if (type == ui::ET_TOUCH_MOVED) { | |
| 131 LOG(ERROR) << "ET_TOUCH_MOVED"; | |
| 132 // Consume if it is the first finger. | |
| 133 // If it is the the first and only finger - generate a mouse move event. | |
| 134 std::vector<int>::iterator it = | |
| 135 std::find(touch_ids_.begin(), touch_ids_.end(), touch_id); | |
| 136 if (it != touch_ids_.end()) { | |
| 137 if (it != touch_ids_.begin()) { | |
|
sadrul
2014/04/22 16:46:12
You can just do 'touch_ids_.front() != touch_id' i
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 138 result = ui::EVENT_REWRITE_CONTINUE; | |
| 139 } else { | |
| 140 if (touch_ids_.size() == 1) { | |
| 141 result = ui::EVENT_REWRITE_REWRITTEN; | |
| 142 rewritten_event->reset(CreateMouseMoveEvent(location, flags)); | |
| 143 } else { | |
| 144 result = ui::EVENT_REWRITE_DISCARD; | |
| 145 } | |
| 146 } | |
| 147 touch_locations_[*it] = location; | |
| 148 } else { | |
| 149 NOTREACHED(); | |
| 150 result = ui::EVENT_REWRITE_CONTINUE; | |
| 151 } | |
| 152 } else { | |
| 153 result = ui::EVENT_REWRITE_CONTINUE; | |
| 154 } | |
|
sadrul
2014/04/22 16:46:12
The nested ifs and incorrect indentation in some p
mfomitchev
2014/04/23 18:27:37
I've tried to make the code more readable. I also
| |
| 155 return result; | |
| 156 } | |
| 157 | |
| 158 ui::EventRewriteStatus TouchExplorationController::NextDispatchEvent( | |
| 159 const ui::Event& last_event, | |
| 160 scoped_ptr<ui::Event>* new_event) { | |
| 161 new_event->reset(next_dispatch_event_.release()); | |
|
sadrul
2014/04/22 16:46:12
I think you can do *new_event = next_dispatch_even
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 162 return ui::EVENT_REWRITE_REWRITTEN; | |
| 163 } | |
| 164 | |
| 165 ui::Event* TouchExplorationController::CreateMouseMoveEvent( | |
| 166 gfx::Point location, int flags) { | |
|
Daniel Erat
2014/04/18 00:44:08
nit: one parameter per line in function sigs if th
| |
| 167 LOG(ERROR) << "Creating mouse move event"; | |
| 168 return new ui::MouseEvent(ui::ET_MOUSE_MOVED, | |
| 169 location, | |
| 170 location, | |
| 171 ui::EF_IS_SYNTHESIZED | flags, | |
|
sadrul
2014/04/22 16:49:22
Should this also set EF_FROM_TOUCH?
mfomitchev
2014/04/23 18:27:37
Done.
| |
| 172 0); | |
| 173 } | |
| 174 | |
| 175 void TouchExplorationController::EnterMouseMoveMode() { | |
| 176 aura::client::CursorClient* cursor_client = | |
| 177 root_window_ ? aura::client::GetCursorClient(root_window_) : NULL; | |
| 178 if (cursor_client) { | |
| 179 cursor_client->EnableMouseEvents(); | |
| 180 cursor_client->HideCursor(); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 } // namespace ash | |
| OLD | NEW |