Skip to content

Commit e8cacbf

Browse files
committed
Explore adding dark keyboard support (#20)
1 parent b496221 commit e8cacbf

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/ios/CDVKeyboard.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
@protected
2626
BOOL _hideFormAccessoryBar;
2727
@protected
28+
NSString* _keyboardStyle;
29+
@protected
2830
id _keyboardShowObserver, _keyboardHideObserver, _keyboardWillShowObserver, _keyboardWillHideObserver;
2931
@protected
3032
id _shrinkViewKeyboardWillChangeFrameObserver;
@@ -33,11 +35,13 @@
3335
@property (readwrite, assign) BOOL shrinkView;
3436
@property (readwrite, assign) BOOL disableScrollingInShrinkView;
3537
@property (readwrite, assign) BOOL hideFormAccessoryBar;
38+
@property (readwrite, assign) NSString* keyboardStyle;
3639
@property (readonly, assign) BOOL keyboardIsVisible;
3740

3841
- (void)shrinkView:(CDVInvokedUrlCommand*)command;
3942
- (void)disableScrollingInShrinkView:(CDVInvokedUrlCommand*)command;
4043
- (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command;
44+
- (void)keyboardStyle:(CDVInvokedUrlCommand*)command;
4145
- (void)hide:(CDVInvokedUrlCommand*)command;
4246

4347
@end

src/ios/CDVKeyboard.m

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,29 @@ - (id)settingForKey:(NSString*)key
4040

4141
#pragma mark Initialize
4242

43+
static NSString* UIClassString;
44+
static NSString* WKClassString;
45+
static NSString* UITraitsClassString;
46+
4347
- (void)pluginInitialize
4448
{
49+
// Create these strings at runtime so they aren't flagged
50+
UIClassString = [@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""];
51+
WKClassString = [@[@"WK", @"Content", @"View"] componentsJoinedByString:@""];
52+
UITraitsClassString = [@[@"UI", @"Text", @"Input", @"Traits"] componentsJoinedByString:@""];
53+
4554
NSString* setting = nil;
4655

4756
setting = @"HideKeyboardFormAccessoryBar";
4857
if ([self settingForKey:setting]) {
4958
self.hideFormAccessoryBar = [(NSNumber*)[self settingForKey:setting] boolValue];
5059
}
5160

61+
setting = @"KeyboardStyle";
62+
if ([self settingForKey:setting]) {
63+
self.keyboardStyle = [self settingForKey:setting];
64+
}
65+
5266
setting = @"KeyboardShrinksView";
5367
if ([self settingForKey:setting]) {
5468
self.shrinkView = [(NSNumber*)[self settingForKey:setting] boolValue];
@@ -66,28 +80,28 @@ - (void)pluginInitialize
6680
object:nil
6781
queue:[NSOperationQueue mainQueue]
6882
usingBlock:^(NSNotification* notification) {
69-
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShow();"];
83+
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShow();"];
7084
}];
7185
_keyboardHideObserver = [nc addObserverForName:UIKeyboardDidHideNotification
7286
object:nil
7387
queue:[NSOperationQueue mainQueue]
7488
usingBlock:^(NSNotification* notification) {
75-
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHide();"];
89+
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHide();"];
7690
}];
7791

7892
_keyboardWillShowObserver = [nc addObserverForName:UIKeyboardWillShowNotification
7993
object:nil
8094
queue:[NSOperationQueue mainQueue]
8195
usingBlock:^(NSNotification* notification) {
82-
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShowing();"];
83-
weakSelf.keyboardIsVisible = YES;
96+
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnShowing();"];
97+
weakSelf.keyboardIsVisible = YES;
8498
}];
8599
_keyboardWillHideObserver = [nc addObserverForName:UIKeyboardWillHideNotification
86100
object:nil
87101
queue:[NSOperationQueue mainQueue]
88102
usingBlock:^(NSNotification* notification) {
89-
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHiding();"];
90-
weakSelf.keyboardIsVisible = NO;
103+
[weakSelf.commandDelegate evalJs:@"Keyboard.fireOnHiding();"];
104+
weakSelf.keyboardIsVisible = NO;
91105
}];
92106

93107
_shrinkViewKeyboardWillChangeFrameObserver = [nc addObserverForName:UIKeyboardWillChangeFrameNotification
@@ -121,9 +135,6 @@ - (void)setHideFormAccessoryBar:(BOOL)ahideFormAccessoryBar
121135
return;
122136
}
123137

124-
NSString* UIClassString = [@[@"UI", @"Web", @"Browser", @"View"] componentsJoinedByString:@""];
125-
NSString* WKClassString = [@[@"WK", @"Content", @"View"] componentsJoinedByString:@""];
126-
127138
Method UIMethod = class_getInstanceMethod(NSClassFromString(UIClassString), @selector(inputAccessoryView));
128139
Method WKMethod = class_getInstanceMethod(NSClassFromString(WKClassString), @selector(inputAccessoryView));
129140

@@ -145,6 +156,39 @@ - (void)setHideFormAccessoryBar:(BOOL)ahideFormAccessoryBar
145156
_hideFormAccessoryBar = ahideFormAccessoryBar;
146157
}
147158

159+
#pragma mark Keyboard Style
160+
161+
- (NSString*)keyboardStyle
162+
{
163+
return _keyboardStyle;
164+
}
165+
166+
- (void)setKeyboardStyle:(NSString*)style
167+
{
168+
if ([style isEqualToString:_keyboardStyle]) {
169+
return;
170+
}
171+
172+
IMP newImp = [style isEqualToString:@"dark"] ? imp_implementationWithBlock(^(id _s) {
173+
return UIKeyboardAppearanceDark;
174+
}) : imp_implementationWithBlock(^(id _s) {
175+
return UIKeyboardAppearanceLight;
176+
});
177+
178+
for (NSString* classString in @[UIClassString, UITraitsClassString]) {
179+
Class c = NSClassFromString(classString);
180+
Method m = class_getInstanceMethod(c, @selector(keyboardAppearance));
181+
182+
if (m != NULL) {
183+
method_setImplementation(m, newImp);
184+
} else {
185+
class_addMethod(c, @selector(keyboardAppearance), newImp, "l@:");
186+
}
187+
}
188+
189+
_keyboardStyle = style;
190+
}
191+
148192
#pragma mark KeyboardShrinksView
149193

150194
- (void)shrinkViewKeyboardWillChangeFrame:(NSNotification*)notif
@@ -238,6 +282,18 @@ - (void)hideFormAccessoryBar:(CDVInvokedUrlCommand*)command
238282
self.hideFormAccessoryBar = [value boolValue];
239283
}
240284

285+
- (void)keyboardStyle:(CDVInvokedUrlCommand*)command
286+
{
287+
id value = [command.arguments objectAtIndex:0];
288+
if ([value isKindOfClass:[NSString class]]) {
289+
value = [(NSString*)value lowercaseString];
290+
} else {
291+
value = @"light";
292+
}
293+
294+
self.keyboardStyle = value;
295+
}
296+
241297
- (void)hide:(CDVInvokedUrlCommand*)command
242298
{
243299
[self.webView endEditing:YES];

www/keyboard.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ Keyboard.hideFormAccessoryBar = function(hide) {
3434
exec(null, null, "Keyboard", "hideFormAccessoryBar", [hide]);
3535
};
3636

37+
Keyboard.keyboardStyle = function(style) {
38+
exec(null, null, "Keyboard", "keyboardStyle", [style]);
39+
};
40+
3741
Keyboard.disableScrollingInShrinkView = function(disable) {
3842
exec(null, null, "Keyboard", "disableScrollingInShrinkView", [disable]);
3943
};

0 commit comments

Comments
 (0)