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 #import "chrome/browser/ui/cocoa/browser_command_executor.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "extensions/browser/app_window/app_window.h"
16 #include "extensions/browser/app_window/native_app_window.h"
17 #include "extensions/browser/app_window/size_constraints.h"
18 #include "extensions/common/draggable_region.h"
19 #include "ui/base/accelerators/accelerator_manager.h"
20 #include "ui/gfx/geometry/rect.h"
23 class ExtensionKeybindingRegistryCocoa
;
24 class NativeAppWindowCocoa
;
27 // A window controller for a minimal window to host a web app view. Passes
28 // Objective-C notifications to the C++ bridge.
29 @interface NativeAppWindowController
: NSWindowController
31 BrowserCommandExecutor
> {
33 NativeAppWindowCocoa
* appWindow_
; // Weak; owns self.
36 @
property(assign
, nonatomic
) NativeAppWindowCocoa
* appWindow
;
38 // Consults the Command Registry to see if this |event| needs to be handled as
39 // an extension command and returns YES if so (NO otherwise).
40 // Only extensions with the given |priority| are considered.
41 - (BOOL
)handledByExtensionCommand
:(NSEvent
*)event
42 priority
:(ui::AcceleratorManager::HandlerPriority
)priority
;
46 // Cocoa bridge to AppWindow.
47 class NativeAppWindowCocoa
: public extensions::NativeAppWindow
,
48 public content::WebContentsObserver
{
50 NativeAppWindowCocoa(extensions::AppWindow
* app_window
,
51 const extensions::AppWindow::CreateParams
& params
);
53 // ui::BaseWindow implementation.
54 bool IsActive() const override
;
55 bool IsMaximized() const override
;
56 bool IsMinimized() const override
;
57 bool IsFullscreen() const override
;
58 gfx::NativeWindow
GetNativeWindow() const override
;
59 gfx::Rect
GetRestoredBounds() const override
;
60 ui::WindowShowState
GetRestoredState() const override
;
61 gfx::Rect
GetBounds() const override
;
63 void ShowInactive() override
;
65 void Close() override
;
66 void Activate() override
;
67 void Deactivate() override
;
68 void Maximize() override
;
69 void Minimize() override
;
70 void Restore() override
;
71 void SetBounds(const gfx::Rect
& bounds
) override
;
72 void FlashFrame(bool flash
) override
;
73 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(
113 ui::AcceleratorManager::HandlerPriority priority
);
115 // Returns true if |point| in local Cocoa coordinate system falls within
116 // the draggable region.
117 bool IsWithinDraggableRegion(NSPoint point
) const;
119 NSRect
restored_bounds() const { return restored_bounds_
; }
122 // NativeAppWindow implementation.
123 void SetFullscreen(int fullscreen_types
) override
;
124 bool IsFullscreenOrPending() const override
;
125 void UpdateWindowIcon() override
;
126 void UpdateWindowTitle() override
;
127 void UpdateShape(scoped_ptr
<SkRegion
> region
) override
;
128 void UpdateDraggableRegions(
129 const std::vector
<extensions::DraggableRegion
>& regions
) override
;
130 SkRegion
* GetDraggableRegion() override
;
131 void HandleKeyboardEvent(
132 const content::NativeWebKeyboardEvent
& event
) override
;
133 bool IsFrameless() const override
;
134 bool HasFrameColor() const override
;
135 SkColor
ActiveFrameColor() const override
;
136 SkColor
InactiveFrameColor() const override
;
137 gfx::Insets
GetFrameInsets() const override
;
138 bool CanHaveAlphaEnabled() const override
;
139 void SetInterceptAllKeys(bool want_all_keys
) override
;
141 // These are used to simulate Mac-style hide/show. Since windows can be hidden
142 // and shown using the app.window API, this sets is_hidden_with_app_ to
143 // differentiate the reason a window was hidden.
144 void ShowWithApp() override
;
145 void HideWithApp() override
;
146 void UpdateShelfMenu() override
;
147 gfx::Size
GetContentMinimumSize() const override
;
148 gfx::Size
GetContentMaximumSize() const override
;
149 void SetContentSizeConstraints(const gfx::Size
& min_size
,
150 const gfx::Size
& max_size
) override
;
151 void SetVisibleOnAllWorkspaces(bool always_visible
) override
;
153 // WebContentsObserver implementation.
154 void RenderViewCreated(content::RenderViewHost
* rvh
) override
;
156 void SetAlwaysOnTop(bool always_on_top
) override
;
158 // WebContentsModalDialogHost implementation.
159 gfx::NativeView
GetHostView() const override
;
160 gfx::Point
GetDialogPosition(const gfx::Size
& size
) override
;
161 gfx::Size
GetMaximumDialogSize() override
;
162 void AddObserver(web_modal::ModalDialogHostObserver
* observer
) override
;
163 void RemoveObserver(web_modal::ModalDialogHostObserver
* observer
) override
;
166 ~NativeAppWindowCocoa() override
;
168 AppNSWindow
* window() const;
169 content::WebContents
* WebContents() const;
171 // Returns the WindowStyleMask based on the type of window frame.
172 // This includes NSResizableWindowMask if the window is resizable.
173 NSUInteger
GetWindowStyleMask() const;
176 void UninstallView();
177 void UpdateDraggableRegionViews();
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 extensions::AppWindow
* app_window_
; // weak - AppWindow owns NativeAppWindow.
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_
;
195 NSRect restored_bounds_
;
198 bool shows_resize_controls_
;
199 bool shows_fullscreen_controls_
;
201 extensions::SizeConstraints size_constraints_
;
203 bool has_frame_color_
;
204 SkColor active_frame_color_
;
205 SkColor inactive_frame_color_
;
207 base::scoped_nsobject
<NativeAppWindowController
> window_controller_
;
209 // For system drag, the whole window is draggable and the non-draggable areas
210 // have to been explicitly excluded.
211 std::vector
<extensions::DraggableRegion
> draggable_regions_
;
213 // The Extension Command Registry used to determine which keyboard events to
215 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
217 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa
);
220 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_