Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multi screen window show #450

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Easydict/Feature/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ - (void)getSelectedTextByAccessibility:(void (^)(NSString *_Nullable text, AXErr
CGRect selectedTextFrame = [self getSelectedTextFrame];
// NSLog(@"selected text: %@", @(selectedTextFrame));

// record screen
NSArray *screens = NSScreen.screens;
for (NSScreen *screen in screens) {
if (NSMouseInRect(selectedTextFrame.origin, screen.frame, NO)) {
if (EZCoordinateUtils.startQueryScreen == nil) {
EZCoordinateUtils.startQueryScreen = screen;
}
}
}

self.selectedTextFrame = [EZCoordinateUtils convertRectToBottomLeft:selectedTextFrame];

if (getFocusedUIElementError == kAXErrorSuccess) {
Expand Down
27 changes: 26 additions & 1 deletion Easydict/Feature/Utility/AppleScript/EZAppleScriptManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import "EZAppleScriptManager.h"
#import "EZConfiguration.h"
#import "Easydict-Swift.h"

#import "EZCoordinateUtils.h"
@interface EZAppleScriptManager ()

@property (nonatomic, strong) EZScriptExecutor *scriptExecutor;
Expand Down Expand Up @@ -70,6 +70,7 @@ - (BOOL)isChromeKernelBrowser:(NSString *)bundleID {

- (void)getBrowserSelectedText:(NSString *)bundleID completion:(AppleScriptCompletionHandler)completion {
// NSLog(@"get Browser selected text: %@", bundleID);
[self recordBrowerLocaledInScreen:bundleID];

if ([self isSafari:bundleID]) {
[self getSafariSelectedText:completion];
Expand All @@ -80,6 +81,30 @@ - (void)getBrowserSelectedText:(NSString *)bundleID completion:(AppleScriptCompl
}
}

- (void)recordBrowerLocaledInScreen:(NSString *)bundleID {
NSArray *runningApplications = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleID];
if (runningApplications.count > 0) {
CGRect browerFrame = CGRectZero;
NSRunningApplication *brower = runningApplications.firstObject;

NSArray *windows = (__bridge_transfer NSArray *)CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
for (NSDictionary *windowInfo in windows) {
if ([[windowInfo objectForKey:@"kCGWindowOwnerName"] isEqualToString:brower.localizedName]) {
CGRectMakeWithDictionaryRepresentation((__bridge CFDictionaryRef)[windowInfo objectForKey:@"kCGWindowBounds"], &browerFrame);
}
}

for (NSScreen *screen in NSScreen.screens) {
if (!CGRectEqualToRect(browerFrame, CGRectZero) &&
NSIntersectsRect(screen.frame, browerFrame)) {
if (EZCoordinateUtils.startQueryScreen == nil) {
EZCoordinateUtils.startQueryScreen = screen;
}
}
}
}
}

/// Get Safari selected text by AppleScript. Cost ~100ms
- (void)getSafariSelectedText:(AppleScriptCompletionHandler)completion {
NSString *bundleID = @"com.apple.Safari";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
NS_ASSUME_NONNULL_BEGIN

@interface EZCoordinateUtils : NSObject
// record start query screen
@property (nonatomic, strong, class, nullable) NSScreen *startQueryScreen;

/// Get safe point, bottom-left coordinate.
+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen;
Expand Down
16 changes: 14 additions & 2 deletions Easydict/Feature/Utility/EZCoordinateUtils/EZCoordinateUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@
//

#import "EZCoordinateUtils.h"

@implementation EZCoordinateUtils

+ (void)setStartQueryScreen:(NSScreen *)startQueryScreen {
objc_setAssociatedObject(self, @selector(startQueryScreen), startQueryScreen, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (NSScreen *)startQueryScreen {
return objc_getAssociatedObject(self, _cmd);
}


+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen {
CGRect newFrame = CGRectMake(point.x, point.y, frame.size.width, frame.size.height);
return [self getSafeLocation:newFrame inScreen:screen];
Expand All @@ -30,7 +38,11 @@ + (CGPoint)getSafeLocation:(CGRect)frame inScreen:(NSScreen *)screen {
/// Make sure frame show in screen visible frame, return left-bottom postion frame.
+ (CGRect)getSafeAreaFrame:(CGRect)frame inScreen:(nullable NSScreen *)screen {
if (!screen) {
screen = [self screenOfMousePosition];
if ([self startQueryScreen]) {
screen = [self startQueryScreen];
} else {
screen = [self screenOfMousePosition];
}
}
CGRect visibleFrame = screen.visibleFrame;
if (CGRectContainsRect(visibleFrame, frame)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ - (void)startQueryText:(NSString *)text actionType:(EZActionType)actionType {
return;
}

// record current screen when start query
if (EZCoordinateUtils.startQueryScreen == nil) {
NSScreen *current = [EZCoordinateUtils screenForPoint: [NSEvent mouseLocation]];
EZCoordinateUtils.startQueryScreen = current;
}

self.inputText = text;
self.queryModel.actionType = actionType;
self.queryView.isTypingChinese = NO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,9 @@ - (void)closeFloatingWindow {
NSNumber *windowType = @(self.floatingWindowType);
[self.floatingWindowTypeArray removeObject:windowType];
[self.floatingWindowTypeArray insertObject:windowType atIndex:1];

// clear start quert screen
EZCoordinateUtils.startQueryScreen = nil;
}

/// Close floating window, except main window.
Expand Down