1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/chrome/browser/ui/ui_util.h"
7 #import <UIKit/UIKit.h>
9 #include "base/ios/ios_util.h"
10 #include "base/logging.h"
11 #import "ios/chrome/browser/ui/uikit_ui_util.h"
12 #include "ui/gfx/ios/uikit_util.h"
15 UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
16 return idiom == UIUserInterfaceIdiomPad;
19 const CGFloat kPortraitWidth[INTERFACE_IDIOM_COUNT] = {
24 bool IsHighResScreen() {
25 return [[UIScreen mainScreen] scale] > 1.0;
29 UIInterfaceOrientation orient = GetInterfaceOrientation();
30 // If building with an SDK prior to iOS 8 don't worry about
31 // UIInterfaceOrientationUnknown because it wasn't defined.
32 #if !defined(__IPHONE_8_0) || __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
33 return UIInterfaceOrientationIsPortrait(orient);
35 return UIInterfaceOrientationIsPortrait(orient) ||
36 orient == UIInterfaceOrientationUnknown;
41 return UIInterfaceOrientationIsLandscape(GetInterfaceOrientation());
44 CGFloat CurrentScreenHeight() {
45 CGSize screenSize = [UIScreen mainScreen].bounds.size;
46 if (base::ios::IsRunningOnIOS8OrLater()) {
47 return screenSize.height;
49 return IsPortrait() ? screenSize.height : screenSize.width;
53 CGFloat CurrentScreenWidth() {
54 CGSize screenSize = [UIScreen mainScreen].bounds.size;
55 if (base::ios::IsRunningOnIOS8OrLater()) {
56 return screenSize.width;
58 return IsPortrait() ? screenSize.width : screenSize.height;
62 CGFloat StatusBarHeight() {
63 // TODO(justincohen): This is likely not correct, but right now iOS7 betas are
64 // doing strange things when handling calls that change the status bar height.
65 // For now, just return 20. crbug/264367
69 CGFloat AlignValueToPixel(CGFloat value) {
70 static CGFloat scale = [[UIScreen mainScreen] scale];
71 return floor(value * scale) / scale;
74 CGPoint AlignPointToPixel(CGPoint point) {
75 return CGPointMake(AlignValueToPixel(point.x), AlignValueToPixel(point.y));
78 CGRect AlignRectToPixel(CGRect rect) {
79 rect.origin = AlignPointToPixel(rect.origin);
83 CGRect AlignRectOriginAndSizeToPixels(CGRect rect) {
84 rect.origin = AlignPointToPixel(rect.origin);
85 rect.size = ui::AlignSizeToUpperPixel(rect.size);
89 CGRect CGRectCopyWithOrigin(CGRect rect, CGFloat x, CGFloat y) {
90 return CGRectMake(x, y, rect.size.width, rect.size.height);
93 CGRect CGRectMakeAlignedAndCenteredAt(CGFloat x, CGFloat y, CGFloat width) {
94 return AlignRectOriginAndSizeToPixels(
95 CGRectMake(x - width / 2.0, y - width / 2.0, width, width));
98 // Based on an original size and a target size applies the transformations.
99 void CalculateProjection(CGSize originalSize,
100 CGSize desiredTargetSize,
101 ProjectionMode projectionMode,
104 targetSize = desiredTargetSize;
105 projectTo = CGRectZero;
106 if (originalSize.height < 1 || originalSize.width < 1)
108 if (targetSize.height < 1 || targetSize.width < 1)
111 CGFloat aspectRatio = originalSize.width / originalSize.height;
112 CGFloat targetAspectRatio = targetSize.width / targetSize.height;
113 switch (projectionMode) {
114 case ProjectionMode::kFill:
115 // Don't preserve the aspect ratio.
116 projectTo.size = targetSize;
119 case ProjectionMode::kAspectFill:
120 if (targetAspectRatio < aspectRatio) {
122 projectTo.size.width = targetSize.height * aspectRatio;
123 projectTo.size.height = targetSize.height;
124 projectTo.origin.x = (targetSize.width - projectTo.size.width) / 2;
125 projectTo.origin.y = 0;
128 projectTo.size.width = targetSize.width;
129 projectTo.size.height = targetSize.width / aspectRatio;
130 projectTo.origin.x = 0;
131 projectTo.origin.y = (targetSize.height - projectTo.size.height) / 2;
135 case ProjectionMode::kAspectFit:
136 if (targetAspectRatio < aspectRatio) {
137 projectTo.size.width = targetSize.width;
138 projectTo.size.height = projectTo.size.width / aspectRatio;
139 targetSize = projectTo.size;
141 projectTo.size.height = targetSize.height;
142 projectTo.size.width = projectTo.size.height * aspectRatio;
143 targetSize = projectTo.size;
147 case ProjectionMode::kAspectFillNoClipping:
148 if (targetAspectRatio < aspectRatio) {
149 targetSize.width = targetSize.height * aspectRatio;
150 targetSize.height = targetSize.height;
152 targetSize.width = targetSize.width;
153 targetSize.height = targetSize.width / aspectRatio;
155 projectTo.size = targetSize;
159 projectTo = CGRectIntegral(projectTo);
160 // There's no CGSizeIntegral, faking one instead.
161 CGRect integralRect = CGRectZero;
162 integralRect.size = targetSize;
163 targetSize = CGRectIntegral(integralRect).size;