1 // Copyright 2012 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 #ifndef IOS_CHROME_BROWSER_UI_UI_UTIL_H_
6 #define IOS_CHROME_BROWSER_UI_UI_UTIL_H_
8 #include <CoreGraphics/CoreGraphics.h>
10 // UI Util containing functions that do not require Objective-C.
12 // Running on an iPad?
15 // Is the screen of the device a high resolution screen, i.e. Retina Display.
16 bool IsHighResScreen();
18 // Returns true if the device is in portrait orientation or if interface
19 // orientation is unknown.
22 // Returns true if the device is in landscape orientation.
25 // Returns the height of the screen in the current orientation.
26 CGFloat
CurrentScreenHeight();
28 // Returns the width of the screen in the current orientation.
29 CGFloat
CurrentScreenWidth();
31 // Returns the height of the status bar, accounting for orientation.
32 CGFloat
StatusBarHeight();
34 // Returns the closest pixel-aligned value less than |value|, taking the scale
35 // factor into account. At a scale of 1, equivalent to floor().
36 CGFloat
AlignValueToPixel(CGFloat value
);
38 // Returns the point resulting from applying AlignValueToPixel() to both
40 CGPoint
AlignPointToPixel(CGPoint point
);
42 // Returns the rectangle resulting from applying AlignPointToPixel() to the
44 CGRect
AlignRectToPixel(CGRect rect
);
46 // Returns the rectangle resulting from applying AlignPointToPixel() to the
47 // origin, and ui::AlignSizeToUpperPixel() to the size.
48 CGRect
AlignRectOriginAndSizeToPixels(CGRect rect
);
50 // Makes a copy of |rect| with a new origin specified by |x| and |y|.
51 CGRect
CGRectCopyWithOrigin(CGRect rect
, CGFloat x
, CGFloat y
);
53 // Returns a square CGRect centered at |x|, |y| with a width of |width|.
54 // Both the position and the size of the CGRect will be aligned to points.
55 CGRect
CGRectMakeAlignedAndCenteredAt(CGFloat x
, CGFloat y
, CGFloat width
);
57 // This function is used to figure out how to resize an image from an
58 // |originalSize| to a |targetSize|. It returns a |revisedTargetSize| of the
59 // resized image and |projectTo| that is used to describe the rectangle in the
60 // target that the image will be covering. Returned values are always floored to
63 // The ProjectionMode describes in which way the stretching will apply.
65 enum class ProjectionMode
{
66 // Just stretches the source into the destination, not preserving aspect ratio
68 // |projectTo| and |revisedTargetSize| will be set to |targetSize|
71 // Scale to the target, maintaining aspect ratio, clipping the excess. Large
72 // original sizes are shrunk until they fit on one side, small original sizes
74 // |projectTo| will be a subset of |originalSize|
75 // |revisedTargetSize| will be set to |targetSize|
78 // Fit the image in the target so it fits completely inside, preserving aspect
79 // ratio. This will leave bands with with no data in the target.
80 // |projectTo| will be set to |originalSize|
81 // |revisedTargetSize| will be a smaller in one direction from |targetSize|
84 // Scale to the target, maintaining aspect ratio and not clipping the excess.
85 // |projectTo| will be set to |originalSize|
86 // |revisedTargetSize| will be a larger in one direction from |targetSize|
87 kAspectFillNoClipping
,
89 void CalculateProjection(CGSize originalSize
,
91 ProjectionMode projectionMode
,
92 CGSize
& revisedTargetSize
,
95 #endif // IOS_CHROME_BROWSER_UI_UI_UTIL_H_