Files
keyboard/CustomKeyboard/View/KBToolBar.m

214 lines
7.9 KiB
Mathematica
Raw Permalink Normal View History

2025-10-27 19:42:27 +08:00
//
// KBToolBar.m
// CustomKeyboard
//
2025-10-28 14:30:03 +08:00
// Created by Mac on 2025/10/28.
2025-10-27 19:42:27 +08:00
//
#import "KBToolBar.h"
2025-11-05 20:11:10 +08:00
#import "KBResponderUtils.h" // UIInputViewController
2025-10-27 19:42:27 +08:00
2025-10-28 10:18:10 +08:00
@interface KBToolBar ()
@property (nonatomic, strong) UIView *leftContainer;
@property (nonatomic, strong) NSArray<UIButton *> *leftButtonsInternal;
@property (nonatomic, strong) UIButton *settingsButtonInternal;
2025-11-05 20:11:10 +08:00
@property (nonatomic, strong) UIButton *globeButtonInternal; //
2025-10-28 10:18:10 +08:00
@end
2025-10-27 19:42:27 +08:00
@implementation KBToolBar
2025-10-27 21:11:28 +08:00
- (instancetype)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame]) {
2025-10-28 10:18:10 +08:00
self.backgroundColor = [UIColor clearColor];
_leftButtonTitles = @[@"Item1", @"Item2", @"Item3", @"Item4"]; //
[self setupUI];
2025-10-27 21:11:28 +08:00
}
return self;
2025-10-27 19:42:27 +08:00
}
2025-10-27 21:11:28 +08:00
2025-10-28 10:18:10 +08:00
#pragma mark - Public
- (NSArray<UIButton *> *)leftButtons {
return self.leftButtonsInternal;
}
- (UIButton *)settingsButton {
return self.settingsButtonInternal;
}
- (void)setLeftButtonTitles:(NSArray<NSString *> *)leftButtonTitles {
_leftButtonTitles = [leftButtonTitles copy];
// Update titles if buttons already exist
[self.leftButtonsInternal enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (idx < self.leftButtonTitles.count) {
[obj setTitle:self.leftButtonTitles[idx] forState:UIControlStateNormal];
}
}];
}
#pragma mark -
- (void)setupUI {
[self addSubview:self.leftContainer];
[self addSubview:self.settingsButtonInternal];
2025-11-05 20:11:10 +08:00
[self addSubview:self.globeButtonInternal];
2025-10-28 10:18:10 +08:00
//
[self.settingsButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.mas_right).offset(-12);
make.centerY.equalTo(self.mas_centerY);
make.width.height.mas_equalTo(32);
}];
2025-11-05 20:11:10 +08:00
//
[self.globeButtonInternal mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.mas_left).offset(12);
make.centerY.equalTo(self.mas_centerY);
make.width.height.mas_equalTo(32);
}];
2025-10-28 10:18:10 +08:00
//
[self.leftContainer mas_makeConstraints:^(MASConstraintMaker *make) {
2025-11-05 20:11:10 +08:00
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
2025-10-28 10:18:10 +08:00
make.right.equalTo(self.settingsButtonInternal.mas_left).offset(-12);
make.centerY.equalTo(self.mas_centerY);
make.height.mas_equalTo(32);
}];
// 4
NSMutableArray<UIButton *> *buttons = [NSMutableArray arrayWithCapacity:4];
UIView *previous = nil;
for (NSInteger i = 0; i < 4; i++) {
UIButton *btn = [self buildActionButtonAtIndex:i];
[self.leftContainer addSubview:btn];
[buttons addObject:btn];
[btn mas_makeConstraints:^(MASConstraintMaker *make) {
if (previous) {
make.left.equalTo(previous.mas_right).offset(8);
make.width.equalTo(previous);
} else {
make.left.equalTo(self.leftContainer.mas_left);
}
make.top.bottom.equalTo(self.leftContainer);
}];
previous = btn;
}
//
[previous mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.leftContainer.mas_right);
}];
self.leftButtonsInternal = buttons.copy;
2025-11-05 20:11:10 +08:00
//
[self kb_refreshGlobeVisibility];
2025-10-28 10:18:10 +08:00
}
- (UIButton *)buildActionButtonAtIndex:(NSInteger)idx {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.layer.cornerRadius = 16;
btn.layer.masksToBounds = YES;
btn.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
btn.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
NSString *title = (idx < self.leftButtonTitles.count) ? self.leftButtonTitles[idx] : [NSString stringWithFormat:@"Item%ld", (long)(idx+1)];
[btn setTitle:title forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag = idx;
[btn addTarget:self action:@selector(onLeftAction:) forControlEvents:UIControlEventTouchUpInside];
return btn;
}
#pragma mark - Actions
- (void)onLeftAction:(UIButton *)sender {
if ([self.delegate respondsToSelector:@selector(toolBar:didTapActionAtIndex:)]) {
[self.delegate toolBar:self didTapActionAtIndex:sender.tag];
}
}
- (void)onSettings {
if ([self.delegate respondsToSelector:@selector(toolBarDidTapSettings:)]) {
[self.delegate toolBarDidTapSettings:self];
}
}
#pragma mark - Lazy
- (UIView *)leftContainer {
if (!_leftContainer) {
_leftContainer = [[UIView alloc] init];
_leftContainer.backgroundColor = [UIColor clearColor];
}
return _leftContainer;
}
- (UIButton *)settingsButtonInternal {
if (!_settingsButtonInternal) {
_settingsButtonInternal = [UIButton buttonWithType:UIButtonTypeSystem];
_settingsButtonInternal.layer.cornerRadius = 16;
_settingsButtonInternal.layer.masksToBounds = YES;
_settingsButtonInternal.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
[_settingsButtonInternal setTitle:@"⚙︎" forState:UIControlStateNormal]; // 齿
[_settingsButtonInternal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_settingsButtonInternal addTarget:self action:@selector(onSettings) forControlEvents:UIControlEventTouchUpInside];
}
return _settingsButtonInternal;
}
2025-10-27 19:42:27 +08:00
2025-11-05 20:11:10 +08:00
- (UIButton *)globeButtonInternal {
if (!_globeButtonInternal) {
_globeButtonInternal = [UIButton buttonWithType:UIButtonTypeSystem];
_globeButtonInternal.layer.cornerRadius = 16;
_globeButtonInternal.layer.masksToBounds = YES;
_globeButtonInternal.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
[_globeButtonInternal setTitle:@"🌐" forState:UIControlStateNormal];
[_globeButtonInternal setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
}
return _globeButtonInternal;
}
#pragma mark - Globe (Input Mode Switch)
// 宿
- (void)kb_refreshGlobeVisibility {
UIInputViewController *ivc = KBFindInputViewController(self);
BOOL needSwitchKey = YES;
if (ivc && [ivc respondsToSelector:@selector(needsInputModeSwitchKey)]) {
needSwitchKey = ivc.needsInputModeSwitchKey; // YES
}
self.globeButtonInternal.hidden = !needSwitchKey;
// leftContainer 12
[self.leftContainer mas_remakeConstraints:^(MASConstraintMaker *make) {
if (needSwitchKey) {
make.left.equalTo(self.globeButtonInternal.mas_right).offset(8);
} else {
make.left.equalTo(self.mas_left).offset(12);
}
make.right.equalTo(self.settingsButtonInternal.mas_left).offset(-12);
make.centerY.equalTo(self.mas_centerY);
make.height.mas_equalTo(32);
}];
//
//
[self.globeButtonInternal removeTarget:nil action:NULL forControlEvents:UIControlEventAllEvents];
if (needSwitchKey && ivc) {
SEL sel = NSSelectorFromString(@"handleInputModeListFromView:withEvent:");
if ([ivc respondsToSelector:sel]) {
[self.globeButtonInternal addTarget:ivc action:sel forControlEvents:UIControlEventAllTouchEvents];
} else {
// 退
[self.globeButtonInternal addTarget:ivc action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];
}
}
}
- (void)didMoveToWindow {
[super didMoveToWindow];
[self kb_refreshGlobeVisibility];
}
2025-10-27 19:42:27 +08:00
@end