@@ -707,10 +707,7 @@ static void Cocoa_UpdateClipCursor(SDL_Window *window)
707707static void Cocoa_SetKeyboardFocus (SDL_Window *window, bool set_active_focus)
708708{
709709 SDL_Window *toplevel = GetParentToplevelWindow (window);
710- SDL_CocoaWindowData *toplevel_data;
711-
712- toplevel_data = (__bridge SDL_CocoaWindowData *)toplevel->internal ;
713- toplevel_data.keyboard_focus = window;
710+ toplevel->keyboard_focus = window;
714711
715712 if (set_active_focus && !window->is_hiding && !window->is_destroying ) {
716713 SDL_SetKeyboardFocus (window);
@@ -1252,7 +1249,7 @@ - (void)windowDidBecomeKey:(NSNotification *)aNotification
12521249
12531250 // We're going to get keyboard events, since we're key.
12541251 // This needs to be done before restoring the relative mouse mode.
1255- Cocoa_SetKeyboardFocus (_data. keyboard_focus ? _data. keyboard_focus : window, true );
1252+ Cocoa_SetKeyboardFocus (window-> keyboard_focus ? window-> keyboard_focus : window, true );
12561253
12571254 // If we just gained focus we need the updated mouse position
12581255 if (!(window->flags & SDL_WINDOW_MOUSE_RELATIVE_MODE)) {
@@ -2244,7 +2241,9 @@ then immediately ordering out (removing) the window does work. */
22442241 [nswindow setIgnoresMouseEvents: YES ];
22452242 [nswindow setAcceptsMouseMovedEvents: NO ];
22462243 } else if ((window->flags & SDL_WINDOW_POPUP_MENU) && !(window->flags & SDL_WINDOW_HIDDEN)) {
2247- Cocoa_SetKeyboardFocus (window, window->parent == SDL_GetKeyboardFocus ());
2244+ if (!(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
2245+ Cocoa_SetKeyboardFocus (window, true );
2246+ }
22482247 Cocoa_UpdateMouseFocus ();
22492248 }
22502249 }
@@ -2334,7 +2333,7 @@ bool Cocoa_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_Properti
23342333 rect.origin .y -= screenRect.origin .y ;
23352334
23362335 // Constrain the popup
2337- if (SDL_WINDOW_IS_POPUP (window)) {
2336+ if (SDL_WINDOW_IS_POPUP (window) && window-> constrain_popup ) {
23382337 if (rect.origin .x + rect.size .width > screenRect.origin .x + screenRect.size .width ) {
23392338 rect.origin .x -= (rect.origin .x + rect.size .width ) - (screenRect.origin .x + screenRect.size .width );
23402339 }
@@ -2490,7 +2489,7 @@ bool Cocoa_SetWindowPosition(SDL_VideoDevice *_this, SDL_Window *window)
24902489 ConvertNSRect (&rect);
24912490
24922491 // Position and constrain the popup
2493- if (SDL_WINDOW_IS_POPUP (window)) {
2492+ if (SDL_WINDOW_IS_POPUP (window) && window-> constrain_popup ) {
24942493 NSRect screenRect = [ScreenForRect (&rect) frame ];
24952494
24962495 if (rect.origin .x + rect.size .width > screenRect.origin .x + screenRect.size .width ) {
@@ -2631,7 +2630,9 @@ void Cocoa_ShowWindow(SDL_VideoDevice *_this, SDL_Window *window)
26312630 }
26322631 }
26332632 } else if (window->flags & SDL_WINDOW_POPUP_MENU) {
2634- Cocoa_SetKeyboardFocus (window, window->parent == SDL_GetKeyboardFocus ());
2633+ if (!(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
2634+ Cocoa_SetKeyboardFocus (window, true );
2635+ }
26352636 Cocoa_UpdateMouseFocus ();
26362637 }
26372638 }
@@ -2665,20 +2666,9 @@ void Cocoa_HideWindow(SDL_VideoDevice *_this, SDL_Window *window)
26652666 Cocoa_SetWindowModal (_this, window, false );
26662667
26672668 // Transfer keyboard focus back to the parent when closing a popup menu
2668- if (window->flags & SDL_WINDOW_POPUP_MENU) {
2669- SDL_Window *new_focus = window->parent ;
2670- bool set_focus = window == SDL_GetKeyboardFocus ();
2671-
2672- // Find the highest level window, up to the toplevel parent, that isn't being hidden or destroyed.
2673- while (SDL_WINDOW_IS_POPUP (new_focus) && (new_focus->is_hiding || new_focus->is_destroying )) {
2674- new_focus = new_focus->parent ;
2675-
2676- // If some window in the chain currently had focus, set it to the new lowest-level window.
2677- if (!set_focus) {
2678- set_focus = new_focus == SDL_GetKeyboardFocus ();
2679- }
2680- }
2681-
2669+ if ((window->flags & SDL_WINDOW_POPUP_MENU) && !(window->flags & SDL_WINDOW_NOT_FOCUSABLE)) {
2670+ SDL_Window *new_focus;
2671+ const bool set_focus = SDL_ShouldRelinquishPopupFocus (window, &new_focus);
26822672 Cocoa_SetKeyboardFocus (new_focus, set_focus);
26832673 Cocoa_UpdateMouseFocus ();
26842674 } else if (window->parent && waskey) {
@@ -3105,20 +3095,19 @@ void Cocoa_DestroyWindow(SDL_VideoDevice *_this, SDL_Window *window)
31053095
31063096#endif // SDL_VIDEO_OPENGL
31073097 SDL_Window *topmost = GetParentToplevelWindow (window);
3108- SDL_CocoaWindowData *topmost_data = (__bridge SDL_CocoaWindowData *)topmost->internal ;
31093098
31103099 /* Reset the input focus of the root window if this window is still set as keyboard focus.
31113100 * SDL_DestroyWindow will have already taken care of reassigning focus if this is the SDL
31123101 * keyboard focus, this ensures that an inactive window with this window set as input focus
31133102 * does not try to reference it the next time it gains focus.
31143103 */
3115- if (topmost_data. keyboard_focus == window) {
3104+ if (topmost-> keyboard_focus == window) {
31163105 SDL_Window *new_focus = window;
31173106 while (SDL_WINDOW_IS_POPUP (new_focus) && (new_focus->is_hiding || new_focus->is_destroying )) {
31183107 new_focus = new_focus->parent ;
31193108 }
31203109
3121- topmost_data. keyboard_focus = new_focus;
3110+ topmost-> keyboard_focus = new_focus;
31223111 }
31233112
31243113 if ([data.listener isInFullscreenSpace ]) {
@@ -3283,6 +3272,20 @@ bool Cocoa_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOper
32833272
32843273bool Cocoa_SetWindowFocusable (SDL_VideoDevice *_this, SDL_Window *window, bool focusable)
32853274{
3275+ if (window->flags & SDL_WINDOW_POPUP_MENU) {
3276+ if (!(window->flags & SDL_WINDOW_HIDDEN)) {
3277+ if (!focusable && (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
3278+ SDL_Window *new_focus;
3279+ const bool set_focus = SDL_ShouldRelinquishPopupFocus (window, &new_focus);
3280+ Cocoa_SetKeyboardFocus (new_focus, set_focus);
3281+ } else if (focusable) {
3282+ if (SDL_ShouldFocusPopup (window)) {
3283+ Cocoa_SetKeyboardFocus (window, true );
3284+ }
3285+ }
3286+ }
3287+ }
3288+
32863289 return true ; // just succeed, the real work is done elsewhere.
32873290}
32883291
0 commit comments