godot_window_delegate.mm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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->mouse_exit_window(window_id);
  60. ds->window_destroy(window_id);
  61. }
  62. - (void)windowWillEnterFullScreen:(NSNotification *)notification {
  63. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  64. if (!ds || !ds->has_window(window_id)) {
  65. return;
  66. }
  67. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  68. wd.fs_transition = true;
  69. // Temporary disable borderless and transparent state.
  70. if ([wd.window_object styleMask] == NSWindowStyleMaskBorderless) {
  71. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable];
  72. }
  73. if (wd.layered_window) {
  74. ds->set_window_per_pixel_transparency_enabled(false, window_id);
  75. }
  76. }
  77. - (void)windowDidFailToEnterFullScreen:(NSWindow *)window {
  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.fs_transition = false;
  84. }
  85. - (void)windowDidEnterFullScreen:(NSNotification *)notification {
  86. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  87. if (!ds || !ds->has_window(window_id)) {
  88. return;
  89. }
  90. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  91. wd.fullscreen = true;
  92. wd.fs_transition = false;
  93. // Reset window size limits.
  94. [wd.window_object setContentMinSize:NSMakeSize(0, 0)];
  95. [wd.window_object setContentMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
  96. // Reset custom window buttons.
  97. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  98. ds->window_set_custom_window_buttons(wd, false);
  99. }
  100. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  101. // Force window resize event and redraw.
  102. [self windowDidResize:notification];
  103. }
  104. - (void)windowWillExitFullScreen:(NSNotification *)notification {
  105. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  106. if (!ds || !ds->has_window(window_id)) {
  107. return;
  108. }
  109. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  110. wd.fs_transition = true;
  111. // Restore custom window buttons.
  112. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  113. ds->window_set_custom_window_buttons(wd, true);
  114. }
  115. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  116. }
  117. - (void)windowDidFailToExitFullScreen:(NSWindow *)window {
  118. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  119. if (!ds || !ds->has_window(window_id)) {
  120. return;
  121. }
  122. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  123. wd.fs_transition = false;
  124. if ([wd.window_object styleMask] & NSWindowStyleMaskFullSizeContentView) {
  125. ds->window_set_custom_window_buttons(wd, false);
  126. }
  127. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_TITLEBAR_CHANGE);
  128. }
  129. - (void)windowDidExitFullScreen:(NSNotification *)notification {
  130. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  131. if (!ds || !ds->has_window(window_id)) {
  132. return;
  133. }
  134. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  135. if (wd.exclusive_fullscreen) {
  136. ds->update_presentation_mode();
  137. }
  138. wd.fullscreen = false;
  139. wd.exclusive_fullscreen = false;
  140. wd.fs_transition = false;
  141. // Set window size limits.
  142. const float scale = ds->screen_get_max_scale();
  143. if (wd.min_size != Size2i()) {
  144. Size2i size = wd.min_size / scale;
  145. [wd.window_object setContentMinSize:NSMakeSize(size.x, size.y)];
  146. }
  147. if (wd.max_size != Size2i()) {
  148. Size2i size = wd.max_size / scale;
  149. [wd.window_object setContentMaxSize:NSMakeSize(size.x, size.y)];
  150. }
  151. // Restore borderless, transparent and resizability state.
  152. if (wd.borderless || wd.layered_window) {
  153. [wd.window_object setStyleMask:NSWindowStyleMaskBorderless];
  154. } else {
  155. [wd.window_object setStyleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | (wd.extend_to_title ? NSWindowStyleMaskFullSizeContentView : 0) | (wd.resize_disabled ? 0 : NSWindowStyleMaskResizable)];
  156. }
  157. if (wd.layered_window) {
  158. ds->set_window_per_pixel_transparency_enabled(true, window_id);
  159. }
  160. // Restore on-top state.
  161. if (wd.on_top) {
  162. [wd.window_object setLevel:NSFloatingWindowLevel];
  163. }
  164. // Force window resize event and redraw.
  165. [self windowDidResize:notification];
  166. }
  167. - (void)windowDidChangeBackingProperties:(NSNotification *)notification {
  168. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  169. if (!ds || !ds->has_window(window_id)) {
  170. return;
  171. }
  172. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  173. CGFloat new_scale_factor = [wd.window_object backingScaleFactor];
  174. CGFloat old_scale_factor = [[[notification userInfo] objectForKey:@"NSBackingPropertyOldScaleFactorKey"] doubleValue];
  175. if (new_scale_factor != old_scale_factor) {
  176. // Set new display scale and window size.
  177. const float scale = ds->screen_get_max_scale();
  178. const NSRect content_rect = [wd.window_view frame];
  179. wd.size.width = content_rect.size.width * scale;
  180. wd.size.height = content_rect.size.height * scale;
  181. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_DPI_CHANGE);
  182. CALayer *layer = [wd.window_view layer];
  183. if (layer) {
  184. layer.contentsScale = scale;
  185. }
  186. //Force window resize event
  187. [self windowDidResize:notification];
  188. }
  189. }
  190. - (void)windowWillStartLiveResize:(NSNotification *)notification {
  191. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  192. if (ds && ds->has_window(window_id)) {
  193. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  194. wd.last_frame_rect = [wd.window_object frame];
  195. ds->set_is_resizing(true);
  196. }
  197. }
  198. - (void)windowDidEndLiveResize:(NSNotification *)notification {
  199. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  200. if (ds) {
  201. ds->set_is_resizing(false);
  202. }
  203. }
  204. - (void)windowDidResize:(NSNotification *)notification {
  205. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  206. if (!ds || !ds->has_window(window_id)) {
  207. return;
  208. }
  209. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  210. const NSRect content_rect = [wd.window_view frame];
  211. const float scale = ds->screen_get_max_scale();
  212. wd.size.width = content_rect.size.width * scale;
  213. wd.size.height = content_rect.size.height * scale;
  214. CALayer *layer = [wd.window_view layer];
  215. if (layer) {
  216. layer.contentsScale = scale;
  217. }
  218. ds->window_resize(window_id, wd.size.width, wd.size.height);
  219. if (wd.rect_changed_callback.is_valid()) {
  220. wd.rect_changed_callback.call(Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id)));
  221. }
  222. }
  223. - (void)windowDidChangeScreen:(NSNotification *)notification {
  224. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  225. if (!ds || !ds->has_window(window_id)) {
  226. return;
  227. }
  228. ds->reparent_check(window_id);
  229. }
  230. - (void)windowDidMove:(NSNotification *)notification {
  231. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  232. if (!ds || !ds->has_window(window_id)) {
  233. return;
  234. }
  235. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  236. ds->release_pressed_events();
  237. if (wd.rect_changed_callback.is_valid()) {
  238. wd.rect_changed_callback.call(Rect2i(ds->window_get_position(window_id), ds->window_get_size(window_id)));
  239. }
  240. }
  241. - (void)windowDidBecomeKey:(NSNotification *)notification {
  242. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  243. if (!ds || !ds->has_window(window_id)) {
  244. return;
  245. }
  246. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  247. if (wd.window_button_view) {
  248. [(GodotButtonView *)wd.window_button_view displayButtons];
  249. }
  250. if (ds->mouse_get_mode() == DisplayServer::MOUSE_MODE_CAPTURED) {
  251. const NSRect content_rect = [wd.window_view frame];
  252. NSRect point_in_window_rect = NSMakeRect(content_rect.size.width / 2, content_rect.size.height / 2, 0, 0);
  253. NSPoint point_on_screen = [[wd.window_view window] convertRectToScreen:point_in_window_rect].origin;
  254. CGPoint mouse_warp_pos = { point_on_screen.x, CGDisplayBounds(CGMainDisplayID()).size.height - point_on_screen.y };
  255. CGWarpMouseCursorPosition(mouse_warp_pos);
  256. } else {
  257. ds->update_mouse_pos(wd, [wd.window_object mouseLocationOutsideOfEventStream]);
  258. }
  259. [self windowDidResize:notification]; // Emit resize event, to ensure content is resized if the window was resized while it was hidden.
  260. wd.focused = true;
  261. ds->set_last_focused_window(window_id);
  262. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
  263. }
  264. - (void)windowDidResignKey:(NSNotification *)notification {
  265. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  266. if (!ds || !ds->has_window(window_id)) {
  267. return;
  268. }
  269. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  270. if (wd.window_button_view) {
  271. [(GodotButtonView *)wd.window_button_view displayButtons];
  272. }
  273. wd.focused = false;
  274. ds->release_pressed_events();
  275. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
  276. }
  277. - (void)windowDidMiniaturize:(NSNotification *)notification {
  278. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  279. if (!ds || !ds->has_window(window_id)) {
  280. return;
  281. }
  282. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  283. wd.focused = false;
  284. ds->release_pressed_events();
  285. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_OUT);
  286. }
  287. - (void)windowDidDeminiaturize:(NSNotification *)notification {
  288. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  289. if (!ds || !ds->has_window(window_id)) {
  290. return;
  291. }
  292. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  293. wd.is_visible = ([wd.window_object occlusionState] & NSWindowOcclusionStateVisible) && [wd.window_object isVisible];
  294. if ([wd.window_object isKeyWindow]) {
  295. wd.focused = true;
  296. ds->set_last_focused_window(window_id);
  297. ds->send_window_event(wd, DisplayServerMacOS::WINDOW_EVENT_FOCUS_IN);
  298. }
  299. }
  300. - (void)windowDidChangeOcclusionState:(NSNotification *)notification {
  301. DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton();
  302. if (!ds || !ds->has_window(window_id)) {
  303. return;
  304. }
  305. DisplayServerMacOS::WindowData &wd = ds->get_window(window_id);
  306. wd.is_visible = ([wd.window_object occlusionState] & NSWindowOcclusionStateVisible) && [wd.window_object isVisible];
  307. }
  308. @end