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 #import "ios/chrome/browser/ui/uikit_ui_util.h"
11 #include "ui/ios/uikit_util.h"
14 UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
15 return idiom == UIUserInterfaceIdiomPad;
18 bool IsHighResScreen() {
19 return [[UIScreen mainScreen] scale] > 1.0;
23 UIInterfaceOrientation orient = GetInterfaceOrientation();
24 // If building with an SDK prior to iOS 8 don't worry about
25 // UIInterfaceOrientationUnknown because it wasn't defined.
26 #if !defined(__IPHONE_8_0) || __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_8_0
27 return UIInterfaceOrientationIsPortrait(orient);
29 return UIInterfaceOrientationIsPortrait(orient) ||
30 orient == UIInterfaceOrientationUnknown;
35 return UIInterfaceOrientationIsLandscape(GetInterfaceOrientation());
38 CGFloat CurrentScreenHeight() {
39 CGSize screenSize = [UIScreen mainScreen].bounds.size;
40 if (base::ios::IsRunningOnIOS8OrLater()) {
41 return screenSize.height;
43 return IsPortrait() ? screenSize.height : screenSize.width;
47 CGFloat CurrentScreenWidth() {
48 CGSize screenSize = [UIScreen mainScreen].bounds.size;
49 if (base::ios::IsRunningOnIOS8OrLater()) {
50 return screenSize.width;
52 return IsPortrait() ? screenSize.width : screenSize.height;
56 CGFloat StatusBarHeight() {
57 // TODO(justincohen): This is likely not correct, but right now iOS7 betas are
58 // doing strange things when handling calls that change the status bar height.
59 // For now, just return 20. crbug/264367
63 CGFloat AlignValueToPixel(CGFloat value) {
64 static CGFloat scale = [[UIScreen mainScreen] scale];
65 return floor(value * scale) / scale;
68 CGPoint AlignPointToPixel(CGPoint point) {
69 return CGPointMake(AlignValueToPixel(point.x), AlignValueToPixel(point.y));
72 CGRect AlignRectToPixel(CGRect rect) {
73 rect.origin = AlignPointToPixel(rect.origin);
77 CGRect AlignRectOriginAndSizeToPixels(CGRect rect) {
78 rect.origin = AlignPointToPixel(rect.origin);
79 rect.size = ui::AlignSizeToUpperPixel(rect.size);
83 CGRect CGRectCopyWithOrigin(CGRect rect, CGFloat x, CGFloat y) {
84 return CGRectMake(x, y, rect.size.width, rect.size.height);
87 CGRect CGRectMakeAlignedAndCenteredAt(CGFloat x, CGFloat y, CGFloat width) {
88 return AlignRectOriginAndSizeToPixels(
89 CGRectMake(x - width / 2.0, y - width / 2.0, width, width));
92 // Based on an original size and a target size applies the transformations.
93 void CalculateProjection(CGSize originalSize,
94 CGSize desiredTargetSize,
95 bool preserveAspectRatio,
99 targetSize = desiredTargetSize;
100 projectTo = CGRectZero;
101 if (originalSize.height < 1 || originalSize.width < 1)
103 if (targetSize.height < 1 || targetSize.width < 1)
106 CGFloat aspectRatio = originalSize.width / originalSize.height;
107 CGFloat targetAspectRatio = targetSize.width / targetSize.height;
108 if (preserveAspectRatio) {
110 // Scale and clip image so that the aspect ratio is preserved and the
111 // target size is filled.
112 if (targetAspectRatio < aspectRatio) {
114 projectTo.size.width = targetSize.height * aspectRatio;
115 projectTo.size.height = targetSize.height;
116 projectTo.origin.x = (targetSize.width - projectTo.size.width) / 2;
117 projectTo.origin.y = 0;
120 projectTo.size.width = targetSize.width;
121 projectTo.size.height = targetSize.width / aspectRatio;
122 projectTo.origin.x = 0;
123 projectTo.origin.y = (targetSize.height - projectTo.size.height) / 2;
126 // Scale image to ensure it fits inside the specified targetSize.
127 if (targetAspectRatio < aspectRatio) {
128 // Target is less wide than the original.
129 projectTo.size.width = targetSize.width;
130 projectTo.size.height = projectTo.size.width / aspectRatio;
131 targetSize = projectTo.size;
133 // Target is wider than the original.
134 projectTo.size.height = targetSize.height;
135 projectTo.size.width = projectTo.size.height * aspectRatio;
136 targetSize = projectTo.size;
140 // Don't preserve the aspect ratio.
141 projectTo.size = targetSize;
144 projectTo = CGRectIntegral(projectTo);
145 // There's no CGSizeIntegral, faking one instead.
146 CGRect integralRect = CGRectZero;
147 integralRect.size = targetSize;
148 targetSize = CGRectIntegral(integralRect).size;