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/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"
24 class ExtensionKeybindingRegistryCocoa
;
25 class NativeAppWindowCocoa
;
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
33 BrowserCommandExecutor
> {
35 NativeAppWindowCocoa
* appWindow_
; // Weak; owns self.
38 @
property(assign
, nonatomic
) NativeAppWindowCocoa
* appWindow
;
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
;
46 // Cocoa bridge to AppWindow.
47 class NativeAppWindowCocoa
: public apps::NativeAppWindow
,
48 public content::WebContentsObserver
{
50 NativeAppWindowCocoa(apps::AppWindow
* app_window
,
51 const apps::AppWindow::CreateParams
& params
);
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
;
75 // Called when the window is about to be closed.
76 void WindowWillClose();
78 // Called when the window is focused.
79 void WindowDidBecomeKey();
81 // Called when the window is defocused.
82 void WindowDidResignKey();
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();
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();
92 // Called when the window is moved.
95 // Called when the window is minimized.
96 void WindowDidMiniaturize();
98 // Called when the window is un-minimized.
99 void WindowDidDeminiaturize();
101 // Called when the window is zoomed (maximized or de-maximized).
102 void WindowWillZoom();
104 // Called when the window enters fullscreen.
105 void WindowDidEnterFullscreen();
107 // Called when the window exits fullscreen.
108 void WindowDidExitFullscreen();
110 // Called to handle a key event.
111 bool HandledByExtensionCommand(NSEvent
* event
);
113 // Returns true if |point| in local Cocoa coordinate system falls within
114 // the draggable region.
115 bool IsWithinDraggableRegion(NSPoint point
) const;
117 NSRect
restored_bounds() const { return restored_bounds_
; }
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
;
139 // These are used to simulate Mac-style hide/show. Since windows can be hidden
140 // and shown using the app.window API, this sets is_hidden_with_app_ to
141 // differentiate the reason a window was hidden.
142 virtual void ShowWithApp() OVERRIDE
;
143 virtual void HideWithApp() OVERRIDE
;
144 virtual void UpdateShelfMenu() OVERRIDE
;
145 virtual gfx::Size
GetContentMinimumSize() const OVERRIDE
;
146 virtual gfx::Size
GetContentMaximumSize() const OVERRIDE
;
147 virtual void SetContentSizeConstraints(const gfx::Size
& min_size
,
148 const gfx::Size
& max_size
) OVERRIDE
;
150 // WebContentsObserver implementation.
151 virtual void RenderViewCreated(content::RenderViewHost
* rvh
) OVERRIDE
;
153 virtual void SetAlwaysOnTop(bool always_on_top
) OVERRIDE
;
155 // WebContentsModalDialogHost implementation.
156 virtual gfx::NativeView
GetHostView() const OVERRIDE
;
157 virtual gfx::Point
GetDialogPosition(const gfx::Size
& size
) OVERRIDE
;
158 virtual gfx::Size
GetMaximumDialogSize() OVERRIDE
;
159 virtual void AddObserver(
160 web_modal::ModalDialogHostObserver
* observer
) OVERRIDE
;
161 virtual void RemoveObserver(
162 web_modal::ModalDialogHostObserver
* observer
) OVERRIDE
;
165 virtual ~NativeAppWindowCocoa();
167 ShellNSWindow
* window() const;
168 content::WebContents
* WebContents() const;
170 // Returns the WindowStyleMask based on the type of window frame.
171 // Specifically, this includes NSResizableWindowMask if the window is
172 // resizable, and does not include NSTexturedBackgroundWindowMask when a
173 // native frame is used.
174 NSUInteger
GetWindowStyleMask() const;
177 void UninstallView();
178 void UpdateDraggableRegionViews();
180 // Cache |restored_bounds_| only if the window is currently restored.
181 void UpdateRestoredBounds();
183 // Hides the window unconditionally. Used by Hide and HideWithApp.
184 void HideWithoutMarkingHidden();
186 apps::AppWindow
* app_window_
; // weak - AppWindow owns NativeAppWindow.
190 // Whether this window last became hidden due to a request to hide the entire
191 // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
192 bool is_hidden_with_app_
;
196 NSRect restored_bounds_
;
199 bool shows_resize_controls_
;
200 bool shows_fullscreen_controls_
;
202 apps::SizeConstraints size_constraints_
;
204 base::scoped_nsobject
<NativeAppWindowController
> window_controller_
;
205 NSInteger attention_request_id_
; // identifier from requestUserAttention
207 // For system drag, the whole window is draggable and the non-draggable areas
208 // have to been explicitly excluded.
209 std::vector
<extensions::DraggableRegion
> draggable_regions_
;
211 // The Extension Command Registry used to determine which keyboard events to
213 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
215 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa
);
218 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_