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: "") } 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 // MARK: - NSRect Extension
public extension NSRect { public extension NSRect {

View File

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