summaryrefslogtreecommitdiff
path: root/chromium/ui/aura/root_window_host_mac.mm
blob: 28943a958f9a9f0ec070d30ff386b49fb8a98e51 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright (c) 2012 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.

#include "ui/aura/root_window_host_mac.h"

#import <Cocoa/Cocoa.h>

#include "base/compiler_specific.h"
#include "base/mac/bundle_locations.h"
#include "base/mac/scoped_nsobject.h"
#include "ui/aura/event.h"
#include "ui/aura/root_window.h"
#include "ui/aura/root_window_host.h"
#include "ui/aura/root_window_mac.h"
#include "ui/aura/root_window_view_mac.h"
#include "ui/base/events/event_utils.h"
#include "ui/gfx/point.h"

namespace aura {

// The Mac-specific implementation of the RootWindowHost interface.  This class
// acts at an intermediary between the Aura shell and an NSWindow.  The
// association between the Aura compositor and the native window's view is
// established with this class as is the association between the native window's
// event dispatch and the Aura event processing.
class RootWindowHostMac : public RootWindowHost,
                          public RootWindowHostMacDelegate {
 public:
  explicit RootWindowHostMac(const gfx::Rect& bounds);
  virtual ~RootWindowHostMac();

  // RootWindowHost:
  virtual void SetRootWindow(RootWindow* root_window) OVERRIDE;
  virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
  virtual void Show() OVERRIDE;
  virtual void ToggleFullScreen() OVERRIDE;
  virtual gfx::Size GetSize() const OVERRIDE;
  virtual void SetSize(const gfx::Size& size) OVERRIDE;
  virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
  virtual void SetCapture() OVERRIDE;
  virtual void ReleaseCapture() OVERRIDE;
  virtual void SetCursor(gfx::NativeCursor cursor) OVERRIDE;
  virtual void ShowCursor(bool show) OVERRIDE;
  virtual bool QueryMouseLocation(gfx::Point* location_return) OVERRIDE;
  virtual void MoveCursorTo(const gfx::Point& location) OVERRIDE;
  virtual bool ConfineCursorToRootWindow() OVERRIDE;
  virtual void UnConfineCursor() OVERRIDE;
  virtual bool CopyAreaToSkCanvas(const gfx::Rect& source_bounds,
                                  const gfx::Point& dest_offset,
                                  SkCanvas* canvas) OVERRIDE;
  // RootWindowHostMacDelegate:
  virtual void SendEvent(const base::NativeEvent& native_event) OVERRIDE;

  // Set the initial location of the root window.  The origin of |bounds| is
  // top-left.  This gets converted to bottom-left to match Mac coordinates on
  // the main screen.
  void SetLocation(const gfx::Rect& bounds);

 private:
  // Weak reference.
  RootWindow* root_window_;

  // The bounds of the Aura desktop.  Relative to Aura's coordinate system.
  // This is currently used only for size information, not location.
  gfx::Rect bounds_;

  // An NSWindowController for the root window.  Controls the actual Cocoa
  // window on Mac.
  base::scoped_nsobject<NSWindowController> controller_;

  DISALLOW_COPY_AND_ASSIGN(RootWindowHostMac);
};

RootWindowHostMacDelegate::RootWindowHostMacDelegate() {
}

RootWindowHostMacDelegate::~RootWindowHostMacDelegate() {
}

RootWindowHostMac::RootWindowHostMac(const gfx::Rect& bounds)
    : root_window_(NULL), bounds_(bounds) {
  NSString* nibpath = [base::mac::FrameworkBundle()
                           pathForResource:@"RootWindow"
                                    ofType:@"nib"];
  NSWindowController* controller = [NSWindowController alloc];
  controller_.reset([controller initWithWindowNibPath:nibpath
                                                owner:controller]);
  SetSize(bounds.size());
  SetLocation(bounds);
}

RootWindowHostMac::~RootWindowHostMac() {
  RootWindowView* view = [[controller_ window] contentView];
  [view setCompositor:NULL];
  [controller_ close];
}

// static
RootWindowHost* RootWindowHost::Create(const gfx::Rect& bounds) {
  return new RootWindowHostMac(bounds);
}

// static
gfx::Size RootWindowHost::GetNativeScreenSize() {
  NSRect screen = [[NSScreen mainScreen] visibleFrame];
  return gfx::Size(NSSizeToCGSize(screen.size));
}

void RootWindowHostMac::SetRootWindow(RootWindow* root_window) {
  root_window_ = root_window;

  RootWindowView* view = [[controller_ window] contentView];
  DCHECK([view respondsToSelector:@selector(setCompositor:)]);
  [view setCompositor:root_window->compositor()];

  RootWindowMac* window = static_cast<RootWindowMac*>([controller_ window]);
  DCHECK([window respondsToSelector:@selector(setHostDelegate:)]);
  [window setHostDelegate:this];
}

gfx::AcceleratedWidget RootWindowHostMac::GetAcceleratedWidget() {
  return [[controller_ window] contentView];
}

void RootWindowHostMac::Show() {
  [controller_ showWindow:controller_];
}

void RootWindowHostMac::ToggleFullScreen() {
}

gfx::Size RootWindowHostMac::GetSize() const {
  NSSize size = [[[controller_ window] contentView] bounds].size;
  return gfx::Size(NSSizeToCGSize(size));
}

void RootWindowHostMac::SetSize(const gfx::Size& size) {
  NSSize nssize = NSSizeFromCGSize(size.ToCGSize());
  [[controller_ window] setContentSize:nssize];
  [[controller_ window] setContentMaxSize:nssize];
  [[controller_ window] setContentMinSize:nssize];
}

gfx::Point RootWindowHostMac::GetLocationOnNativeScreen() const {
  return gfx::Point();
}

void RootWindowHostMac::SetCapture() {
}

void RootWindowHostMac::ReleaseCapture() {
}

void RootWindowHostMac::SetCursor(gfx::NativeCursor cursor) {
}

void RootWindowHostMac::ShowCursor(bool show) {
}

bool RootWindowHostMac::QueryMouseLocation(gfx::Point* location_return) {
  *location_return = gfx::Point();
  return true;
}

void RootWindowHostMac::MoveCursorTo(const gfx::Point& location) {
}

bool RootWindowHostMac::ConfineCursorToRootWindow() {
  return false;
}

void RootWindowHostMac::UnConfineCursor() {
}

void RootWindowHostMac::SendEvent(const base::NativeEvent& native_event) {
  ui::EventType type = ui::EventTypeFromNative(native_event);
  switch (type) {
    case ui::ET_MOUSE_PRESSED:
    case ui::ET_MOUSE_DRAGGED:
    case ui::ET_MOUSE_RELEASED:
    case ui::ET_MOUSE_MOVED:
    case ui::ET_MOUSE_ENTERED:
    case ui::ET_MOUSE_EXITED: {
      MouseEvent mouse_event(native_event);
      root_window_->DispatchMouseEvent(&mouse_event);
      break;
    }
    case ui::ET_KEY_PRESSED:
    case ui::ET_KEY_RELEASED: {
      KeyEvent key_event(native_event, false);
      root_window_->DispatchKeyEvent(&key_event);
      break;
    }
    case ui::ET_MOUSEWHEEL:
    case ui::ET_TOUCH_RELEASED:
    case ui::ET_TOUCH_PRESSED:
    case ui::ET_TOUCH_MOVED:
    case ui::ET_TOUCH_STATIONARY:
    case ui::ET_TOUCH_CANCELLED:
    case ui::ET_DROP_TARGET_EVENT:
    case ui::ET_FOCUS_CHANGE:
    case ui::ET_SCROLL:
    case ui::ET_UNKNOWN:
    default:
      break;
  }
}

void RootWindowHostMac::SetLocation(const gfx::Rect& bounds) {
  NSRect screen = [[NSScreen mainScreen] visibleFrame];
  NSPoint origin = NSMakePoint(screen.origin.x + bounds.x(),
                               screen.origin.y + screen.size.height -
                                   bounds.y() - bounds.height());
  [[controller_ window] setFrameOrigin:origin];
}

}  // namespace aura