Files
keyboard/CustomKeyboard/KeyboardViewController.m

215 lines
7.4 KiB
Mathematica
Raw Normal View History

2025-10-27 19:42:27 +08:00
//
// KeyboardViewController.m
// CustomKeyboard
//
// Created by Mac on 2025/10/27.
//
#import "KeyboardViewController.h"
2025-10-28 15:10:38 +08:00
#import "KBKeyBoardMainView.h"
2025-10-28 10:18:10 +08:00
#import "KBKey.h"
2025-10-28 15:10:38 +08:00
#import "KBFunctionView.h"
2025-10-28 18:02:10 +08:00
#import "KBSettingView.h"
2025-10-28 15:10:38 +08:00
#import "Masonry.h"
2025-10-31 16:06:54 +08:00
#import "KBAuthManager.h"
2025-10-27 19:42:27 +08:00
2025-10-28 15:34:19 +08:00
static CGFloat KEYBOARDHEIGHT = 256 + 20;
2025-10-27 19:42:27 +08:00
2025-10-28 15:18:12 +08:00
@interface KeyboardViewController () <KBKeyBoardMainViewDelegate, KBFunctionViewDelegate>
2025-10-28 10:18:10 +08:00
@property (nonatomic, strong) UIButton *nextKeyboardButton; //
2025-10-28 15:10:38 +08:00
@property (nonatomic, strong) KBKeyBoardMainView *keyBoardMainView; // 0
@property (nonatomic, strong) KBFunctionView *functionView; // 0
2025-10-28 18:02:10 +08:00
@property (nonatomic, strong) KBSettingView *settingView; //
2025-10-27 19:42:27 +08:00
@end
@implementation KeyboardViewController
2025-10-30 20:23:34 +08:00
{
BOOL _kb_didTriggerLoginDeepLinkOnce;
}
2025-10-27 19:42:27 +08:00
- (void)viewDidLoad {
[super viewDidLoad];
[self setupUI];
2025-10-31 15:08:30 +08:00
// HUD App KeyWindow
[KBHUD setContainerView:self.view];
2025-10-27 19:42:27 +08:00
}
- (void)setupUI {
2025-10-28 10:18:10 +08:00
//
[self.view.heightAnchor constraintEqualToConstant:KEYBOARDHEIGHT].active = YES;
2025-10-28 15:10:38 +08:00
//
self.functionView.hidden = YES;
[self.view addSubview:self.functionView];
[self.functionView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-10-28 10:18:10 +08:00
make.left.right.equalTo(self.view);
2025-10-28 15:10:38 +08:00
make.top.equalTo(self.view).offset(4);
make.bottom.equalTo(self.view.mas_bottom).offset(-4);
2025-10-28 10:18:10 +08:00
}];
2025-10-28 15:10:38 +08:00
[self.view addSubview:self.keyBoardMainView];
[self.keyBoardMainView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-10-28 10:18:10 +08:00
make.left.right.equalTo(self.view);
2025-10-28 15:10:38 +08:00
make.top.equalTo(self.view).offset(4);
2025-10-28 10:18:10 +08:00
make.bottom.equalTo(self.view.mas_bottom).offset(-4);
}];
2025-10-27 19:42:27 +08:00
}
2025-10-28 10:18:10 +08:00
2025-10-28 15:10:38 +08:00
#pragma mark - Private
2025-10-28 14:30:03 +08:00
2025-10-28 15:10:38 +08:00
/// /
- (void)showFunctionPanel:(BOOL)show {
//
self.functionView.hidden = !show;
self.keyBoardMainView.hidden = show;
2025-10-28 15:18:12 +08:00
2025-10-28 15:10:38 +08:00
//
if (show) {
[self.view bringSubviewToFront:self.functionView];
} else {
[self.view bringSubviewToFront:self.keyBoardMainView];
}
2025-10-28 10:18:10 +08:00
}
2025-10-28 18:02:10 +08:00
/// / keyBoardMainView /
- (void)showSettingView:(BOOL)show {
if (show) {
2025-10-30 20:23:34 +08:00
// if (!self.settingView) {
self.settingView = [[KBSettingView alloc] init];
self.settingView.hidden = YES;
[self.view addSubview:self.settingView];
[self.settingView mas_makeConstraints:^(MASConstraintMaker *make) {
//
make.edges.equalTo(self.keyBoardMainView);
}];
[self.settingView.backButton addTarget:self action:@selector(onTapSettingsBack) forControlEvents:UIControlEventTouchUpInside];
// }
2025-10-28 18:02:10 +08:00
[self.view bringSubviewToFront:self.settingView];
// keyBoardMainView self.view
[self.view layoutIfNeeded];
CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds);
if (w <= 0) { w = CGRectGetWidth(self.view.bounds); }
if (w <= 0) { w = [UIScreen mainScreen].bounds.size.width; }
self.settingView.transform = CGAffineTransformMakeTranslation(w, 0);
self.settingView.hidden = NO;
[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
self.settingView.transform = CGAffineTransformIdentity;
} completion:nil];
} else {
if (!self.settingView || self.settingView.hidden) return;
CGFloat w = CGRectGetWidth(self.keyBoardMainView.bounds);
if (w <= 0) { w = CGRectGetWidth(self.view.bounds); }
if (w <= 0) { w = [UIScreen mainScreen].bounds.size.width; }
[UIView animateWithDuration:0.22 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
self.settingView.transform = CGAffineTransformMakeTranslation(w, 0);
} completion:^(BOOL finished) {
self.settingView.hidden = YES;
}];
}
}
2025-10-28 10:18:10 +08:00
2025-10-28 15:10:38 +08:00
// MARK: - KBKeyBoardMainViewDelegate
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapKey:(KBKey *)key {
2025-10-28 10:18:10 +08:00
switch (key.type) {
case KBKeyTypeCharacter:
[self.textDocumentProxy insertText:key.output ?: key.title ?: @""]; break;
case KBKeyTypeBackspace:
[self.textDocumentProxy deleteBackward]; break;
case KBKeyTypeSpace:
[self.textDocumentProxy insertText:@" "]; break;
case KBKeyTypeReturn:
[self.textDocumentProxy insertText:@"\n"]; break;
case KBKeyTypeGlobe:
[self advanceToNextInputMode]; break;
case KBKeyTypeCustom:
2025-10-29 12:59:22 +08:00
// AI
[self showFunctionPanel:YES];
break;
2025-10-28 15:10:38 +08:00
case KBKeyTypeModeChange:
2025-10-28 10:18:10 +08:00
case KBKeyTypeShift:
2025-10-28 15:10:38 +08:00
// KBKeyBoardMainView/KBKeyboardView
2025-10-28 10:18:10 +08:00
break;
}
}
2025-10-28 15:10:38 +08:00
- (void)keyBoardMainView:(KBKeyBoardMainView *)keyBoardMainView didTapToolActionAtIndex:(NSInteger)index {
if (index == 0) {
[self showFunctionPanel:YES];
} else {
[self showFunctionPanel:NO];
}
}
2025-10-28 18:02:10 +08:00
- (void)keyBoardMainViewDidTapSettings:(KBKeyBoardMainView *)keyBoardMainView {
[self showSettingView:YES];
}
2025-10-28 15:18:12 +08:00
// MARK: - KBFunctionViewDelegate
- (void)functionView:(KBFunctionView *)functionView didTapToolActionAtIndex:(NSInteger)index {
// index == 0
if (index == 0) {
[self showFunctionPanel:NO];
}
}
2025-10-28 15:10:38 +08:00
#pragma mark - lazy
- (KBKeyBoardMainView *)keyBoardMainView{
if (!_keyBoardMainView) {
_keyBoardMainView = [[KBKeyBoardMainView alloc] init];
2025-10-28 15:18:12 +08:00
_keyBoardMainView.delegate = self;
2025-10-28 15:10:38 +08:00
}
return _keyBoardMainView;
}
- (KBFunctionView *)functionView{
if (!_functionView) {
_functionView = [[KBFunctionView alloc] init];
2025-10-28 15:18:12 +08:00
_functionView.delegate = self; // Bar
2025-10-28 15:10:38 +08:00
}
return _functionView;
}
2025-10-28 18:02:10 +08:00
- (KBSettingView *)settingView {
if (!_settingView) {
_settingView = [[KBSettingView alloc] init];
}
return _settingView;
}
2025-10-28 15:10:38 +08:00
2025-10-28 18:02:10 +08:00
#pragma mark - Actions
- (void)onTapSettingsBack {
[self showSettingView:NO];
}
2025-10-28 15:18:12 +08:00
2025-10-30 20:23:34 +08:00
// App App
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (!_kb_didTriggerLoginDeepLinkOnce) {
_kb_didTriggerLoginDeepLinkOnce = YES;
2025-10-31 16:06:54 +08:00
// App
if (!KBAuthManager.shared.isLoggedIn) {
[self kb_tryOpenContainerForLoginIfNeeded];
}
2025-10-30 20:23:34 +08:00
}
}
- (void)kb_tryOpenContainerForLoginIfNeeded {
NSURL *url = [NSURL URLWithString:@"kbkeyboard://login?src=keyboard"];
if (!url) return;
__weak typeof(self) weakSelf = self;
[self.extensionContext openURL:url completionHandler:^(__unused BOOL success) {
// 使
__unused typeof(weakSelf) selfStrong = weakSelf;
}];
}
2025-10-28 15:18:12 +08:00
@end