Compare commits

...

2 Commits

Author SHA1 Message Date
Gellert Hegyi
ee2cde505a chore: adds docs 2021-10-25 14:13:52 +02:00
Gellert Hegyi
693afef9e8 fix: determines resizing edge based on current cursor on macos 2021-10-25 13:04:24 +02:00
3 changed files with 65 additions and 7 deletions

View File

@@ -535,9 +535,11 @@ Note that this is only emitted when the window is being resized manually. Resizi
The possible values and behaviors of the `edge` option are platform dependent. Possible values are:
* On Windows, possible values are `bottom`, `top`, `left`, `right`, `top-left`, `top-right`, `bottom-left`, `bottom-right`.
* On macOS, possible values are `bottom` and `right`.
* On macOS, possible values are `bottom`, `right`, `bottom-left` and `bottom-right`.
* The value `bottom` is used to denote vertical resizing.
* The value `right` is used to denote horizontal resizing.
* The value `bottom-left` is also used to denote diagonal resizing in the `top-right` case.
* The value `bottom-right` is also used to denote diagonal resizing in the `top-left` case.
#### Event: 'resize'

View File

@@ -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

View File

@@ -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