This source file includes following definitions.
- CanStartTwoFingerMove
- CanStartOneFingerDrag
- ConvertPointToParent
- GetWindowComponent
- resizer
- resizer_
- IsMove
- OnPreWindowStateTypeChange
- OnWindowDestroying
- destroyed_
- OnKeyEvent
- OnMouseEvent
- OnGestureEvent
- RunMoveLoop
- EndMoveLoop
- OnDisplayConfigurationChanging
- AttemptToStartDrag
- CompleteDrag
- HandleMousePressed
- HandleMouseReleased
- HandleDrag
- HandleMouseMoved
- HandleMouseExited
- SetWindowStateTypeFromGesture
- ResizerWindowDestroyed
#include "ash/wm/toplevel_window_event_handler.h"
#include "ash/shell.h"
#include "ash/wm/resize_shadow_controller.h"
#include "ash/wm/window_resizer.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_state_observer.h"
#include "ash/wm/window_util.h"
#include "ash/wm/wm_event.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "ui/aura/client/cursor_client.h"
#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/aura/window_delegate.h"
#include "ui/aura/window_event_dispatcher.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/hit_test.h"
#include "ui/base/ui_base_types.h"
#include "ui/events/event.h"
#include "ui/events/event_utils.h"
#include "ui/events/gestures/gesture_recognizer.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/screen.h"
namespace {
const double kMinHorizVelocityForWindowSwipe = 1100;
const double kMinVertVelocityForWindowMinimize = 1000;
}
namespace ash {
namespace {
bool CanStartTwoFingerMove(aura::Window* window,
int window_component1,
int window_component2) {
if (!wm::GetWindowState(window)->IsNormalOrSnapped() ||
window->type() != ui::wm::WINDOW_TYPE_NORMAL) {
return false;
}
int component1_behavior =
WindowResizer::GetBoundsChangeForWindowComponent(window_component1);
int component2_behavior =
WindowResizer::GetBoundsChangeForWindowComponent(window_component2);
return (component1_behavior & WindowResizer::kBoundsChange_Resizes) == 0 &&
(component2_behavior & WindowResizer::kBoundsChange_Resizes) == 0;
}
bool CanStartOneFingerDrag(int window_component) {
return WindowResizer::GetBoundsChangeForWindowComponent(
window_component) != 0;
}
gfx::Point ConvertPointToParent(aura::Window* window,
const gfx::Point& point) {
gfx::Point result(point);
aura::Window::ConvertPointToTarget(window, window->parent(), &result);
return result;
}
int GetWindowComponent(aura::Window* window, const ui::LocatedEvent& event) {
return window->delegate()->GetNonClientComponent(event.location());
}
}
class ToplevelWindowEventHandler::ScopedWindowResizer
: public aura::WindowObserver,
public wm::WindowStateObserver {
public:
ScopedWindowResizer(ToplevelWindowEventHandler* handler,
WindowResizer* resizer);
virtual ~ScopedWindowResizer();
bool IsMove() const;
WindowResizer* resizer() { return resizer_.get(); }
virtual void OnWindowDestroying(aura::Window* window) OVERRIDE;
virtual void OnPreWindowStateTypeChange(wm::WindowState* window_state,
wm::WindowStateType type) OVERRIDE;
private:
ToplevelWindowEventHandler* handler_;
scoped_ptr<WindowResizer> resizer_;
DISALLOW_COPY_AND_ASSIGN(ScopedWindowResizer);
};
ToplevelWindowEventHandler::ScopedWindowResizer::ScopedWindowResizer(
ToplevelWindowEventHandler* handler,
WindowResizer* resizer)
: handler_(handler),
resizer_(resizer) {
resizer_->GetTarget()->AddObserver(this);
wm::GetWindowState(resizer_->GetTarget())->AddObserver(this);
}
ToplevelWindowEventHandler::ScopedWindowResizer::~ScopedWindowResizer() {
resizer_->GetTarget()->RemoveObserver(this);
wm::GetWindowState(resizer_->GetTarget())->RemoveObserver(this);
}
bool ToplevelWindowEventHandler::ScopedWindowResizer::IsMove() const {
return resizer_->details().bounds_change ==
WindowResizer::kBoundsChange_Repositions;
}
void
ToplevelWindowEventHandler::ScopedWindowResizer::OnPreWindowStateTypeChange(
wm::WindowState* window_state,
wm::WindowStateType old) {
handler_->CompleteDrag(DRAG_COMPLETE);
}
void ToplevelWindowEventHandler::ScopedWindowResizer::OnWindowDestroying(
aura::Window* window) {
DCHECK_EQ(resizer_->GetTarget(), window);
handler_->ResizerWindowDestroyed();
}
ToplevelWindowEventHandler::ToplevelWindowEventHandler()
: first_finger_hittest_(HTNOWHERE),
in_move_loop_(false),
in_gesture_drag_(false),
drag_reverted_(false),
destroyed_(NULL) {
Shell::GetInstance()->display_controller()->AddObserver(this);
}
ToplevelWindowEventHandler::~ToplevelWindowEventHandler() {
Shell::GetInstance()->display_controller()->RemoveObserver(this);
if (destroyed_)
*destroyed_ = true;
}
void ToplevelWindowEventHandler::OnKeyEvent(ui::KeyEvent* event) {
if (window_resizer_.get() && event->type() == ui::ET_KEY_PRESSED &&
event->key_code() == ui::VKEY_ESCAPE) {
CompleteDrag(DRAG_REVERT);
}
}
void ToplevelWindowEventHandler::OnMouseEvent(
ui::MouseEvent* event) {
if (event->handled())
return;
if ((event->flags() &
(ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_RIGHT_MOUSE_BUTTON)) != 0)
return;
if (in_gesture_drag_)
return;
aura::Window* target = static_cast<aura::Window*>(event->target());
switch (event->type()) {
case ui::ET_MOUSE_PRESSED:
HandleMousePressed(target, event);
break;
case ui::ET_MOUSE_DRAGGED:
HandleDrag(target, event);
break;
case ui::ET_MOUSE_CAPTURE_CHANGED:
case ui::ET_MOUSE_RELEASED:
HandleMouseReleased(target, event);
break;
case ui::ET_MOUSE_MOVED:
HandleMouseMoved(target, event);
break;
case ui::ET_MOUSE_EXITED:
HandleMouseExited(target, event);
break;
default:
break;
}
}
void ToplevelWindowEventHandler::OnGestureEvent(ui::GestureEvent* event) {
if (event->handled())
return;
aura::Window* target = static_cast<aura::Window*>(event->target());
if (!target->delegate())
return;
if (window_resizer_.get() && !in_gesture_drag_)
return;
if (window_resizer_.get() &&
window_resizer_->resizer()->GetTarget() != target) {
return;
}
if (event->details().touch_points() > 2) {
if (window_resizer_.get()) {
CompleteDrag(DRAG_COMPLETE);
event->StopPropagation();
}
return;
}
switch (event->type()) {
case ui::ET_GESTURE_TAP_DOWN: {
int component = GetWindowComponent(target, *event);
if (!(WindowResizer::GetBoundsChangeForWindowComponent(component) &
WindowResizer::kBoundsChange_Resizes))
return;
ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller)
controller->ShowShadow(target, component);
return;
}
case ui::ET_GESTURE_END: {
ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller)
controller->HideShadow(target);
if (window_resizer_.get() &&
(event->details().touch_points() == 1 ||
!CanStartOneFingerDrag(first_finger_hittest_))) {
CompleteDrag(DRAG_COMPLETE);
event->StopPropagation();
}
return;
}
case ui::ET_GESTURE_BEGIN: {
if (event->details().touch_points() == 1) {
first_finger_hittest_ = GetWindowComponent(target, *event);
} else if (window_resizer_.get()) {
if (!window_resizer_->IsMove()) {
CompleteDrag(DRAG_COMPLETE);
event->StopPropagation();
}
} else {
int second_finger_hittest = GetWindowComponent(target, *event);
if (CanStartTwoFingerMove(
target, first_finger_hittest_, second_finger_hittest)) {
gfx::Point location_in_parent =
event->details().bounding_box().CenterPoint();
AttemptToStartDrag(target, location_in_parent, HTCAPTION,
aura::client::WINDOW_MOVE_SOURCE_TOUCH);
event->StopPropagation();
}
}
return;
}
case ui::ET_GESTURE_SCROLL_BEGIN: {
if (window_resizer_.get())
return;
int component = GetWindowComponent(target, *event);
if (!CanStartOneFingerDrag(component))
return;
gfx::Point location_in_parent(
ConvertPointToParent(target, event->location()));
AttemptToStartDrag(target, location_in_parent, component,
aura::client::WINDOW_MOVE_SOURCE_TOUCH);
event->StopPropagation();
return;
}
default:
break;
}
if (!window_resizer_.get())
return;
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_UPDATE:
HandleDrag(target, event);
event->StopPropagation();
return;
case ui::ET_GESTURE_SCROLL_END:
CompleteDrag(DRAG_COMPLETE);
event->StopPropagation();
return;
case ui::ET_SCROLL_FLING_START:
CompleteDrag(DRAG_COMPLETE);
if (GetWindowComponent(target, *event) != HTCAPTION ||
!wm::GetWindowState(target)->IsNormalOrSnapped()) {
return;
}
if (event->details().velocity_y() > kMinVertVelocityForWindowMinimize) {
SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
} else if (event->details().velocity_y() <
-kMinVertVelocityForWindowMinimize) {
SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
} else if (event->details().velocity_x() >
kMinHorizVelocityForWindowSwipe) {
SetWindowStateTypeFromGesture(target,
wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
} else if (event->details().velocity_x() <
-kMinHorizVelocityForWindowSwipe) {
SetWindowStateTypeFromGesture(target,
wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
}
event->StopPropagation();
return;
case ui::ET_GESTURE_MULTIFINGER_SWIPE:
if (!wm::GetWindowState(target)->IsNormalOrSnapped())
return;
CompleteDrag(DRAG_COMPLETE);
if (event->details().swipe_down()) {
SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MINIMIZED);
} else if (event->details().swipe_up()) {
SetWindowStateTypeFromGesture(target, wm::WINDOW_STATE_TYPE_MAXIMIZED);
} else if (event->details().swipe_right()) {
SetWindowStateTypeFromGesture(target,
wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED);
} else {
SetWindowStateTypeFromGesture(target,
wm::WINDOW_STATE_TYPE_LEFT_SNAPPED);
}
event->StopPropagation();
return;
default:
return;
}
}
aura::client::WindowMoveResult ToplevelWindowEventHandler::RunMoveLoop(
aura::Window* source,
const gfx::Vector2d& drag_offset,
aura::client::WindowMoveSource move_source) {
DCHECK(!in_move_loop_);
aura::Window* root_window = source->GetRootWindow();
DCHECK(root_window);
gfx::Point drag_location;
if (move_source == aura::client::WINDOW_MOVE_SOURCE_TOUCH &&
aura::Env::GetInstance()->is_touch_down()) {
gfx::PointF drag_location_f;
bool has_point = ui::GestureRecognizer::Get()->
GetLastTouchPointForTarget(source, &drag_location_f);
drag_location = gfx::ToFlooredPoint(drag_location_f);
DCHECK(has_point);
} else {
drag_location =
root_window->GetHost()->dispatcher()->GetLastMouseLocationInRoot();
aura::Window::ConvertPointToTarget(
root_window, source->parent(), &drag_location);
}
aura::client::CursorClient* cursor_client =
aura::client::GetCursorClient(root_window);
if (cursor_client)
cursor_client->SetCursor(ui::kCursorPointer);
if (!AttemptToStartDrag(source, drag_location, HTCAPTION, move_source))
return aura::client::MOVE_CANCELED;
in_move_loop_ = true;
bool destroyed = false;
destroyed_ = &destroyed;
base::MessageLoopForUI* loop = base::MessageLoopForUI::current();
base::MessageLoop::ScopedNestableTaskAllower allow_nested(loop);
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
run_loop.Run();
if (destroyed)
return aura::client::MOVE_CANCELED;
destroyed_ = NULL;
in_move_loop_ = false;
return drag_reverted_ ? aura::client::MOVE_CANCELED :
aura::client::MOVE_SUCCESSFUL;
}
void ToplevelWindowEventHandler::EndMoveLoop() {
if (in_move_loop_)
CompleteDrag(DRAG_REVERT);
}
void ToplevelWindowEventHandler::OnDisplayConfigurationChanging() {
CompleteDrag(DRAG_REVERT);
}
bool ToplevelWindowEventHandler::AttemptToStartDrag(
aura::Window* window,
const gfx::Point& point_in_parent,
int window_component,
aura::client::WindowMoveSource source) {
if (window_resizer_.get())
return false;
WindowResizer* resizer = CreateWindowResizer(window, point_in_parent,
window_component, source).release();
if (!resizer)
return false;
window_resizer_.reset(new ScopedWindowResizer(this, resizer));
pre_drag_window_bounds_ = window->bounds();
in_gesture_drag_ = (source == aura::client::WINDOW_MOVE_SOURCE_TOUCH);
return true;
}
void ToplevelWindowEventHandler::CompleteDrag(DragCompletionStatus status) {
scoped_ptr<ScopedWindowResizer> resizer(window_resizer_.release());
if (resizer) {
if (status == DRAG_COMPLETE)
resizer->resizer()->CompleteDrag();
else
resizer->resizer()->RevertDrag();
}
drag_reverted_ = (status == DRAG_REVERT);
first_finger_hittest_ = HTNOWHERE;
in_gesture_drag_ = false;
if (in_move_loop_)
quit_closure_.Run();
}
void ToplevelWindowEventHandler::HandleMousePressed(
aura::Window* target,
ui::MouseEvent* event) {
if (event->phase() != ui::EP_PRETARGET || !target->delegate())
return;
int component = GetWindowComponent(target, *event);
if ((event->flags() &
(ui::EF_IS_DOUBLE_CLICK | ui::EF_IS_TRIPLE_CLICK)) == 0 &&
WindowResizer::GetBoundsChangeForWindowComponent(component)) {
gfx::Point location_in_parent(
ConvertPointToParent(target, event->location()));
AttemptToStartDrag(target, location_in_parent, component,
aura::client::WINDOW_MOVE_SOURCE_MOUSE);
event->StopPropagation();
} else {
CompleteDrag(DRAG_COMPLETE);
}
}
void ToplevelWindowEventHandler::HandleMouseReleased(
aura::Window* target,
ui::MouseEvent* event) {
if (event->phase() != ui::EP_PRETARGET)
return;
CompleteDrag(event->type() == ui::ET_MOUSE_RELEASED ?
DRAG_COMPLETE : DRAG_REVERT);
if (window_resizer_ &&
event->type() == ui::ET_MOUSE_CAPTURE_CHANGED &&
!target->IsVisible()) {
event->StopPropagation();
}
}
void ToplevelWindowEventHandler::HandleDrag(
aura::Window* target,
ui::LocatedEvent* event) {
DCHECK(event->type() == ui::ET_MOUSE_DRAGGED ||
event->type() == ui::ET_TOUCH_MOVED ||
event->type() == ui::ET_GESTURE_SCROLL_UPDATE);
if (event->phase() != ui::EP_PRETARGET)
return;
if (!window_resizer_)
return;
window_resizer_->resizer()->Drag(
ConvertPointToParent(target, event->location()), event->flags());
event->StopPropagation();
}
void ToplevelWindowEventHandler::HandleMouseMoved(
aura::Window* target,
ui::LocatedEvent* event) {
if (event->phase() != ui::EP_POSTTARGET || !target->delegate())
return;
ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller) {
if (event->flags() & ui::EF_IS_NON_CLIENT) {
int component =
target->delegate()->GetNonClientComponent(event->location());
controller->ShowShadow(target, component);
} else {
controller->HideShadow(target);
}
}
}
void ToplevelWindowEventHandler::HandleMouseExited(
aura::Window* target,
ui::LocatedEvent* event) {
if (event->phase() != ui::EP_POSTTARGET)
return;
ResizeShadowController* controller =
Shell::GetInstance()->resize_shadow_controller();
if (controller)
controller->HideShadow(target);
}
void ToplevelWindowEventHandler::SetWindowStateTypeFromGesture(
aura::Window* window,
wm::WindowStateType new_state_type) {
wm::WindowState* window_state = ash::wm::GetWindowState(window);
switch (new_state_type) {
case wm::WINDOW_STATE_TYPE_MINIMIZED:
if (window_state->CanMinimize()) {
window_state->Minimize();
window_state->set_unminimize_to_restore_bounds(true);
window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
}
break;
case wm::WINDOW_STATE_TYPE_MAXIMIZED:
if (window_state->CanMaximize()) {
window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
window_state->Maximize();
}
break;
case wm::WINDOW_STATE_TYPE_LEFT_SNAPPED:
if (window_state->CanSnap()) {
window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
const wm::WMEvent event(wm::WM_EVENT_SNAP_LEFT);
window_state->OnWMEvent(&event);
}
break;
case wm::WINDOW_STATE_TYPE_RIGHT_SNAPPED:
if (window_state->CanSnap()) {
window_state->SetRestoreBoundsInParent(pre_drag_window_bounds_);
const wm::WMEvent event(wm::WM_EVENT_SNAP_RIGHT);
window_state->OnWMEvent(&event);
}
break;
default:
NOTREACHED();
}
}
void ToplevelWindowEventHandler::ResizerWindowDestroyed() {
window_resizer_.reset();
CompleteDrag(DRAG_REVERT);
}
}