Browse Source

fix: BrowserView rendering flicker (#27585)

Shelley Vohr 4 years ago
parent
commit
fa09183ed1
2 changed files with 62 additions and 11 deletions
  1. 1 1
      package.json
  2. 61 10
      shell/browser/native_browser_view_mac.mm

+ 1 - 1
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}": [

+ 61 - 10
shell/browser/native_browser_view_mac.mm

@@ -4,6 +4,7 @@
 
 #include "shell/browser/native_browser_view_mac.h"
 
+#import <objc/runtime.h>
 #include <vector>
 
 #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