1 // Copyright 2013 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 "chrome/browser/ui/tabs/dock_info.h"
7 #include "base/debug/trace_event.h"
8 #include "chrome/browser/ui/host_desktop.h"
9 #include "ui/aura/root_window.h"
10 #include "ui/aura/window.h"
12 #include "ui/base/x/x11_util.h"
15 #if !defined(OS_CHROMEOS) && defined(USE_X11)
16 #include "ui/views/widget/desktop_aura/desktop_root_window_host_x11.h"
20 ////////////////////////////////////////////////////////////////////////////////
23 // Base class used to locate a window. A subclass need only override
24 // ShouldStopIterating to determine when iteration should stop.
25 class BaseWindowFinder
: public ui::EnumerateWindowsDelegate
{
27 explicit BaseWindowFinder(const std::set
<aura::Window
*>& ignore
) {
28 std::set
<aura::Window
*>::iterator iter
;
29 for (iter
= ignore
.begin(); iter
!= ignore
.end(); iter
++) {
30 XID xid
= (*iter
)->GetDispatcher()->host()->GetAcceleratedWidget();
35 virtual ~BaseWindowFinder() {}
38 // Returns true if |window| is in the ignore list.
39 bool ShouldIgnoreWindow(XID window
) {
40 return (ignore_
.find(window
) != ignore_
.end());
43 // Returns true if iteration should stop, false otherwise.
44 virtual bool ShouldStopIterating(XID window
) OVERRIDE
{
49 std::set
<XID
> ignore_
;
51 DISALLOW_COPY_AND_ASSIGN(BaseWindowFinder
);
54 ////////////////////////////////////////////////////////////////////////////////
57 // Helper class to determine if a particular point of a window is not obscured
59 class TopMostFinder
: public BaseWindowFinder
{
61 // Returns true if |window| is not obscured by another window at the
62 // location |screen_loc|, not including the windows in |ignore|.
63 static bool IsTopMostWindowAtPoint(XID window
,
64 const gfx::Point
& screen_loc
,
65 const std::set
<aura::Window
*>& ignore
) {
66 TopMostFinder
finder(window
, screen_loc
, ignore
);
67 return finder
.is_top_most_
;
71 virtual bool ShouldStopIterating(XID window
) OVERRIDE
{
72 if (BaseWindowFinder::ShouldIgnoreWindow(window
))
75 if (window
== target_
) {
76 // Window is topmost, stop iterating.
81 if (!ui::IsWindowVisible(window
)) {
82 // The window isn't visible, keep iterating.
86 // At this point we haven't found our target window, so this window is
87 // higher in the z-order than the target window. If this window contains
88 // the point, then we can stop the search now because this window is
89 // obscuring the target window at this point.
90 return ui::WindowContainsPoint(window
, screen_loc_
);
94 TopMostFinder(XID window
,
95 const gfx::Point
& screen_loc
,
96 const std::set
<aura::Window
*>& ignore
)
97 : BaseWindowFinder(ignore
),
99 screen_loc_(screen_loc
),
100 is_top_most_(false) {
101 ui::EnumerateTopLevelWindows(this);
104 // The window we're looking for.
107 // Location of window to find.
108 gfx::Point screen_loc_
;
110 // Is target_ the top most window? This is initially false but set to true
111 // in ShouldStopIterating if target_ is passed in.
114 DISALLOW_COPY_AND_ASSIGN(TopMostFinder
);
117 ////////////////////////////////////////////////////////////////////////////////
118 // LocalProcessWindowFinder
120 // Helper class to determine if a particular point of a window from our process
121 // is not obscured by another window.
122 class LocalProcessWindowFinder
: public BaseWindowFinder
{
124 // Returns the XID from our process at screen_loc that is not obscured by
125 // another window. Returns 0 otherwise.
126 static XID
GetProcessWindowAtPoint(const gfx::Point
& screen_loc
,
127 const std::set
<aura::Window
*>& ignore
) {
128 LocalProcessWindowFinder
finder(screen_loc
, ignore
);
129 if (finder
.result_
&&
130 TopMostFinder::IsTopMostWindowAtPoint(finder
.result_
, screen_loc
,
132 return finder
.result_
;
138 virtual bool ShouldStopIterating(XID window
) OVERRIDE
{
139 if (BaseWindowFinder::ShouldIgnoreWindow(window
))
142 // Check if this window is in our process.
143 if (!aura::RootWindow::GetForAcceleratedWidget(window
))
146 if (!ui::IsWindowVisible(window
))
149 if (ui::WindowContainsPoint(window
, screen_loc_
)) {
158 LocalProcessWindowFinder(const gfx::Point
& screen_loc
,
159 const std::set
<aura::Window
*>& ignore
)
160 : BaseWindowFinder(ignore
),
161 screen_loc_(screen_loc
),
163 ui::EnumerateTopLevelWindows(this);
166 // Position of the mouse.
167 gfx::Point screen_loc_
;
169 // The resulting window. This is initially null but set to true in
170 // ShouldStopIterating if an appropriate window is found.
173 DISALLOW_COPY_AND_ASSIGN(LocalProcessWindowFinder
);
179 gfx::NativeView
DockInfo::GetLocalProcessWindowAtPoint(
180 chrome::HostDesktopType host_desktop_type
,
181 const gfx::Point
& screen_point
,
182 const std::set
<gfx::NativeView
>& ignore
) {
183 TRACE_EVENT1("ui", "DockInfo::GetLocalProcessWindowAtPoint",
184 "screen_point", screen_point
.ToString());
186 // The X11 server is the canonical state of what the window stacking order
189 LocalProcessWindowFinder::GetProcessWindowAtPoint(screen_point
, ignore
);
190 return views::DesktopWindowTreeHostX11::GetContentWindowForXID(xid
);
194 gfx::NativeView
DockInfo::GetLocalProcessWindowAtPoint(
195 chrome::HostDesktopType host_desktop_type
,
196 const gfx::Point
& screen_point
,
197 const std::set
<gfx::NativeView
>& ignore
) {
206 DockInfo
DockInfo::GetDockInfoAtPoint(chrome::HostDesktopType host_desktop_type
,
207 const gfx::Point
& screen_point
,
208 const std::set
<gfx::NativeView
>& ignore
) {
214 bool DockInfo::GetWindowBounds(gfx::Rect
* bounds
) const {
217 *bounds
= window_
->bounds();
221 void DockInfo::SizeOtherWindowTo(const gfx::Rect
& bounds
) const {
222 window_
->SetBounds(bounds
);