Browse Source

fix: determines resizing edge based on current cursor on macos

Gellert Hegyi 3 years ago
parent
commit
693afef9e8

+ 3 - 0
shell/browser/ui/cocoa/electron_ns_window_delegate.h

@@ -9,6 +9,7 @@
 
 #include "components/remote_cocoa/app_shim/views_nswindow_delegate.h"
 #include "third_party/abseil-cpp/absl/types/optional.h"
+#include "ui/gfx/geometry/resize_utils.h"
 
 namespace electron {
 class NativeWindowMac;
@@ -26,6 +27,8 @@ class NativeWindowMac;
   // Used to keep track of whether a resize is happening horizontally or
   // vertically, even if physically the user is resizing in both directions.
   absl::optional<bool> resizingHorizontally_;
+
+  absl::optional<gfx::ResizeEdge> resizeDirection_;
 }
 - (id)initWithShell:(electron::NativeWindowMac*)shell;
 @end

+ 59 - 6
shell/browser/ui/cocoa/electron_ns_window_delegate.mm

@@ -12,7 +12,6 @@
 #include "shell/browser/native_window_mac.h"
 #include "shell/browser/ui/cocoa/electron_preview_item.h"
 #include "shell/browser/ui/cocoa/electron_touch_bar.h"
-#include "ui/gfx/geometry/resize_utils.h"
 #include "ui/gfx/mac/coordinate_conversion.h"
 #include "ui/views/cocoa/native_widget_mac_ns_window_host.h"
 #include "ui/views/widget/native_widget_mac.h"
@@ -147,22 +146,46 @@ using FullScreenTransitionState =
                extraHeightPlusFrame);
   }
 
-  if (!resizingHorizontally_) {
+  gfx::ResizeEdge resizeEdge = gfx::ResizeEdge::kBottom;
+
+  if (!resizeDirection_) {
+    NSString* resizeDirection = [self _getResizeDirectionBasedOnCursor];
+    if (!resizeDirection_) {
+      if ([resizeDirection isEqualToString:@"eastWest"]) {
+        resizeDirection_ = gfx::ResizeEdge::kRight;
+      } else if ([resizeDirection isEqualToString:@"northSouth"]) {
+        resizeDirection_ = gfx::ResizeEdge::kBottom;
+      } else if ([resizeDirection isEqualToString:@"northWestSouthEast"]) {
+        resizeDirection_ = gfx::ResizeEdge::kBottomRight;
+      } else if ([resizeDirection isEqualToString:@"northEastSouthWest"]) {
+        resizeDirection_ = gfx::ResizeEdge::kBottomLeft;
+      }
+    }
+  }
+
+  // If resize direction cannot be determined based on current cursor, fallback
+  // to determine it based on how the window size changed.
+  if (!resizingHorizontally_ && !resizeDirection_) {
     NSWindow* window = shell_->GetNativeWindow().GetNativeNSWindow();
     const auto widthDelta = frameSize.width - [window frame].size.width;
     const auto heightDelta = frameSize.height - [window frame].size.height;
     resizingHorizontally_ = std::abs(widthDelta) > std::abs(heightDelta);
   }
 
+  if (resizeDirection_) {
+    resizeEdge = *resizeDirection_;
+  } else {
+    if (*resizingHorizontally_) {
+      resizeEdge = gfx::ResizeEdge::kRight;
+    }
+  }
+
   {
     bool prevent_default = false;
     NSRect new_bounds = NSMakeRect(sender.frame.origin.x, sender.frame.origin.y,
                                    newSize.width, newSize.height);
     shell_->NotifyWindowWillResize(gfx::ScreenRectFromNSRect(new_bounds),
-                                   *resizingHorizontally_
-                                       ? gfx::ResizeEdge::kRight
-                                       : gfx::ResizeEdge::kBottom,
-                                   &prevent_default);
+                                   resizeEdge, &prevent_default);
     if (prevent_default) {
       return sender.frame.size;
     }
@@ -223,6 +246,7 @@ using FullScreenTransitionState =
 
 - (void)windowDidEndLiveResize:(NSNotification*)notification {
   resizingHorizontally_.reset();
+  resizeDirection_.reset();
   shell_->NotifyWindowResized();
   if (is_zooming_) {
     if (shell_->IsMaximized())
@@ -342,4 +366,33 @@ using FullScreenTransitionState =
   return shell_->preview_item();
 }
 
+- (NSString*)_getResizeDirectionBasedOnCursor {
+  NSDictionary* resizeSelectorNames = @{
+    @"eastWest" : @"_windowResizeEastWestCursor",
+    @"northSouth" : @"_windowResizeNorthSouthCursor",
+    @"northWestSouthEast" : @"_windowResizeNorthWestSouthEastCursor",
+    @"northEastSouthWest" : @"_windowResizeNorthEastSouthWestCursor"
+  };
+
+  NSCursor* currentCursor = [NSCursor currentSystemCursor];
+  if (currentCursor) {
+    NSData* currentCursorImage = [[currentCursor image] TIFFRepresentation];
+
+    for (NSString* resizeDirection in resizeSelectorNames) {
+      NSString* selectorName = resizeSelectorNames[resizeDirection];
+      SEL resizeSelector = NSSelectorFromString(selectorName);
+
+      if ([NSCursor respondsToSelector:resizeSelector]) {
+        NSCursor* resizeCursor = [NSCursor performSelector:resizeSelector];
+        NSData* resizeCursorImage = [[resizeCursor image] TIFFRepresentation];
+        if ([currentCursorImage isEqualToData:resizeCursorImage]) {
+          return resizeDirection;
+        }
+      }
+    }
+  }
+
+  return @"unknown";
+}
+
 @end