summaryrefslogtreecommitdiffstats
path: root/ui/aura/desktop.h
blob: e6c12b3fad4be4bf150a728cbe66e52061617e4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_AURA_DESKTOP_H_
#define UI_AURA_DESKTOP_H_
#pragma once

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/task.h"
#include "ui/aura/aura_export.h"
#include "ui/aura/cursor.h"
#include "ui/aura/focus_manager.h"
#include "ui/aura/window.h"
#include "ui/base/events.h"
#include "ui/gfx/compositor/compositor.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/point.h"

namespace gfx {
class Size;
}

namespace ui {
class Transform;
}

namespace aura {

class DesktopDelegate;
class DesktopHost;
class DesktopObserver;
class KeyEvent;
class MouseEvent;
class ScreenAura;
class TouchEvent;

// Desktop is responsible for hosting a set of windows.
class AURA_EXPORT Desktop : public ui::CompositorDelegate,
                            public Window,
                            public internal::FocusManager {
 public:
  Desktop();
  virtual ~Desktop();

  static Desktop* GetInstance();
  static void DeleteInstanceForTesting();

  static void set_compositor_factory_for_testing(ui::Compositor*(*factory)()) {
    compositor_factory_ = factory;
  }
  static ui::Compositor* (*compositor_factory())() {
    return compositor_factory_;
  }

  ui::Compositor* compositor() { return compositor_.get(); }
  gfx::Point last_mouse_location() const { return last_mouse_location_; }
  DesktopDelegate* delegate() { return delegate_.get(); }
  Window* active_window() { return active_window_; }
  Window* mouse_pressed_handler() { return mouse_pressed_handler_; }
  Window* capture_window() { return capture_window_; }
  ScreenAura* screen() { return screen_; }

  void SetDelegate(DesktopDelegate* delegate);

  // Shows the desktop host.
  void ShowDesktop();

  // Sets the size of the desktop.
  void SetHostSize(const gfx::Size& size);
  gfx::Size GetHostSize() const;

  // Shows the specified cursor.
  void SetCursor(gfx::NativeCursor cursor);

  // Shows the desktop host and runs an event loop for it.
  void Run();

  // Draws the necessary set of windows.
  void Draw();

  // Handles a mouse event. Returns true if handled.
  bool DispatchMouseEvent(MouseEvent* event);

  // Handles a key event. Returns true if handled.
  bool DispatchKeyEvent(KeyEvent* event);

  // Handles a touch event. Returns true if handled.
  bool DispatchTouchEvent(TouchEvent* event);

  // Called when the host changes size.
  void OnHostResized(const gfx::Size& size);

  // Sets the active window to |window| and the focused window to |to_focus|.
  // If |to_focus| is NULL, |window| is focused.
  void SetActiveWindow(Window* window, Window* to_focus);

  // Activates the topmost window. Does nothing if the topmost window is already
  // active.
  void ActivateTopmostWindow();

  // Deactivates |window| and activates the topmost window. Does nothing if
  // |window| is not a topmost window, or there are no other suitable windows to
  // activate.
  void Deactivate(Window* window);

  // Invoked when |window| is being destroyed.
  void WindowDestroying(Window* window);

  // Returns the desktop's dispatcher. The result should only be passed to
  // MessageLoopForUI::Run() or MessageLoopForUI::RunAllPendingWithDispatcher(),
  // or used to dispatch an event by |Dispatch(const NativeEvent&)| on it.
  // It must never be stored.
  MessageLoop::Dispatcher* GetDispatcher();

  // Add/remove observer.
  void AddObserver(DesktopObserver* observer);
  void RemoveObserver(DesktopObserver* observer);

  // Capture -------------------------------------------------------------------

  // Sets capture to the specified window.
  void SetCapture(Window* window);

  // If |window| has mouse capture, the current capture window is set to NULL.
  void ReleaseCapture(Window* window);

  // Overridden from Window:
  virtual void SetTransform(const ui::Transform& transform) OVERRIDE;

 private:
  // Called whenever the mouse moves, tracks the current |mouse_moved_handler_|,
  // sending exited and entered events as its value changes.
  void HandleMouseMoved(const MouseEvent& event, Window* target);

  // Overridden from ui::CompositorDelegate
  virtual void ScheduleDraw();

  // Overridden from Window:
  virtual bool CanFocus() const OVERRIDE;
  virtual internal::FocusManager* GetFocusManager() OVERRIDE;
  virtual Desktop* GetDesktop() OVERRIDE;

  // Overridden from FocusManager:
  virtual void SetFocusedWindow(Window* window) OVERRIDE;
  virtual Window* GetFocusedWindow() OVERRIDE;
  virtual bool IsFocusedWindow(const Window* window) const OVERRIDE;

  // Initializes the desktop.
  void Init();

  scoped_refptr<ui::Compositor> compositor_;

  scoped_ptr<DesktopHost> host_;

  scoped_ptr<DesktopDelegate> delegate_;

  static Desktop* instance_;

  // Used to schedule painting.
  base::WeakPtrFactory<Desktop> schedule_paint_factory_;

  // Factory used to create Compositors. Settable by tests.
  static ui::Compositor*(*compositor_factory_)();

  Window* active_window_;

  // Last location seen in a mouse event.
  gfx::Point last_mouse_location_;

  // Are we in the process of being destroyed? Used to avoid processing during
  // destruction.
  bool in_destructor_;

  ObserverList<DesktopObserver> observers_;

  ScreenAura* screen_;

  // The capture window. When not-null, this window receives all the mouse and
  // touch events.
  Window* capture_window_;

  Window* mouse_pressed_handler_;
  Window* mouse_moved_handler_;
  Window* focused_window_;
  Window* touch_event_handler_;

  DISALLOW_COPY_AND_ASSIGN(Desktop);
};

}  // namespace aura

#endif  // UI_AURA_DESKTOP_H_