修改 我已经退出界面,然后从新进入界面弹起键盘,为什么撤销删除按钮显示?

This commit is contained in:
2025-12-26 13:55:07 +08:00
parent 203f104ece
commit ae37730da6
2 changed files with 98 additions and 33 deletions

View File

@@ -65,6 +65,8 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
- (void)viewDidLoad { - (void)viewDidLoad {
[super viewDidLoad]; [super viewDidLoad];
// /
[[KBBackspaceUndoManager shared] registerNonClearAction];
[self setupUI]; [self setupUI];
self.suggestionEngine = [KBSuggestionEngine shared]; self.suggestionEngine = [KBSuggestionEngine shared];
self.currentWord = @""; self.currentWord = @"";
@@ -93,11 +95,20 @@ static void KBSkinInstallNotificationCallback(CFNotificationCenterRef center,
- (void)viewWillAppear:(BOOL)animated{ - (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated]; [super viewWillAppear:animated];
// /
[[KBBackspaceUndoManager shared] registerNonClearAction];
[[KBInputBufferManager shared] resetWithText:@""];
[[KBLocalizationManager shared] reloadFromSharedStorageIfNeeded]; [[KBLocalizationManager shared] reloadFromSharedStorageIfNeeded];
[[KBInputBufferManager shared] seedIfEmptyWithContextBefore:self.textDocumentProxy.documentContextBeforeInput // /QQ 宿 documentContext liveText manualSnapshot
[[KBInputBufferManager shared] updateFromExternalContextBefore:self.textDocumentProxy.documentContextBeforeInput
after:self.textDocumentProxy.documentContextAfterInput]; after:self.textDocumentProxy.documentContextAfterInput];
} }
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[KBBackspaceUndoManager shared] registerNonClearAction];
}
- (void)textDidChange:(id<UITextInput>)textInput { - (void)textDidChange:(id<UITextInput>)textInput {
[super textDidChange:textInput]; [super textDidChange:textInput];
[[KBInputBufferManager shared] updateFromExternalContextBefore:self.textDocumentProxy.documentContextBeforeInput [[KBInputBufferManager shared] updateFromExternalContextBefore:self.textDocumentProxy.documentContextBeforeInput

View File

@@ -11,11 +11,11 @@
static const NSTimeInterval kKBBackspaceLongPressMinDuration = 0.35; static const NSTimeInterval kKBBackspaceLongPressMinDuration = 0.35;
static const NSTimeInterval kKBBackspaceRepeatInterval = 0.06; static const NSTimeInterval kKBBackspaceRepeatInterval = 0.06;
static const NSTimeInterval kKBBackspaceChunkStartDelay = 1.0; static const NSTimeInterval kKBBackspaceChunkStartDelay = 0.6;
static const NSTimeInterval kKBBackspaceChunkRepeatInterval = 0.1; static const NSTimeInterval kKBBackspaceChunkRepeatInterval = 0.1;
static const NSTimeInterval kKBBackspaceChunkFastDelay = 1.4; static const NSTimeInterval kKBBackspaceChunkFastDelay = 1.2;
static const NSInteger kKBBackspaceChunkSize = 6; static const NSInteger kKBBackspaceChunkSize = 8;
static const NSInteger kKBBackspaceChunkSizeFast = 12; static const NSInteger kKBBackspaceChunkSizeFast = 16;
static const CGFloat kKBBackspaceClearLabelCornerRadius = 8.0; static const CGFloat kKBBackspaceClearLabelCornerRadius = 8.0;
static const CGFloat kKBBackspaceClearLabelHeight = 26.0; static const CGFloat kKBBackspaceClearLabelHeight = 26.0;
static const CGFloat kKBBackspaceClearLabelPaddingX = 10.0; static const CGFloat kKBBackspaceClearLabelPaddingX = 10.0;
@@ -210,34 +210,77 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
asciiWordSet = [NSCharacterSet characterSetWithCharactersInString: asciiWordSet = [NSCharacterSet characterSetWithCharactersInString:
@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"]; @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"];
punctuationSet = [NSCharacterSet punctuationCharacterSet]; NSMutableCharacterSet *punct = [[NSCharacterSet punctuationCharacterSet] mutableCopy];
// / chunk 1
[punct addCharactersInString:@",。!?;:、()【】《》“”‘’·…—"];
punctuationSet = [punct copy];
}); });
__block NSInteger deleteCount = 0; __block NSInteger deleteCount = 0;
__block KBBackspaceChunkClass chunkClass = KBBackspaceChunkClassUnknown; typedef NS_ENUM(NSInteger, KBBackspaceChunkPhase) {
KBBackspaceChunkPhaseWhitespace = 0,
KBBackspaceChunkPhasePunctuation,
KBBackspaceChunkPhaseCore
};
__block KBBackspaceChunkPhase phase = KBBackspaceChunkPhaseWhitespace;
__block KBBackspaceChunkClass coreClass = KBBackspaceChunkClassUnknown;
[context enumerateSubstringsInRange:NSMakeRange(0, context.length) [context enumerateSubstringsInRange:NSMakeRange(0, context.length)
options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationReverse options:NSStringEnumerationByComposedCharacterSequences | NSStringEnumerationReverse
usingBlock:^(NSString *substring, __unused NSRange substringRange, __unused NSRange enclosingRange, BOOL *stop) { usingBlock:^(NSString *substring, __unused NSRange substringRange, __unused NSRange enclosingRange, BOOL *stop) {
if (substring.length == 0) { return; } if (substring.length == 0) { return; }
KBBackspaceChunkClass currentClass = KBBackspaceChunkClassOther; if (deleteCount >= maxCount) {
if ([substring rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassWhitespace;
} else if ([substring rangeOfCharacterFromSet:asciiWordSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassASCIIWord;
} else if ([substring rangeOfCharacterFromSet:punctuationSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassPunctuation;
}
if (chunkClass == KBBackspaceChunkClassUnknown) {
chunkClass = currentClass;
} else if (chunkClass != currentClass) {
*stop = YES; *stop = YES;
return; return;
} }
KBBackspaceChunkClass currentClass = KBBackspaceChunkClassOther;
if ([substring rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassWhitespace;
} else if ([substring rangeOfCharacterFromSet:punctuationSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassPunctuation;
} else if ([substring rangeOfCharacterFromSet:asciiWordSet].location != NSNotFound) {
currentClass = KBBackspaceChunkClassASCIIWord;
}
BOOL consumed = NO;
while (!consumed) {
if (phase == KBBackspaceChunkPhaseWhitespace) {
if (currentClass == KBBackspaceChunkClassWhitespace) {
deleteCount += 1; deleteCount += 1;
consumed = YES;
} else {
phase = KBBackspaceChunkPhasePunctuation;
}
continue;
}
if (phase == KBBackspaceChunkPhasePunctuation) {
if (currentClass == KBBackspaceChunkClassPunctuation) {
deleteCount += 1;
consumed = YES;
} else {
phase = KBBackspaceChunkPhaseCore;
}
continue;
}
// phase == CoreASCII /
if (coreClass == KBBackspaceChunkClassUnknown) {
coreClass = currentClass;
}
if (currentClass != coreClass) {
*stop = YES;
consumed = YES;
continue;
}
deleteCount += 1;
consumed = YES;
}
if (deleteCount >= maxCount) { if (deleteCount >= maxCount) {
*stop = YES; *stop = YES;
return;
} }
}]; }];
@@ -480,12 +523,23 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
if (!ivc) { return; } if (!ivc) { return; }
id<UITextDocumentProxy> proxy = ivc.textDocumentProxy; id<UITextDocumentProxy> proxy = ivc.textDocumentProxy;
NSInteger nextEmptyRounds = emptyRounds; NSInteger nextEmptyRounds = emptyRounds;
static NSCharacterSet *sentenceBoundarySet = nil; static NSCharacterSet *stopBoundarySet = nil;
static NSCharacterSet *whitespaceSet = nil; static NSCharacterSet *trailingBoundarySet = nil;
static NSCharacterSet *trailingWhitespaceSet = nil;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
sentenceBoundarySet = [NSCharacterSet characterSetWithCharactersInString:@".!?。!?"]; // stopBoundary:
whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet]; // - . ! ?
// - /
// - \n \r
stopBoundarySet = [NSCharacterSet characterSetWithCharactersInString:@".!?。!?…\n\r\u2028\u2029"];
// trailingBoundary:
// //
trailingBoundarySet = [NSCharacterSet characterSetWithCharactersInString:@".!?。!?"];
// trailingWhitespace: /Tab stopBoundarySet
trailingWhitespaceSet = [NSCharacterSet whitespaceCharacterSet];
}); });
KBClearPhase phase = self.backspaceClearPhase; KBClearPhase phase = self.backspaceClearPhase;
@@ -496,10 +550,9 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
NSString *before = proxy.documentContextBeforeInput ?: @""; NSString *before = proxy.documentContextBeforeInput ?: @"";
if (before.length == 0) { if (before.length == 0) {
nextEmptyRounds += 1; nextEmptyRounds += 1;
// 使 context 宿 // 宿/QQ context使
[[KBBackspaceUndoManager shared] captureAndDeleteBackwardFromProxy:proxy count:1]; // before
[[KBInputBufferManager shared] applyClearDeleteCount:1]; shouldStop = YES;
deletedThisTick += 1;
break; break;
} }
nextEmptyRounds = 0; nextEmptyRounds = 0;
@@ -520,8 +573,9 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
}]; }];
if (lastChar.length == 0) { break; } if (lastChar.length == 0) { break; }
BOOL isWhitespace = ([lastChar rangeOfCharacterFromSet:whitespaceSet].location != NSNotFound); BOOL isWhitespace = ([lastChar rangeOfCharacterFromSet:trailingWhitespaceSet].location != NSNotFound);
BOOL isBoundary = ([lastChar rangeOfCharacterFromSet:sentenceBoundarySet].location != NSNotFound); BOOL isStopBoundary = ([lastChar rangeOfCharacterFromSet:stopBoundarySet].location != NSNotFound);
BOOL isTrailingBoundary = ([lastChar rangeOfCharacterFromSet:trailingBoundarySet].location != NSNotFound);
if (phase == KBClearPhaseSkipWhitespace) { if (phase == KBClearPhaseSkipWhitespace) {
if (isWhitespace) { if (isWhitespace) {
@@ -534,7 +588,7 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
} }
if (phase == KBClearPhaseSkipTrailingBoundary) { if (phase == KBClearPhaseSkipTrailingBoundary) {
if (isBoundary) { if (isTrailingBoundary) {
[[KBBackspaceUndoManager shared] captureAndDeleteBackwardFromProxy:proxy count:1]; [[KBBackspaceUndoManager shared] captureAndDeleteBackwardFromProxy:proxy count:1];
[[KBInputBufferManager shared] applyClearDeleteCount:1]; [[KBInputBufferManager shared] applyClearDeleteCount:1];
deletedThisTick += 1; deletedThisTick += 1;
@@ -544,7 +598,7 @@ typedef NS_ENUM(NSInteger, KBClearPhase) {
} }
// phase == DeleteUntilBoundary // phase == DeleteUntilBoundary
if (isBoundary) { if (isStopBoundary) {
shouldStop = YES; // shouldStop = YES; //
break; break;
} }