Skip to content

Commit

Permalink
fix: show fixed window position former consider screen
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Feb 28, 2025
1 parent 20632dc commit 5c431aa
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
1 change: 1 addition & 0 deletions Easydict/Swift/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class Configuration: NSObject {
@Default(.forceGetSelectedTextType) var forceGetSelectedTextType: ForceGetSelectedTextType

@Default(.enableAppleOfflineTranslation) var enableAppleOfflineTranslation: Bool
@Default(.screenVisibleFrame) var screenVisibleFrame: CGRect

@DefaultsWrapper(.allowCrashLog) var allowCrashLog: Bool
@DefaultsWrapper(.allowAnalytics) var allowAnalytics: Bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ extension Defaults.Keys {
"EZConfiguration_kForceGetSelectedTextTypeKey",
default: .menuBarActionCopy
)

/// Cannot use NSScreen, so we use CGRect to record the screen visible frame for EZShowWindowPositionFormer
static var screenVisibleFrame = Key<CGRect>("EZConfiguration_kScreenVisibleFrameKey", default: .zero)
}

extension Defaults.Keys {
Expand Down
9 changes: 7 additions & 2 deletions Easydict/objc/EventMonitor/EZEventMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,10 @@ - (void)handleMonitorEvent:(NSEvent *)event {

switch (event.type) {
case NSEventTypeLeftMouseUp: {
// MMLogInfo(@"mouse up");

EZWindowManager.shared.lastPoint = NSEvent.mouseLocation;

if ([self checkIfLeftMouseDragged]) {
self.triggerType = EZTriggerTypeDragged;
if (self.frontmostAppTriggerType & self.triggerType) {
Expand All @@ -740,7 +744,7 @@ - (void)handleMonitorEvent:(NSEvent *)event {
break;
}
case NSEventTypeLeftMouseDown: {
// MMLogInfo(@"mouse down");
// MMLogInfo(@"mouse down");

// Record some mouse event except dragged event.
[self updateRecordedEvents:event];
Expand All @@ -749,9 +753,10 @@ - (void)handleMonitorEvent:(NSEvent *)event {
break;
}
case NSEventTypeLeftMouseDragged: {
// NSLog(@"mouse dragged");

// Record dragged event.
[self updateRecordedEvents:event];
// MMLogInfo(@"NSEventTypeLeftMouseDragged");
break;
}
case NSEventTypeRightMouseDown: {
Expand Down
8 changes: 3 additions & 5 deletions Easydict/objc/Utility/EZCoordinateUtils/EZCoordinateUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ NS_ASSUME_NONNULL_BEGIN
@interface EZCoordinateUtils : NSObject

/// Get safe point, bottom-left coordinate.
+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen;
+ (CGRect)getSafeFrame:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen;
+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreenVisibleFrame:(CGRect)screenVisibleFrame;

/// Make sure frame show in screen visible frame, return left-bottom postion frame.
+ (CGRect)getSafeAreaFrame:(CGRect)frame inScreen:(nullable NSScreen *)screen;
+ (CGPoint)getSafeLocation:(CGRect)frame inScreen:(NSScreen *)screen;

+ (CGRect)getSafeAreaFrame:(CGRect)frame inScreenVisibleFrame:(CGRect)screenVisibleFrame;
+ (CGPoint)getSafeLocation:(CGRect)frame inScreenVisibleFrame:(CGRect)screenVisibleFrame;

/// Convert point from top-left to bottom-left coordinate system
+ (CGPoint)convertPointToBottomLeft:(CGPoint)point;
Expand Down
38 changes: 17 additions & 21 deletions Easydict/objc/Utility/EZCoordinateUtils/EZCoordinateUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,26 @@

@implementation EZCoordinateUtils

+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen {
/// left-bottom safe postion.
+ (CGPoint)getFrameSafePoint:(CGRect)frame moveToPoint:(CGPoint)point inScreenVisibleFrame:(CGRect)screenVisibleFrame {
CGRect newFrame = CGRectMake(point.x, point.y, frame.size.width, frame.size.height);
return [self getSafeLocation:newFrame inScreen:screen];
return [self getSafeLocation:newFrame inScreenVisibleFrame:screenVisibleFrame];
}

+ (CGRect)getSafeFrame:(CGRect)frame moveToPoint:(CGPoint)point inScreen:(NSScreen *)screen {
+ (CGRect)getSafeFrame:(CGRect)frame moveToPoint:(CGPoint)point inScreenVisibleFrame:(CGRect)screenVisibleFrame {
CGRect newFrame = CGRectMake(point.x, point.y, frame.size.width, frame.size.height);
return [self getSafeAreaFrame:newFrame inScreen:screen];
return [self getSafeAreaFrame:newFrame inScreenVisibleFrame:screenVisibleFrame];
}


/// left-bottom safe postion.
+ (CGPoint)getSafeLocation:(CGRect)frame inScreen:(NSScreen *)screen {
CGRect safeFrame = [self getSafeAreaFrame:frame inScreen:screen];
+ (CGPoint)getSafeLocation:(CGRect)frame inScreenVisibleFrame:(CGRect)screenVisibleFrame {
CGRect safeFrame = [self getSafeAreaFrame:frame inScreenVisibleFrame:screenVisibleFrame];
return safeFrame.origin;
}

/// 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];
}
CGRect visibleFrame = screen.visibleFrame;
if (CGRectContainsRect(visibleFrame, frame)) {
+ (CGRect)getSafeAreaFrame:(CGRect)frame inScreenVisibleFrame:(CGRect)screenVisibleFrame {
if (CGRectContainsRect(screenVisibleFrame, frame)) {
return frame;
}

Expand All @@ -43,21 +39,21 @@ + (CGRect)getSafeAreaFrame:(CGRect)frame inScreen:(nullable NSScreen *)screen {
CGFloat height = frame.size.height;

// left safe
if (x < visibleFrame.origin.x) {
x = visibleFrame.origin.x;
if (x < screenVisibleFrame.origin.x) {
x = screenVisibleFrame.origin.x;
}
// right safe
if (x + width > visibleFrame.origin.x + visibleFrame.size.width) {
x = visibleFrame.origin.x + visibleFrame.size.width - width;
if (x + width > screenVisibleFrame.origin.x + screenVisibleFrame.size.width) {
x = screenVisibleFrame.origin.x + screenVisibleFrame.size.width - width;
}

// top safe
if (y > visibleFrame.origin.y + visibleFrame.size.height) {
y = visibleFrame.origin.y + visibleFrame.size.height;
if (y > screenVisibleFrame.origin.y + screenVisibleFrame.size.height) {
y = screenVisibleFrame.origin.y + screenVisibleFrame.size.height;
}
// keep bottom safe, if frame bottom beyond visibleFrame bottom and frame height <= visibleFrame height , try to move it up.
if (y < visibleFrame.origin.y && height <= visibleFrame.size.height) {
y = visibleFrame.origin.y;
if (y < screenVisibleFrame.origin.y && height <= screenVisibleFrame.size.height) {
y = screenVisibleFrame.origin.y;
}

return CGRectMake(x, y, width, height);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,13 @@ - (void)updateWindowHeightWithLock:(BOOL)lockFlag {
CGFloat y = window.y + deltaHeight;

CGRect newFrame = CGRectMake(window.x, y, window.width, showingWindowHeight);
CGRect safeFrame = [EZCoordinateUtils getSafeAreaFrame:newFrame inScreen:EZLayoutManager.shared.screen];

CGRect screenVisibleFrame = EZLayoutManager.shared.screen.visibleFrame;
if (Configuration.shared.fixedWindowPosition == EZShowWindowPositionFormer) {
screenVisibleFrame = Configuration.shared.screenVisibleFrame;
}

CGRect safeFrame = [EZCoordinateUtils getSafeAreaFrame:newFrame inScreenVisibleFrame:screenVisibleFrame];

// ???: why set window frame will change tableView height?
// ???: why this window animation will block cell rendering?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ - (void)updateWindowFrame:(EZBaseQueryWindow *)window {
break;
case EZWindowTypeFixed:
_fixedWindowFrame = window.frame;

// Record screenVisibleFrame when fixedWindowPosition is EZShowWindowPositionFormer
if (Configuration.shared.fixedWindowPosition == EZShowWindowPositionFormer) {
CGPoint fixedWindowCenter = NSMakePoint(NSMidX(_fixedWindowFrame), NSMidY(_fixedWindowFrame));

// Update lastPoint to update current active screen
EZWindowManager.shared.lastPoint = fixedWindowCenter;

Configuration.shared.screenVisibleFrame = self.screen.visibleFrame;
}

break;
case EZWindowTypeMini:
_miniWindowFrame = window.frame;
Expand All @@ -225,6 +236,8 @@ - (BOOL)showInputTextField:(EZWindowType)windowType {
- (void)updateScreen:(NSScreen *)screen {
_screen = screen;

// MMLogInfo(@"update screen: %@", @(screen.visibleFrame));

[self setupMaximumWindowSize:screen];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ - (void)setupEventMonitor {

CGPoint point = [self getPopButtonWindowLocation]; // This is top-left point
CGPoint bottomLeftPoint = CGPointMake(point.x, point.y - self.popButtonWindow.height);
CGPoint safePoint = [EZCoordinateUtils getFrameSafePoint:self.popButtonWindow.frame moveToPoint:bottomLeftPoint inScreen:self.screen];
CGPoint safePoint = [EZCoordinateUtils getFrameSafePoint:self.popButtonWindow.frame
moveToPoint:bottomLeftPoint
inScreenVisibleFrame:self.screen.visibleFrame];

safePoint = [self getSafePointForPopButtonWindow:safePoint];

Expand Down Expand Up @@ -229,6 +231,8 @@ - (NSScreen *)screen {
- (void)setLastPoint:(CGPoint)lastPoint {
_lastPoint = lastPoint;

// MMLogInfo(@"lastPoint: %@", @(lastPoint));

[EZLayoutManager.shared updateScreen:self.screen];
}

Expand Down Expand Up @@ -424,7 +428,16 @@ - (void)showFloatingWindow:(EZBaseQueryWindow *)window atPoint:(CGPoint)point {
[[self currentShowingSettingsWindow] close];

// get safe window position
CGPoint safeLocation = [EZCoordinateUtils getFrameSafePoint:window.frame moveToPoint:point inScreen:self.screen];

CGRect screenVisibleFrame = self.screen.visibleFrame;
if (Configuration.shared.fixedWindowPosition == EZShowWindowPositionFormer) {
// If fixed window position is former, we need to get the screen frame when fixed window is shown.
screenVisibleFrame = Configuration.shared.screenVisibleFrame;
}

CGPoint safeLocation = [EZCoordinateUtils getFrameSafePoint:window.frame
moveToPoint:point
inScreenVisibleFrame:screenVisibleFrame];
[window setFrameOrigin:safeLocation];
window.level = EZFloatingWindowLevel;

Expand Down

0 comments on commit 5c431aa

Please sign in to comment.