The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
apple_video.mm
Go to the documentation of this file.
1/*
2 Copyright (C) 2020 - 2025
3 by Martin HrubĂ˝ <hrubymar10@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#ifdef __APPLE__
17
18#include "apple_video.hpp"
19
20#include <TargetConditionals.h>
21
22#if TARGET_OS_OSX
23
24#import <AppKit/AppKit.h>
25#import <Foundation/Foundation.h>
26
27namespace desktop {
28namespace apple {
29 CGFloat get_scale_factor(int display_index) {
30 CGFloat scale_factor = 1.0f;
31
32 NSArray *screens = [NSScreen screens];
33
34 if ([screens[display_index] respondsToSelector:@selector(backingScaleFactor)]) { // Mac OS X 10.7 and later
35 scale_factor = [screens[display_index] backingScaleFactor];
36 }
37
38 return scale_factor;
39 }
40
42 {
43 // iOS-only, no-op on macOS.
44 }
45} // end namespace apple
46} // end namespace desktop
47
48#elif TARGET_OS_IOS
49
50#include "video.hpp"
51
52#include <SDL3/SDL_keyboard.h>
53
54#import <UIKit/UIKit.h>
55
56static __weak UIResponder* first_responder = nil;
57
58@interface UIResponder (WesnothFirstResponderCapture)
59- (void)wesnoth_capture_first_responder:(id)sender;
60@end
61
62@implementation UIResponder (WesnothFirstResponderCapture)
63- (void)wesnoth_capture_first_responder:(id)sender
64{
65 (void)sender;
66 first_responder = self;
67}
68@end
69
70@interface wesnoth_keyboard_toolbar : NSObject
71+ (instancetype)shared;
72- (void)attach_toolbar_to_first_responder;
73@end
74
75@implementation wesnoth_keyboard_toolbar
76
77+ (instancetype)shared
78{
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];
83 });
84 return instance;
85}
86
87- (instancetype)init
88{
89 self = [super init];
90 if(self) {
91 [[NSNotificationCenter defaultCenter]
92 addObserver:self
93 selector:@selector(on_keyboard_will_show:)
94 name:UIKeyboardWillShowNotification
95 object:nil];
96 }
97 return self;
98}
99
100- (UIResponder*)current_first_responder
101{
102 first_responder = nil;
103 [[UIApplication sharedApplication]
104 sendAction:@selector(wesnoth_capture_first_responder:)
105 to:nil
106 from:nil
107 forEvent:nil];
108 return first_responder;
109}
110
111- (UIToolbar*)dismiss_toolbar
112{
113 static UIToolbar* toolbar = nil;
114 if(!toolbar) {
115 toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, 44.0)];
116 UIBarButtonItem* flex = [[UIBarButtonItem alloc]
117 initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
118 target:nil
119 action:nil];
120 UIBarButtonItem* dismiss = [[UIBarButtonItem alloc]
121 initWithTitle:NSLocalizedString(@"Dismiss", @"Dismiss keyboard button")
122 style:UIBarButtonItemStyleDone
123 target:self
124 action:@selector(on_dismiss_keyboard:)];
125 [toolbar setItems:@[flex, dismiss]];
126 [toolbar sizeToFit];
127 }
128 return toolbar;
129}
130
131- (void)on_keyboard_will_show:(NSNotification*)notification
132{
133 (void)notification;
134 [self attach_toolbar_to_first_responder];
135}
136
137- (void)attach_toolbar_to_first_responder
138{
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];
146 }
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];
153 }
154 }
155}
156
157- (void)on_dismiss_keyboard:(id)sender
158{
159 (void)sender;
160 SDL_StopTextInput(video::get_window());
161}
162
163@end
164
165namespace desktop {
166namespace apple {
167 CGFloat get_scale_factor(int display_index) {
168 NSArray<UIScreen*>* screens = [UIScreen screens];
169 if(display_index >= 0 && static_cast<NSUInteger>(display_index) < screens.count) {
170 return screens[display_index].scale;
171 }
172 return UIScreen.mainScreen.scale;
173 }
174
176 {
177 if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
178 [wesnoth_keyboard_toolbar shared];
179 }
180 }
181} // end namespace apple
182} // end namespace desktop
183
184#endif //end TARGET_OS_OSX / TARGET_OS_IOS
185#endif //end __APPLE__
void install_keyboard_dismiss_toolbar()
CGFloat get_scale_factor(int display_index)
struct utils::detail::formula_initer init
SDL_Window * get_window()
Definition video.cpp:712