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#ifndef CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
6#define CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
7
8#import <Cocoa/Cocoa.h>
9#include <vector>
10
11#include "apps/native_app_window.h"
12#include "apps/shell_window.h"
13#include "base/mac/scoped_nsobject.h"
14#include "base/memory/scoped_ptr.h"
15#import "chrome/browser/ui/cocoa/browser_command_executor.h"
16#include "content/public/browser/notification_registrar.h"
17#include "extensions/common/draggable_region.h"
18#include "ui/gfx/rect.h"
19
20class ExtensionKeybindingRegistryCocoa;
21class Profile;
22class NativeAppWindowCocoa;
23@class ShellNSWindow;
24class SkRegion;
25
26// A window controller for a minimal window to host a web app view. Passes
27// Objective-C notifications to the C++ bridge.
28@interface NativeAppWindowController : NSWindowController
29                                      <NSWindowDelegate,
30                                       BrowserCommandExecutor> {
31 @private
32  NativeAppWindowCocoa* appWindow_;  // Weak; owns self.
33}
34
35@property(assign, nonatomic) NativeAppWindowCocoa* appWindow;
36
37// Consults the Command Registry to see if this |event| needs to be handled as
38// an extension command and returns YES if so (NO otherwise).
39- (BOOL)handledByExtensionCommand:(NSEvent*)event;
40
41@end
42
43// Cocoa bridge to AppWindow.
44class NativeAppWindowCocoa : public apps::NativeAppWindow {
45 public:
46  NativeAppWindowCocoa(apps::ShellWindow* shell_window,
47                       const apps::ShellWindow::CreateParams& params);
48
49  // ui::BaseWindow implementation.
50  virtual bool IsActive() const OVERRIDE;
51  virtual bool IsMaximized() const OVERRIDE;
52  virtual bool IsMinimized() const OVERRIDE;
53  virtual bool IsFullscreen() const OVERRIDE;
54  virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
55  virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
56  virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
57  virtual gfx::Rect GetBounds() const OVERRIDE;
58  virtual void Show() OVERRIDE;
59  virtual void ShowInactive() OVERRIDE;
60  virtual void Hide() OVERRIDE;
61  virtual void Close() OVERRIDE;
62  virtual void Activate() OVERRIDE;
63  virtual void Deactivate() OVERRIDE;
64  virtual void Maximize() OVERRIDE;
65  virtual void Minimize() OVERRIDE;
66  virtual void Restore() OVERRIDE;
67  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
68  virtual void FlashFrame(bool flash) OVERRIDE;
69  virtual bool IsAlwaysOnTop() const OVERRIDE;
70
71  // Called when the window is about to be closed.
72  void WindowWillClose();
73
74  // Called when the window is focused.
75  void WindowDidBecomeKey();
76
77  // Called when the window is defocused.
78  void WindowDidResignKey();
79
80  // Called when the window is resized.
81  void WindowDidResize();
82
83  // Called when the window is moved.
84  void WindowDidMove();
85
86  // Called when the window is minimized.
87  void WindowDidMiniaturize();
88
89  // Called when the window is un-minimized.
90  void WindowDidDeminiaturize();
91
92  // Called when the window is zoomed (maximized or de-maximized).
93  void WindowWillZoom();
94
95  // Called to handle a key event.
96  bool HandledByExtensionCommand(NSEvent* event);
97
98  // Called to handle a mouse event.
99  void HandleMouseEvent(NSEvent* event);
100
101  // Returns true if |point| in local Cocoa coordinate system falls within
102  // the draggable region.
103  bool IsWithinDraggableRegion(NSPoint point) const;
104
105  bool use_system_drag() const { return use_system_drag_; }
106
107 protected:
108  // NativeAppWindow implementation.
109  virtual void SetFullscreen(bool fullscreen) OVERRIDE;
110  virtual bool IsFullscreenOrPending() const OVERRIDE;
111  virtual bool IsDetached() const OVERRIDE;
112  virtual void UpdateWindowIcon() OVERRIDE;
113  virtual void UpdateWindowTitle() OVERRIDE;
114  virtual void UpdateDraggableRegions(
115      const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
116  virtual void HandleKeyboardEvent(
117      const content::NativeWebKeyboardEvent& event) OVERRIDE;
118  virtual void RenderViewHostChanged() OVERRIDE;
119  virtual gfx::Insets GetFrameInsets() const OVERRIDE;
120
121  // WebContentsModalDialogHost implementation.
122  virtual gfx::NativeView GetHostView() const OVERRIDE;
123  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
124  virtual void AddObserver(
125      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
126  virtual void RemoveObserver(
127      web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE;
128
129 private:
130  virtual ~NativeAppWindowCocoa();
131
132  ShellNSWindow* window() const;
133
134  content::WebContents* web_contents() const {
135    return shell_window_->web_contents();
136  }
137  const extensions::Extension* extension() const {
138    return shell_window_->extension();
139  }
140
141  // Returns the WindowStyleMask based on the type of window frame.
142  // Specifically, this includes NSResizableWindowMask if the window is
143  // resizable, and does not include NSTexturedBackgroundWindowMask when a
144  // native frame is used.
145  NSUInteger GetWindowStyleMask() const;
146
147  void InstallView();
148  void UninstallView();
149  void InstallDraggableRegionViews();
150  void UpdateDraggableRegionsForSystemDrag(
151      const std::vector<extensions::DraggableRegion>& regions,
152      const extensions::DraggableRegion* draggable_area);
153  void UpdateDraggableRegionsForCustomDrag(
154      const std::vector<extensions::DraggableRegion>& regions);
155
156  apps::ShellWindow* shell_window_; // weak - ShellWindow owns NativeAppWindow.
157
158  bool has_frame_;
159
160  bool is_maximized_;
161  bool is_fullscreen_;
162  NSRect restored_bounds_;
163
164  gfx::Size min_size_;
165  gfx::Size max_size_;
166  bool resizable_;
167
168  base::scoped_nsobject<NativeAppWindowController> window_controller_;
169  NSInteger attention_request_id_;  // identifier from requestUserAttention
170
171  // Indicates whether system drag or custom drag should be used, depending on
172  // the complexity of draggable regions.
173  bool use_system_drag_;
174
175  // For system drag, the whole window is draggable and the non-draggable areas
176  // have to been explicitly excluded.
177  std::vector<gfx::Rect> system_drag_exclude_areas_;
178
179  // For custom drag, the whole window is non-draggable and the draggable region
180  // has to been explicitly provided.
181  scoped_ptr<SkRegion> draggable_region_;  // used in custom drag.
182
183  // Mouse location since the last mouse event, in screen coordinates. This is
184  // used in custom drag to compute the window movement.
185  NSPoint last_mouse_location_;
186
187  // The Extension Command Registry used to determine which keyboard events to
188  // handle.
189  scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
190
191  DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
192};
193
194#endif  // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
195