Files
keyboard/CustomKeyboard/View/KBKeyBoardMainView.m

305 lines
11 KiB
Mathematica
Raw Normal View History

2025-10-28 15:10:38 +08:00
//
// KBKeyBoardMainView.m
// CustomKeyboard
//
// Created by Mac on 2025/10/28.
//
#import "KBKeyBoardMainView.h"
#import "KBToolBar.h"
#import "KBKeyboardView.h"
#import "KBFunctionView.h"
#import "KBKey.h"
2025-12-15 13:24:43 +08:00
#import "KBEmojiPanelView.h"
2025-12-22 12:54:28 +08:00
#import "KBSuggestionBarView.h"
2025-10-28 15:10:38 +08:00
#import "Masonry.h"
2025-11-04 21:01:46 +08:00
#import "KBSkinManager.h"
#import "KBBackspaceUndoManager.h"
2026-01-08 16:54:38 +08:00
#import "KBKeyboardLayoutConfig.h"
2025-10-28 15:10:38 +08:00
2025-12-22 12:54:28 +08:00
@interface KBKeyBoardMainView ()<KBToolBarDelegate, KBKeyboardViewDelegate, KBEmojiPanelViewDelegate, KBSuggestionBarViewDelegate>
2025-10-28 15:10:38 +08:00
@property (nonatomic, strong) KBToolBar *topBar;
2025-12-22 12:54:28 +08:00
@property (nonatomic, strong) KBSuggestionBarView *suggestionBar;
2025-10-28 15:10:38 +08:00
@property (nonatomic, strong) KBKeyboardView *keyboardView;
2025-12-15 13:24:43 +08:00
@property (nonatomic, strong) KBEmojiPanelView *emojiView;
@property (nonatomic, assign) BOOL emojiPanelVisible;
2025-12-22 12:54:28 +08:00
@property (nonatomic, assign) BOOL suggestionBarHasItems;
2025-10-28 15:10:38 +08:00
// /
@end
@implementation KBKeyBoardMainView
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// self.backgroundColor = [KBSkinManager shared].current.keyboardBackground;
self.backgroundColor = [UIColor colorWithHex:0xD1D3DB];
2025-12-15 13:24:43 +08:00
2025-10-28 15:10:38 +08:00
//
self.topBar = [[KBToolBar alloc] init];
self.topBar.delegate = self;
[self addSubview:self.topBar];
2025-12-22 12:54:28 +08:00
//
self.suggestionBar = [[KBSuggestionBarView alloc] init];
self.suggestionBar.delegate = self;
self.suggestionBar.hidden = YES;
[self addSubview:self.suggestionBar];
2025-11-21 21:50:40 +08:00
// /
CGFloat keyboardAreaHeight = KBFit(200.0f);
2026-01-08 16:54:38 +08:00
KBKeyboardLayoutConfig *layoutConfig = [KBKeyboardLayoutConfig sharedConfig];
if (layoutConfig) {
CGFloat configHeight = [layoutConfig keyboardAreaScaledHeight];
if (configHeight > 0.0) {
keyboardAreaHeight = configHeight;
}
}
2025-11-21 21:50:40 +08:00
CGFloat bottomInset = KBFit(4.0f);
2025-12-22 13:46:45 +08:00
// CGFloat topBarHeight = KBFit(40.0f);
2025-11-21 21:50:40 +08:00
CGFloat barSpacing = KBFit(6.0f);
2025-10-28 15:10:38 +08:00
self.keyboardView = [[KBKeyboardView alloc] init];
self.keyboardView.delegate = self;
[self addSubview:self.keyboardView];
[self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
2025-11-21 21:50:40 +08:00
make.height.mas_equalTo(keyboardAreaHeight);
make.bottom.equalTo(self.mas_bottom).offset(-bottomInset);
2025-10-28 15:10:38 +08:00
}];
2025-11-21 21:50:40 +08:00
2025-12-15 13:24:43 +08:00
self.emojiView = [[KBEmojiPanelView alloc] init];
self.emojiView.hidden = YES;
self.emojiView.alpha = 0.0;
self.emojiView.delegate = self;
[self addSubview:self.emojiView];
[self.emojiView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
2025-12-22 13:46:45 +08:00
// [self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.right.equalTo(self);
// make.top.equalTo(self.mas_top).offset(0);
// make.height.mas_equalTo(topBarHeight);
// }];
[self.topBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.mas_top).offset(0);
2025-12-19 22:00:52 +08:00
make.bottom.equalTo(self.keyboardView.mas_top).offset(0);
}];
2025-12-22 12:54:28 +08:00
[self.suggestionBar mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self);
make.top.equalTo(self.topBar);
make.bottom.equalTo(self.topBar);
}];
[self.keyboardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.topBar.mas_bottom).offset(barSpacing);
}];
2025-10-28 15:10:38 +08:00
// /
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(kb_undoStateChanged)
name:KBBackspaceUndoStateDidChangeNotification
object:nil];
2025-10-28 15:10:38 +08:00
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
2025-12-15 13:24:43 +08:00
- (void)setEmojiPanelVisible:(BOOL)visible animated:(BOOL)animated {
if (self.emojiPanelVisible == visible) return;
self.emojiPanelVisible = visible;
if (visible) {
[self.emojiView reloadData];
self.emojiView.hidden = NO;
[self bringSubviewToFront:self.emojiView];
} else {
self.keyboardView.hidden = NO;
self.topBar.hidden = NO;
2025-12-22 12:54:28 +08:00
self.suggestionBar.hidden = !self.suggestionBarHasItems;
2025-12-15 13:24:43 +08:00
}
void (^changes)(void) = ^{
self.emojiView.alpha = visible ? 1.0 : 0.0;
self.keyboardView.alpha = visible ? 0.0 : 1.0;
self.topBar.alpha = visible ? 0.0 : 1.0;
self.suggestionBar.alpha = visible ? 0.0 : ([self kb_shouldShowSuggestions] ? 1.0 : 0.0);
2025-12-15 13:24:43 +08:00
};
void (^completion)(BOOL) = ^(BOOL finished) {
self.emojiView.hidden = !visible;
self.keyboardView.hidden = visible;
self.topBar.hidden = visible;
2025-12-22 12:54:28 +08:00
if (visible) {
self.suggestionBar.hidden = YES;
} else {
self.suggestionBar.hidden = ![self kb_shouldShowSuggestions];
2025-12-22 12:54:28 +08:00
}
2025-12-15 13:24:43 +08:00
};
if (animated) {
[UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:changes completion:completion];
} else {
changes();
completion(YES);
}
}
- (void)toggleEmojiPanel {
[self setEmojiPanelVisible:!self.emojiPanelVisible animated:YES];
}
2025-10-28 15:10:38 +08:00
#pragma mark - KBToolBarDelegate
- (void)toolBar:(KBToolBar *)toolBar didTapActionAtIndex:(NSInteger)index {
//
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapToolActionAtIndex:)]) {
[self.delegate keyBoardMainView:self didTapToolActionAtIndex:index];
}
}
- (void)toolBarDidTapSettings:(KBToolBar *)toolBar {
2025-10-28 18:02:10 +08:00
if ([self.delegate respondsToSelector:@selector(keyBoardMainViewDidTapSettings:)]) {
[self.delegate keyBoardMainViewDidTapSettings:self];
}
2025-10-28 15:10:38 +08:00
}
2025-12-19 19:21:08 +08:00
- (void)toolBarDidTapUndo:(KBToolBar *)toolBar {
if ([self.delegate respondsToSelector:@selector(keyBoardMainViewDidTapUndo:)]) {
[self.delegate keyBoardMainViewDidTapUndo:self];
}
}
2025-10-28 15:10:38 +08:00
#pragma mark - KBKeyboardViewDelegate
- (void)keyboardView:(KBKeyboardView *)keyboard didTapKey:(KBKey *)key {
switch (key.type) {
case KBKeyTypeCharacter:
//
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
break;
case KBKeyTypeBackspace:
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
break;
case KBKeyTypeSpace:
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
break;
case KBKeyTypeReturn:
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
break;
case KBKeyTypeModeChange: {
// <->
keyboard.layoutStyle = (keyboard.layoutStyle == KBKeyboardLayoutStyleLetters) ? KBKeyboardLayoutStyleNumbers : KBKeyboardLayoutStyleLetters;
[keyboard reloadKeys];
} break;
case KBKeyTypeGlobe:
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
break;
2025-12-15 13:24:43 +08:00
case KBKeyTypeCustom: {
if ([key.identifier isEqualToString:KBKeyIdentifierEmojiPanel]) {
[self toggleEmojiPanel];
break;
}
2025-10-28 15:10:38 +08:00
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
[self.delegate keyBoardMainView:self didTapKey:key];
}
2025-12-15 13:24:43 +08:00
} break;
2025-10-28 15:10:38 +08:00
case KBKeyTypeShift:
// Shift KBKeyboardView
break;
}
}
//
2025-10-28 18:02:10 +08:00
// KeyboardViewController
2025-12-15 13:24:43 +08:00
#pragma mark - KBEmojiPanelViewDelegate
- (void)emojiPanelView:(KBEmojiPanelView *)panel didSelectEmoji:(NSString *)emoji {
if (emoji.length == 0) return;
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didSelectEmoji:)]) {
[self.delegate keyBoardMainView:self didSelectEmoji:emoji];
}
}
- (void)emojiPanelViewDidRequestClose:(KBEmojiPanelView *)panel {
[self setEmojiPanelVisible:NO animated:YES];
}
- (void)emojiPanelViewDidTapSearch:(KBEmojiPanelView *)panel {
if ([self.delegate respondsToSelector:@selector(keyBoardMainViewDidTapEmojiSearch:)]) {
[self.delegate keyBoardMainViewDidTapEmojiSearch:self];
}
}
2025-12-15 18:32:54 +08:00
- (void)emojiPanelViewDidTapDelete:(KBEmojiPanelView *)panel {
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didTapKey:)]) {
KBKey *backspace = [KBKey keyWithTitle:@"" type:KBKeyTypeBackspace];
[self.delegate keyBoardMainView:self didTapKey:backspace];
}
}
2025-11-04 21:01:46 +08:00
#pragma mark - Theme
- (void)kb_applyTheme {
KBSkinManager *mgr = [KBSkinManager shared];
2026-01-09 19:13:35 +08:00
self.backgroundColor = [UIColor clearColor];
self.keyboardView.backgroundColor = [UIColor clearColor];
if ([self.topBar respondsToSelector:@selector(kb_applyTheme)]) {
[self.topBar kb_applyTheme];
}
2025-12-22 12:54:28 +08:00
[self.suggestionBar applyTheme:mgr.current];
2025-11-04 21:01:46 +08:00
[self.keyboardView reloadKeys];
2025-12-15 13:24:43 +08:00
if (self.emojiView) {
[self.emojiView applyTheme:mgr.current];
}
2025-11-04 21:01:46 +08:00
}
2025-10-28 18:02:10 +08:00
2025-12-22 12:54:28 +08:00
#pragma mark - Suggestions
- (void)kb_setSuggestions:(NSArray<NSString *> *)suggestions {
self.suggestionBarHasItems = (suggestions.count > 0);
[self.suggestionBar updateSuggestions:suggestions];
[self kb_applySuggestionVisibility];
2025-12-22 12:54:28 +08:00
}
#pragma mark - KBSuggestionBarViewDelegate
- (void)suggestionBarView:(KBSuggestionBarView *)view didSelectSuggestion:(NSString *)suggestion {
if ([self.delegate respondsToSelector:@selector(keyBoardMainView:didSelectSuggestion:)]) {
[self.delegate keyBoardMainView:self didSelectSuggestion:suggestion];
}
}
- (void)kb_undoStateChanged {
[self kb_applySuggestionVisibility];
}
- (BOOL)kb_shouldShowSuggestions {
if (self.emojiPanelVisible) { return NO; }
if (![KBBackspaceUndoManager shared].hasUndo && self.suggestionBarHasItems) {
return YES;
}
return NO;
}
- (void)kb_applySuggestionVisibility {
BOOL shouldShow = [self kb_shouldShowSuggestions];
self.suggestionBar.hidden = !shouldShow;
self.suggestionBar.alpha = shouldShow ? 1.0 : 0.0;
}
2025-10-28 15:10:38 +08:00
@end