Fixes wrong map view dimension on CarPlay. (#3890)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Stephan Partzsch
2025-10-16 15:20:10 +02:00
committed by GitHub
parent c2dd538f49
commit d8080ee5dc
4 changed files with 46 additions and 12 deletions

View File

@@ -63,6 +63,7 @@ MLN_DARWIN_OBJC_HEADERS = [
"src/MLNCustomStyleLayer.h",
"src/MLNCustomDrawableStyleLayer.h",
"src/MLNDefaultStyle.h",
"src/MLNDisplayUtils.h",
"src/MLNDistanceFormatter.h",
"src/MLNFeature.h",
"src/MLNForegroundStyleLayer.h",
@@ -175,6 +176,7 @@ MLN_DARWIN_PUBLIC_OBJCPP_SOURCE = [
"src/MLNComputedShapeSource.mm",
"src/MLNCustomStyleLayer.mm",
"src/MLNDefaultStyle.mm",
"src/MLNDisplayUtils.mm",
"src/MLNFeature.mm",
"src/MLNForegroundStyleLayer.mm",
"src/MLNFoundation.mm",

View File

@@ -0,0 +1,10 @@
#import <Foundation/Foundation.h>
#import <TargetConditionals.h>
#if TARGET_OS_IPHONE || TARGET_OS_TV
#import <UIKit/UIKit.h>
#else
#import <AppKit/AppKit.h>
#endif
FOUNDATION_EXTERN CGFloat MLNEffectiveScaleFactorForView(id viewOrNil);

View File

@@ -0,0 +1,27 @@
#import "MLNDisplayUtils.h"
CGFloat MLNEffectiveScaleFactorForView(id viewOrNil) {
#if TARGET_OS_IPHONE || TARGET_OS_TV
UIView *view = (UIView *)viewOrNil;
UIScreen *screen = view.window.screen ?: [UIScreen mainScreen];
CGFloat logicalToPixelRatio = screen.nativeBounds.size.width / screen.bounds.size.width;
if (logicalToPixelRatio > 0.0) {
return logicalToPixelRatio;
}
return [screen respondsToSelector:@selector(nativeScale)] ? screen.nativeScale : screen.scale;
#elif TARGET_OS_OSX
NSView *view = (NSView *)viewOrNil;
NSScreen *screen = view ? (view.window.screen ?: [NSScreen mainScreen]) : [NSScreen mainScreen];
return screen ? screen.backingScaleFactor : 1.0;
#else
return 1.0;
#endif
}

View File

@@ -1,3 +1,4 @@
#import "MLNDisplayUtils.h"
#import "MLNFoundation_Private.h"
#import "MLNLoggingConfiguration_Private.h"
#import "MLNMapView+Metal.h"
@@ -32,14 +33,6 @@
@end
namespace {
CGFloat contentScaleFactor() {
return [UIScreen instancesRespondToSelector:@selector(nativeScale)]
? [[UIScreen mainScreen] nativeScale]
: [[UIScreen mainScreen] scale];
}
} // namespace
class MLNMapViewMetalRenderableResource final : public mbgl::mtl::RenderableResource {
public:
MLNMapViewMetalRenderableResource(MLNMapViewMetalImpl& backend_)
@@ -164,11 +157,12 @@ void MLNMapViewMetalImpl::createView() {
}
id<MTLDevice> device = (__bridge id<MTLDevice>)resource.getBackend().getDevice().get();
const auto scaleFactor = MLNEffectiveScaleFactorForView(resource.mtlView);
resource.mtlView = [[MTKView alloc] initWithFrame:mapView.bounds device:device];
resource.mtlView.delegate = resource.delegate;
resource.mtlView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
resource.mtlView.contentScaleFactor = contentScaleFactor();
resource.mtlView.contentScaleFactor = scaleFactor;
resource.mtlView.contentMode = UIViewContentModeCenter;
resource.mtlView.colorPixelFormat = MTLPixelFormatBGRA8Unorm;
resource.mtlView.depthStencilPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
@@ -222,9 +216,10 @@ UIImage* MLNMapViewMetalImpl::snapshot() {
}
void MLNMapViewMetalImpl::layoutChanged() {
const auto scaleFactor = contentScaleFactor();
size = { static_cast<uint32_t>(mapView.bounds.size.width * scaleFactor),
static_cast<uint32_t>(mapView.bounds.size.height * scaleFactor) };
const auto scaleFactor = MLNEffectiveScaleFactorForView(mapView);
const auto screenSize = mapView.window.screen.nativeBounds.size;
size = { static_cast<uint32_t>(screenSize.width * scaleFactor),
static_cast<uint32_t>(screenSize.height * scaleFactor) };
}
MLNBackendResource* MLNMapViewMetalImpl::getObject() {