godot_window_delegate.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "godot_window_delegate.h"
  31. #include "display_server_macos.h"
  32. #include "godot_button_view.h"
  33. #include "godot_window.h"
  34. @implementation GodotWindowDelegate
  35. - (void)setWindowID:(DisplayServer::WindowID)wid {
  36. window_id = wid;
  37. }
  38. - (BOOL)windowShouldClose:(id)sender {
  39. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  40. if (!ds || !ds->has_window(window_id)) {
  41. return YES;
  42. }
  43. ds->send_window_event(ds->get_window(window_id), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
  44. return NO;
  45. }
  46. - (void)windowWillClose:(NSNotification *)notification {
  47. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  48. if (!ds || !ds->has_window(window_id)) {
  49. return;
  50. }
  51. ds->popup_close(window_id);
  52. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  53. while (wd.transient_children.size()) {
  54. ds->window_set_transient(*wd.transient_children.begin(), DisplayServerMacOS::INVALID_WINDOW_ID);
  55. }
  56. if (wd.transient_parent != DisplayServerMacOS::INVALID_WINDOW_ID) {
  57. ds->window_set_transient(window_id, DisplayServerMacOS::INVALID_WINDOW_ID);
  58. }
  59. ds->window_destroy(window_id);
  60. }
  61. - (void)windowWillEnterFullScreen:(NSNotification *)notification {
  62. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  63. if (!ds || !ds->has_window(window_id)) {
  64. return;
  65. }
  66. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  67. wd.fs_transition = true;
  68. }
  69. - (void)windowDidFailToEnterFullScreen:(NSWindow *)window {
  70. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  71. if (!ds || !ds->has_window(window_id)) {
  72. return;
  73. }
  74. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  75. wd.fs_transition = false;
  76. }
  77. - (void)windowDidEnterFullScreen:(NSNotification *)notification {
  78. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  79. if (!ds || !ds->has_window(window_id)) {
  80. return;
  81. }
  82. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  83. wd.fullscreen = true;
  84. wd.fs_transition = false;
  85. // Reset window size limits.
  86. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  87. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  88. // Reset custom window buttons.
  89. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  90. ds->window_set_custom_window_buttons(wd, false);
  91. }
  92. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  93. // Force window resize event and redraw.
  94. [self windowDidResize:notification];
  95. }
  96. - (void)windowWillExitFullScreen:(NSNotification *)notification {
  97. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  98. if (!ds || !ds->has_window(window_id)) {
  99. return;
  100. }
  101. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  102. wd.fs_transition = true;
  103. // Restore custom window buttons.
  104. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  105. ds->window_set_custom_window_buttons(wd, true);
  106. }
  107. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  108. }
  109. - (void)windowDidFailToExitFullScreen:(NSWindow *)window {
  110. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  111. if (!ds || !ds->has_window(window_id)) {
  112. return;
  113. }
  114. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  115. wd.fs_transition = false;
  116. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  117. ds->window_set_custom_window_buttons(wd, false);
  118. }
  119. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  120. }
  121. - (void)windowDidExitFullScreen:(NSNotification *)notification {
  122. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  123. if (!ds || !ds->has_window(window_id)) {
  124. return;
  125. }
  126. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  127. if (wd.exclusive_fullscreen) {
  128. [NSApp setPresentationOptions:NSApplicationPresentationDefault];
  129. }
  130. wd.fullscreen = false;
  131. wd.exclusive_fullscreen = false;
  132. wd.fs_transition = false;
  133. // Set window size limits.
  134. const float scale = ds->screen_get_max_scale();
  135. if (wd.min_size != Size2i()) {
  136. Size2i size = wd.min_size / scale;
  137. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  138. }
  139. if (wd.max_size != Size2i()) {
  140. Size2i size = wd.max_size / scale;
  141. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  142. }
  143. // Restore resizability state.
  144. if (wd.resize_disabled) {
  145. [wd.window_object setStyleMask:[wd.window_object styleMask] & ~NSWindowStyleMaskResizable];
  146. }
  147. // Restore on-top state.
  148. if (wd.on_top) {
  149. [wd.window_object setLevel:NSFloatingWindowLevel];
  150. }
  151. // Force window resize event and redraw.
  152. [self windowDidResize:notification];
  153. }
  154. - (void)windowDidChangeBackingProperties:(NSNotification *)notification {
  155. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  156. if (!ds || !ds->has_window(window_id)) {
  157. return;
  158. }
  159. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  160. CGFloat new_scale_factor = [wd.window_object backingScaleFactor];
  161. CGFloat old_scale_factor = [[[notification userInfo] objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue];
  162. if (new_scale_factor != old_scale_factor) {
  163. // Set new display scale and window size.
  164. const float scale = ds->screen_get_max_scale();
  165. const NSRect content_rect = [wd.window_view frame];
  166. wd.size.width = content_rect.size.width * scale;
  167. wd.size.height = content_rect.size.height * scale;
  168. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_DPI_CHANGE);
  169. CALayer *layer = [wd.window_view layer];
  170. if (layer) {
  171. layer.contentsScale = scale;
  172. }
  173. //Force window resize event
  174. [self windowDidResize:notification];
  175. }
  176. }
  177. - (void)windowWillStartLiveResize:(NSNotification *)notification {
  178. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  179. if (ds && ds->has_window(window_id)) {
  180. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  181. wd.last_frame_rect = [wd.window_object frame];
  182. ds->set_is_resizing(true);
  183. }
  184. }
  185. - (void)windowDidEndLiveResize:(NSNotification *)notification {
  186. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  187. if (ds) {
  188. ds->set_is_resizing(false);
  189. }
  190. }
  191. - (void)windowDidResize:(NSNotification *)notification {
  192. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  193. if (!ds || !ds->has_window(window_id)) {
  194. return;
  195. }
  196. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  197. const NSRect content_rect = [wd.window_view frame];
  198. const float scale = ds->screen_get_max_scale();
  199. wd.size.width = content_rect.size.width * scale;
  200. wd.size.height = content_rect.size.height * scale;
  201. CALayer *layer = [wd.window_view layer];
  202. if (layer) {
  203. layer.contentsScale = scale;
  204. }
  205. ds->window_resize(window_id, wd.size.width, wd.size.height);
  206. if (!wd.rect_changed_callback.is_null()) {
  207. Variant size = Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id));
  208. Variant *sizep = &size;
  209. Variant ret;
  210. Callable::CallError ce;
  211. wd.rect_changed_callback.callp((const Variant **)&sizep, 1, ret, ce);
  212. }
  213. }
  214. - (void)windowDidChangeScreen:(NSNotification *)notification {
  215. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  216. if (!ds || !ds->has_window(window_id)) {
  217. return;
  218. }
  219. ds->reparent_check(window_id);
  220. }
  221. - (void)windowDidMove:(NSNotification *)notification {
  222. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  223. if (!ds || !ds->has_window(window_id)) {
  224. return;
  225. }
  226. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  227. ds->release_pressed_events();
  228. if (!wd.rect_changed_callback.is_null()) {
  229. Variant size = Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id));
  230. Variant *sizep = &size;
  231. Variant ret;
  232. Callable::CallError ce;
  233. wd.rect_changed_callback.callp((const Variant **)&sizep, 1, ret, ce);
  234. }
  235. }
  236. - (void)windowDidBecomeKey:(NSNotification *)notification {
  237. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  238. if (!ds || !ds->has_window(window_id)) {
  239. return;
  240. }
  241. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  242. if (wd.window_button_view) {
  243. [(GodotButtonView *)wd.window_button_view displayButtons];
  244. }
  245. if (ds->mouse_get_mode() == DisplayServer::MOUSE_MODE_CAPTURED) {
  246. const NSRect content_rect = [wd.window_view frame];
  247. NSRect point_in_window_rect = NSMakeRect(content_rect.size.width / 2, content_rect.size.height / 2, 0, 0);
  248. NSPoint point_on_screen = [[wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
  249. CGPoint mouse_warp_pos = { point_on_screen.x, CGDisplayBounds(CGMainDisplayID()).size.height - point_on_screen.y };
  250. CGWarpMouseCursorPosition(mouse_warp_pos);
  251. } else {
  252. ds->update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
  253. }
  254. [self windowDidResize:notification]; // Emit resize event, to ensure content is resized if the window was resized while it was hidden.
  255. wd.focused = true;
  256. ds->set_last_focused_window(window_id);
  257. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
  258. }
  259. - (void)windowDidResignKey:(NSNotification *)notification {
  260. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  261. if (!ds || !ds->has_window(window_id)) {
  262. return;
  263. }
  264. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  265. if (wd.window_button_view) {
  266. [(GodotButtonView *)wd.window_button_view displayButtons];
  267. }
  268. wd.focused = false;
  269. ds->release_pressed_events();
  270. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
  271. }
  272. - (void)windowDidMiniaturize:(NSNotification *)notification {
  273. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  274. if (!ds || !ds->has_window(window_id)) {
  275. return;
  276. }
  277. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  278. wd.focused = false;
  279. ds->release_pressed_events();
  280. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
  281. }
  282. - (void)windowDidDeminiaturize:(NSNotification *)notification {
  283. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  284. if (!ds || !ds->has_window(window_id)) {
  285. return;
  286. }
  287. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  288. if ([wd.window_object isKeyWindow]) {
  289. wd.focused = true;
  290. ds->set_last_focused_window(window_id);
  291. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
  292. }
  293. }
  294. @end