From fa09183ed1326238a51e0e7f6370d466276dfc7b Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Mon, 8 Feb 2021 09:14:46 -0800 Subject: [PATCH] fix: BrowserView rendering flicker (#27585) --- package.json | 2 +- shell/browser/native_browser_view_mac.mm | 71 ++++++++++++++++++++---- 2 files changed, 62 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 3fb7019135..9ebaad9268 100644 --- a/package.json +++ b/package.json @@ -135,7 +135,7 @@ ], "docs/api/**/*.md": [ "ts-node script/gen-filenames.ts", - "markdownlint --config .markdownlint.auotfix.json --fix", + "markdownlint --config .markdownlint.autofix.json --fix", "git add filenames.auto.gni" ], "{*.patch,.patches}": [ diff --git a/shell/browser/native_browser_view_mac.mm b/shell/browser/native_browser_view_mac.mm index 74d2c67925..a7cfca4f3b 100644 --- a/shell/browser/native_browser_view_mac.mm +++ b/shell/browser/native_browser_view_mac.mm @@ -4,6 +4,7 @@ #include "shell/browser/native_browser_view_mac.h" +#import #include #include "shell/browser/ui/drag_util.h" @@ -30,6 +31,33 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask = @synthesize initialLocation; ++ (void)load { + if (getenv("ELECTRON_DEBUG_DRAG_REGIONS")) { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + SEL originalSelector = @selector(drawRect:); + SEL swizzledSelector = @selector(drawDebugRect:); + + Method originalMethod = + class_getInstanceMethod([self class], originalSelector); + Method swizzledMethod = + class_getInstanceMethod([self class], swizzledSelector); + BOOL didAddMethod = + class_addMethod([self class], originalSelector, + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod)); + + if (didAddMethod) { + class_replaceMethod([self class], swizzledSelector, + method_getImplementation(originalMethod), + method_getTypeEncoding(originalMethod)); + } else { + method_exchangeImplementations(originalMethod, swizzledMethod); + } + }); + } +} + - (BOOL)mouseDownCanMoveWindow { return NO; } @@ -134,11 +162,9 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask = } // For debugging purposes only. -- (void)drawRect:(NSRect)aRect { - if (getenv("ELECTRON_DEBUG_DRAG_REGIONS")) { - [[[NSColor greenColor] colorWithAlphaComponent:0.5] set]; - NSRectFill([self bounds]); - } +- (void)drawDebugRect:(NSRect)aRect { + [[[NSColor greenColor] colorWithAlphaComponent:0.5] set]; + NSRectFill([self bounds]); } @end @@ -148,16 +174,41 @@ const NSAutoresizingMaskOptions kDefaultAutoResizingMask = @implementation ExcludeDragRegionView ++ (void)load { + if (getenv("ELECTRON_DEBUG_DRAG_REGIONS")) { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + SEL originalSelector = @selector(drawRect:); + SEL swizzledSelector = @selector(drawDebugRect:); + + Method originalMethod = + class_getInstanceMethod([self class], originalSelector); + Method swizzledMethod = + class_getInstanceMethod([self class], swizzledSelector); + BOOL didAddMethod = + class_addMethod([self class], originalSelector, + method_getImplementation(swizzledMethod), + method_getTypeEncoding(swizzledMethod)); + + if (didAddMethod) { + class_replaceMethod([self class], swizzledSelector, + method_getImplementation(originalMethod), + method_getTypeEncoding(originalMethod)); + } else { + method_exchangeImplementations(originalMethod, swizzledMethod); + } + }); + } +} + - (BOOL)mouseDownCanMoveWindow { return NO; } // For debugging purposes only. -- (void)drawRect:(NSRect)aRect { - if (getenv("ELECTRON_DEBUG_DRAG_REGIONS")) { - [[[NSColor redColor] colorWithAlphaComponent:0.5] set]; - NSRectFill([self bounds]); - } +- (void)drawDebugRect:(NSRect)aRect { + [[[NSColor redColor] colorWithAlphaComponent:0.5] set]; + NSRectFill([self bounds]); } @end