Debian Codegraph @

platform/macos/godot_window_delegate.mm

raw
1 /**************************************************************************/
2 /* godot_window_delegate.mm */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #import "godot_window_delegate.h"
32
33 #import "display_server_macos.h"
34 #import "godot_button_view.h"
35 #import "godot_content_view.h"
36 #import "godot_window.h"
37
38 @implementation GodotWindowDelegate {
39 DisplayServer::WindowID window_id;
40 DisplayServerMacOS *ds;
41 }
42
43 - (instancetype)initWithDisplayServer:(DisplayServerMacOS *)p_ds {
44 if (self = [super init]) {
45 ds = p_ds;
46 window_id = DisplayServerMacOS::INVALID_WINDOW_ID;
47 }
48 return self;
49 }
50
51 - (void)setWindowID:(DisplayServer::WindowID)wid {
52 window_id = wid;
53 }
54
55 - (BOOL)windowShouldClose:(id)sender {
56 if (!ds->has_window(window_id)) {
57 return YES;
58 }
59
60 ds->send_window_event(ds->get_window(window_id), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
61 return NO;
62 }
63
64 - (void)windowWillClose:(NSNotification *)notification {
65 if (!ds->has_window(window_id)) {
66 return;
67 }
68
69 ds->popup_close(window_id);
70
71 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
72 while (wd.transient_children.size()) {
73 ds->window_set_transient(*wd.transient_children.begin(), DisplayServerMacOS::INVALID_WINDOW_ID);
74 }
75
76 if (wd.transient_parent != DisplayServerMacOS::INVALID_WINDOW_ID) {
77 ds->window_set_transient(window_id, DisplayServerMacOS::INVALID_WINDOW_ID);
78 }
79
80 ds->mouse_exit_window(window_id);
81 ds->window_destroy(window_id);
82 }
83
84 - (void)windowWillEnterFullScreen:(NSNotification *)notification {
85 if (!ds->has_window(window_id)) {
86 return;
87 }
88
89 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
90 wd.fs_transition = true;
91
92 // Temporary disable borderless and transparent state.
93 if ([wd.window_object styleMask] == NSWindowStyleMaskBorderless) {
94 [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable];
95 }
96 if (wd.layered_window) {
97 ds->set_window_per_pixel_transparency_enabled(false, window_id);
98 }
99 }
100
101 - (void)windowDidFailToEnterFullScreen:(NSWindow *)window {
102 if (!ds->has_window(window_id)) {
103 return;
104 }
105
106 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
107 wd.fs_transition = false;
108 }
109
110 - (void)windowDidEnterFullScreen:(NSNotification *)notification {
111 if (!ds->has_window(window_id)) {
112 return;
113 }
114
115 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
116 wd.fullscreen = true;
117 wd.fs_transition = false;
118
119 // Reset window size limits.
120 [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
121 [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
122
123 // Reset custom window buttons.
124 if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
125 ds->window_set_custom_window_buttons(wd, false);
126 }
127
128 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
129
130 // Force window resize event and redraw.
131 [self windowDidResize:notification];
132 }
133
134 - (void)windowWillExitFullScreen:(NSNotification *)notification {
135 if (!ds->has_window(window_id)) {
136 return;
137 }
138
139 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
140 wd.fs_transition = true;
141
142 // Restore custom window buttons.
143 if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
144 ds->window_set_custom_window_buttons(wd, true);
145 }
146
147 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
148 }
149
150 - (void)windowDidFailToExitFullScreen:(NSWindow *)window {
151 if (!ds->has_window(window_id)) {
152 return;
153 }
154
155 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
156 wd.fs_transition = false;
157
158 if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
159 ds->window_set_custom_window_buttons(wd, false);
160 }
161
162 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
163 }
164
165 - (void)windowDidExitFullScreen:(NSNotification *)notification {
166 if (!ds->has_window(window_id)) {
167 return;
168 }
169
170 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
171 if (wd.exclusive_fullscreen) {
172 ds->update_presentation_mode();
173 }
174
175 wd.fullscreen = false;
176 wd.exclusive_fullscreen = false;
177 wd.fs_transition = false;
178
179 // Set window size limits.
180 const float scale = ds->screen_get_max_scale();
181 if (wd.min_size != Size2i()) {
182 Size2i size = wd.min_size / scale;
183 [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
184 }
185 if (wd.max_size != Size2i()) {
186 Size2i size = wd.max_size / scale;
187 [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
188 }
189
190 // Restore borderless, transparent and resizability state.
191 if (wd.borderless) {
192 [wd.window_object setStyleMask:NSWindowStyleMaskBorderless];
193 [wd.window_object setHasShadow:NO];
194 } else {
195 [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
196 [wd.window_object setHasShadow:YES];
197 }
198 if (wd.layered_window) {
199 ds->set_window_per_pixel_transparency_enabled(true, window_id);
200 }
201
202 // Restore on-top state.
203 if (ds->is_always_on_top_recursive(window_id)) {
204 [wd.window_object setLevel:NSFloatingWindowLevel];
205 }
206
207 // Force window resize event and redraw.
208 [self windowDidResize:notification];
209 }
210
211 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame {
212 if (ds->has_window(window_id)) {
213 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
214 if (!wd.borderless && !(NSEqualRects([wd.window_object frame], [[wd.window_object screen] visibleFrame]))) {
215 wd.pre_zoom_rect = [wd.window_object frame];
216 }
217 }
218 return YES;
219 }
220
221 - (void)windowDidChangeBackingProperties:(NSNotification *)notification {
222 if (!ds->has_window(window_id)) {
223 return;
224 }
225
226 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
227
228 CGFloat new_scale_factor = [wd.window_object backingScaleFactor];
229 CGFloat old_scale_factor = [[[notification userInfo] objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue];
230
231 if (new_scale_factor != old_scale_factor) {
232 // Set new display scale and window size.
233 const float scale = ds->screen_get_max_scale();
234 const NSRect content_rect = [wd.window_view frame];
235
236 wd.size.width = content_rect.size.width * scale;
237 wd.size.height = content_rect.size.height * scale;
238
239 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_DPI_CHANGE);
240
241 CALayer *layer = [wd.window_view layer];
242 if (layer) {
243 layer.contentsScale = scale;
244 }
245
246 //Force window resize event
247 [self windowDidResize:notification];
248 }
249 }
250
251 - (void)windowWillStartLiveResize:(NSNotification *)notification {
252 if (ds->has_window(window_id)) {
253 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
254 wd.last_frame_rect = [wd.window_object frame];
255 ds->set_is_resizing(true);
256 }
257 }
258
259 - (void)windowDidEndLiveResize:(NSNotification *)notification {
260 ds->set_is_resizing(false);
261 }
262
263 - (void)windowDidResize:(NSNotification *)notification {
264 if (!ds->has_window(window_id)) {
265 return;
266 }
267
268 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
269 const NSRect content_rect = [wd.window_view frame];
270 const float scale = ds->screen_get_max_scale();
271 wd.size.width = content_rect.size.width * scale;
272 wd.size.height = content_rect.size.height * scale;
273
274 CALayer *layer = [wd.window_view layer];
275 if (layer) {
276 layer.contentsScale = scale;
277 }
278
279 ds->window_resize(window_id, wd.size.width, wd.size.height);
280
281 if (wd.rect_changed_callback.is_valid()) {
282 wd.rect_changed_callback.call(Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id)));
283 }
284 }
285
286 - (void)windowDidChangeScreen:(NSNotification *)notification {
287 if (!ds->has_window(window_id)) {
288 return;
289 }
290
291 ds->reparent_check(window_id);
292 }
293
294 - (void)windowDidMove:(NSNotification *)notification {
295 if (!ds->has_window(window_id)) {
296 return;
297 }
298
299 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
300 ds->release_pressed_events();
301
302 if (wd.rect_changed_callback.is_valid()) {
303 wd.rect_changed_callback.call(Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id)));
304 }
305 }
306
307 - (void)windowDidBecomeKey:(NSNotification *)notification {
308 if (!ds->has_window(window_id)) {
309 return;
310 }
311
312 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
313
314 if (wd.window_button_view) {
315 [wd.window_button_view displayButtons];
316 }
317
318 if (ds->mouse_get_mode() == DisplayServer::MOUSE_MODE_CAPTURED) {
319 const NSRect content_rect = [wd.window_view frame];
320 NSRect point_in_window_rect = NSMakeRect(content_rect.size.width / 2, content_rect.size.height / 2, 0, 0);
321 NSPoint point_on_screen = [[wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
322 CGPoint mouse_warp_pos = { point_on_screen.x, CGDisplayBounds(CGMainDisplayID()).size.height - point_on_screen.y };
323 CGWarpMouseCursorPosition(mouse_warp_pos);
324 } else {
325 ds->update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
326 }
327
328 [self windowDidResize:notification]; // Emit resize event, to ensure content is resized if the window was resized while it was hidden.
329
330 wd.focused = true;
331 ds->set_last_focused_window(window_id);
332 #ifdef ACCESSKIT_ENABLED
333 ds->accessibility_set_window_focused(window_id, true);
334 #endif
335 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
336 }
337
338 - (void)windowDidResignKey:(NSNotification *)notification {
339 if (!ds->has_window(window_id)) {
340 return;
341 }
342
343 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
344
345 if (wd.window_button_view) {
346 [wd.window_button_view displayButtons];
347 }
348
349 wd.focused = false;
350 ds->release_pressed_events();
351 #ifdef ACCESSKIT_ENABLED
352 ds->accessibility_set_window_focused(window_id, false);
353 #endif
354 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
355 }
356
357 - (void)windowDidMiniaturize:(NSNotification *)notification {
358 if (!ds->has_window(window_id)) {
359 return;
360 }
361
362 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
363
364 wd.focused = false;
365 ds->release_pressed_events();
366 #ifdef ACCESSKIT_ENABLED
367 ds->accessibility_set_window_focused(window_id, false);
368 #endif
369 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
370 }
371
372 - (void)windowDidDeminiaturize:(NSNotification *)notification {
373 if (!ds->has_window(window_id)) {
374 return;
375 }
376
377 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
378 wd.is_visible = ([wd.window_object occlusionState] & NSWindowOcclusionStateVisible) && [wd.window_object isVisible];
379 if ([wd.window_object isKeyWindow]) {
380 wd.focused = true;
381 ds->set_last_focused_window(window_id);
382 #ifdef ACCESSKIT_ENABLED
383 ds->accessibility_set_window_focused(window_id, true);
384 #endif
385 ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
386 }
387 }
388
389 - (void)windowDidChangeOcclusionState:(NSNotification *)notification {
390 if (!ds->has_window(window_id)) {
391 return;
392 }
393 DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
394 wd.is_visible = ([wd.window_object occlusionState] & NSWindowOcclusionStateVisible) && [wd.window_object isVisible];
395 }
396
397 @end