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"
22 class ExtensionKeybindingRegistryCocoa
;
23 class NativeAppWindowCocoa
;
26 @
class TitlebarBackgroundView
;
28 // A window controller for a minimal window to host a web app view. Passes
29 // Objective-C notifications to the C++ bridge.
30 @interface NativeAppWindowController
: NSWindowController
32 BrowserCommandExecutor
> {
34 NativeAppWindowCocoa
* appWindow_
; // Weak; owns self.
37 @
property(assign
, nonatomic
) NativeAppWindowCocoa
* appWindow
;
39 // Consults the Command Registry to see if this |event| needs to be handled as
40 // an extension command and returns YES if so (NO otherwise).
41 // Only extensions with the given |priority| are considered.
42 - (BOOL
)handledByExtensionCommand
:(NSEvent
*)event
43 priority
:(ui::AcceleratorManager::HandlerPriority
)priority
;
47 // Cocoa bridge to AppWindow.
48 class NativeAppWindowCocoa
: public extensions::NativeAppWindow
,
49 public content::WebContentsObserver
{
51 NativeAppWindowCocoa(extensions::AppWindow
* app_window
,
52 const extensions::AppWindow::CreateParams
& params
);
54 // ui::BaseWindow implementation.
55 bool IsActive() const override
;
56 bool IsMaximized() const override
;
57 bool IsMinimized() const override
;
58 bool IsFullscreen() const override
;
59 gfx::NativeWindow
GetNativeWindow() const override
;
60 gfx::Rect
GetRestoredBounds() const override
;
61 ui::WindowShowState
GetRestoredState() const override
;
62 gfx::Rect
GetBounds() const override
;
64 void ShowInactive() override
;
66 void Close() override
;
67 void Activate() override
;
68 void Deactivate() override
;
69 void Maximize() override
;
70 void Minimize() override
;
71 void Restore() override
;
72 void SetBounds(const gfx::Rect
& bounds
) override
;
73 void FlashFrame(bool flash
) override
;
74 bool IsAlwaysOnTop() const override
;
76 // Called when the window is about to be closed.
77 void WindowWillClose();
79 // Called when the window is focused.
80 void WindowDidBecomeKey();
82 // Called when the window is defocused.
83 void WindowDidResignKey();
85 // Called when the window finishes resizing, i.e. after zoom/unzoom, after
86 // entering/leaving fullscreen, and after a user is done resizing.
87 void WindowDidFinishResize();
89 // Called when the window is resized. This is called repeatedly during a
90 // zoom/unzoom, and while a user is resizing.
91 void WindowDidResize();
93 // Called when the window is moved.
96 // Called when the window is minimized.
97 void WindowDidMiniaturize();
99 // Called when the window is un-minimized.
100 void WindowDidDeminiaturize();
102 // Called when the window is zoomed (maximized or de-maximized).
103 void WindowWillZoom();
105 // Called when the window enters fullscreen.
106 void WindowDidEnterFullscreen();
108 // Called when the window exits fullscreen.
109 void WindowDidExitFullscreen();
111 // Called to handle a key event.
112 bool HandledByExtensionCommand(
114 ui::AcceleratorManager::HandlerPriority priority
);
116 // Returns true if |point| in local Cocoa coordinate system falls within
117 // the draggable region.
118 bool IsWithinDraggableRegion(NSPoint point
) const;
120 NSRect
restored_bounds() const { return restored_bounds_
; }
123 // NativeAppWindow implementation.
124 void SetFullscreen(int fullscreen_types
) override
;
125 bool IsFullscreenOrPending() const override
;
126 void UpdateWindowIcon() override
;
127 void UpdateWindowTitle() override
;
128 void UpdateBadgeIcon() override
;
129 void UpdateShape(scoped_ptr
<SkRegion
> region
) override
;
130 void UpdateDraggableRegions(
131 const std::vector
<extensions::DraggableRegion
>& regions
) override
;
132 SkRegion
* GetDraggableRegion() override
;
133 void HandleKeyboardEvent(
134 const content::NativeWebKeyboardEvent
& event
) override
;
135 bool IsFrameless() const override
;
136 bool HasFrameColor() const override
;
137 SkColor
ActiveFrameColor() const override
;
138 SkColor
InactiveFrameColor() const override
;
139 gfx::Insets
GetFrameInsets() const override
;
140 bool CanHaveAlphaEnabled() const override
;
141 void SetInterceptAllKeys(bool want_all_keys
) override
;
143 // These are used to simulate Mac-style hide/show. Since windows can be hidden
144 // and shown using the app.window API, this sets is_hidden_with_app_ to
145 // differentiate the reason a window was hidden.
146 void ShowWithApp() override
;
147 void HideWithApp() override
;
148 void UpdateShelfMenu() 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 ShellNSWindow
* 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_
;
210 base::scoped_nsobject
<TitlebarBackgroundView
> titlebar_background_view_
;
212 // For system drag, the whole window is draggable and the non-draggable areas
213 // have to been explicitly excluded.
214 std::vector
<extensions::DraggableRegion
> draggable_regions_
;
216 // The Extension Command Registry used to determine which keyboard events to
218 scoped_ptr
<ExtensionKeybindingRegistryCocoa
> extension_keybinding_registry_
;
220 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa
);
223 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_