Commit 5efc817a authored by Blake O'Hare's avatar Blake O'Hare Committed by Commit Bot

Random Cleanup: Floating keyboard dragging code

Was trying to fix something in this code for an unrelated bug and came
across a few things that could be improved:
* There were too many stateful booleans making the code unnecessarily
complex.
* The type of event that initialized the drag should be in the
DragDescriptor, because it describes the drag and also has the same
life cycle.
* Flatten complicated if statements into chains of if/else's that only
use simple conditions.
* Use a switch statement for the event type.

Change-Id: I74712df2502960c6a98be0a705a5ced94a61ec75
Reviewed-on: https://chromium-review.googlesource.com/979378Reviewed-by: default avatarYuichiro Hanada <yhanada@chromium.org>
Commit-Queue: Blake O'Hare <blakeo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545737}
parent a6d19f5c
...@@ -190,48 +190,57 @@ void ContainerFloatingBehavior::HandlePointerEvent( ...@@ -190,48 +190,57 @@ void ContainerFloatingBehavior::HandlePointerEvent(
if (keyboard_bounds.height() <= 0) if (keyboard_bounds.height() <= 0)
return; return;
bool handle_drag = false;
const ui::EventType type = event.type(); const ui::EventType type = event.type();
if (IsDragHandle(kb_offset, keyboard_bounds.size())) { switch (type) {
if (type == ui::ET_TOUCH_PRESSED || case ui::ET_TOUCH_PRESSED:
(type == ui::ET_MOUSE_PRESSED && case ui::ET_MOUSE_PRESSED:
((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton())) { if (!IsDragHandle(kb_offset, keyboard_bounds.size())) {
// Drag is starting. drag_descriptor_ = nullptr;
// Mouse events are limited to just the left mouse button. } else if (type == ui::ET_MOUSE_PRESSED &&
!((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton()) {
drag_started_by_touch_ = (type == ui::ET_TOUCH_PRESSED); // Mouse events are limited to just the left mouse button.
if (!drag_descriptor_) { drag_descriptor_ = nullptr;
} else if (!drag_descriptor_) {
// If there is no active drag descriptor, start a new one. // If there is no active drag descriptor, start a new one.
drag_descriptor_.reset( bool drag_started_by_touch = (type == ui::ET_TOUCH_PRESSED);
new DragDescriptor(keyboard_bounds.origin(), kb_offset)); drag_descriptor_.reset(new DragDescriptor(
keyboard_bounds.origin(), kb_offset, drag_started_by_touch));
} }
handle_drag = true; break;
}
} case ui::ET_MOUSE_DRAGGED:
if (drag_descriptor_ && case ui::ET_TOUCH_MOVED:
(type == ui::ET_MOUSE_DRAGGED || if (!drag_descriptor_) {
(drag_started_by_touch_ && type == ui::ET_TOUCH_MOVED))) { // do nothing
// Drag continues. } else if (drag_descriptor_->is_touch_drag() !=
// If there is an active drag, use it to determine the new location (type == ui::ET_TOUCH_MOVED)) {
// of the keyboard. // If the event isn't of the same type that started the drag, end the
const gfx::Point original_click_location = // drag to prevent confusion.
drag_descriptor_->original_keyboard_location() + drag_descriptor_ = nullptr;
drag_descriptor_->original_click_offset(); } else {
const gfx::Point current_drag_location = // Drag continues.
keyboard_bounds.origin() + kb_offset; // If there is an active drag, use it to determine the new location
const gfx::Vector2d cumulative_drag_offset = // of the keyboard.
current_drag_location - original_click_location; const gfx::Point original_click_location =
const gfx::Point new_keyboard_location = drag_descriptor_->original_keyboard_location() +
drag_descriptor_->original_keyboard_location() + cumulative_drag_offset; drag_descriptor_->original_click_offset();
const gfx::Rect new_bounds = const gfx::Point current_drag_location =
gfx::Rect(new_keyboard_location, keyboard_bounds.size()); keyboard_bounds.origin() + kb_offset;
controller_->MoveKeyboard(new_bounds); const gfx::Vector2d cumulative_drag_offset =
SavePosition(container->bounds(), display_bounds.size()); current_drag_location - original_click_location;
handle_drag = true; const gfx::Point new_keyboard_location =
} drag_descriptor_->original_keyboard_location() +
if (!handle_drag && drag_descriptor_) { cumulative_drag_offset;
// drag has ended const gfx::Rect new_bounds =
drag_descriptor_ = nullptr; gfx::Rect(new_keyboard_location, keyboard_bounds.size());
controller_->MoveKeyboard(new_bounds);
SavePosition(container->bounds(), display_bounds.size());
}
break;
default:
drag_descriptor_ = nullptr;
break;
} }
} }
......
...@@ -84,10 +84,6 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior { ...@@ -84,10 +84,6 @@ class KEYBOARD_EXPORT ContainerFloatingBehavior : public ContainerBehavior {
// Otherwise nullptr. // Otherwise nullptr.
std::unique_ptr<DragDescriptor> drag_descriptor_ = nullptr; std::unique_ptr<DragDescriptor> drag_descriptor_ = nullptr;
// Distinguish whether the current drag is from a touch event or mouse event,
// so drag/move events can be filtered accordingly
bool drag_started_by_touch_ = false;
gfx::Rect draggable_area_ = gfx::Rect(); gfx::Rect draggable_area_ = gfx::Rect();
}; };
......
...@@ -10,8 +10,10 @@ ...@@ -10,8 +10,10 @@
namespace keyboard { namespace keyboard {
DragDescriptor::DragDescriptor(const gfx::Point& keyboard_location, DragDescriptor::DragDescriptor(const gfx::Point& keyboard_location,
const gfx::Vector2d& click_offset) const gfx::Vector2d& click_offset,
bool is_touch_drag)
: original_keyboard_location_(keyboard_location), : original_keyboard_location_(keyboard_location),
original_click_offset_(click_offset) {} original_click_offset_(click_offset),
is_touch_drag_(is_touch_drag) {}
} // namespace keyboard } // namespace keyboard
...@@ -18,16 +18,22 @@ namespace keyboard { ...@@ -18,16 +18,22 @@ namespace keyboard {
class DragDescriptor { class DragDescriptor {
public: public:
DragDescriptor(const gfx::Point& keyboard_location, DragDescriptor(const gfx::Point& keyboard_location,
const gfx::Vector2d& click_offset); const gfx::Vector2d& click_offset,
bool is_touch_drag);
gfx::Point original_keyboard_location() const { gfx::Point original_keyboard_location() const {
return original_keyboard_location_; return original_keyboard_location_;
} }
gfx::Vector2d original_click_offset() const { return original_click_offset_; } gfx::Vector2d original_click_offset() const { return original_click_offset_; }
bool is_touch_drag() { return is_touch_drag_; }
private: private:
const gfx::Point original_keyboard_location_; const gfx::Point original_keyboard_location_;
const gfx::Vector2d original_click_offset_; const gfx::Vector2d original_click_offset_;
// Distinguish whether the current drag is from a touch event or mouse event,
// so drag/move events can be filtered accordingly
const bool is_touch_drag_;
}; };
} // namespace keyboard } // namespace keyboard
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment