Home | History | Annotate | Download | only in apps
      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.
      4 
      5 #ifndef CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
      6 #define CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
      7 
      8 #import <Cocoa/Cocoa.h>
      9 #include <vector>
     10 
     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/rect.h"
     21 
     22 class ExtensionKeybindingRegistryCocoa;
     23 class NativeAppWindowCocoa;
     24 @class ShellNSWindow;
     25 class SkRegion;
     26 
     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
     30                                       <NSWindowDelegate,
     31                                        BrowserCommandExecutor> {
     32  @private
     33   NativeAppWindowCocoa* appWindow_;  // Weak; owns self.
     34 }
     35 
     36 @property(assign, nonatomic) NativeAppWindowCocoa* appWindow;
     37 
     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;
     43 
     44 @end
     45 
     46 // Cocoa bridge to AppWindow.
     47 class NativeAppWindowCocoa : public extensions::NativeAppWindow,
     48                              public content::WebContentsObserver {
     49  public:
     50   NativeAppWindowCocoa(extensions::AppWindow* app_window,
     51                        const extensions::AppWindow::CreateParams& params);
     52 
     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;
     74 
     75   // Called when the window is about to be closed.
     76   void WindowWillClose();
     77 
     78   // Called when the window is focused.
     79   void WindowDidBecomeKey();
     80 
     81   // Called when the window is defocused.
     82   void WindowDidResignKey();
     83 
     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();
     87 
     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();
     91 
     92   // Called when the window is moved.
     93   void WindowDidMove();
     94 
     95   // Called when the window is minimized.
     96   void WindowDidMiniaturize();
     97 
     98   // Called when the window is un-minimized.
     99   void WindowDidDeminiaturize();
    100 
    101   // Called when the window is zoomed (maximized or de-maximized).
    102   void WindowWillZoom();
    103 
    104   // Called when the window enters fullscreen.
    105   void WindowDidEnterFullscreen();
    106 
    107   // Called when the window exits fullscreen.
    108   void WindowDidExitFullscreen();
    109 
    110   // Called to handle a key event.
    111   bool HandledByExtensionCommand(
    112       NSEvent* event,
    113       ui::AcceleratorManager::HandlerPriority priority);
    114 
    115   // Returns true if |point| in local Cocoa coordinate system falls within
    116   // the draggable region.
    117   bool IsWithinDraggableRegion(NSPoint point) const;
    118 
    119   NSRect restored_bounds() const { return restored_bounds_; }
    120 
    121  protected:
    122   // NativeAppWindow implementation.
    123   virtual void SetFullscreen(int fullscreen_types) OVERRIDE;
    124   virtual bool IsFullscreenOrPending() const OVERRIDE;
    125   virtual void UpdateWindowIcon() OVERRIDE;
    126   virtual void UpdateWindowTitle() OVERRIDE;
    127   virtual void UpdateBadgeIcon() OVERRIDE;
    128   virtual void UpdateShape(scoped_ptr<SkRegion> region) OVERRIDE;
    129   virtual void UpdateDraggableRegions(
    130       const std::vector<extensions::DraggableRegion>& regions) OVERRIDE;
    131   virtual SkRegion* GetDraggableRegion() OVERRIDE;
    132   virtual void HandleKeyboardEvent(
    133       const content::NativeWebKeyboardEvent& event) OVERRIDE;
    134   virtual bool IsFrameless() const OVERRIDE;
    135   virtual bool HasFrameColor() const OVERRIDE;
    136   virtual SkColor ActiveFrameColor() const OVERRIDE;
    137   virtual SkColor InactiveFrameColor() const OVERRIDE;
    138   virtual gfx::Insets GetFrameInsets() const OVERRIDE;
    139   virtual bool CanHaveAlphaEnabled() const OVERRIDE;
    140 
    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   virtual void ShowWithApp() OVERRIDE;
    145   virtual void HideWithApp() OVERRIDE;
    146   virtual void UpdateShelfMenu() OVERRIDE;
    147   virtual gfx::Size GetContentMinimumSize() const OVERRIDE;
    148   virtual gfx::Size GetContentMaximumSize() const OVERRIDE;
    149   virtual void SetContentSizeConstraints(const gfx::Size& min_size,
    150                                          const gfx::Size& max_size) OVERRIDE;
    151   virtual void SetVisibleOnAllWorkspaces(bool always_visible) OVERRIDE;
    152 
    153   // WebContentsObserver implementation.
    154   virtual void RenderViewCreated(content::RenderViewHost* rvh) OVERRIDE;
    155 
    156   virtual void SetAlwaysOnTop(bool always_on_top) OVERRIDE;
    157 
    158   // WebContentsModalDialogHost implementation.
    159   virtual gfx::NativeView GetHostView() const OVERRIDE;
    160   virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
    161   virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
    162   virtual void AddObserver(
    163       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
    164   virtual void RemoveObserver(
    165       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
    166 
    167  private:
    168   virtual ~NativeAppWindowCocoa();
    169 
    170   ShellNSWindow* window() const;
    171   content::WebContents* WebContents() const;
    172 
    173   // Returns the WindowStyleMask based on the type of window frame.
    174   // This includes NSResizableWindowMask if the window is resizable.
    175   NSUInteger GetWindowStyleMask() const;
    176 
    177   void InstallView();
    178   void UninstallView();
    179   void UpdateDraggableRegionViews();
    180 
    181   // Cache |restored_bounds_| only if the window is currently restored.
    182   void UpdateRestoredBounds();
    183 
    184   // Hides the window unconditionally. Used by Hide and HideWithApp.
    185   void HideWithoutMarkingHidden();
    186 
    187   extensions::AppWindow* app_window_;  // weak - AppWindow owns NativeAppWindow.
    188 
    189   bool has_frame_;
    190 
    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_;
    194 
    195   bool is_maximized_;
    196   bool is_fullscreen_;
    197   NSRect restored_bounds_;
    198 
    199   bool is_resizable_;
    200   bool shows_resize_controls_;
    201   bool shows_fullscreen_controls_;
    202 
    203   extensions::SizeConstraints size_constraints_;
    204 
    205   bool has_frame_color_;
    206   SkColor active_frame_color_;
    207   SkColor inactive_frame_color_;
    208 
    209   base::scoped_nsobject<NativeAppWindowController> window_controller_;
    210 
    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_;
    214 
    215   // The Extension Command Registry used to determine which keyboard events to
    216   // handle.
    217   scoped_ptr<ExtensionKeybindingRegistryCocoa> extension_keybinding_registry_;
    218 
    219   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowCocoa);
    220 };
    221 
    222 #endif  // CHROME_BROWSER_UI_COCOA_APPS_NATIVE_APP_WINDOW_COCOA_H_
    223