From 41df037f645f2c0b7d6dc3d6f2c331cfc744e4f8 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 4 Aug 2016 15:34:29 +0900 Subject: [PATCH] Show dummy view under offscreen mode --- atom/browser/osr/osr_web_contents_view.cc | 8 +++ atom/browser/osr/osr_web_contents_view.h | 17 ++++++ atom/browser/osr/osr_web_contents_view_mac.mm | 52 +++++++++++++++++++ filenames.gypi | 1 + 4 files changed, 78 insertions(+) create mode 100644 atom/browser/osr/osr_web_contents_view_mac.mm diff --git a/atom/browser/osr/osr_web_contents_view.cc b/atom/browser/osr/osr_web_contents_view.cc index 25d9f858537..35aa7dcc3a7 100644 --- a/atom/browser/osr/osr_web_contents_view.cc +++ b/atom/browser/osr/osr_web_contents_view.cc @@ -11,9 +11,15 @@ OffScreenWebContentsView::OffScreenWebContentsView( : transparent_(transparent), callback_(callback), web_contents_(nullptr) { +#if defined(OS_MACOSX) + PlatformCreate(); +#endif } OffScreenWebContentsView::~OffScreenWebContentsView() { +#if defined(OS_MACOSX) + PlatformDestroy(); +#endif } void OffScreenWebContentsView::SetWebContents( @@ -21,6 +27,7 @@ void OffScreenWebContentsView::SetWebContents( web_contents_ = web_contents; } +#if !defined(OS_MACOSX) gfx::NativeView OffScreenWebContentsView::GetNativeView() const { return gfx::NativeView(); } @@ -32,6 +39,7 @@ gfx::NativeView OffScreenWebContentsView::GetContentNativeView() const { gfx::NativeWindow OffScreenWebContentsView::GetTopLevelNativeWindow() const { return gfx::NativeWindow(); } +#endif void OffScreenWebContentsView::GetContainerBounds(gfx::Rect* out) const { *out = GetViewBounds(); diff --git a/atom/browser/osr/osr_web_contents_view.h b/atom/browser/osr/osr_web_contents_view.h index 1af3eeb83aa..d47f71b1e90 100644 --- a/atom/browser/osr/osr_web_contents_view.h +++ b/atom/browser/osr/osr_web_contents_view.h @@ -10,6 +10,14 @@ #include "content/browser/web_contents/web_contents_view.h" #include "content/public/browser/web_contents.h" +#if defined(OS_MACOSX) +#ifdef __OBJC__ +@class OffScreenView; +#else +class OffScreenView; +#endif +#endif + namespace atom { class OffScreenWebContentsView : public content::WebContentsView, @@ -61,12 +69,21 @@ class OffScreenWebContentsView : public content::WebContentsView, void UpdateDragCursor(blink::WebDragOperation operation) override; private: +#if defined(OS_MACOSX) + void PlatformCreate(); + void PlatformDestroy(); +#endif + const bool transparent_; OnPaintCallback callback_; // Weak refs. OffScreenRenderWidgetHostView* view_; content::WebContents* web_contents_; + +#if defined(OS_MACOSX) + OffScreenView* offScreenView_; +#endif }; } // namespace atom diff --git a/atom/browser/osr/osr_web_contents_view_mac.mm b/atom/browser/osr/osr_web_contents_view_mac.mm new file mode 100644 index 00000000000..ba99c0cb959 --- /dev/null +++ b/atom/browser/osr/osr_web_contents_view_mac.mm @@ -0,0 +1,52 @@ +// Copyright (c) 2016 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/osr/osr_web_contents_view.h" + +@interface OffScreenView : NSView +@end + +@implementation OffScreenView + +- (void)drawRect:(NSRect)dirtyRect { + NSString* str = @"No content under offscreen mode"; + NSMutableParagraphStyle* paragraphStyle = + [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease]; + [paragraphStyle setAlignment:NSCenterTextAlignment]; + NSDictionary* attributes = [NSDictionary + dictionaryWithObject:paragraphStyle + forKey:NSParagraphStyleAttributeName]; + NSAttributedString* text = + [[[NSAttributedString alloc] initWithString:str + attributes:attributes] autorelease]; + NSRect frame = NSMakeRect(0, (self.frame.size.height - text.size.height) / 2, + self.frame.size.width, text.size.height); + [str drawInRect:frame withAttributes:attributes]; +} + +@end + +namespace atom { + +gfx::NativeView OffScreenWebContentsView::GetNativeView() const { + return offScreenView_; +} + +gfx::NativeView OffScreenWebContentsView::GetContentNativeView() const { + return offScreenView_; +} + +gfx::NativeWindow OffScreenWebContentsView::GetTopLevelNativeWindow() const { + return [offScreenView_ window]; +} + +void OffScreenWebContentsView::PlatformCreate() { + offScreenView_ = [[OffScreenView alloc] init]; +} + +void OffScreenWebContentsView::PlatformDestroy() { + [offScreenView_ release]; +} + +} // namespace atom diff --git a/filenames.gypi b/filenames.gypi index 6d3dd48018b..7335ae9acc0 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -206,6 +206,7 @@ 'atom/browser/native_window_mac.h', 'atom/browser/native_window_mac.mm', 'atom/browser/native_window_observer.h', + 'atom/browser/osr/osr_web_contents_view_mac.mm', 'atom/browser/osr/osr_web_contents_view.cc', 'atom/browser/osr/osr_web_contents_view.h', 'atom/browser/osr/osr_output_device.cc',