20#include <TargetConditionals.h>
24#import <AppKit/AppKit.h>
25#import <Foundation/Foundation.h>
30 CGFloat scale_factor = 1.0f;
32 NSArray *screens = [NSScreen screens];
34 if ([screens[display_index] respondsToSelector:@selector(backingScaleFactor)]) {
35 scale_factor = [screens[display_index] backingScaleFactor];
52#include <SDL3/SDL_keyboard.h>
54#import <UIKit/UIKit.h>
56static __weak UIResponder* first_responder = nil;
58@interface UIResponder (WesnothFirstResponderCapture)
59- (void)wesnoth_capture_first_responder:(
id)sender;
62@implementation UIResponder (WesnothFirstResponderCapture)
63- (void)wesnoth_capture_first_responder:(
id)sender
66 first_responder = self;
70@interface wesnoth_keyboard_toolbar : NSObject
71+ (instancetype)shared;
72- (void)attach_toolbar_to_first_responder;
75@implementation wesnoth_keyboard_toolbar
79 static wesnoth_keyboard_toolbar* instance = nil;
80 static dispatch_once_t once_token;
81 dispatch_once(&once_token, ^{
82 instance = [[wesnoth_keyboard_toolbar alloc] init];
91 [[NSNotificationCenter defaultCenter]
93 selector:@selector(on_keyboard_will_show:)
94 name:UIKeyboardWillShowNotification
100- (UIResponder*)current_first_responder
102 first_responder = nil;
103 [[UIApplication sharedApplication]
104 sendAction:@selector(wesnoth_capture_first_responder:)
108 return first_responder;
111- (UIToolbar*)dismiss_toolbar
113 static UIToolbar* toolbar = nil;
115 toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 44.0)];
116 UIBarButtonItem* flex = [[UIBarButtonItem alloc]
117 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
120 UIBarButtonItem* dismiss = [[UIBarButtonItem alloc]
121 initWithTitle:NSLocalizedString(@"Dismiss", @"Dismiss keyboard button")
122 style:UIBarButtonItemStyleDone
124 action:@selector(on_dismiss_keyboard:)];
125 [toolbar setItems:@[flex, dismiss]];
131- (void)on_keyboard_will_show:(NSNotification*)notification
134 [
self attach_toolbar_to_first_responder];
137- (void)attach_toolbar_to_first_responder
139 UIResponder* responder = [
self current_first_responder];
140 if([responder isKindOfClass:[UITextField class]]) {
141 UITextField* text_field = (UITextField*)responder;
142 UIToolbar* toolbar = [
self dismiss_toolbar];
143 if(text_field.inputAccessoryView != toolbar) {
144 text_field.inputAccessoryView = toolbar;
145 [text_field reloadInputViews];
147 }
else if([responder isKindOfClass:[UITextView class]]) {
148 UITextView* text_view = (UITextView*)responder;
149 UIToolbar* toolbar = [
self dismiss_toolbar];
150 if(text_view.inputAccessoryView != toolbar) {
151 text_view.inputAccessoryView = toolbar;
152 [text_view reloadInputViews];
157- (void)on_dismiss_keyboard:(
id)sender
168 NSArray<UIScreen*>* screens = [UIScreen screens];
169 if(display_index >= 0 &&
static_cast<NSUInteger
>(display_index) < screens.count) {
170 return screens[display_index].scale;
172 return UIScreen.mainScreen.scale;
177 if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
178 [wesnoth_keyboard_toolbar shared];
void install_keyboard_dismiss_toolbar()
CGFloat get_scale_factor(int display_index)
struct utils::detail::formula_initer init
SDL_Window * get_window()