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/size_constraints.h"
12 #include "apps/ui/native_app_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/web_contents_observer.h"
17 #include "extensions/common/draggable_region.h"
18 #include "ui/gfx/rect.h"
19
20 namespace apps {
21 class AppWindow;
22 }
23
24 class ExtensionKeybindingRegistryCocoa;
25 class NativeAppWindowCocoa;
26 @class ShellNSWindow;
27 class SkRegion;
28
29 // A window controller for a minimal window to host a web app view. Passes
30 // Objective-C notifications to the C++ bridge.
31 @interface NativeAppWindowController : NSWindowController
32 <NSWindowDelegate,
33 BrowserCommandExecutor> {
34 @private
35 NativeAppWindowCocoa* appWindow_; // Weak; owns self.
36 }
37
property(assign,nonatomic)38 @property(assign, nonatomic) NativeAppWindowCocoa* appWindow;
39
40 // Consults the Command Registry to see if this |event| needs to be handled as
41 // an extension command and returns YES if so (NO otherwise).
42 - (BOOL)handledByExtensionCommand:(NSEvent*)event;
43
44 @end
45
46 // Cocoa bridge to AppWindow.
47 class NativeAppWindowCocoa : public apps::NativeAppWindow,
48 public content::WebContentsObserver {
49 public:
50 NativeAppWindowCocoa(apps::AppWindow* app_window,
51 const apps::AppWindow::CreateParams& params);
52
53 // ui::BaseWindow implementation.
54 virtual bool IsActive() const OVERRIDE;
55 virtual bool IsMaximized() const OVERRIDE;
56 virtual bool IsMinimized() const OVERRIDE;
57 virtual bool IsFullscreen() const OVERRIDE;
58 virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
59 virtual gfx::Rect GetRestoredBounds() const OVERRIDE;
60 virtual ui::WindowShowState GetRestoredState() const OVERRIDE;
61 virtual gfx::Rect GetBounds() const OVERRIDE;
62 virtual void Show() OVERRIDE;
63 virtual void ShowInactive() OVERRIDE;
64 virtual void Hide() OVERRIDE;
65 virtual void Close() OVERRIDE;
66 virtual void Activate() OVERRIDE;
67 virtual void Deactivate() OVERRIDE;
68 virtual void Maximize() OVERRIDE;
69 virtual void Minimize() OVERRIDE;
70 virtual void Restore() OVERRIDE;
71 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
72 virtual void FlashFrame(bool flash) OVERRIDE;
73 virtual bool IsAlwaysOnTop() const OVERRIDE;
74
75 // Called when the window is about to be closed.
76 void WindowWillClose();
77
78 // Called when the window is focused.
79 void WindowDidBecomeKey();
80
81 // Called when the window is defocused.
82 void WindowDidResignKey();
83
84 // Called when the window finishes resizing, i.e. after zoom/unzoom, after
85 // entering/leaving fullscreen, and after a user is done resizing.
86 void WindowDidFinishResize();
87
88 // Called when the window is resized. This is called repeatedly during a
89 // zoom/unzoom, and while a user is resizing.
90 void WindowDidResize();
91
92 // Called when the window is moved.
93 void WindowDidMove();
94
95 // Called when the window is minimized.
96 void WindowDidMiniaturize();
97
98 // Called when the window is un-minimized.
99 void WindowDidDeminiaturize();
100
101 // Called when the window is zoomed (maximized or de-maximized).
102 void WindowWillZoom();
103
104 // Called when the window enters fullscreen.
105 void WindowDidEnterFullscreen();
106
107 // Called when the window exits fullscreen.
108 void WindowDidExitFullscreen();
109
110 // Called to handle a key event.
111 bool HandledByExtensionCommand(NSEvent* event);
112
113 // Returns true if |point| in local Cocoa coordinate system falls within
114 // the draggable region.
115 bool IsWithinDraggableRegion(NSPoint point) const;
116
117 NSRect restored_bounds() const { return restored_bounds_; }
118
119 protected:
120 // NativeAppWindow implementation.
121 virtual void SetFullscreen(int fullscreen_types) OVERRIDE;
122 virtual bool IsFullscreenOrPending() const OVERRIDE;
123 virtual bool IsDetached() const OVERRIDE;
124 virtual void UpdateWindowIcon() OVERRIDE;
125 virtual void UpdateWindowTitle() OVERRIDE;
126 virtual void UpdateBadgeIcon() OVERRIDE;
127 virtual void UpdateShape(scoped_ptr<SkRegion> region) OVERRIDE;
128 virtual void UpdateDraggableRegions(
129 const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
130 virtual SkRegion* GetDraggableRegion() OVERRIDE;
131 virtual void HandleKeyboardEvent(
132 const content::NativeWebKeyboardEvent& event) OVERRIDE;
133 virtual bool IsFrameless() const OVERRIDE;
134 virtual bool HasFrameColor() const OVERRIDE;
135 virtual SkColor ActiveFrameColor() const OVERRIDE;
136 virtual SkColor InactiveFrameColor() const OVERRIDE;
137 virtual gfx::Insets GetFrameInsets() const OVERRIDE;
138 virtual bool CanHaveAlphaEnabled() const OVERRIDE;
139
140 // These are used to simulate Mac-style hide/show. Since windows can be hidden
141 // and shown using the app.window API, this sets is_hidden_with_app_ to
142 // differentiate the reason a window was hidden.
143 virtual void ShowWithApp() OVERRIDE;
144 virtual void HideWithApp() OVERRIDE;
145 virtual void UpdateShelfMenu() OVERRIDE;
146 virtual gfx::Size GetContentMinimumSize() const OVERRIDE;
147 virtual gfx::Size GetContentMaximumSize() const OVERRIDE;
148 virtual void SetContentSizeConstraints(const gfx::Size& min_size,
149 const gfx::Size& max_size) OVERRIDE;
150
151 // WebContentsObserver implementation.
152 virtual void RenderViewCreated(content::RenderViewHost* rvh) OVERRIDE;
153
154 virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
155
156 // WebContentsModalDialogHost implementation.
157 virtual gfx::NativeView GetHostView() const OVERRIDE;
158 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
159 virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
160 virtual void AddObserver(
161 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
162 virtual void RemoveObserver(
163 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
164
165 private:
166 virtual ~NativeAppWindowCocoa();
167
168 ShellNSWindow* window() const;
169 content::WebContents* WebContents() const;
170
171 // Returns the WindowStyleMask based on the type of window frame.
172 // This includes NSResizableWindowMask if the window is resizable.
173 NSUInteger GetWindowStyleMask() const;
174
175 void InstallView();
176 void UninstallView();
177 void UpdateDraggableRegionViews();
178
179 // Cache |restored_bounds_| only if the window is currently restored.
180 void UpdateRestoredBounds();
181
182 // Hides the window unconditionally. Used by Hide and HideWithApp.
183 void HideWithoutMarkingHidden();
184
185 apps::AppWindow* app_window_; // weak - AppWindow owns NativeAppWindow.
186
187 bool has_frame_;
188
189 // Whether this window last became hidden due to a request to hide the entire
190 // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
191 bool is_hidden_with_app_;
192
193 bool is_maximized_;
194 bool is_fullscreen_;
195 NSRect restored_bounds_;
196
197 bool is_resizable_;
198 bool shows_resize_controls_;
199 bool shows_fullscreen_controls_;
200
201 apps::SizeConstraints size_constraints_;
202
203 bool has_frame_color_;
204 SkColor active_frame_color_;
205 SkColor inactive_frame_color_;
206
207 base::scoped_nsobject<NativeAppWindowController> window_controller_;
208 NSInteger attention_request_id_; // identifier from requestUserAttention
209
210 // For system drag, the whole window is draggable and the non-draggable areas
211 // have to been explicitly excluded.
212 std::vector<extensions::DraggableRegion> draggable_regions_;
213
214 // The Extension Command Registry used to determine which keyboard events to
215 // handle.
216 scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
217
218 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
219 };
220
221 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
222