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(bool fullscreen) 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 UpdateInputRegion(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 RenderViewHostChanged(
140 content::RenderViewHost* old_host,
141 content::RenderViewHost* new_host) OVERRIDE;
143 virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
145 // WebContentsModalDialogHost implementation.
146 virtual gfx::NativeView GetHostView() const OVERRIDE;
147 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
148 virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
149 virtual void AddObserver(
150 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
151 virtual void RemoveObserver(
152 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
155 virtual ~NativeAppWindowCocoa();
157 ShellNSWindow* window() const;
159 content::WebContents* web_contents() const {
160 return shell_window_->web_contents();
162 const extensions::Extension* extension() const {
163 return shell_window_->extension();
166 // Returns the WindowStyleMask based on the type of window frame.
167 // Specifically, this includes NSResizableWindowMask if the window is
168 // resizable, and does not include NSTexturedBackgroundWindowMask when a
169 // native frame is used.
170 NSUInteger GetWindowStyleMask() const;
173 void UninstallView();
174 void InstallDraggableRegionViews();
175 void UpdateDraggableRegionsForSystemDrag(
176 const std::vector<extensions::DraggableRegion>& regions,
177 const extensions::DraggableRegion* draggable_area);
178 void UpdateDraggableRegionsForCustomDrag(
179 const std::vector<extensions::DraggableRegion>& regions);
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 apps::ShellWindow* shell_window_; // weak - ShellWindow owns NativeAppWindow.
191 // Whether this window is hidden according to the app.window API. This is set
192 // by Hide, Show, and ShowInactive.
194 // Whether this window last became hidden due to a request to hide the entire
195 // app, e.g. via the dock menu or Cmd+H. This is set by Hide/ShowWithApp.
196 bool is_hidden_with_app_;
200 NSRect restored_bounds_;
202 bool shows_resize_controls_;
203 bool shows_fullscreen_controls_;
205 base::scoped_nsobject<NativeAppWindowController> window_controller_;
206 NSInteger attention_request_id_; // identifier from requestUserAttention
208 // Indicates whether system drag or custom drag should be used, depending on
209 // the complexity of draggable regions.
210 bool use_system_drag_;
212 // For system drag, the whole window is draggable and the non-draggable areas
213 // have to been explicitly excluded.
214 std::vector<gfx::Rect> system_drag_exclude_areas_;
216 // For custom drag, the whole window is non-draggable and the draggable region
217 // has to been explicitly provided.
218 scoped_ptr<SkRegion> draggable_region_; // used in custom drag.
220 // Mouse location since the last mouse event, in screen coordinates. This is
221 // used in custom drag to compute the window movement.
222 NSPoint last_mouse_location_;
224 // The Extension Command Registry used to determine which keyboard events to
226 scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
228 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
231 #endif // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_