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 "base/mac/scoped_nsobject.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "extensions/browser/app_window/app_window.h"
15 #include "extensions/browser/app_window/native_app_window.h"
16 #include "extensions/browser/app_window/size_constraints.h"
17 #include "extensions/common/draggable_region.h"
18 #include "ui/base/accelerators/accelerator_manager.h"
19 #include "ui/gfx/geometry/rect.h"
22 class ExtensionKeybindingRegistryCocoa
;
23 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
<NSWindowDelegate
> {
30 NativeAppWindowCocoa
* appWindow_
; // Weak; owns self.
31 base::scoped_nsobject
<NSView
> titlebar_background_view_
;
34 @
property(assign
, nonatomic
) NativeAppWindowCocoa
* appWindow
;
36 // NativeAppWindowController will retain this view and call
37 // -[NSView setNeedsDisplay:YES] when the window changes main status. This is
38 // necessary because it does not always happen. See http://crbug.com/508722.
39 - (void)setTitlebarBackgroundView
:(NSView
*)view
;
41 // Consults the Command Registry to see if this |event| needs to be handled as
42 // an extension command and returns YES if so (NO otherwise).
43 // Only extensions with the given |priority| are considered.
44 - (BOOL
)handledByExtensionCommand
:(NSEvent
*)event
45 priority
:(ui::AcceleratorManager::HandlerPriority
)priority
;
49 // Cocoa bridge to AppWindow.
50 class NativeAppWindowCocoa
: public extensions::NativeAppWindow
,
51 public content::WebContentsObserver
{
53 NativeAppWindowCocoa(extensions::AppWindow
* app_window
,
54 const extensions::AppWindow::CreateParams
& params
);
56 // ui::BaseWindow implementation.
57 bool IsActive() const override
;
58 bool IsMaximized() const override
;
59 bool IsMinimized() const override
;
60 bool IsFullscreen() const override
;
61 gfx::NativeWindow
GetNativeWindow() const override
;
62 gfx::Rect
GetRestoredBounds() const override
;
63 ui::WindowShowState
GetRestoredState() const override
;
64 gfx::Rect
GetBounds() const override
;
66 void ShowInactive() override
;
68 void Close() override
;
69 void Activate() override
;
70 void Deactivate() override
;
71 void Maximize() override
;
72 void Minimize() override
;
73 void Restore() override
;
74 void SetBounds(const gfx::Rect
& bounds
) override
;
75 void FlashFrame(bool flash
) override
;
76 bool IsAlwaysOnTop() const override
;
78 // Called when the window is about to be closed.
79 void WindowWillClose();
81 // Called when the window is focused.
82 void WindowDidBecomeKey();
84 // Called when the window is defocused.
85 void WindowDidResignKey();
87 // Called when the window finishes resizing, i.e. after zoom/unzoom, after
88 // entering/leaving fullscreen, and after a user is done resizing.
89 void WindowDidFinishResize();
91 // Called when the window is resized. This is called repeatedly during a
92 // zoom/unzoom, and while a user is resizing.
93 void WindowDidResize();
95 // Called when the window is moved.
98 // Called when the window is minimized.
99 void WindowDidMiniaturize();
101 // Called when the window is un-minimized.
102 void WindowDidDeminiaturize();
104 // Called when the window is zoomed (maximized or de-maximized).
105 void WindowWillZoom();
107 // Called when the window enters fullscreen.
108 void WindowDidEnterFullscreen();
110 // Called when the window exits fullscreen.
111 void WindowDidExitFullscreen();
113 // Called to handle a key event.
114 bool HandledByExtensionCommand(
116 ui::AcceleratorManager::HandlerPriority priority
);
118 // Returns true if |point| in local Cocoa coordinate system falls within
119 // the draggable region.
120 bool IsWithinDraggableRegion(NSPoint point
) const;
122 NSRect
restored_bounds() const { return restored_bounds_
; }
125 // NativeAppWindow implementation.
126 void SetFullscreen(int fullscreen_types
) override
;
127 bool IsFullscreenOrPending() const override
;
128 void UpdateWindowIcon() override
;
129 void UpdateWindowTitle() override
;
130 void UpdateShape(scoped_ptr
<SkRegion
> region
) override
;
131 void UpdateDraggableRegions(
132 const std::vector
<extensions::DraggableRegion
>& regions
) override
;
133 SkRegion
* GetDraggableRegion() override
;
134 void HandleKeyboardEvent(
135 const content::NativeWebKeyboardEvent
& event
) override
;
136 bool IsFrameless() const override
;
137 bool HasFrameColor() const override
;
138 SkColor
ActiveFrameColor() const override
;
139 SkColor
InactiveFrameColor() const override
;
140 gfx::Insets
GetFrameInsets() const override
;
141 bool CanHaveAlphaEnabled() const override
;
142 void SetInterceptAllKeys(bool want_all_keys
) override
;
144 // These are used to simulate Mac-style hide/show. Since windows can be hidden
145 // and shown using the app.window API, this sets is_hidden_with_app_ to
146 // differentiate the reason a window was hidden.
147 void ShowWithApp() override
;
148 void HideWithApp() override
;
149 gfx::Size
GetContentMinimumSize() const override
;
150 gfx::Size
GetContentMaximumSize() const override
;
151 void SetContentSizeConstraints(const gfx::Size
& min_size
,
152 const gfx::Size
& max_size
) override
;
153 void SetVisibleOnAllWorkspaces(bool always_visible
) override
;
155 // WebContentsObserver implementation.
156 void RenderViewCreated(content::RenderViewHost
* rvh
) override
;
158 void SetAlwaysOnTop(bool always_on_top
) override
;
160 // WebContentsModalDialogHost implementation.
161 gfx::NativeView
GetHostView() const override
;
162 gfx::Point
GetDialogPosition(const gfx::Size
& size
) override
;
163 gfx::Size
GetMaximumDialogSize() override
;
164 void AddObserver(web_modal::ModalDialogHostObserver
* observer
) override
;
165 void RemoveObserver(web_modal::ModalDialogHostObserver
* observer
) override
;
168 ~NativeAppWindowCocoa() override
;
170 AppNSWindow
* window() const;
171 content::WebContents
* WebContents() const;
173 // Returns the WindowStyleMask based on the type of window frame.
174 // This includes NSResizableWindowMask if the window is resizable.
175 NSUInteger
GetWindowStyleMask() const;
178 void UninstallView();
179 void UpdateDraggableRegionViews();
181 // Cache |restored_bounds_| only if the window is currently restored.
182 void UpdateRestoredBounds();
184 // Hides the window unconditionally. Used by Hide and HideWithApp.
185 void HideWithoutMarkingHidden();
187 extensions::AppWindow
* app_window_
; // weak - AppWindow owns NativeAppWindow.
191 // Whether this window last became hidden due to a request to hide the entire
192 // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
193 bool is_hidden_with_app_
;
197 NSRect restored_bounds_
;
200 bool shows_resize_controls_
;
201 bool shows_fullscreen_controls_
;
203 extensions::SizeConstraints size_constraints_
;
205 bool has_frame_color_
;
206 SkColor active_frame_color_
;
207 SkColor inactive_frame_color_
;
209 base::scoped_nsobject
<NativeAppWindowController
> window_controller_
;
211 // For system drag, the whole window is draggable and the non-draggable areas
212 // have to been explicitly excluded.
213 std::vector
<extensions::DraggableRegion
> draggable_regions_
;
215 // The Extension Command Registry used to determine which keyboard events to
217 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
219 // Tracks the last time the extension asked the window to activate.
220 base::Time last_activate_
;
222 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa
);
225 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_