482986b9f3f3d38fa4c0298c929cea1f677da07a
[platform/framework/web/crosswalk.git] / src / ui / views / widget / desktop_aura / x11_desktop_window_move_client.cc
1 // Copyright (c) 2012 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 "ui/views/widget/desktop_aura/x11_desktop_window_move_client.h"
6
7 #include <X11/Xlib.h>
8
9 #include "base/debug/stack_trace.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/message_loop/message_pump_x11.h"
12 #include "base/run_loop.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/window.h"
15 #include "ui/aura/window_tree_host.h"
16 #include "ui/base/x/x11_util.h"
17 #include "ui/events/event.h"
18 #include "ui/gfx/screen.h"
19
20 namespace {
21
22 // Delay moving the window.
23 //
24 // When we receive a mouse move event, we have to have it processed in a
25 // different run through the message pump because moving the window will
26 // otherwise prevent tasks from running.
27 //
28 // This constant was derived with playing with builds of chrome; it has no
29 // theoretical justification.
30 //
31 // TODO(erg): This helps with the performance of dragging windows, but it
32 // doesn't really solve the hard problems, which is that various calls to X11,
33 // such as XQueryPointer, ui::IsWindowVisible() and ui::WindowContainsPoint()
34 // take a while to get replies and block in the process. I've seen all of the
35 // above take as long as 20ms to respond.
36 const int kMoveDelay = 3;
37
38 }  // namespace
39
40 namespace views {
41
42 X11DesktopWindowMoveClient::X11DesktopWindowMoveClient()
43     : move_loop_(this),
44       host_(NULL) {
45 }
46
47 X11DesktopWindowMoveClient::~X11DesktopWindowMoveClient() {}
48
49 void X11DesktopWindowMoveClient::OnMouseMovement(XMotionEvent* event) {
50   gfx::Point cursor_point(event->x_root, event->y_root);
51   gfx::Point system_loc = cursor_point - window_offset_;
52
53   gfx::Rect target_rect(system_loc, host_->GetBounds().size());
54
55   window_move_timer_.Start(
56       FROM_HERE,
57       base::TimeDelta::FromMilliseconds(kMoveDelay),
58       base::Bind(&X11DesktopWindowMoveClient::SetHostBounds,
59                  base::Unretained(this),
60                  target_rect));
61 }
62
63 void X11DesktopWindowMoveClient::OnMouseReleased() {
64   EndMoveLoop();
65 }
66
67 void X11DesktopWindowMoveClient::OnMoveLoopEnded() {
68   host_ = NULL;
69 }
70
71 ////////////////////////////////////////////////////////////////////////////////
72 // DesktopWindowTreeHostLinux, aura::client::WindowMoveClient implementation:
73
74 aura::client::WindowMoveResult X11DesktopWindowMoveClient::RunMoveLoop(
75     aura::Window* source,
76     const gfx::Vector2d& drag_offset,
77     aura::client::WindowMoveSource move_source) {
78   window_offset_ = drag_offset;
79   host_ = source->GetHost();
80
81   bool success = move_loop_.RunMoveLoop(source, host_->last_cursor());
82   return success ? aura::client::MOVE_SUCCESSFUL : aura::client::MOVE_CANCELED;
83 }
84
85 void X11DesktopWindowMoveClient::EndMoveLoop() {
86   window_move_timer_.Stop();
87   move_loop_.EndMoveLoop();
88 }
89
90 ////////////////////////////////////////////////////////////////////////////////
91 // DesktopWindowTreeHostLinux, private:
92
93 void X11DesktopWindowMoveClient::SetHostBounds(const gfx::Rect& rect) {
94   host_->SetBounds(rect);
95 }
96
97 }  // namespace views