Add love.window.getSafeArea (resolves issue #1437). Returns a rectangle (x, y, w, h) inside the window which is known to be unobstructed by a system title bar, the iPhone X notch, etc. Useful for making sure UI elements can be seen by the user.

Currently it only has a proper implementation on iOS and will return the window's full size otherwise.
This commit is contained in:
Alex Szpakowski
2018-12-26 00:34:14 -04:00
parent 1c853cbcc3
commit d41aca836e
6 changed files with 81 additions and 3 deletions
+41 -3
View File
@@ -31,6 +31,8 @@
#include <vector>
#include <SDL_events.h>
#include <SDL_video.h>
#include <SDL_syswm.h>
static NSArray *getLovesInDocuments();
static bool deleteFileInDocuments(NSString *filename);
@@ -362,9 +364,45 @@ void setAudioMixWithOthers(bool mixEnabled)
bool hasBackgroundMusic()
{
if ([[AVAudioSession sharedInstance] respondsToSelector:@selector(secondaryAudioShouldBeSilencedHint)])
return [[AVAudioSession sharedInstance] secondaryAudioShouldBeSilencedHint];
return false;
@autoreleasepool
{
AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session respondsToSelector:@selector(secondaryAudioShouldBeSilencedHint)])
return session.secondaryAudioShouldBeSilencedHint;
return false;
}
}
Rect getSafeArea(SDL_Window *window)
{
@autoreleasepool
{
Rect rect = {};
SDL_GetWindowSize(window, &rect.w, &rect.h);
SDL_SysWMinfo info = {};
SDL_VERSION(&info.version);
if (SDL_GetWindowWMInfo(window, &info))
{
UIView *view = info.info.uikit.window.rootViewController.view;
if (@available(iOS 11.0, tvOS 11.0, *))
{
UIEdgeInsets insets = view.safeAreaInsets;
rect.x += insets.left;
rect.w -= insets.left;
rect.w -= insets.right;
rect.y += insets.top;
rect.h -= insets.top;
rect.h -= insets.bottom;
}
}
return rect;
}
}
} // ios