Skip to content

Commit

Permalink
Fix translator win would disappear as dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
ddouble committed May 13, 2023
1 parent 6517d00 commit 465472d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 29 deletions.
27 changes: 25 additions & 2 deletions ZTranslator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
5605C64029DD6B4800454941 /* ZTranslatorUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605C63F29DD6B4800454941 /* ZTranslatorUITests.swift */; };
5605C64229DD6B4800454941 /* ZTranslatorUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5605C64129DD6B4800454941 /* ZTranslatorUITestsLaunchTests.swift */; };
56F858AF29DEDBAF00AA46E3 /* MASShortcut in Frameworks */ = {isa = PBXBuildFile; productRef = 56F858AE29DEDBAF00AA46E3 /* MASShortcut */; };
56FB548F29FBDEB500399C17 /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = 56FB548E29FBDEB500399C17 /* KeyboardShortcuts */; };
5AE6A27628AE3BAC3169C1CA /* FloatingPanel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AE6A62A4047CD1E4F6A96D8 /* FloatingPanel.swift */; };
5AE6A715DB9FD52CB08021AA /* ZTranslatorSettings.swift.bk in Resources */ = {isa = PBXBuildFile; fileRef = 5AE6A74D1899115C144329F4 /* ZTranslatorSettings.swift.bk */; };
5AE6A8DE9E1C7A0804FFF729 /* .gitignore in Resources */ = {isa = PBXBuildFile; fileRef = 5AE6ACEB7460F49F317FE7DA /* .gitignore */; };
5AE6A98DDD33605E6C081909 /* ZTranslatorSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5AE6A7AF14BCAF2FE606B774 /* ZTranslatorSettings.swift */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -50,7 +52,9 @@
5605C64129DD6B4800454941 /* ZTranslatorUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZTranslatorUITestsLaunchTests.swift; sourceTree = "<group>"; };
5605C64E29DD849200454941 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
5AE6A62A4047CD1E4F6A96D8 /* FloatingPanel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FloatingPanel.swift; sourceTree = "<group>"; };
5AE6A74D1899115C144329F4 /* ZTranslatorSettings.swift.bk */ = {isa = PBXFileReference; lastKnownFileType = file.bk; path = ZTranslatorSettings.swift.bk; sourceTree = "<group>"; };
5AE6A7AF14BCAF2FE606B774 /* ZTranslatorSettings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ZTranslatorSettings.swift; sourceTree = "<group>"; };
5AE6AA231F73CAB6F572DB7A /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = readme.md; sourceTree = "<group>"; };
5AE6ACEB7460F49F317FE7DA /* .gitignore */ = {isa = PBXFileReference; lastKnownFileType = file.gitignore; path = .gitignore; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand All @@ -60,6 +64,7 @@
buildActionMask = 2147483647;
files = (
56F858AF29DEDBAF00AA46E3 /* MASShortcut in Frameworks */,
56FB548F29FBDEB500399C17 /* KeyboardShortcuts in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -88,6 +93,7 @@
5605C63E29DD6B4800454941 /* ZTranslatorUITests */,
5605C62129DD6B4500454941 /* Products */,
5AE6ACEB7460F49F317FE7DA /* .gitignore */,
5AE6AA231F73CAB6F572DB7A /* readme.md */,
);
sourceTree = "<group>";
};
Expand All @@ -111,6 +117,7 @@
5605C62929DD6B4700454941 /* Preview Content */,
5AE6A7AF14BCAF2FE606B774 /* ZTranslatorSettings.swift */,
5AE6A62A4047CD1E4F6A96D8 /* FloatingPanel.swift */,
5AE6A74D1899115C144329F4 /* ZTranslatorSettings.swift.bk */,
);
path = ZTranslator;
sourceTree = "<group>";
Expand Down Expand Up @@ -158,6 +165,7 @@
name = ZTranslator;
packageProductDependencies = (
56F858AE29DEDBAF00AA46E3 /* MASShortcut */,
56FB548E29FBDEB500399C17 /* KeyboardShortcuts */,
);
productName = ZTranslator;
productType = "com.apple.product-type.application";
Expand Down Expand Up @@ -232,6 +240,7 @@
mainGroup = 5605C61729DD6B4500454941;
packageReferences = (
56F858AD29DEDBAF00AA46E3 /* XCRemoteSwiftPackageReference "MASShortcut" */,
56FB548D29FBDEB500399C17 /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */,
);
productRefGroup = 5605C62129DD6B4500454941 /* Products */;
projectDirPath = "";
Expand All @@ -252,6 +261,7 @@
5605C62B29DD6B4700454941 /* Preview Assets.xcassets in Resources */,
5605C62829DD6B4700454941 /* Assets.xcassets in Resources */,
5AE6A8DE9E1C7A0804FFF729 /* .gitignore in Resources */,
5AE6A715DB9FD52CB08021AA /* ZTranslatorSettings.swift.bk in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -452,7 +462,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = xyz.cat.ZTranslator;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -484,7 +494,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.0;
MARKETING_VERSION = 1.1;
PRODUCT_BUNDLE_IDENTIFIER = xyz.cat.ZTranslator;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down Expand Up @@ -611,6 +621,14 @@
kind = branch;
};
};
56FB548D29FBDEB500399C17 /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/sindresorhus/KeyboardShortcuts";
requirement = {
branch = main;
kind = branch;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -619,6 +637,11 @@
package = 56F858AD29DEDBAF00AA46E3 /* XCRemoteSwiftPackageReference "MASShortcut" */;
productName = MASShortcut;
};
56FB548E29FBDEB500399C17 /* KeyboardShortcuts */ = {
isa = XCSwiftPackageProductDependency;
package = 56FB548D29FBDEB500399C17 /* XCRemoteSwiftPackageReference "KeyboardShortcuts" */;
productName = KeyboardShortcuts;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = 5605C61829DD6B4500454941 /* Project object */;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "keyboardshortcuts",
"kind" : "remoteSourceControl",
"location" : "https://github.com/sindresorhus/KeyboardShortcuts",
"state" : {
"branch" : "main",
"revision" : "d168f629edcd687383e18c908168520b4f156324"
}
},
{
"identity" : "masshortcut",
"kind" : "remoteSourceControl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
<dict>
<key>SchemeUserState</key>
<dict>
<key>ZTranslator.xcscheme</key>
<key>ZTranslator - Release.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>ZTranslator - Debug.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
</dict>
</plist>
9 changes: 9 additions & 0 deletions ZTranslator/TranslatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,15 @@ struct TranslatorView: View {
}
.frame(minWidth: 800)
}
.gesture(DragGesture(minimumDistance: 0)
.onChanged { _ in
// Handle drag start event
}
.onEnded { value in
// Handle drag end event
// print(value)
}
)
.onAppear() {
NotificationCenter.default.addObserver(forName: .wakeUp, object: nil, queue: .main) { notification in
if let newOriginalText = notification.object as? String {
Expand Down
58 changes: 32 additions & 26 deletions ZTranslator/ZTranslatorApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func getSelectedTextRect() {
if (selectedBoundsError == .success) {
AXValueGetValue(selectBounds as! AXValue, .cgRect, &selectRect)
//do whatever you want with your selectRect
print(selectRect)
// print(selectRect)
}
}
}
Expand Down Expand Up @@ -331,28 +331,6 @@ func getOpenAIResponse(text: String, completion: @escaping ((originalLang: Strin
task.resume()
}

/**
report to caller if mouse point is in a specific area
- Parameters:
- area:
- callback:
- Returns:
*/
func monitorMousePosition(area: NSRect, callback: @escaping (Bool) -> Void) -> Timer {
let timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { timer in
let point = NSEvent.mouseLocation
let isPointInArea = area.contains(point)
callback(isPointInArea)
if !isPointInArea {
timer.invalidate()
// print("Point is out of area, stopping timer")
} else {
// print("Point is within area")
}
}
return timer
}

/**
resize a rect from its center by x, y amount
Expand Down Expand Up @@ -480,6 +458,35 @@ class ZTranslatorApp: App {
#endif
}

/**
report to caller if mouse point is in a specific area
- Parameters:
- callback:
- Returns:
*/
func monitorMousePositionInTranslatorView(callback: @escaping (Bool) -> Void) -> Timer {
let timer = Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) { timer in
let point = NSEvent.mouseLocation

if let popup = self.translatorPopup {
var area = popup.frame
resizeRectFromCenter(&area, xAmount: 40, yAmount: 80)

let mouseIsOut = !area.contains(point)
callback(mouseIsOut)
if !mouseIsOut {
timer.invalidate()
// print("Point is out of area, stopping timer")
} else {
// print("Point is within area")
}

}
}
return timer
}

func showTranslatorPopup() {
if self.translatorPopupTimer != nil && self.translatorPopupTimer?.isValid == true {
self.translatorPopupTimer?.invalidate()
Expand All @@ -505,11 +512,10 @@ class ZTranslatorApp: App {
popup.orderFront(nil)
// print("1 visible:", popup.isVisible)


var area = popup.frame
resizeRectFromCenter(&area, xAmount: 40, yAmount: 80)
self.translatorPopupTimer = monitorMousePosition(area: area) { (isPointInArea) in
if (!isPointInArea) {
self.translatorPopupTimer = self.monitorMousePositionInTranslatorView() { (mouseIsOut) in
if (mouseIsOut) {
// print("hide")
popup.orderOut(nil)
// print("2 visible:", popup.isVisible)
Expand Down

0 comments on commit 465472d

Please sign in to comment.