Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: ios/chrome/browser/ui/ui_util.mm

Issue 802633007: Upstream iOS UI utilities. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "ios/chrome/browser/ui/ui_util.h"
6
7 #import <UIKit/UIKit.h>
8
9 #include "base/ios/ios_util.h"
10 #import "ios/chrome/browser/ui/uikit_ui_util.h"
11 #include "ui/ios/uikit_util.h"
12
13 bool IsIPadIdiom() {
14 UIUserInterfaceIdiom idiom = [[UIDevice currentDevice] userInterfaceIdiom];
15 return idiom == UIUserInterfaceIdiomPad;
16 }
17
18 bool IsHighResScreen() {
19 return [[UIScreen mainScreen] scale] > 1.0;
20 }
21
22 bool IsPortrait() {
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);
28 #else
29 return UIInterfaceOrientationIsPortrait(orient) ||
30 orient == UIInterfaceOrientationUnknown;
31 #endif // SDK
32 }
33
34 bool IsLandscape() {
35 return UIInterfaceOrientationIsLandscape(GetInterfaceOrientation());
36 }
37
38 CGFloat CurrentScreenHeight() {
39 CGSize screenSize = [UIScreen mainScreen].bounds.size;
40 if (base::ios::IsRunningOnIOS8OrLater()) {
41 return screenSize.height;
42 } else {
43 return IsPortrait() ? screenSize.height : screenSize.width;
44 }
45 }
46
47 CGFloat CurrentScreenWidth() {
48 CGSize screenSize = [UIScreen mainScreen].bounds.size;
49 if (base::ios::IsRunningOnIOS8OrLater()) {
50 return screenSize.width;
51 } else {
52 return IsPortrait() ? screenSize.width : screenSize.height;
53 }
54 }
55
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
60 return 20;
61 }
62
63 CGFloat AlignValueToPixel(CGFloat value) {
64 static CGFloat scale = [[UIScreen mainScreen] scale];
65 return floor(value * scale) / scale;
66 }
67
68 CGPoint AlignPointToPixel(CGPoint point) {
69 return CGPointMake(AlignValueToPixel(point.x), AlignValueToPixel(point.y));
70 }
71
72 CGRect AlignRectToPixel(CGRect rect) {
73 rect.origin = AlignPointToPixel(rect.origin);
74 return rect;
75 }
76
77 CGRect AlignRectOriginAndSizeToPixels(CGRect rect) {
78 rect.origin = AlignPointToPixel(rect.origin);
79 rect.size = ui::AlignSizeToUpperPixel(rect.size);
80 return rect;
81 }
82
83 CGRect CGRectCopyWithOrigin(CGRect rect, CGFloat x, CGFloat y) {
84 return CGRectMake(x, y, rect.size.width, rect.size.height);
85 }
86
87 CGRect CGRectMakeAlignedAndCenteredAt(CGFloat x, CGFloat y, CGFloat width) {
88 return AlignRectOriginAndSizeToPixels(
89 CGRectMake(x - width / 2.0, y - width / 2.0, width, width));
90 }
91
92 // Based on an original size and a target size applies the transformations.
93 void CalculateProjection(CGSize originalSize,
94 CGSize desiredTargetSize,
95 bool preserveAspectRatio,
96 bool trimToFit,
97 CGSize& targetSize,
98 CGRect& projectTo) {
99 targetSize = desiredTargetSize;
100 projectTo = CGRectZero;
101 if (originalSize.height < 1 || originalSize.width < 1)
102 return;
103 if (targetSize.height < 1 || targetSize.width < 1)
104 return;
105
106 CGFloat aspectRatio = originalSize.width / originalSize.height;
107 CGFloat targetAspectRatio = targetSize.width / targetSize.height;
108 if (preserveAspectRatio) {
109 if (trimToFit) {
110 // Scale and clip image so that the aspect ratio is preserved and the
111 // target size is filled.
112 if (targetAspectRatio < aspectRatio) {
113 // Clip the x-axis.
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;
118 } else {
119 // Clip the y-axis.
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;
124 }
125 } else {
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;
132 } else {
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;
137 }
138 }
139 } else {
140 // Don't preserve the aspect ratio.
141 projectTo.size = targetSize;
142 }
143
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;
149 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698