OLD | NEW |
| (Empty) |
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/aura/root_window_host_mac.h" | |
6 | |
7 #import <Cocoa/Cocoa.h> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/mac/bundle_locations.h" | |
11 #include "base/mac/scoped_nsobject.h" | |
12 #include "ui/aura/event.h" | |
13 #include "ui/aura/root_window.h" | |
14 #include "ui/aura/root_window_host.h" | |
15 #include "ui/aura/root_window_mac.h" | |
16 #include "ui/aura/root_window_view_mac.h" | |
17 #include "ui/base/events/event_utils.h" | |
18 #include "ui/gfx/point.h" | |
19 | |
20 namespace aura { | |
21 | |
22 // The Mac-specific implementation of the RootWindowHost interface. This class | |
23 // acts at an intermediary between the Aura shell and an NSWindow. The | |
24 // association between the Aura compositor and the native window's view is | |
25 // established with this class as is the association between the native window's | |
26 // event dispatch and the Aura event processing. | |
27 class RootWindowHostMac : public RootWindowHost, | |
28 public RootWindowHostMacDelegate { | |
29 public: | |
30 explicit RootWindowHostMac(const gfx::Rect& bounds); | |
31 virtual ~RootWindowHostMac(); | |
32 | |
33 // RootWindowHost: | |
34 virtual void SetRootWindow(RootWindow* root_window) OVERRIDE; | |
35 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; | |
36 virtual void Show() OVERRIDE; | |
37 virtual void ToggleFullScreen() OVERRIDE; | |
38 virtual gfx::Size GetSize() const OVERRIDE; | |
39 virtual void SetSize(const gfx::Size& size) OVERRIDE; | |
40 virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE; | |
41 virtual void SetCapture() OVERRIDE; | |
42 virtual void ReleaseCapture() OVERRIDE; | |
43 virtual void SetCursor(gfx::NativeCursor cursor) OVERRIDE; | |
44 virtual void ShowCursor(bool show) OVERRIDE; | |
45 virtual bool QueryMouseLocation(gfx::Point* location_return) OVERRIDE; | |
46 virtual void MoveCursorTo(const gfx::Point& location) OVERRIDE; | |
47 virtual bool ConfineCursorToRootWindow() OVERRIDE; | |
48 virtual void UnConfineCursor() OVERRIDE; | |
49 // RootWindowHostMacDelegate: | |
50 virtual void SendEvent(const base::NativeEvent& native_event) OVERRIDE; | |
51 | |
52 // Set the initial location of the root window. The origin of |bounds| is | |
53 // top-left. This gets converted to bottom-left to match Mac coordinates on | |
54 // the main screen. | |
55 void SetLocation(const gfx::Rect& bounds); | |
56 | |
57 private: | |
58 // Weak reference. | |
59 RootWindow* root_window_; | |
60 | |
61 // The bounds of the Aura desktop. Relative to Aura's coordinate system. | |
62 // This is currently used only for size information, not location. | |
63 gfx::Rect bounds_; | |
64 | |
65 // An NSWindowController for the root window. Controls the actual Cocoa | |
66 // window on Mac. | |
67 base::scoped_nsobject<NSWindowController> controller_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(RootWindowHostMac); | |
70 }; | |
71 | |
72 RootWindowHostMacDelegate::RootWindowHostMacDelegate() { | |
73 } | |
74 | |
75 RootWindowHostMacDelegate::~RootWindowHostMacDelegate() { | |
76 } | |
77 | |
78 RootWindowHostMac::RootWindowHostMac(const gfx::Rect& bounds) | |
79 : root_window_(NULL), bounds_(bounds) { | |
80 NSString* nibpath = [base::mac::FrameworkBundle() | |
81 pathForResource:@"RootWindow" | |
82 ofType:@"nib"]; | |
83 NSWindowController* controller = [NSWindowController alloc]; | |
84 controller_.reset([controller initWithWindowNibPath:nibpath | |
85 owner:controller]); | |
86 SetSize(bounds.size()); | |
87 SetLocation(bounds); | |
88 } | |
89 | |
90 RootWindowHostMac::~RootWindowHostMac() { | |
91 RootWindowView* view = [[controller_ window] contentView]; | |
92 [view setCompositor:NULL]; | |
93 [controller_ close]; | |
94 } | |
95 | |
96 // static | |
97 RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) { | |
98 return new RootWindowHostMac(bounds); | |
99 } | |
100 | |
101 // static | |
102 gfx::Size RootWindowHost::GetNativeScreenSize() { | |
103 NSRect screen = [[NSScreen mainScreen] visibleFrame]; | |
104 return gfx::Size(NSSizeToCGSize(screen.size)); | |
105 } | |
106 | |
107 void RootWindowHostMac::SetRootWindow(RootWindow* root_window) { | |
108 root_window_ = root_window; | |
109 | |
110 RootWindowView* view = [[controller_ window] contentView]; | |
111 DCHECK([view respondsToSelector:@selector(setCompositor:)]); | |
112 [view setCompositor:root_window->compositor()]; | |
113 | |
114 RootWindowMac* window = static_cast<RootWindowMac*>([controller_ window]); | |
115 DCHECK([window respondsToSelector:@selector(setHostDelegate:)]); | |
116 [window setHostDelegate:this]; | |
117 } | |
118 | |
119 gfx::AcceleratedWidget RootWindowHostMac::GetAcceleratedWidget() { | |
120 return [[controller_ window] contentView]; | |
121 } | |
122 | |
123 void RootWindowHostMac::Show() { | |
124 [controller_ showWindow:controller_]; | |
125 } | |
126 | |
127 void RootWindowHostMac::ToggleFullScreen() { | |
128 } | |
129 | |
130 gfx::Size RootWindowHostMac::GetSize() const { | |
131 NSSize size = [[[controller_ window] contentView] bounds].size; | |
132 return gfx::Size(NSSizeToCGSize(size)); | |
133 } | |
134 | |
135 void RootWindowHostMac::SetSize(const gfx::Size& size) { | |
136 NSSize nssize = NSSizeFromCGSize(size.ToCGSize()); | |
137 [[controller_ window] setContentSize:nssize]; | |
138 [[controller_ window] setContentMaxSize:nssize]; | |
139 [[controller_ window] setContentMinSize:nssize]; | |
140 } | |
141 | |
142 gfx::Point RootWindowHostMac::GetLocationOnNativeScreen() const { | |
143 return gfx::Point(); | |
144 } | |
145 | |
146 void RootWindowHostMac::SetCapture() { | |
147 } | |
148 | |
149 void RootWindowHostMac::ReleaseCapture() { | |
150 } | |
151 | |
152 void RootWindowHostMac::SetCursor(gfx::NativeCursor cursor) { | |
153 } | |
154 | |
155 void RootWindowHostMac::ShowCursor(bool show) { | |
156 } | |
157 | |
158 bool RootWindowHostMac::QueryMouseLocation(gfx::Point* location_return) { | |
159 *location_return = gfx::Point(); | |
160 return true; | |
161 } | |
162 | |
163 void RootWindowHostMac::MoveCursorTo(const gfx::Point& location) { | |
164 } | |
165 | |
166 bool RootWindowHostMac::ConfineCursorToRootWindow() { | |
167 return false; | |
168 } | |
169 | |
170 void RootWindowHostMac::UnConfineCursor() { | |
171 } | |
172 | |
173 void RootWindowHostMac::SendEvent(const base::NativeEvent& native_event) { | |
174 ui::EventType type = ui::EventTypeFromNative(native_event); | |
175 switch (type) { | |
176 case ui::ET_MOUSE_PRESSED: | |
177 case ui::ET_MOUSE_DRAGGED: | |
178 case ui::ET_MOUSE_RELEASED: | |
179 case ui::ET_MOUSE_MOVED: | |
180 case ui::ET_MOUSE_ENTERED: | |
181 case ui::ET_MOUSE_EXITED: { | |
182 MouseEvent mouse_event(native_event); | |
183 root_window_->DispatchMouseEvent(&mouse_event); | |
184 break; | |
185 } | |
186 case ui::ET_KEY_PRESSED: | |
187 case ui::ET_KEY_RELEASED: { | |
188 KeyEvent key_event(native_event, false); | |
189 root_window_->DispatchKeyEvent(&key_event); | |
190 break; | |
191 } | |
192 case ui::ET_MOUSEWHEEL: | |
193 case ui::ET_TOUCH_RELEASED: | |
194 case ui::ET_TOUCH_PRESSED: | |
195 case ui::ET_TOUCH_MOVED: | |
196 case ui::ET_TOUCH_STATIONARY: | |
197 case ui::ET_TOUCH_CANCELLED: | |
198 case ui::ET_DROP_TARGET_EVENT: | |
199 case ui::ET_FOCUS_CHANGE: | |
200 case ui::ET_SCROLL: | |
201 case ui::ET_UNKNOWN: | |
202 default: | |
203 break; | |
204 } | |
205 } | |
206 | |
207 void RootWindowHostMac::SetLocation(const gfx::Rect& bounds) { | |
208 NSRect screen = [[NSScreen mainScreen] visibleFrame]; | |
209 NSPoint origin = NSMakePoint(screen.origin.x + bounds.x(), | |
210 screen.origin.y + screen.size.height - | |
211 bounds.y() - bounds.height()); | |
212 [[controller_ window] setFrameOrigin:origin]; | |
213 } | |
214 | |
215 } // namespace aura | |
OLD | NEW |