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.
4
5#include "mojo/services/native_viewport/platform_viewport.h"
6
7#import <AppKit/NSApplication.h>
8#import <AppKit/NSView.h>
9#import <AppKit/NSWindow.h>
10
11#include "base/bind.h"
12#include "ui/gfx/rect.h"
13
14namespace mojo {
15
16class PlatformViewportMac : public PlatformViewport {
17 public:
18  PlatformViewportMac(Delegate* delegate)
19      : delegate_(delegate),
20        window_(nil) {
21  }
22
23  virtual ~PlatformViewportMac() {
24    [window_ orderOut:nil];
25    [window_ close];
26  }
27
28 private:
29  // Overridden from PlatformViewport:
30  virtual void Init(const gfx::Rect& bounds) OVERRIDE {
31    [NSApplication sharedApplication];
32
33    rect_ = bounds;
34    window_ = [[NSWindow alloc]
35                  initWithContentRect:NSRectFromCGRect(rect_.ToCGRect())
36                            styleMask:NSTitledWindowMask
37                              backing:NSBackingStoreBuffered
38                                defer:NO];
39    delegate_->OnAcceleratedWidgetAvailable([window_ contentView]);
40    delegate_->OnBoundsChanged(rect_);
41  }
42
43  virtual void Show() OVERRIDE {
44    [window_ orderFront:nil];
45  }
46
47  virtual void Hide() OVERRIDE {
48    [window_ orderOut:nil];
49  }
50
51  virtual void Close() OVERRIDE {
52    // TODO(beng): perform this in response to NSWindow destruction.
53    delegate_->OnDestroyed();
54  }
55
56  virtual gfx::Size GetSize() OVERRIDE {
57    return rect_.size();
58  }
59
60  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
61    NOTIMPLEMENTED();
62  }
63
64  virtual void SetCapture() OVERRIDE {
65    NOTIMPLEMENTED();
66  }
67
68  virtual void ReleaseCapture() OVERRIDE {
69    NOTIMPLEMENTED();
70  }
71
72  Delegate* delegate_;
73  NSWindow* window_;
74  gfx::Rect rect_;
75
76  DISALLOW_COPY_AND_ASSIGN(PlatformViewportMac);
77};
78
79// static
80scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) {
81  return scoped_ptr<PlatformViewport>(new PlatformViewportMac(delegate)).Pass();
82}
83
84}  // namespace mojo
85