SessionCtl // Simplify handing of NSRange parameters, etc.

This commit is contained in:
ShikiSuen 2023-02-16 01:11:11 +08:00
parent 75a1540589
commit 3984cb79e4
2 changed files with 21 additions and 7 deletions

View File

@ -15,6 +15,13 @@ public extension NSMutableString {
var localized: String { NSLocalizedString(description, comment: "") }
}
// MARK: - NSRange Extension
public extension NSRange {
static var zero = NSRange(location: 0, length: 0)
static var notFound = NSRange(location: NSNotFound, length: NSNotFound)
}
// MARK: - NSRect Extension
public extension NSRect {

View File

@ -104,16 +104,14 @@ public extension SessionCtl {
/// 0 replacementRangeNSNotFound
///
doSetMarkedText(
attributedStringSecured.0, selectionRange: attributedStringSecured.1,
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
attributedStringSecured.0, selectionRange: attributedStringSecured.1
)
}
/// 使
func clearInlineDisplay() {
doSetMarkedText(
"", selectionRange: NSRange(location: 0, length: 0),
replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
NSAttributedString(), selectionRange: NSRange.zero
)
}
@ -126,19 +124,28 @@ public extension SessionCtl {
DispatchQueue.main.async {
guard let client = self.client() else { return }
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
buffer, replacementRange: NSRange.notFound
)
}
} else {
guard let client = client() else { return }
client.insertText(
buffer, replacementRange: NSRange(location: NSNotFound, length: NSNotFound)
buffer, replacementRange: NSRange.notFound
)
}
}
/// setMarkedText GCD
func doSetMarkedText(_ string: Any!, selectionRange: NSRange, replacementRange: NSRange) {
/// - Parameters:
/// - string: NSAttributedString
/// - selectionRange: .thick
/// - replacementRange:
/// replacementRange Microsoft Office
/// /
func doSetMarkedText(
_ string: NSAttributedString, selectionRange: NSRange,
replacementRange: NSRange = .notFound
) {
if isServingIMEItself || !isActivated {
DispatchQueue.main.async {
guard let client = self.client() else { return }