-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: improve getting selected text by shortcut
- Loading branch information
Showing
10 changed files
with
227 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
Easydict/Swift/Utility/GetSelectedText/SharedUtilities.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// SharedUtilities.swift | ||
// Easydict | ||
// | ||
// Created by tisfeng on 10/15/24. | ||
// Copyright © 2024 izual. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@objc | ||
public class SharedUtilities: NSObject { | ||
@objc | ||
@discardableResult | ||
public static func pollTask(_ task: @escaping () -> Bool) -> Bool { | ||
pollTask(every: 0.005, timeout: 0.1, task: task) | ||
} | ||
|
||
/// Sync poll task, if task is true, return true, else continue polling. | ||
/// | ||
/// - Warning: ⚠️ This method will block the current thread, only use when necessary. | ||
/// - Returns: true if the task succeeded, false if it timed out. | ||
@objc | ||
@discardableResult | ||
public static func pollTask( | ||
every interval: TimeInterval = 0.005, | ||
timeout: TimeInterval = 0.1, | ||
task: @escaping () -> Bool, | ||
timeoutCallback: @escaping () -> () = {} | ||
) | ||
-> Bool { | ||
let startTime = Date() | ||
while Date().timeIntervalSince(startTime) < timeout { | ||
if task() { | ||
return true | ||
} | ||
Thread.sleep(forTimeInterval: interval) | ||
} | ||
timeoutCallback() | ||
logInfo("pollTask timeout call back") | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.