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.
5 #ifndef CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
6 #define CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
8 #import <Cocoa/Cocoa.h>
11 #include "apps/shell_window.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"
20 class ExtensionKeybindingRegistryCocoa
;
22 class NativeAppWindowCocoa
;
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
30 BrowserCommandExecutor
> {
32 NativeAppWindowCocoa
* appWindow_
; // Weak; owns self.
35 @
property(assign
, nonatomic
) NativeAppWindowCocoa
* appWindow
;
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
;
43 // Cocoa bridge to AppWindow.
44 class NativeAppWindowCocoa
: public apps::NativeAppWindow
,
45 public content::WebContentsObserver
{
47 NativeAppWindowCocoa(apps::ShellWindow
* shell_window
,
48 const apps::ShellWindow::CreateParams
& params
);
50 // ui::BaseWindow implementation.
51 virtual bool IsActive() const OVERRIDE
;
52 virtual bool IsMaximized() const OVERRIDE
;
53 virtual bool IsMinimized() const OVERRIDE
;
54 virtual bool IsFullscreen() const OVERRIDE
;
55 virtual gfx::NativeWindow
GetNativeWindow() OVERRIDE
;
56 virtual gfx::Rect
GetRestoredBounds() const OVERRIDE
;
57 virtual ui::WindowShowState
GetRestoredState() const OVERRIDE
;
58 virtual gfx::Rect
GetBounds() const OVERRIDE
;
59 virtual void Show() OVERRIDE
;
60 virtual void ShowInactive() OVERRIDE
;
61 virtual void Hide() OVERRIDE
;
62 virtual void Close() OVERRIDE
;
63 virtual void Activate() OVERRIDE
;
64 virtual void Deactivate() OVERRIDE
;
65 virtual void Maximize() OVERRIDE
;
66 virtual void Minimize() OVERRIDE
;
67 virtual void Restore() OVERRIDE
;
68 virtual void SetBounds(const gfx::Rect
& bounds
) OVERRIDE
;
69 virtual void FlashFrame(bool flash
) OVERRIDE
;
70 virtual bool IsAlwaysOnTop() const OVERRIDE
;
72 // Called when the window is about to be closed.
73 void WindowWillClose();
75 // Called when the window is focused.
76 void WindowDidBecomeKey();
78 // Called when the window is defocused.
79 void WindowDidResignKey();
81 // Called when the window finishes resizing, i.e. after zoom/unzoom, after
82 // entering/leaving fullscreen, and after a user is done resizing.
83 void WindowDidFinishResize();
85 // Called when the window is resized. This is called repeatedly during a
86 // zoom/unzoom, and while a user is resizing.
87 void WindowDidResize();
89 // Called when the window is moved.
92 // Called when the window is minimized.
93 void WindowDidMiniaturize();
95 // Called when the window is un-minimized.
96 void WindowDidDeminiaturize();
98 // Called when the window is zoomed (maximized or de-maximized).
99 void WindowWillZoom();
101 // Called to handle a key event.
102 bool HandledByExtensionCommand(NSEvent
* event
);
104 // Called to handle a mouse event.
105 void HandleMouseEvent(NSEvent
* event
);
107 // Returns true if |point| in local Cocoa coordinate system falls within
108 // the draggable region.
109 bool IsWithinDraggableRegion(NSPoint point
) const;
111 NSRect
restored_bounds() const { return restored_bounds_
; }
112 bool use_system_drag() const { return use_system_drag_
; }
115 // NativeAppWindow implementation.
116 virtual void SetFullscreen(int fullscreen_types
) OVERRIDE
;
117 virtual bool IsFullscreenOrPending() const OVERRIDE
;
118 virtual bool IsDetached() const OVERRIDE
;
119 virtual void UpdateWindowIcon() OVERRIDE
;
120 virtual void UpdateWindowTitle() OVERRIDE
;
121 virtual void UpdateShape(scoped_ptr
<SkRegion
> region
) OVERRIDE
;
122 virtual void UpdateDraggableRegions(
123 const std::vector
<extensions::DraggableRegion
>& regions
) OVERRIDE
;
124 virtual SkRegion
* GetDraggableRegion() OVERRIDE
;
125 virtual void HandleKeyboardEvent(
126 const content::NativeWebKeyboardEvent
& event
) OVERRIDE
;
127 virtual bool IsFrameless() const OVERRIDE
;
128 virtual gfx::Insets
GetFrameInsets() const OVERRIDE
;
130 // These are used to simulate Mac-style hide/show. Since windows can be hidden
131 // and shown using the app.window API, this sets is_hidden_with_app_ to
132 // differentiate the reason a window was hidden.
133 virtual void ShowWithApp() OVERRIDE
;
134 virtual void HideWithApp() OVERRIDE
;
135 // Calls setContent[Min|Max]Size with the current size constraints.
136 virtual void UpdateWindowMinMaxSize() OVERRIDE
;
138 // WebContentsObserver implementation.
139 virtual void RenderViewCreated(content::RenderViewHost
* rvh
) OVERRIDE
;
141 virtual void SetAlwaysOnTop(bool always_on_top
) OVERRIDE
;
143 // WebContentsModalDialogHost implementation.
144 virtual gfx::NativeView
GetHostView() const OVERRIDE
;
145 virtual gfx::Point
GetDialogPosition(const gfx::Size
& size
) OVERRIDE
;
146 virtual gfx::Size
GetMaximumDialogSize() OVERRIDE
;
147 virtual void AddObserver(
148 web_modal::ModalDialogHostObserver
* observer
) OVERRIDE
;
149 virtual void RemoveObserver(
150 web_modal::ModalDialogHostObserver
* observer
) OVERRIDE
;
153 virtual ~NativeAppWindowCocoa();
155 ShellNSWindow
* window() const;
157 content::WebContents
* web_contents() const {
158 return shell_window_
->web_contents();
160 const extensions::Extension
* extension() const {
161 return shell_window_
->extension();
164 // Returns the WindowStyleMask based on the type of window frame.
165 // Specifically, this includes NSResizableWindowMask if the window is
166 // resizable, and does not include NSTexturedBackgroundWindowMask when a
167 // native frame is used.
168 NSUInteger
GetWindowStyleMask() const;
171 void UninstallView();
172 void InstallDraggableRegionViews();
173 void UpdateDraggableRegionsForSystemDrag(
174 const std::vector
<extensions::DraggableRegion
>& regions
,
175 const extensions::DraggableRegion
* draggable_area
);
176 void UpdateDraggableRegionsForCustomDrag(
177 const std::vector
<extensions::DraggableRegion
>& regions
);
179 // Cache |restored_bounds_| only if the window is currently restored.
180 void UpdateRestoredBounds();
182 // Hides the window unconditionally. Used by Hide and HideWithApp.
183 void HideWithoutMarkingHidden();
185 apps::ShellWindow
* shell_window_
; // weak - ShellWindow owns NativeAppWindow.
189 // Whether this window is hidden according to the app.window API. This is set
190 // by Hide, Show, and ShowInactive.
192 // Whether this window last became hidden due to a request to hide the entire
193 // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
194 bool is_hidden_with_app_
;
198 NSRect restored_bounds_
;
200 bool shows_resize_controls_
;
201 bool shows_fullscreen_controls_
;
203 base::scoped_nsobject
<NativeAppWindowController
> window_controller_
;
204 NSInteger attention_request_id_
; // identifier from requestUserAttention
206 // Indicates whether system drag or custom drag should be used, depending on
207 // the complexity of draggable regions.
208 bool use_system_drag_
;
210 // For system drag, the whole window is draggable and the non-draggable areas
211 // have to been explicitly excluded.
212 std::vector
<gfx::Rect
> system_drag_exclude_areas_
;
214 // For custom drag, the whole window is non-draggable and the draggable region
215 // has to been explicitly provided.
216 scoped_ptr
<SkRegion
> draggable_region_
; // used in custom drag.
218 // Mouse location since the last mouse event, in screen coordinates. This is
219 // used in custom drag to compute the window movement.
220 NSPoint last_mouse_location_
;
222 // The Extension Command Registry used to determine which keyboard events to
224 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
226 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa
);
229 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_