17 #include <TargetConditionals.h>
22 #import <AppKit/AppKit.h>
23 #import <Foundation/Foundation.h>
28 CGFloat scale_factor = 1.0f;
30 NSArray *screens = [NSScreen screens];
32 if ([screens[display_index] respondsToSelector:
@selector(backingScaleFactor)]) {
33 scale_factor = [screens[display_index] backingScaleFactor];
50 #include <SDL2/SDL_keyboard.h>
52 #import <UIKit/UIKit.h>
54 static __weak UIResponder* first_responder = nil;
56 @interface UIResponder (WesnothFirstResponderCapture)
57 - (void)wesnoth_capture_first_responder:(
id)sender;
60 @implementation UIResponder (WesnothFirstResponderCapture)
61 - (void)wesnoth_capture_first_responder:(
id)sender
64 first_responder =
self;
68 @interface wesnoth_keyboard_toolbar : NSObject
69 + (instancetype)shared;
70 - (void)attach_toolbar_to_first_responder;
73 @implementation wesnoth_keyboard_toolbar
75 + (instancetype)shared
77 static wesnoth_keyboard_toolbar* instance = nil;
78 static dispatch_once_t once_token;
79 dispatch_once(&once_token, ^{
80 instance = [[wesnoth_keyboard_toolbar alloc] init];
89 [[NSNotificationCenter defaultCenter]
91 selector:@selector(on_keyboard_will_show:)
92 name:UIKeyboardWillShowNotification
98 - (UIResponder*)current_first_responder
100 first_responder = nil;
101 [[UIApplication sharedApplication]
102 sendAction:@selector(wesnoth_capture_first_responder:)
106 return first_responder;
109 - (UIToolbar*)dismiss_toolbar
111 static UIToolbar* toolbar = nil;
113 toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 44.0)];
114 UIBarButtonItem* flex = [[UIBarButtonItem alloc]
115 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
118 UIBarButtonItem* dismiss = [[UIBarButtonItem alloc]
119 initWithTitle:NSLocalizedString(@"Dismiss", @"Dismiss keyboard button")
120 style:UIBarButtonItemStyleDone
122 action:@selector(on_dismiss_keyboard:)];
123 [toolbar setItems:@[flex, dismiss]];
129 - (void)on_keyboard_will_show:(NSNotification*)notification
132 [
self attach_toolbar_to_first_responder];
135 - (void)attach_toolbar_to_first_responder
137 UIResponder* responder = [
self current_first_responder];
138 if([responder isKindOfClass:[UITextField
class]]) {
139 UITextField* text_field = (UITextField*)responder;
140 UIToolbar* toolbar = [
self dismiss_toolbar];
141 if(text_field.inputAccessoryView != toolbar) {
142 text_field.inputAccessoryView = toolbar;
143 [text_field reloadInputViews];
145 }
else if([responder isKindOfClass:[UITextView
class]]) {
146 UITextView* text_view = (UITextView*)responder;
147 UIToolbar* toolbar = [
self dismiss_toolbar];
148 if(text_view.inputAccessoryView != toolbar) {
149 text_view.inputAccessoryView = toolbar;
150 [text_view reloadInputViews];
155 - (void)on_dismiss_keyboard:(
id)sender
166 NSArray<UIScreen*>* screens = [UIScreen screens];
167 if(display_index >= 0 &&
static_cast<NSUInteger
>(display_index) < screens.count) {
168 return screens[display_index].scale;
170 return UIScreen.mainScreen.scale;
175 if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
176 [wesnoth_keyboard_toolbar shared];
void install_keyboard_dismiss_toolbar()
CGFloat get_scale_factor(int display_index)
struct utils::detail::formula_initer init