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.
5 #include "ui/views/widget/desktop_aura/x11_desktop_window_move_client.h"
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"
22 // Delay moving the window.
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.
28 // This constant was derived with playing with builds of chrome; it has no
29 // theoretical justification.
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;
42 X11DesktopWindowMoveClient::X11DesktopWindowMoveClient()
47 X11DesktopWindowMoveClient::~X11DesktopWindowMoveClient() {}
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_;
53 gfx::Rect target_rect(system_loc, host_->GetBounds().size());
55 window_move_timer_.Start(
57 base::TimeDelta::FromMilliseconds(kMoveDelay),
58 base::Bind(&X11DesktopWindowMoveClient::SetHostBounds,
59 base::Unretained(this),
63 void X11DesktopWindowMoveClient::OnMouseReleased() {
67 void X11DesktopWindowMoveClient::OnMoveLoopEnded() {
71 ////////////////////////////////////////////////////////////////////////////////
72 // DesktopWindowTreeHostLinux, aura::client::WindowMoveClient implementation:
74 aura::client::WindowMoveResult X11DesktopWindowMoveClient::RunMoveLoop(
76 const gfx::Vector2d& drag_offset,
77 aura::client::WindowMoveSource move_source) {
78 window_offset_ = drag_offset;
79 host_ = source->GetHost();
81 bool success = move_loop_.RunMoveLoop(source, host_->last_cursor());
82 return success ? aura::client::MOVE_SUCCESSFUL : aura::client::MOVE_CANCELED;
85 void X11DesktopWindowMoveClient::EndMoveLoop() {
86 window_move_timer_.Stop();
87 move_loop_.EndMoveLoop();
90 ////////////////////////////////////////////////////////////////////////////////
91 // DesktopWindowTreeHostLinux, private:
93 void X11DesktopWindowMoveClient::SetHostBounds(const gfx::Rect& rect) {
94 host_->SetBounds(rect);