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(
if (keyboard_bounds.height() <= 0)
return;
bool handle_drag = false;
const ui::EventType type = event.type();
if (IsDragHandle(kb_offset, keyboard_bounds.size())) {
if (type == ui::ET_TOUCH_PRESSED ||
(type == ui::ET_MOUSE_PRESSED &&
((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton())) {
// Drag is starting.
// Mouse events are limited to just the left mouse button.
drag_started_by_touch_ = (type == ui::ET_TOUCH_PRESSED);
if (!drag_descriptor_) {
switch (type) {
case ui::ET_TOUCH_PRESSED:
case ui::ET_MOUSE_PRESSED:
if (!IsDragHandle(kb_offset, keyboard_bounds.size())) {
drag_descriptor_ = nullptr;
} else if (type == ui::ET_MOUSE_PRESSED &&
!((const ui::MouseEvent*)&event)->IsOnlyLeftMouseButton()) {
// Mouse events are limited to just the left mouse button.
drag_descriptor_ = nullptr;
} else if (!drag_descriptor_) {
// If there is no active drag descriptor, start a new one.
drag_descriptor_.reset(
new DragDescriptor(keyboard_bounds.origin(), kb_offset));
bool drag_started_by_touch = (type == ui::ET_TOUCH_PRESSED);
drag_descriptor_.reset(new DragDescriptor(
keyboard_bounds.origin(), kb_offset, drag_started_by_touch));
}
handle_drag = true;
}
}
if (drag_descriptor_ &&
(type == ui::ET_MOUSE_DRAGGED ||
(drag_started_by_touch_ && type == ui::ET_TOUCH_MOVED))) {
// Drag continues.
// If there is an active drag, use it to determine the new location
// of the keyboard.
const gfx::Point original_click_location =
drag_descriptor_->original_keyboard_location() +
drag_descriptor_->original_click_offset();
const gfx::Point current_drag_location =
keyboard_bounds.origin() + kb_offset;
const gfx::Vector2d cumulative_drag_offset =
current_drag_location - original_click_location;
const gfx::Point new_keyboard_location =
drag_descriptor_->original_keyboard_location() + cumulative_drag_offset;
const gfx::Rect new_bounds =
gfx::Rect(new_keyboard_location, keyboard_bounds.size());
controller_->MoveKeyboard(new_bounds);
SavePosition(container->bounds(), display_bounds.size());
handle_drag = true;
}
if (!handle_drag && drag_descriptor_) {
// drag has ended
drag_descriptor_ = nullptr;
break;
case ui::ET_MOUSE_DRAGGED:
case ui::ET_TOUCH_MOVED:
if (!drag_descriptor_) {
// do nothing
} else if (drag_descriptor_->is_touch_drag() !=
(type == ui::ET_TOUCH_MOVED)) {
// If the event isn't of the same type that started the drag, end the
// drag to prevent confusion.
drag_descriptor_ = nullptr;
} else {
// Drag continues.
// If there is an active drag, use it to determine the new location
// of the keyboard.
const gfx::Point original_click_location =
drag_descriptor_->original_keyboard_location() +
drag_descriptor_->original_click_offset();
const gfx::Point current_drag_location =
keyboard_bounds.origin() + kb_offset;
const gfx::Vector2d cumulative_drag_offset =
current_drag_location - original_click_location;
const gfx::Point new_keyboard_location =
drag_descriptor_->original_keyboard_location() +
cumulative_drag_offset;
const gfx::Rect new_bounds =
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 {
// Otherwise 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();
};
......
......@@ -10,8 +10,10 @@
namespace keyboard {
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_click_offset_(click_offset) {}
original_click_offset_(click_offset),
is_touch_drag_(is_touch_drag) {}
} // namespace keyboard
......@@ -18,16 +18,22 @@ namespace keyboard {
class DragDescriptor {
public:
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 {
return original_keyboard_location_;
}
gfx::Vector2d original_click_offset() const { return original_click_offset_; }
bool is_touch_drag() { return is_touch_drag_; }
private:
const gfx::Point original_keyboard_location_;
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
......
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